PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/dmCorePlugin/data/generator/dmDoctrineForm/default/template/sfDoctrineFormGeneratedTemplate.php

https://github.com/stephaneerard/diem
PHP | 190 lines | 141 code | 28 blank | 21 comment | 15 complexity | 635444cb95b6e18aa7160afbbe860f81 MD5 | raw file
  1. [?php
  2. /**
  3. * <?php echo $this->modelName ?> form base class.
  4. *
  5. * @method <?php echo $this->modelName ?> getObject() Returns the current form's model object
  6. *
  7. * @package ##PROJECT_NAME##
  8. * @subpackage form
  9. * @author ##AUTHOR_NAME##
  10. * @version SVN: $Id$
  11. */
  12. abstract class Base<?php echo $this->modelName ?>Form extends <?php echo $this->getFormClassToExtend() . "\n" ?>
  13. {
  14. public function setup()
  15. {
  16. $this->setWidgets(array(
  17. <?php foreach ($this->getColumns() as $column): ?>
  18. '<?php echo $column->getFieldName() ?>'<?php echo str_repeat(' ', $this->getColumnNameMaxLength() - strlen($column->getFieldName())) ?> => new <?php echo $this->getWidgetClassForColumn($column) ?>(<?php echo $this->getWidgetOptionsForColumn($column) ?>),
  19. <?php endforeach; ?>
  20. <?php foreach ($this->getManyToManyRelations() as $relation): ?>
  21. <?php if ('DmMedia' === $relation->getClass()) continue; ?>
  22. '<?php echo $this->underscore($relation['alias']) ?>_list'<?php echo str_repeat(' ', $this->getColumnNameMaxLength() - strlen($this->underscore($relation['alias']).'_list')) ?> => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => '<?php echo $relation['table']->getOption('name') ?>', 'expanded' => true)),
  23. <?php endforeach; ?>
  24. ));
  25. $this->setValidators(array(
  26. <?php foreach ($this->getColumns() as $column): ?>
  27. '<?php echo $column->getFieldName() ?>'<?php echo str_repeat(' ', $this->getColumnNameMaxLength() - strlen($column->getFieldName())) ?> => new <?php echo $this->getValidatorClassForColumn($column) ?>(<?php echo $this->getValidatorOptionsForColumn($column) ?>),
  28. <?php endforeach; ?>
  29. <?php foreach ($this->getManyToManyRelations() as $relation): ?>
  30. <?php if ('DmMedia' === $relation->getClass()) continue; ?>
  31. '<?php echo $this->underscore($relation['alias']) ?>_list'<?php echo str_repeat(' ', $this->getColumnNameMaxLength() - strlen($this->underscore($relation['alias']).'_list')) ?> => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => '<?php echo $relation['table']->getOption('name') ?>', 'required' => false)),
  32. <?php endforeach; ?>
  33. ));
  34. <?php foreach($this->getMediaRelations() as $mediaRelation): ?>
  35. /*
  36. * Embed Media form for <?php echo $mediaRelation['local']."\n"; ?>
  37. */
  38. $this->embedForm('<?php echo $mediaRelation['local'].'_form' ?>', $this->createMediaFormFor<?php echo dmString::camelize($mediaRelation['local']); ?>());
  39. unset($this['<?php echo $mediaRelation['local']; ?>']);
  40. <?php endforeach; ?>
  41. <?php if ($uniqueColumns = $this->getUniqueColumnNames()): ?>
  42. $this->validatorSchema->setPostValidator(
  43. <?php if (count($uniqueColumns) > 1): ?>
  44. new sfValidatorAnd(array(
  45. <?php foreach ($uniqueColumns as $uniqueColumn): ?>
  46. new sfValidatorDoctrineUnique(array('model' => '<?php echo $this->table->getOption('name') ?>', 'column' => array('<?php echo implode("', '", $uniqueColumn) ?>'))),
  47. <?php endforeach; ?>
  48. ))
  49. <?php else: ?>
  50. new sfValidatorDoctrineUnique(array('model' => '<?php echo $this->table->getOption('name') ?>', 'column' => array('<?php echo implode("', '", $uniqueColumns[0]) ?>')))
  51. <?php endif; ?>
  52. );
  53. <?php endif; ?>
  54. <?php if ($this->table->hasI18n()): ?>
  55. if('embed' == sfConfig::get('dm_i18n_form'))
  56. {
  57. $this->embedI18n(sfConfig::get('dm_i18n_cultures'));
  58. }
  59. else
  60. {
  61. $this->mergeI18nForm();
  62. }
  63. <?php endif; ?>
  64. $this->widgetSchema->setNameFormat('<?php echo $this->underscore($this->modelName) ?>[%s]');
  65. $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
  66. $this->setupInheritance();
  67. parent::setup();
  68. // Unset automatic fields like 'created_at', 'updated_at', 'position'
  69. // override this method in your form to keep them
  70. parent::unsetAutoFields();
  71. }
  72. <?php foreach($this->getMediaRelations() as $mediaRelation): ?>
  73. /**
  74. * Creates a DmMediaForm instance for <?php echo $mediaRelation['local']."\n"; ?>
  75. *
  76. * @return DmMediaForm a form instance for the related media
  77. */
  78. protected function createMediaFormFor<?php echo dmString::camelize($mediaRelation['local']); ?>()
  79. {
  80. return DmMediaForRecordForm::factory($this->object, '<?php echo $mediaRelation['local'] ?>', '<?php echo $mediaRelation['alias'] ?>', $this->validatorSchema['<?php echo $mediaRelation['local']; ?>']->getOption('required'));
  81. }
  82. <?php endforeach; ?>
  83. protected function doBind(array $values)
  84. {
  85. <?php foreach($this->getMediaRelations() as $mediaRelation): ?>
  86. $values = $this->filterValuesByEmbeddedMediaForm($values, '<?php echo $mediaRelation['local'] ?>');
  87. <?php endforeach; ?>
  88. parent::doBind($values);
  89. }
  90. public function processValues($values)
  91. {
  92. $values = parent::processValues($values);
  93. <?php foreach($this->getMediaRelations() as $mediaRelation): ?>
  94. $values = $this->processValuesForEmbeddedMediaForm($values, '<?php echo $mediaRelation['local'] ?>');
  95. <?php endforeach; ?>
  96. return $values;
  97. }
  98. protected function doUpdateObject($values)
  99. {
  100. parent::doUpdateObject($values);
  101. <?php foreach($this->getMediaRelations() as $mediaRelation): ?>
  102. $this->doUpdateObjectForEmbeddedMediaForm($values, '<?php echo $mediaRelation['local'] ?>', '<?php echo $mediaRelation['alias'] ?>');
  103. <?php endforeach; ?>
  104. }
  105. public function getModelName()
  106. {
  107. return '<?php echo $this->modelName ?>';
  108. }
  109. <?php if ($this->getManyToManyRelations()): ?>
  110. public function updateDefaultsFromObject()
  111. {
  112. parent::updateDefaultsFromObject();
  113. <?php foreach ($this->getManyToManyRelations() as $relation): ?>
  114. if (isset($this->widgetSchema['<?php echo $this->underscore($relation['alias']) ?>_list']))
  115. {
  116. $this->setDefault('<?php echo $this->underscore($relation['alias']) ?>_list', $this->object-><?php echo $relation['alias']; ?>->getPrimaryKeys());
  117. }
  118. <?php endforeach; ?>
  119. }
  120. protected function doSave($con = null)
  121. {
  122. <?php foreach ($this->getManyToManyRelations() as $relation): ?>
  123. $this->save<?php echo $relation['alias'] ?>List($con);
  124. <?php endforeach; ?>
  125. parent::doSave($con);
  126. }
  127. <?php foreach ($this->getManyToManyRelations() as $relation): ?>
  128. public function save<?php echo $relation['alias'] ?>List($con = null)
  129. {
  130. if (!$this->isValid())
  131. {
  132. throw $this->getErrorSchema();
  133. }
  134. if (!isset($this->widgetSchema['<?php echo $this->underscore($relation['alias']) ?>_list']))
  135. {
  136. // somebody has unset this widget
  137. return;
  138. }
  139. if (null === $con)
  140. {
  141. $con = $this->getConnection();
  142. }
  143. $existing = $this->object-><?php echo $relation['alias']; ?>->getPrimaryKeys();
  144. $values = $this->getValue('<?php echo $this->underscore($relation['alias']) ?>_list');
  145. if (!is_array($values))
  146. {
  147. $values = array();
  148. }
  149. $unlink = array_diff($existing, $values);
  150. if (count($unlink))
  151. {
  152. $this->object->unlink('<?php echo $relation['alias'] ?>', array_values($unlink));
  153. }
  154. $link = array_diff($values, $existing);
  155. if (count($link))
  156. {
  157. $this->object->link('<?php echo $relation['alias'] ?>', array_values($link));
  158. }
  159. }
  160. <?php endforeach; ?>
  161. <?php endif; ?>
  162. }