PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/form/doctrine/sfDoctrineGuardPlugin/base/BasesfGuardUserForm.class.php

https://github.com/diegodorado/fundacion-sancor
PHP | 220 lines | 173 code | 34 blank | 13 comment | 21 complexity | f398a2fed6d5fb877d8ba2ffffdeda1a MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, LGPL-3.0, Apache-2.0, ISC, AGPL-3.0
  1. <?php
  2. /**
  3. * sfGuardUser form base class.
  4. *
  5. * @method sfGuardUser getObject() Returns the current form's model object
  6. *
  7. * @package asandbox
  8. * @subpackage form
  9. * @author Your name here
  10. * @version SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
  11. */
  12. abstract class BasesfGuardUserForm extends BaseFormDoctrine
  13. {
  14. public function setup()
  15. {
  16. $this->setWidgets(array(
  17. 'id' => new sfWidgetFormInputHidden(),
  18. 'first_name' => new sfWidgetFormInputText(),
  19. 'last_name' => new sfWidgetFormInputText(),
  20. 'email_address' => new sfWidgetFormInputText(),
  21. 'username' => new sfWidgetFormInputText(),
  22. 'algorithm' => new sfWidgetFormInputText(),
  23. 'salt' => new sfWidgetFormInputText(),
  24. 'password' => new sfWidgetFormInputText(),
  25. 'is_active' => new sfWidgetFormInputCheckbox(),
  26. 'is_super_admin' => new sfWidgetFormInputCheckbox(),
  27. 'last_login' => new sfWidgetFormDateTime(),
  28. 'created_at' => new sfWidgetFormDateTime(),
  29. 'updated_at' => new sfWidgetFormDateTime(),
  30. 'groups_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardGroup')),
  31. 'permissions_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardPermission')),
  32. 'categories_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'aCategory')),
  33. ));
  34. $this->setValidators(array(
  35. 'id' => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
  36. 'first_name' => new sfValidatorString(array('max_length' => 255, 'required' => false)),
  37. 'last_name' => new sfValidatorString(array('max_length' => 255, 'required' => false)),
  38. 'email_address' => new sfValidatorString(array('max_length' => 255)),
  39. 'username' => new sfValidatorString(array('max_length' => 128)),
  40. 'algorithm' => new sfValidatorString(array('max_length' => 128, 'required' => false)),
  41. 'salt' => new sfValidatorString(array('max_length' => 128, 'required' => false)),
  42. 'password' => new sfValidatorString(array('max_length' => 128, 'required' => false)),
  43. 'is_active' => new sfValidatorBoolean(array('required' => false)),
  44. 'is_super_admin' => new sfValidatorBoolean(array('required' => false)),
  45. 'last_login' => new sfValidatorDateTime(array('required' => false)),
  46. 'created_at' => new sfValidatorDateTime(),
  47. 'updated_at' => new sfValidatorDateTime(),
  48. 'groups_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardGroup', 'required' => false)),
  49. 'permissions_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardPermission', 'required' => false)),
  50. 'categories_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'aCategory', 'required' => false)),
  51. ));
  52. $this->validatorSchema->setPostValidator(
  53. new sfValidatorAnd(array(
  54. new sfValidatorDoctrineUnique(array('model' => 'sfGuardUser', 'column' => array('email_address'))),
  55. new sfValidatorDoctrineUnique(array('model' => 'sfGuardUser', 'column' => array('username'))),
  56. ))
  57. );
  58. $this->widgetSchema->setNameFormat('sf_guard_user[%s]');
  59. $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
  60. $this->setupInheritance();
  61. parent::setup();
  62. }
  63. public function getModelName()
  64. {
  65. return 'sfGuardUser';
  66. }
  67. public function updateDefaultsFromObject()
  68. {
  69. parent::updateDefaultsFromObject();
  70. if (isset($this->widgetSchema['groups_list']))
  71. {
  72. $this->setDefault('groups_list', $this->object->Groups->getPrimaryKeys());
  73. }
  74. if (isset($this->widgetSchema['permissions_list']))
  75. {
  76. $this->setDefault('permissions_list', $this->object->Permissions->getPrimaryKeys());
  77. }
  78. if (isset($this->widgetSchema['categories_list']))
  79. {
  80. $this->setDefault('categories_list', $this->object->Categories->getPrimaryKeys());
  81. }
  82. }
  83. protected function doSave($con = null)
  84. {
  85. $this->saveGroupsList($con);
  86. $this->savePermissionsList($con);
  87. $this->saveCategoriesList($con);
  88. parent::doSave($con);
  89. }
  90. public function saveGroupsList($con = null)
  91. {
  92. if (!$this->isValid())
  93. {
  94. throw $this->getErrorSchema();
  95. }
  96. if (!isset($this->widgetSchema['groups_list']))
  97. {
  98. // somebody has unset this widget
  99. return;
  100. }
  101. if (null === $con)
  102. {
  103. $con = $this->getConnection();
  104. }
  105. $existing = $this->object->Groups->getPrimaryKeys();
  106. $values = $this->getValue('groups_list');
  107. if (!is_array($values))
  108. {
  109. $values = array();
  110. }
  111. $unlink = array_diff($existing, $values);
  112. if (count($unlink))
  113. {
  114. $this->object->unlink('Groups', array_values($unlink));
  115. }
  116. $link = array_diff($values, $existing);
  117. if (count($link))
  118. {
  119. $this->object->link('Groups', array_values($link));
  120. }
  121. }
  122. public function savePermissionsList($con = null)
  123. {
  124. if (!$this->isValid())
  125. {
  126. throw $this->getErrorSchema();
  127. }
  128. if (!isset($this->widgetSchema['permissions_list']))
  129. {
  130. // somebody has unset this widget
  131. return;
  132. }
  133. if (null === $con)
  134. {
  135. $con = $this->getConnection();
  136. }
  137. $existing = $this->object->Permissions->getPrimaryKeys();
  138. $values = $this->getValue('permissions_list');
  139. if (!is_array($values))
  140. {
  141. $values = array();
  142. }
  143. $unlink = array_diff($existing, $values);
  144. if (count($unlink))
  145. {
  146. $this->object->unlink('Permissions', array_values($unlink));
  147. }
  148. $link = array_diff($values, $existing);
  149. if (count($link))
  150. {
  151. $this->object->link('Permissions', array_values($link));
  152. }
  153. }
  154. public function saveCategoriesList($con = null)
  155. {
  156. if (!$this->isValid())
  157. {
  158. throw $this->getErrorSchema();
  159. }
  160. if (!isset($this->widgetSchema['categories_list']))
  161. {
  162. // somebody has unset this widget
  163. return;
  164. }
  165. if (null === $con)
  166. {
  167. $con = $this->getConnection();
  168. }
  169. $existing = $this->object->Categories->getPrimaryKeys();
  170. $values = $this->getValue('categories_list');
  171. if (!is_array($values))
  172. {
  173. $values = array();
  174. }
  175. $unlink = array_diff($existing, $values);
  176. if (count($unlink))
  177. {
  178. $this->object->unlink('Categories', array_values($unlink));
  179. }
  180. $link = array_diff($values, $existing);
  181. if (count($link))
  182. {
  183. $this->object->link('Categories', array_values($link));
  184. }
  185. }
  186. }