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

https://github.com/arturolinares/asandbox · PHP · 160 lines · 121 code · 27 blank · 12 comment · 14 complexity · 7168d906c39426b668c4d946900879d3 MD5 · raw file

  1. <?php
  2. /**
  3. * aMediaCategory form base class.
  4. *
  5. * @method aMediaCategory 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 BaseaMediaCategoryForm 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. ));
  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('required' => false)),
  30. 'created_at' => new sfValidatorDateTime(),
  31. 'updated_at' => new sfValidatorDateTime(),
  32. 'slug' => new sfValidatorString(array('max_length' => 255, 'required' => false)),
  33. 'media_items_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'aMediaItem', 'required' => false)),
  34. 'pages_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'aPage', 'required' => false)),
  35. ));
  36. $this->validatorSchema->setPostValidator(
  37. new sfValidatorAnd(array(
  38. new sfValidatorDoctrineUnique(array('model' => 'aMediaCategory', 'column' => array('name'))),
  39. new sfValidatorDoctrineUnique(array('model' => 'aMediaCategory', 'column' => array('slug'))),
  40. ))
  41. );
  42. $this->widgetSchema->setNameFormat('a_media_category[%s]');
  43. $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
  44. $this->setupInheritance();
  45. parent::setup();
  46. }
  47. public function getModelName()
  48. {
  49. return 'aMediaCategory';
  50. }
  51. public function updateDefaultsFromObject()
  52. {
  53. parent::updateDefaultsFromObject();
  54. if (isset($this->widgetSchema['media_items_list']))
  55. {
  56. $this->setDefault('media_items_list', $this->object->MediaItems->getPrimaryKeys());
  57. }
  58. if (isset($this->widgetSchema['pages_list']))
  59. {
  60. $this->setDefault('pages_list', $this->object->Pages->getPrimaryKeys());
  61. }
  62. }
  63. protected function doSave($con = null)
  64. {
  65. $this->saveMediaItemsList($con);
  66. $this->savePagesList($con);
  67. parent::doSave($con);
  68. }
  69. public function saveMediaItemsList($con = null)
  70. {
  71. if (!$this->isValid())
  72. {
  73. throw $this->getErrorSchema();
  74. }
  75. if (!isset($this->widgetSchema['media_items_list']))
  76. {
  77. // somebody has unset this widget
  78. return;
  79. }
  80. if (null === $con)
  81. {
  82. $con = $this->getConnection();
  83. }
  84. $existing = $this->object->MediaItems->getPrimaryKeys();
  85. $values = $this->getValue('media_items_list');
  86. if (!is_array($values))
  87. {
  88. $values = array();
  89. }
  90. $unlink = array_diff($existing, $values);
  91. if (count($unlink))
  92. {
  93. $this->object->unlink('MediaItems', array_values($unlink));
  94. }
  95. $link = array_diff($values, $existing);
  96. if (count($link))
  97. {
  98. $this->object->link('MediaItems', array_values($link));
  99. }
  100. }
  101. public function savePagesList($con = null)
  102. {
  103. if (!$this->isValid())
  104. {
  105. throw $this->getErrorSchema();
  106. }
  107. if (!isset($this->widgetSchema['pages_list']))
  108. {
  109. // somebody has unset this widget
  110. return;
  111. }
  112. if (null === $con)
  113. {
  114. $con = $this->getConnection();
  115. }
  116. $existing = $this->object->Pages->getPrimaryKeys();
  117. $values = $this->getValue('pages_list');
  118. if (!is_array($values))
  119. {
  120. $values = array();
  121. }
  122. $unlink = array_diff($existing, $values);
  123. if (count($unlink))
  124. {
  125. $this->object->unlink('Pages', array_values($unlink));
  126. }
  127. $link = array_diff($values, $existing);
  128. if (count($link))
  129. {
  130. $this->object->link('Pages', array_values($link));
  131. }
  132. }
  133. }