Advertisement
Guest User

BasesfGuardUserForm

a guest
Nov 15th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * sfGuardUser form base class.
  5. *
  6. * @method sfGuardUser getObject() Returns the current form's model object
  7. *
  8. * @package ##PROJECT_NAME##
  9. * @subpackage form
  10. * @author ##AUTHOR_NAME##
  11. */
  12. abstract class BasesfGuardUserForm extends BaseFormPropel
  13. {
  14. public function setup()
  15. {
  16. $this->setWidgets(array(
  17. 'id' => new sfWidgetFormInputHidden(),
  18. 'username' => new sfWidgetFormInputText(),
  19. 'algorithm' => new sfWidgetFormInputText(),
  20. 'salt' => new sfWidgetFormInputText(),
  21. 'password' => new sfWidgetFormInputText(),
  22. 'created_at' => new sfWidgetFormDateTime(),
  23. 'last_login' => new sfWidgetFormDateTime(),
  24. 'is_active' => new sfWidgetFormInputCheckbox(),
  25. 'is_super_admin' => new sfWidgetFormInputCheckbox(),
  26. 'sf_guard_user_group_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'sfGuardGroup')),
  27. 'sf_guard_user_permission_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'sfGuardPermission')),
  28. ));
  29.  
  30. $this->setValidators(array(
  31. 'id' => new sfValidatorChoice(array('choices' => array($this->getObject()->getId()), 'empty_value' => $this->getObject()->getId(), 'required' => false)),
  32. 'username' => new sfValidatorString(array('max_length' => 128)),
  33. 'algorithm' => new sfValidatorString(array('max_length' => 128)),
  34. 'salt' => new sfValidatorString(array('max_length' => 128)),
  35. 'password' => new sfValidatorString(array('max_length' => 128)),
  36. 'created_at' => new sfValidatorDateTime(array('required' => false)),
  37. 'last_login' => new sfValidatorDateTime(array('required' => false)),
  38. 'is_active' => new sfValidatorBoolean(),
  39. 'is_super_admin' => new sfValidatorBoolean(),
  40. 'sf_guard_user_group_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'sfGuardGroup', 'required' => false)),
  41. 'sf_guard_user_permission_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'sfGuardPermission', 'required' => false)),
  42. ));
  43.  
  44. $this->validatorSchema->setPostValidator(
  45. new sfValidatorPropelUnique(array('model' => 'sfGuardUser', 'column' => array('username')))
  46. );
  47.  
  48. $this->widgetSchema->setNameFormat('sf_guard_user[%s]');
  49.  
  50. $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
  51.  
  52. parent::setup();
  53. }
  54.  
  55. public function getModelName()
  56. {
  57. return 'sfGuardUser';
  58. }
  59.  
  60.  
  61. public function updateDefaultsFromObject()
  62. {
  63. parent::updateDefaultsFromObject();
  64.  
  65. if (isset($this->widgetSchema['sf_guard_user_group_list']))
  66. {
  67. $values = array();
  68. foreach ($this->object->getsfGuardUserGroups() as $obj)
  69. {
  70. $values[] = $obj->getGroupId();
  71. }
  72.  
  73. $this->setDefault('sf_guard_user_group_list', $values);
  74. }
  75.  
  76. if (isset($this->widgetSchema['sf_guard_user_permission_list']))
  77. {
  78. $values = array();
  79. foreach ($this->object->getsfGuardUserPermissions() as $obj)
  80. {
  81. $values[] = $obj->getPermissionId();
  82. }
  83.  
  84. $this->setDefault('sf_guard_user_permission_list', $values);
  85. }
  86.  
  87. }
  88.  
  89. protected function doSave($con = null)
  90. {
  91. parent::doSave($con);
  92.  
  93. $this->savesfGuardUserGroupList($con);
  94. $this->savesfGuardUserPermissionList($con);
  95. }
  96.  
  97. public function savesfGuardUserGroupList($con = null)
  98. {
  99. if (!$this->isValid())
  100. {
  101. throw $this->getErrorSchema();
  102. }
  103.  
  104. if (!isset($this->widgetSchema['sf_guard_user_group_list']))
  105. {
  106. // somebody has unset this widget
  107. return;
  108. }
  109.  
  110. if (null === $con)
  111. {
  112. $con = $this->getConnection();
  113. }
  114.  
  115. $c = new Criteria();
  116. $c->add(sfGuardUserGroupPeer::USER_ID, $this->object->getPrimaryKey());
  117. sfGuardUserGroupPeer::doDelete($c, $con);
  118.  
  119. $values = $this->getValue('sf_guard_user_group_list');
  120. if (is_array($values))
  121. {
  122. foreach ($values as $value)
  123. {
  124. $obj = new sfGuardUserGroup();
  125. $obj->setUserId($this->object->getPrimaryKey());
  126. $obj->setGroupId($value);
  127. $obj->save();
  128. }
  129. }
  130. }
  131.  
  132. public function savesfGuardUserPermissionList($con = null)
  133. {
  134. if (!$this->isValid())
  135. {
  136. throw $this->getErrorSchema();
  137. }
  138.  
  139. if (!isset($this->widgetSchema['sf_guard_user_permission_list']))
  140. {
  141. // somebody has unset this widget
  142. return;
  143. }
  144.  
  145. if (null === $con)
  146. {
  147. $con = $this->getConnection();
  148. }
  149.  
  150. $c = new Criteria();
  151. $c->add(sfGuardUserPermissionPeer::USER_ID, $this->object->getPrimaryKey());
  152. sfGuardUserPermissionPeer::doDelete($c, $con);
  153.  
  154. $values = $this->getValue('sf_guard_user_permission_list');
  155. if (is_array($values))
  156. {
  157. foreach ($values as $value)
  158. {
  159. $obj = new sfGuardUserPermission();
  160. $obj->setUserId($this->object->getPrimaryKey());
  161. $obj->setPermissionId($value);
  162. $obj->save();
  163. }
  164. }
  165. }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement