PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/diegodorado/fundacion-sancor
PHP | 201 lines | 154 code | 34 blank | 13 comment | 21 complexity | 41ea139e4bd717e4a71a102382dad505 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. * sfGuardGroup form base class.
  4. *
  5. * @method sfGuardGroup 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 BasesfGuardGroupForm extends BaseFormDoctrine
  13. {
  14. public function setup()
  15. {
  16. $this->setWidgets(array(
  17. 'id' => new sfWidgetFormInputHidden(),
  18. 'name' => new sfWidgetFormInputText(),
  19. 'description' => new sfWidgetFormTextarea(),
  20. 'created_at' => new sfWidgetFormDateTime(),
  21. 'updated_at' => new sfWidgetFormDateTime(),
  22. 'users_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardUser')),
  23. 'permissions_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardPermission')),
  24. 'categories_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'aCategory')),
  25. ));
  26. $this->setValidators(array(
  27. 'id' => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
  28. 'name' => new sfValidatorString(array('max_length' => 255, 'required' => false)),
  29. 'description' => new sfValidatorString(array('max_length' => 1000, 'required' => false)),
  30. 'created_at' => new sfValidatorDateTime(),
  31. 'updated_at' => new sfValidatorDateTime(),
  32. 'users_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardUser', 'required' => false)),
  33. 'permissions_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardPermission', 'required' => false)),
  34. 'categories_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'aCategory', 'required' => false)),
  35. ));
  36. $this->validatorSchema->setPostValidator(
  37. new sfValidatorDoctrineUnique(array('model' => 'sfGuardGroup', 'column' => array('name')))
  38. );
  39. $this->widgetSchema->setNameFormat('sf_guard_group[%s]');
  40. $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
  41. $this->setupInheritance();
  42. parent::setup();
  43. }
  44. public function getModelName()
  45. {
  46. return 'sfGuardGroup';
  47. }
  48. public function updateDefaultsFromObject()
  49. {
  50. parent::updateDefaultsFromObject();
  51. if (isset($this->widgetSchema['users_list']))
  52. {
  53. $this->setDefault('users_list', $this->object->Users->getPrimaryKeys());
  54. }
  55. if (isset($this->widgetSchema['permissions_list']))
  56. {
  57. $this->setDefault('permissions_list', $this->object->Permissions->getPrimaryKeys());
  58. }
  59. if (isset($this->widgetSchema['categories_list']))
  60. {
  61. $this->setDefault('categories_list', $this->object->Categories->getPrimaryKeys());
  62. }
  63. }
  64. protected function doSave($con = null)
  65. {
  66. $this->saveUsersList($con);
  67. $this->savePermissionsList($con);
  68. $this->saveCategoriesList($con);
  69. parent::doSave($con);
  70. }
  71. public function saveUsersList($con = null)
  72. {
  73. if (!$this->isValid())
  74. {
  75. throw $this->getErrorSchema();
  76. }
  77. if (!isset($this->widgetSchema['users_list']))
  78. {
  79. // somebody has unset this widget
  80. return;
  81. }
  82. if (null === $con)
  83. {
  84. $con = $this->getConnection();
  85. }
  86. $existing = $this->object->Users->getPrimaryKeys();
  87. $values = $this->getValue('users_list');
  88. if (!is_array($values))
  89. {
  90. $values = array();
  91. }
  92. $unlink = array_diff($existing, $values);
  93. if (count($unlink))
  94. {
  95. $this->object->unlink('Users', array_values($unlink));
  96. }
  97. $link = array_diff($values, $existing);
  98. if (count($link))
  99. {
  100. $this->object->link('Users', array_values($link));
  101. }
  102. }
  103. public function savePermissionsList($con = null)
  104. {
  105. if (!$this->isValid())
  106. {
  107. throw $this->getErrorSchema();
  108. }
  109. if (!isset($this->widgetSchema['permissions_list']))
  110. {
  111. // somebody has unset this widget
  112. return;
  113. }
  114. if (null === $con)
  115. {
  116. $con = $this->getConnection();
  117. }
  118. $existing = $this->object->Permissions->getPrimaryKeys();
  119. $values = $this->getValue('permissions_list');
  120. if (!is_array($values))
  121. {
  122. $values = array();
  123. }
  124. $unlink = array_diff($existing, $values);
  125. if (count($unlink))
  126. {
  127. $this->object->unlink('Permissions', array_values($unlink));
  128. }
  129. $link = array_diff($values, $existing);
  130. if (count($link))
  131. {
  132. $this->object->link('Permissions', array_values($link));
  133. }
  134. }
  135. public function saveCategoriesList($con = null)
  136. {
  137. if (!$this->isValid())
  138. {
  139. throw $this->getErrorSchema();
  140. }
  141. if (!isset($this->widgetSchema['categories_list']))
  142. {
  143. // somebody has unset this widget
  144. return;
  145. }
  146. if (null === $con)
  147. {
  148. $con = $this->getConnection();
  149. }
  150. $existing = $this->object->Categories->getPrimaryKeys();
  151. $values = $this->getValue('categories_list');
  152. if (!is_array($values))
  153. {
  154. $values = array();
  155. }
  156. $unlink = array_diff($existing, $values);
  157. if (count($unlink))
  158. {
  159. $this->object->unlink('Categories', array_values($unlink));
  160. }
  161. $link = array_diff($values, $existing);
  162. if (count($link))
  163. {
  164. $this->object->link('Categories', array_values($link));
  165. }
  166. }
  167. }