/dmCorePlugin/test/project/lib/form/doctrine/base/BaseDmTestPostForm.class.php

https://github.com/fospald/diem · PHP · 455 lines · 340 code · 66 blank · 49 comment · 60 complexity · c5d6d884922eecbaed3e14681bbe58e9 MD5 · raw file

  1. <?php
  2. /**
  3. * DmTestPost form base class.
  4. *
  5. * @method DmTestPost getObject() Returns the current form's model object
  6. *
  7. * @package retest
  8. * @subpackage form
  9. * @author Your name here
  10. * @version SVN: $Id$
  11. * @generator Diem 5.4.0-DEV
  12. */
  13. abstract class BaseDmTestPostForm extends BaseFormDoctrine
  14. {
  15. public function setup()
  16. {
  17. parent::setup();
  18. //column
  19. if($this->needsWidget('id')){
  20. $this->setWidget('id', new sfWidgetFormInputHidden());
  21. $this->setValidator('id', new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)));
  22. }
  23. //column
  24. if($this->needsWidget('date')){
  25. $this->setWidget('date', new sfWidgetFormDmDate());
  26. $this->setValidator('date', new dmValidatorDate());
  27. }
  28. //column
  29. if($this->needsWidget('created_at')){
  30. $this->setWidget('created_at', new sfWidgetFormDateTime());
  31. $this->setValidator('created_at', new sfValidatorDateTime());
  32. }
  33. //column
  34. if($this->needsWidget('updated_at')){
  35. $this->setWidget('updated_at', new sfWidgetFormDateTime());
  36. $this->setValidator('updated_at', new sfValidatorDateTime());
  37. }
  38. //column
  39. if($this->needsWidget('position')){
  40. $this->setWidget('position', new sfWidgetFormInputText());
  41. $this->setValidator('position', new sfValidatorInteger(array('required' => false)));
  42. }
  43. //many to many
  44. if($this->needsWidget('tags_list')){
  45. $this->setWidget('tags_list', new sfWidgetFormDmPaginatedDoctrineChoice(array('multiple' => true, 'model' => 'DmTestTag', 'expanded' => true)));
  46. $this->setValidator('tags_list', new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'DmTestTag', 'required' => false)));
  47. }
  48. //one to many
  49. if($this->needsWidget('dm_test_post_tag_list')){
  50. $this->setWidget('dm_test_post_tag_list', new sfWidgetFormDmPaginatedDoctrineChoice(array('multiple' => true, 'model' => 'DmTestPostTag', 'expanded' => true)));
  51. $this->setValidator('dm_test_post_tag_list', new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'DmTestPostTag', 'required' => false)));
  52. }
  53. //one to many
  54. if($this->needsWidget('comments_list')){
  55. $this->setWidget('comments_list', new sfWidgetFormDmPaginatedDoctrineChoice(array('multiple' => true, 'model' => 'DmTestComment', 'expanded' => true)));
  56. $this->setValidator('comments_list', new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'DmTestComment', 'required' => false)));
  57. }
  58. //one to many
  59. if($this->needsWidget('dm_test_post_dm_media_list')){
  60. $this->setWidget('dm_test_post_dm_media_list', new sfWidgetFormDmPaginatedDoctrineChoice(array('multiple' => true, 'model' => 'DmTestPostDmMedia', 'expanded' => true)));
  61. $this->setValidator('dm_test_post_dm_media_list', new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'DmTestPostDmMedia', 'required' => false)));
  62. }
  63. //one to one
  64. if($this->needsWidget('categ_id')){
  65. $this->setWidget('categ_id', new sfWidgetFormDmDoctrineChoice(array('multiple' => false, 'model' => 'DmTestCateg', 'expanded' => false)));
  66. $this->setValidator('categ_id', new sfValidatorDoctrineChoice(array('multiple' => false, 'model' => 'DmTestCateg', 'required' => true)));
  67. }
  68. //one to one
  69. if($this->needsWidget('user_id')){
  70. $this->setWidget('user_id', new sfWidgetFormDmDoctrineChoice(array('multiple' => false, 'model' => 'DmUser', 'expanded' => false)));
  71. $this->setValidator('user_id', new sfValidatorDoctrineChoice(array('multiple' => false, 'model' => 'DmUser', 'required' => true)));
  72. }
  73. //one to one
  74. if($this->needsWidget('image_id')){
  75. $this->setWidget('image_id', new sfWidgetFormDmDoctrineChoice(array('multiple' => false, 'model' => 'DmMedia', 'expanded' => false)));
  76. $this->setValidator('image_id', new sfValidatorDoctrineChoice(array('multiple' => false, 'model' => 'DmMedia', 'required' => false)));
  77. }
  78. //one to one
  79. if($this->needsWidget('file_id')){
  80. $this->setWidget('file_id', new sfWidgetFormDmDoctrineChoice(array('multiple' => false, 'model' => 'DmMedia', 'expanded' => false)));
  81. $this->setValidator('file_id', new sfValidatorDoctrineChoice(array('multiple' => false, 'model' => 'DmMedia', 'required' => false)));
  82. }
  83. //one to one
  84. if($this->needsWidget('created_by')){
  85. $this->setWidget('created_by', new sfWidgetFormDmDoctrineChoice(array('multiple' => false, 'model' => 'DmUser', 'expanded' => false)));
  86. $this->setValidator('created_by', new sfValidatorDoctrineChoice(array('multiple' => false, 'model' => 'DmUser', 'required' => false)));
  87. }
  88. /*
  89. * Embed Media form for image_id
  90. */
  91. if($this->needsWidget('image_id')){
  92. $this->embedForm('image_id_form', $this->createMediaFormForImageId());
  93. unset($this['image_id']);
  94. }
  95. /*
  96. * Embed Media form for file_id
  97. */
  98. if($this->needsWidget('file_id')){
  99. $this->embedForm('file_id_form', $this->createMediaFormForFileId());
  100. unset($this['file_id']);
  101. }
  102. if('embed' == sfConfig::get('dm_i18n_form'))
  103. {
  104. $this->embedI18n(sfConfig::get('dm_i18n_cultures'));
  105. }
  106. else
  107. {
  108. $this->mergeI18nForm();
  109. }
  110. $this->widgetSchema->setNameFormat('dm_test_post[%s]');
  111. $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
  112. $this->setupInheritance();
  113. parent::setup();
  114. // Unset automatic fields like 'created_at', 'updated_at', 'position'
  115. // override this method in your form to keep them
  116. parent::unsetAutoFields();
  117. }
  118. /**
  119. * Creates a DmMediaForm instance for image_id
  120. *
  121. * @return DmMediaForm a form instance for the related media
  122. */
  123. protected function createMediaFormForImageId()
  124. {
  125. return DmMediaForRecordForm::factory($this->object, 'image_id', 'Image', $this->validatorSchema['image_id']->getOption('required'));
  126. }
  127. /**
  128. * Creates a DmMediaForm instance for file_id
  129. *
  130. * @return DmMediaForm a form instance for the related media
  131. */
  132. protected function createMediaFormForFileId()
  133. {
  134. return DmMediaForRecordForm::factory($this->object, 'file_id', 'File', $this->validatorSchema['file_id']->getOption('required'));
  135. }
  136. protected function doBind(array $values)
  137. {
  138. $values = $this->filterValuesByEmbeddedMediaForm($values, 'image_id');
  139. $values = $this->filterValuesByEmbeddedMediaForm($values, 'file_id');
  140. parent::doBind($values);
  141. }
  142. public function processValues($values)
  143. {
  144. $values = parent::processValues($values);
  145. $values = $this->processValuesForEmbeddedMediaForm($values, 'image_id');
  146. $values = $this->processValuesForEmbeddedMediaForm($values, 'file_id');
  147. return $values;
  148. }
  149. protected function doUpdateObject($values)
  150. {
  151. parent::doUpdateObject($values);
  152. $this->doUpdateObjectForEmbeddedMediaForm($values, 'image_id', 'Image');
  153. $this->doUpdateObjectForEmbeddedMediaForm($values, 'file_id', 'File');
  154. }
  155. public function getModelName()
  156. {
  157. return 'DmTestPost';
  158. }
  159. public function updateDefaultsFromObject()
  160. {
  161. parent::updateDefaultsFromObject();
  162. if (isset($this->widgetSchema['tags_list']))
  163. {
  164. $this->setDefault('tags_list', array_merge((array)$this->getDefault('tags_list'),$this->object->Tags->getPrimaryKeys()));
  165. }
  166. if (isset($this->widgetSchema['medias_list']))
  167. {
  168. $this->setDefault('medias_list', array_merge((array)$this->getDefault('medias_list'),$this->object->Medias->getPrimaryKeys()));
  169. }
  170. if (isset($this->widgetSchema['dm_test_post_tag_list']))
  171. {
  172. $this->setDefault('dm_test_post_tag_list', array_merge((array)$this->getDefault('dm_test_post_tag_list'),$this->object->DmTestPostTag->getPrimaryKeys()));
  173. }
  174. if (isset($this->widgetSchema['comments_list']))
  175. {
  176. $this->setDefault('comments_list', array_merge((array)$this->getDefault('comments_list'),$this->object->Comments->getPrimaryKeys()));
  177. }
  178. if (isset($this->widgetSchema['translation_list']))
  179. {
  180. $this->setDefault('translation_list', array_merge((array)$this->getDefault('translation_list'),$this->object->Translation->getPrimaryKeys()));
  181. }
  182. if (isset($this->widgetSchema['dm_test_post_dm_media_list']))
  183. {
  184. $this->setDefault('dm_test_post_dm_media_list', array_merge((array)$this->getDefault('dm_test_post_dm_media_list'),$this->object->DmTestPostDmMedia->getPrimaryKeys()));
  185. }
  186. }
  187. protected function doSave($con = null)
  188. {
  189. $this->saveTagsList($con);
  190. $this->saveMediasList($con);
  191. $this->saveDmTestPostTagList($con);
  192. $this->saveCommentsList($con);
  193. $this->saveTranslationList($con);
  194. $this->saveDmTestPostDmMediaList($con);
  195. parent::doSave($con);
  196. }
  197. public function saveTagsList($con = null)
  198. {
  199. if (!$this->isValid())
  200. {
  201. throw $this->getErrorSchema();
  202. }
  203. if (!isset($this->widgetSchema['tags_list']))
  204. {
  205. // somebody has unset this widget
  206. return;
  207. }
  208. if (null === $con)
  209. {
  210. $con = $this->getConnection();
  211. }
  212. $existing = $this->object->Tags->getPrimaryKeys();
  213. $values = $this->getValue('tags_list');
  214. if (!is_array($values))
  215. {
  216. $values = array();
  217. }
  218. $unlink = array_diff($existing, $values);
  219. if (count($unlink))
  220. {
  221. $this->object->unlink('Tags', array_values($unlink));
  222. }
  223. $link = array_diff($values, $existing);
  224. if (count($link))
  225. {
  226. $this->object->link('Tags', array_values($link));
  227. }
  228. }
  229. public function saveMediasList($con = null)
  230. {
  231. if (!$this->isValid())
  232. {
  233. throw $this->getErrorSchema();
  234. }
  235. if (!isset($this->widgetSchema['medias_list']))
  236. {
  237. // somebody has unset this widget
  238. return;
  239. }
  240. if (null === $con)
  241. {
  242. $con = $this->getConnection();
  243. }
  244. $existing = $this->object->Medias->getPrimaryKeys();
  245. $values = $this->getValue('medias_list');
  246. if (!is_array($values))
  247. {
  248. $values = array();
  249. }
  250. $unlink = array_diff($existing, $values);
  251. if (count($unlink))
  252. {
  253. $this->object->unlink('Medias', array_values($unlink));
  254. }
  255. $link = array_diff($values, $existing);
  256. if (count($link))
  257. {
  258. $this->object->link('Medias', array_values($link));
  259. }
  260. }
  261. public function saveDmTestPostTagList($con = null)
  262. {
  263. if (!$this->isValid())
  264. {
  265. throw $this->getErrorSchema();
  266. }
  267. if (!isset($this->widgetSchema['dm_test_post_tag_list']))
  268. {
  269. // somebody has unset this widget
  270. return;
  271. }
  272. if (null === $con)
  273. {
  274. $con = $this->getConnection();
  275. }
  276. $existing = $this->object->DmTestPostTag->getPrimaryKeys();
  277. $values = $this->getValue('dm_test_post_tag_list');
  278. if (!is_array($values))
  279. {
  280. $values = array();
  281. }
  282. $unlink = array_diff($existing, $values);
  283. if (count($unlink))
  284. {
  285. $this->object->unlink('DmTestPostTag', array_values($unlink));
  286. }
  287. $link = array_diff($values, $existing);
  288. if (count($link))
  289. {
  290. $this->object->link('DmTestPostTag', array_values($link));
  291. }
  292. }
  293. public function saveCommentsList($con = null)
  294. {
  295. if (!$this->isValid())
  296. {
  297. throw $this->getErrorSchema();
  298. }
  299. if (!isset($this->widgetSchema['comments_list']))
  300. {
  301. // somebody has unset this widget
  302. return;
  303. }
  304. if (null === $con)
  305. {
  306. $con = $this->getConnection();
  307. }
  308. $existing = $this->object->Comments->getPrimaryKeys();
  309. $values = $this->getValue('comments_list');
  310. if (!is_array($values))
  311. {
  312. $values = array();
  313. }
  314. $unlink = array_diff($existing, $values);
  315. if (count($unlink))
  316. {
  317. $this->object->unlink('Comments', array_values($unlink));
  318. }
  319. $link = array_diff($values, $existing);
  320. if (count($link))
  321. {
  322. $this->object->link('Comments', array_values($link));
  323. }
  324. }
  325. public function saveTranslationList($con = null)
  326. {
  327. if (!$this->isValid())
  328. {
  329. throw $this->getErrorSchema();
  330. }
  331. if (!isset($this->widgetSchema['translation_list']))
  332. {
  333. // somebody has unset this widget
  334. return;
  335. }
  336. if (null === $con)
  337. {
  338. $con = $this->getConnection();
  339. }
  340. $existing = $this->object->Translation->getPrimaryKeys();
  341. $values = $this->getValue('translation_list');
  342. if (!is_array($values))
  343. {
  344. $values = array();
  345. }
  346. $unlink = array_diff($existing, $values);
  347. if (count($unlink))
  348. {
  349. $this->object->unlink('Translation', array_values($unlink));
  350. }
  351. $link = array_diff($values, $existing);
  352. if (count($link))
  353. {
  354. $this->object->link('Translation', array_values($link));
  355. }
  356. }
  357. public function saveDmTestPostDmMediaList($con = null)
  358. {
  359. if (!$this->isValid())
  360. {
  361. throw $this->getErrorSchema();
  362. }
  363. if (!isset($this->widgetSchema['dm_test_post_dm_media_list']))
  364. {
  365. // somebody has unset this widget
  366. return;
  367. }
  368. if (null === $con)
  369. {
  370. $con = $this->getConnection();
  371. }
  372. $existing = $this->object->DmTestPostDmMedia->getPrimaryKeys();
  373. $values = $this->getValue('dm_test_post_dm_media_list');
  374. if (!is_array($values))
  375. {
  376. $values = array();
  377. }
  378. $unlink = array_diff($existing, $values);
  379. if (count($unlink))
  380. {
  381. $this->object->unlink('DmTestPostDmMedia', array_values($unlink));
  382. }
  383. $link = array_diff($values, $existing);
  384. if (count($link))
  385. {
  386. $this->object->link('DmTestPostDmMedia', array_values($link));
  387. }
  388. }
  389. }