/plugins/pkToolkitPlugin/lib/pkSubCrudActions.class.php

https://github.com/staunchRobots/MorrillAndCompany · PHP · 153 lines · 102 code · 28 blank · 23 comment · 9 complexity · 43365b9878bddf20a96b386dd2c88931 MD5 · raw file

  1. <?php
  2. // A typical Doctrine route collection CRUD action class framework, with the addition of support for subforms that
  3. // edit subsets of the object's fields via AJAX. Note that the name of your module determines the name of the
  4. // variable. TODO: a list of allowed subforms (although the existence of the class is
  5. // a good first pass at that).
  6. // TODO: think about whether $singular and $list are worth the trouble. It's nice to
  7. // refer to things as 'event' and 'events' rather than 'item' and 'items' in templates,
  8. // but this code would be more readable if we dumped the metavariables
  9. class pkSubCrudActions extends sfActions
  10. {
  11. // These must be public to allow poor-man's mix-ins like pkRosterTools to work.
  12. // You can set them explicitly in your subclass initialize() if the default guesses
  13. // do not work for you (see initialize() below).
  14. // The module we're in (this is always set correctly by initialize() below)
  15. public $module;
  16. // The singular, lowercase name of the type we're editing; for model class Event this is typically 'event'.
  17. // $this->$singular is often set by methods here and in pkRosterTools, allowing $this->event to be referenced
  18. // in subclass code for convenience. By default, the module name with the first character lowercased
  19. public $singular;
  20. // The plural name, by default event_list if singular is event
  21. public $list;
  22. // The model class name, by default ucfirst() of $singular
  23. public $model;
  24. public function initialize($context, $moduleName, $actionName)
  25. {
  26. parent::initialize($context, $moduleName, $actionName);
  27. $this->module = $moduleName;
  28. if (!isset($this->singular))
  29. {
  30. // 5.2.x doesn't have lcfirst(), that arrives in 5.3.0
  31. $this->singular = strtolower(substr($this->module, 0, 1)) . substr($this->module, 1);
  32. }
  33. $this->list = $this->singular . "_list";
  34. if (!isset($this->model))
  35. {
  36. $this->model = ucfirst($this->singular);
  37. }
  38. }
  39. public function executeIndex(sfWebRequest $request)
  40. {
  41. $list = $this->list;
  42. $this->$list = $this->getRoute()->getObjects();
  43. }
  44. public function executeShow(sfWebRequest $request)
  45. {
  46. $singular = $this->singular;
  47. $this->$singular = $this->getRoute()->getObject();
  48. }
  49. public function executeEdit(sfWebRequest $request)
  50. {
  51. $this->getForm($request);
  52. return 'Ajax';
  53. }
  54. public function executeUpdate(sfWebRequest $request)
  55. {
  56. $this->getForm($request);
  57. if ($this->processForm($request, $this->form))
  58. {
  59. return $this->renderPartial($this->module . '/' . $this->form->subtype);
  60. }
  61. $this->setTemplate('edit');
  62. return 'Ajax';
  63. }
  64. public function executeDelete(sfWebRequest $request)
  65. {
  66. $request->checkCSRFProtection();
  67. $this->getRoute()->getObject()->delete();
  68. $this->redirect($this->module . '/index');
  69. }
  70. public function executeNew(sfWebRequest $request)
  71. {
  72. $className = $this->model . 'CreateForm';
  73. $this->form = new $className();
  74. }
  75. public function executeCreate(sfWebRequest $request)
  76. {
  77. $className = $this->model . 'CreateForm';
  78. $this->form = new $className();
  79. if ($this->processForm($request, $this->form))
  80. {
  81. $singular = $this->singular;
  82. return $this->redirect($this->module . '/show?id='.$this->$singular->id);
  83. }
  84. $this->setTemplate('new');
  85. }
  86. protected function processForm(sfWebRequest $request, sfForm $form)
  87. {
  88. $form->bind($request->getParameter($form->getName()));
  89. if ($form->isValid())
  90. {
  91. $singular = $this->singular;
  92. $this->$singular = $form->save();
  93. // Without this, one-to-many relationships don't show the
  94. // effects of the changes we just made when we render the partial
  95. // for the static view
  96. $this->$singular->refreshRelated();
  97. return true;
  98. }
  99. return false;
  100. }
  101. protected function getForm($request)
  102. {
  103. if ($request->hasParameter('form'))
  104. {
  105. $class = pkSubCrudTools::getFormClass($this->model, $request->getParameter('form'));
  106. // Custom form getters in the subform classes allow for dependency objection in a way
  107. // that permits a chunk to operate on a relation class (like EventUser) or an unrelated class (like sfGuardUserProfile)
  108. // rather than directly on the object itself (like Event)
  109. if (method_exists($class, 'getForm'))
  110. {
  111. $this->form = call_user_func(array($class, 'getForm'), $this->getRoute()->getObject(), $request);
  112. }
  113. else
  114. {
  115. $this->form = new $class($this->getRoute()->getObject());
  116. }
  117. if (method_exists($this->form, 'userCanEdit') && (!$this->form->userCanEdit()))
  118. {
  119. $this->forward404();
  120. }
  121. return;
  122. }
  123. throw new sfException('No form parameter.');
  124. }
  125. }