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

https://github.com/malacon/rbwords · PHP · 177 lines · 139 code · 26 blank · 12 comment · 14 complexity · ba6acc47ca53e9335e644e8332a20592 MD5 · raw file

  1. <?php
  2. /**
  3. * aPage form base class.
  4. *
  5. * @method aPage getObject() Returns the current form's model object
  6. *
  7. * @package pprl
  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 BaseaPageForm extends BaseFormDoctrine
  13. {
  14. public function setup()
  15. {
  16. $this->setWidgets(array(
  17. 'id' => new sfWidgetFormInputHidden(),
  18. 'slug' => new sfWidgetFormTextarea(),
  19. 'template' => new sfWidgetFormInputText(),
  20. 'view_is_secure' => new sfWidgetFormInputCheckbox(),
  21. 'view_guest' => new sfWidgetFormInputCheckbox(),
  22. 'edit_admin_lock' => new sfWidgetFormInputCheckbox(),
  23. 'view_admin_lock' => new sfWidgetFormInputCheckbox(),
  24. 'published_at' => new sfWidgetFormDateTime(),
  25. 'archived' => new sfWidgetFormInputCheckbox(),
  26. 'admin' => new sfWidgetFormInputCheckbox(),
  27. 'author_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Author'), 'add_empty' => true)),
  28. 'deleter_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Deleter'), 'add_empty' => true)),
  29. 'engine' => new sfWidgetFormInputText(),
  30. 'created_at' => new sfWidgetFormDateTime(),
  31. 'updated_at' => new sfWidgetFormDateTime(),
  32. 'lft' => new sfWidgetFormInputText(),
  33. 'rgt' => new sfWidgetFormInputText(),
  34. 'level' => new sfWidgetFormInputText(),
  35. 'a_search_documents_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'aSearchDocument')),
  36. 'categories_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'aCategory')),
  37. ));
  38. $this->setValidators(array(
  39. 'id' => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
  40. 'slug' => new sfValidatorString(array('max_length' => 1000, 'required' => false)),
  41. 'template' => new sfValidatorString(array('max_length' => 100, 'required' => false)),
  42. 'view_is_secure' => new sfValidatorBoolean(array('required' => false)),
  43. 'view_guest' => new sfValidatorBoolean(array('required' => false)),
  44. 'edit_admin_lock' => new sfValidatorBoolean(array('required' => false)),
  45. 'view_admin_lock' => new sfValidatorBoolean(array('required' => false)),
  46. 'published_at' => new sfValidatorDateTime(array('required' => false)),
  47. 'archived' => new sfValidatorBoolean(array('required' => false)),
  48. 'admin' => new sfValidatorBoolean(array('required' => false)),
  49. 'author_id' => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Author'), 'required' => false)),
  50. 'deleter_id' => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Deleter'), 'required' => false)),
  51. 'engine' => new sfValidatorString(array('max_length' => 255, 'required' => false)),
  52. 'created_at' => new sfValidatorDateTime(),
  53. 'updated_at' => new sfValidatorDateTime(),
  54. 'lft' => new sfValidatorInteger(array('required' => false)),
  55. 'rgt' => new sfValidatorInteger(array('required' => false)),
  56. 'level' => new sfValidatorInteger(array('required' => false)),
  57. 'a_search_documents_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'aSearchDocument', 'required' => false)),
  58. 'categories_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'aCategory', 'required' => false)),
  59. ));
  60. $this->widgetSchema->setNameFormat('a_page[%s]');
  61. $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
  62. $this->setupInheritance();
  63. parent::setup();
  64. }
  65. public function getModelName()
  66. {
  67. return 'aPage';
  68. }
  69. public function updateDefaultsFromObject()
  70. {
  71. parent::updateDefaultsFromObject();
  72. if (isset($this->widgetSchema['a_search_documents_list']))
  73. {
  74. $this->setDefault('a_search_documents_list', $this->object->aSearchDocuments->getPrimaryKeys());
  75. }
  76. if (isset($this->widgetSchema['categories_list']))
  77. {
  78. $this->setDefault('categories_list', $this->object->Categories->getPrimaryKeys());
  79. }
  80. }
  81. protected function doSave($con = null)
  82. {
  83. $this->saveaSearchDocumentsList($con);
  84. $this->saveCategoriesList($con);
  85. parent::doSave($con);
  86. }
  87. public function saveaSearchDocumentsList($con = null)
  88. {
  89. if (!$this->isValid())
  90. {
  91. throw $this->getErrorSchema();
  92. }
  93. if (!isset($this->widgetSchema['a_search_documents_list']))
  94. {
  95. // somebody has unset this widget
  96. return;
  97. }
  98. if (null === $con)
  99. {
  100. $con = $this->getConnection();
  101. }
  102. $existing = $this->object->aSearchDocuments->getPrimaryKeys();
  103. $values = $this->getValue('a_search_documents_list');
  104. if (!is_array($values))
  105. {
  106. $values = array();
  107. }
  108. $unlink = array_diff($existing, $values);
  109. if (count($unlink))
  110. {
  111. $this->object->unlink('aSearchDocuments', array_values($unlink));
  112. }
  113. $link = array_diff($values, $existing);
  114. if (count($link))
  115. {
  116. $this->object->link('aSearchDocuments', array_values($link));
  117. }
  118. }
  119. public function saveCategoriesList($con = null)
  120. {
  121. if (!$this->isValid())
  122. {
  123. throw $this->getErrorSchema();
  124. }
  125. if (!isset($this->widgetSchema['categories_list']))
  126. {
  127. // somebody has unset this widget
  128. return;
  129. }
  130. if (null === $con)
  131. {
  132. $con = $this->getConnection();
  133. }
  134. $existing = $this->object->Categories->getPrimaryKeys();
  135. $values = $this->getValue('categories_list');
  136. if (!is_array($values))
  137. {
  138. $values = array();
  139. }
  140. $unlink = array_diff($existing, $values);
  141. if (count($unlink))
  142. {
  143. $this->object->unlink('Categories', array_values($unlink));
  144. }
  145. $link = array_diff($values, $existing);
  146. if (count($link))
  147. {
  148. $this->object->link('Categories', array_values($link));
  149. }
  150. }
  151. }