/src/Backend/Modules/Mailmotor/Actions/EditGroup.php

http://github.com/forkcms/forkcms · PHP · 140 lines · 76 code · 21 blank · 43 comment · 8 complexity · 841e1e860fce8e01777374035247462b MD5 · raw file

  1. <?php
  2. namespace Backend\Modules\Mailmotor\Actions;
  3. /*
  4. * This file is part of Fork CMS.
  5. *
  6. * For the full copyright and license information, please view the license
  7. * file that was distributed with this source code.
  8. */
  9. use Backend\Core\Engine\Base\ActionEdit as BackendBaseActionEdit;
  10. use Backend\Core\Engine\Form as BackendForm;
  11. use Backend\Core\Engine\Language as BL;
  12. use Backend\Core\Engine\Model as BackendModel;
  13. use Backend\Modules\Mailmotor\Engine\Model as BackendMailmotorModel;
  14. use Backend\Modules\Mailmotor\Engine\CMHelper as BackendMailmotorCMHelper;
  15. /**
  16. * This is the edit-action, it will display a form to edit a group
  17. */
  18. class EditGroup extends BackendBaseActionEdit
  19. {
  20. /**
  21. * Execute the action
  22. */
  23. public function execute()
  24. {
  25. // get parameters
  26. $this->id = $this->getParameter('id', 'int');
  27. // does the item exist
  28. if (BackendMailmotorModel::existsGroup($this->id)) {
  29. parent::execute();
  30. $this->getData();
  31. $this->loadForm();
  32. $this->validateForm();
  33. $this->parse();
  34. $this->display();
  35. } else {
  36. $this->redirect(BackendModel::createURLForAction('Groups') . '&error=non-existing');
  37. }
  38. }
  39. /**
  40. * Get the data
  41. */
  42. private function getData()
  43. {
  44. // get the record
  45. $this->record = (array) BackendMailmotorModel::getGroup($this->id);
  46. // no item found, throw an exceptions, because somebody is fucking with our URL
  47. if (empty($this->record)) {
  48. $this->redirect(BackendModel::createURLForAction('Groups') . '&error=non-existing');
  49. }
  50. }
  51. /**
  52. * Load the form
  53. */
  54. private function loadForm()
  55. {
  56. // create form
  57. $this->frm = new BackendForm('edit');
  58. // add "no default group" option for radiobuttons
  59. $chkDefaultForLanguageValues[] = array('label' => BL::msg('NoDefault'), 'value' => '0');
  60. // set default for language radiobutton values
  61. foreach (BL::getWorkingLanguages() as $key => $value) {
  62. $chkDefaultForLanguageValues[] = array('label' => $value, 'value' => $key);
  63. }
  64. // create elements
  65. $this->frm->addText('name', $this->record['name']);
  66. $this->frm->addRadiobutton('default', $chkDefaultForLanguageValues, $this->record['language']);
  67. }
  68. /**
  69. * Parse the form
  70. */
  71. protected function parse()
  72. {
  73. parent::parse();
  74. // assign the active record and additional variables
  75. $this->tpl->assign('group', $this->record);
  76. }
  77. /**
  78. * Validate the form
  79. */
  80. private function validateForm()
  81. {
  82. // is the form submitted?
  83. if ($this->frm->isSubmitted()) {
  84. // cleanup the submitted fields, ignore fields that were added by hackers
  85. $this->frm->cleanupFields();
  86. // shorten fields
  87. $txtName = $this->frm->getField('name');
  88. $rbtDefaultForLanguage = $this->frm->getField('default');
  89. // validate fields
  90. if ($txtName->isFilled(BL::err('NameIsRequired'))) {
  91. if ($txtName->getValue() != $this->record['name'] &&
  92. BackendMailmotorModel::existsGroupByName($txtName->getValue())
  93. ) {
  94. $txtName->addError(BL::err('GroupAlreadyExists'));
  95. }
  96. }
  97. // no errors?
  98. if ($this->frm->isCorrect()) {
  99. // build item
  100. $item['id'] = $this->id;
  101. $item['name'] = $txtName->getValue();
  102. $item['language'] = $rbtDefaultForLanguage->getValue() === '0' ? null : $rbtDefaultForLanguage->getValue();
  103. $item['is_default'] = $rbtDefaultForLanguage->getChecked() ? 'Y' : 'N';
  104. // update the item
  105. BackendMailmotorCMHelper::updateGroup($item);
  106. // check if all default groups were set
  107. BackendMailmotorModel::checkDefaultGroups();
  108. // trigger event
  109. BackendModel::triggerEvent($this->getModule(), 'after_edit_group', array('item' => $item));
  110. // everything is saved, so redirect to the overview
  111. $this->redirect(
  112. BackendModel::createURLForAction('Groups') . '&report=edited&var=' . rawurlencode(
  113. $item['name']
  114. ) . '&highlight=id-' . $item['id']
  115. );
  116. }
  117. }
  118. }
  119. }