PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/form/doctrine/apostrophePlugin/base/BaseaCategoryForm.class.php

https://github.com/diegodorado/fundacion-sancor
PHP | 252 lines | 197 code | 41 blank | 14 comment | 28 complexity | 808b59e767329bfcf3232d700ce8ca1e 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. * aCategory form base class.
  4. *
  5. * @method aCategory 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 BaseaCategoryForm 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. 'slug' => new sfWidgetFormInputText(),
  23. 'media_items_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'aMediaItem')),
  24. 'pages_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'aPage')),
  25. 'users_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardUser')),
  26. 'groups_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardGroup')),
  27. ));
  28. $this->setValidators(array(
  29. 'id' => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
  30. 'name' => new sfValidatorString(array('max_length' => 255, 'required' => false)),
  31. 'description' => new sfValidatorString(array('required' => false)),
  32. 'created_at' => new sfValidatorDateTime(),
  33. 'updated_at' => new sfValidatorDateTime(),
  34. 'slug' => new sfValidatorString(array('max_length' => 255, 'required' => false)),
  35. 'media_items_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'aMediaItem', 'required' => false)),
  36. 'pages_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'aPage', 'required' => false)),
  37. 'users_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardUser', 'required' => false)),
  38. 'groups_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardGroup', 'required' => false)),
  39. ));
  40. $this->validatorSchema->setPostValidator(
  41. new sfValidatorAnd(array(
  42. new sfValidatorDoctrineUnique(array('model' => 'aCategory', 'column' => array('name'))),
  43. new sfValidatorDoctrineUnique(array('model' => 'aCategory', 'column' => array('slug'))),
  44. ))
  45. );
  46. $this->widgetSchema->setNameFormat('a_category[%s]');
  47. $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
  48. $this->setupInheritance();
  49. parent::setup();
  50. }
  51. public function getModelName()
  52. {
  53. return 'aCategory';
  54. }
  55. public function updateDefaultsFromObject()
  56. {
  57. parent::updateDefaultsFromObject();
  58. if (isset($this->widgetSchema['media_items_list']))
  59. {
  60. $this->setDefault('media_items_list', $this->object->MediaItems->getPrimaryKeys());
  61. }
  62. if (isset($this->widgetSchema['pages_list']))
  63. {
  64. $this->setDefault('pages_list', $this->object->Pages->getPrimaryKeys());
  65. }
  66. if (isset($this->widgetSchema['users_list']))
  67. {
  68. $this->setDefault('users_list', $this->object->Users->getPrimaryKeys());
  69. }
  70. if (isset($this->widgetSchema['groups_list']))
  71. {
  72. $this->setDefault('groups_list', $this->object->Groups->getPrimaryKeys());
  73. }
  74. }
  75. protected function doSave($con = null)
  76. {
  77. $this->saveMediaItemsList($con);
  78. $this->savePagesList($con);
  79. $this->saveUsersList($con);
  80. $this->saveGroupsList($con);
  81. parent::doSave($con);
  82. }
  83. public function saveMediaItemsList($con = null)
  84. {
  85. if (!$this->isValid())
  86. {
  87. throw $this->getErrorSchema();
  88. }
  89. if (!isset($this->widgetSchema['media_items_list']))
  90. {
  91. // somebody has unset this widget
  92. return;
  93. }
  94. if (null === $con)
  95. {
  96. $con = $this->getConnection();
  97. }
  98. $existing = $this->object->MediaItems->getPrimaryKeys();
  99. $values = $this->getValue('media_items_list');
  100. if (!is_array($values))
  101. {
  102. $values = array();
  103. }
  104. $unlink = array_diff($existing, $values);
  105. if (count($unlink))
  106. {
  107. $this->object->unlink('MediaItems', array_values($unlink));
  108. }
  109. $link = array_diff($values, $existing);
  110. if (count($link))
  111. {
  112. $this->object->link('MediaItems', array_values($link));
  113. }
  114. }
  115. public function savePagesList($con = null)
  116. {
  117. if (!$this->isValid())
  118. {
  119. throw $this->getErrorSchema();
  120. }
  121. if (!isset($this->widgetSchema['pages_list']))
  122. {
  123. // somebody has unset this widget
  124. return;
  125. }
  126. if (null === $con)
  127. {
  128. $con = $this->getConnection();
  129. }
  130. $existing = $this->object->Pages->getPrimaryKeys();
  131. $values = $this->getValue('pages_list');
  132. if (!is_array($values))
  133. {
  134. $values = array();
  135. }
  136. $unlink = array_diff($existing, $values);
  137. if (count($unlink))
  138. {
  139. $this->object->unlink('Pages', array_values($unlink));
  140. }
  141. $link = array_diff($values, $existing);
  142. if (count($link))
  143. {
  144. $this->object->link('Pages', array_values($link));
  145. }
  146. }
  147. public function saveUsersList($con = null)
  148. {
  149. if (!$this->isValid())
  150. {
  151. throw $this->getErrorSchema();
  152. }
  153. if (!isset($this->widgetSchema['users_list']))
  154. {
  155. // somebody has unset this widget
  156. return;
  157. }
  158. if (null === $con)
  159. {
  160. $con = $this->getConnection();
  161. }
  162. $existing = $this->object->Users->getPrimaryKeys();
  163. $values = $this->getValue('users_list');
  164. if (!is_array($values))
  165. {
  166. $values = array();
  167. }
  168. $unlink = array_diff($existing, $values);
  169. if (count($unlink))
  170. {
  171. $this->object->unlink('Users', array_values($unlink));
  172. }
  173. $link = array_diff($values, $existing);
  174. if (count($link))
  175. {
  176. $this->object->link('Users', array_values($link));
  177. }
  178. }
  179. public function saveGroupsList($con = null)
  180. {
  181. if (!$this->isValid())
  182. {
  183. throw $this->getErrorSchema();
  184. }
  185. if (!isset($this->widgetSchema['groups_list']))
  186. {
  187. // somebody has unset this widget
  188. return;
  189. }
  190. if (null === $con)
  191. {
  192. $con = $this->getConnection();
  193. }
  194. $existing = $this->object->Groups->getPrimaryKeys();
  195. $values = $this->getValue('groups_list');
  196. if (!is_array($values))
  197. {
  198. $values = array();
  199. }
  200. $unlink = array_diff($existing, $values);
  201. if (count($unlink))
  202. {
  203. $this->object->unlink('Groups', array_values($unlink));
  204. }
  205. $link = array_diff($values, $existing);
  206. if (count($link))
  207. {
  208. $this->object->link('Groups', array_values($link));
  209. }
  210. }
  211. }