/lib/form/doctrine/sfDoctrineGuardPlugin/base/BasesfGuardPermissionForm.class.php

https://github.com/frhumanes/PLM · PHP · 155 lines · 116 code · 27 blank · 12 comment · 14 complexity · 9f52ae9160f10259583b6d546f271ef6 MD5 · raw file

  1. <?php
  2. /**
  3. * sfGuardPermission form base class.
  4. *
  5. * @method sfGuardPermission getObject() Returns the current form's model object
  6. *
  7. * @package jobeet
  8. * @subpackage form
  9. * @author Your name here
  10. * @version SVN: $Id: sfDoctrineFormGeneratedTemplate.php 24171 2009-11-19 16:37:50Z Kris.Wallsmith $
  11. */
  12. abstract class BasesfGuardPermissionForm 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. 'groups_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardGroup')),
  23. 'users_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardUser')),
  24. ));
  25. $this->setValidators(array(
  26. 'id' => new sfValidatorDoctrineChoice(array('model' => $this->getModelName(), 'column' => 'id', 'required' => false)),
  27. 'name' => new sfValidatorString(array('max_length' => 255, 'required' => false)),
  28. 'description' => new sfValidatorString(array('max_length' => 1000, 'required' => false)),
  29. 'created_at' => new sfValidatorDateTime(),
  30. 'updated_at' => new sfValidatorDateTime(),
  31. 'groups_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardGroup', 'required' => false)),
  32. 'users_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardUser', 'required' => false)),
  33. ));
  34. $this->validatorSchema->setPostValidator(
  35. new sfValidatorDoctrineUnique(array('model' => 'sfGuardPermission', 'column' => array('name')))
  36. );
  37. $this->widgetSchema->setNameFormat('sf_guard_permission[%s]');
  38. $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
  39. $this->setupInheritance();
  40. parent::setup();
  41. }
  42. public function getModelName()
  43. {
  44. return 'sfGuardPermission';
  45. }
  46. public function updateDefaultsFromObject()
  47. {
  48. parent::updateDefaultsFromObject();
  49. if (isset($this->widgetSchema['groups_list']))
  50. {
  51. $this->setDefault('groups_list', $this->object->Groups->getPrimaryKeys());
  52. }
  53. if (isset($this->widgetSchema['users_list']))
  54. {
  55. $this->setDefault('users_list', $this->object->Users->getPrimaryKeys());
  56. }
  57. }
  58. protected function doSave($con = null)
  59. {
  60. $this->saveGroupsList($con);
  61. $this->saveUsersList($con);
  62. parent::doSave($con);
  63. }
  64. public function saveGroupsList($con = null)
  65. {
  66. if (!$this->isValid())
  67. {
  68. throw $this->getErrorSchema();
  69. }
  70. if (!isset($this->widgetSchema['groups_list']))
  71. {
  72. // somebody has unset this widget
  73. return;
  74. }
  75. if (null === $con)
  76. {
  77. $con = $this->getConnection();
  78. }
  79. $existing = $this->object->Groups->getPrimaryKeys();
  80. $values = $this->getValue('groups_list');
  81. if (!is_array($values))
  82. {
  83. $values = array();
  84. }
  85. $unlink = array_diff($existing, $values);
  86. if (count($unlink))
  87. {
  88. $this->object->unlink('Groups', array_values($unlink));
  89. }
  90. $link = array_diff($values, $existing);
  91. if (count($link))
  92. {
  93. $this->object->link('Groups', array_values($link));
  94. }
  95. }
  96. public function saveUsersList($con = null)
  97. {
  98. if (!$this->isValid())
  99. {
  100. throw $this->getErrorSchema();
  101. }
  102. if (!isset($this->widgetSchema['users_list']))
  103. {
  104. // somebody has unset this widget
  105. return;
  106. }
  107. if (null === $con)
  108. {
  109. $con = $this->getConnection();
  110. }
  111. $existing = $this->object->Users->getPrimaryKeys();
  112. $values = $this->getValue('users_list');
  113. if (!is_array($values))
  114. {
  115. $values = array();
  116. }
  117. $unlink = array_diff($existing, $values);
  118. if (count($unlink))
  119. {
  120. $this->object->unlink('Users', array_values($unlink));
  121. }
  122. $link = array_diff($values, $existing);
  123. if (count($link))
  124. {
  125. $this->object->link('Users', array_values($link));
  126. }
  127. }
  128. }