PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/backend/modules/form_builder/actions/edit.php

http://github.com/forkcms/forkcms
PHP | 246 lines | 141 code | 40 blank | 65 comment | 15 complexity | ab864d0907812dff1318d0821cf65175 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, MIT, AGPL-3.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of Fork CMS.
  4. *
  5. * For the full copyright and license information, please view the license
  6. * file that was distributed with this source code.
  7. */
  8. /**
  9. * This is the edit-action, it will display a form to edit an existing item
  10. *
  11. * @author Dieter Vanden Eynde <dieter.vandeneynde@netlash.com>
  12. * @author Tijs Verkoyen <tijs@sumocoders.be>
  13. */
  14. class BackendFormBuilderEdit extends BackendBaseActionEdit
  15. {
  16. /**
  17. * Execute the action
  18. */
  19. public function execute()
  20. {
  21. $this->id = $this->getParameter('id', 'int');
  22. // does the item exist
  23. if($this->id !== null && BackendFormBuilderModel::exists($this->id))
  24. {
  25. parent::execute();
  26. $this->getData();
  27. $this->loadForm();
  28. $this->validateForm();
  29. $this->parse();
  30. $this->display();
  31. }
  32. // no item found, throw an exceptions, because somebody is fucking with our url
  33. else $this->redirect(BackendModel::createURLForAction('index') . '&error=non-existing');
  34. }
  35. /**
  36. * Get the data
  37. */
  38. private function getData()
  39. {
  40. $this->record = BackendFormBuilderModel::get($this->id);
  41. }
  42. /**
  43. * Load the form
  44. */
  45. private function loadForm()
  46. {
  47. $this->frm = new BackendForm('edit');
  48. $this->frm->addText('name', $this->record['name']);
  49. $this->frm->addDropdown('method', array('database' => BL::getLabel('MethodDatabase'), 'database_email' => BL::getLabel('MethodDatabaseEmail')), $this->record['method']);
  50. $this->frm->addText('email', implode(',', (array) $this->record['email']));
  51. $this->frm->addText('identifier', $this->record['identifier']);
  52. $this->frm->addEditor('success_message', $this->record['success_message']);
  53. // textfield dialog
  54. $this->frm->addText('textbox_label');
  55. $this->frm->addText('textbox_value');
  56. $this->frm->addCheckbox('textbox_required');
  57. $this->frm->addText('textbox_required_error_message');
  58. $this->frm->addDropdown('textbox_validation', array('' => '', 'email' => BL::getLabel('Email'), 'numeric' => BL::getLabel('Numeric')));
  59. $this->frm->addText('textbox_validation_parameter');
  60. $this->frm->addText('textbox_error_message');
  61. // textarea dialog
  62. $this->frm->addText('textarea_label');
  63. $this->frm->addTextarea('textarea_value');
  64. $this->frm->getField('textarea_value')->setAttribute('cols', 30);
  65. $this->frm->addCheckbox('textarea_required');
  66. $this->frm->addText('textarea_required_error_message');
  67. $this->frm->addDropdown('textarea_validation', array('' => ''));
  68. $this->frm->addText('textarea_validation_parameter');
  69. $this->frm->addText('textarea_error_message');
  70. // dropdown dialog
  71. $this->frm->addText('dropdown_label');
  72. $this->frm->addText('dropdown_values');
  73. $this->frm->addDropdown('dropdown_default_value', array('' => ''))->setAttribute('rel', 'dropDownValues');
  74. $this->frm->addCheckbox('dropdown_required');
  75. $this->frm->addText('dropdown_required_error_message');
  76. // radiobutton dialog
  77. $this->frm->addText('radiobutton_label');
  78. $this->frm->addText('radiobutton_values');
  79. $this->frm->addDropdown('radiobutton_default_value', array('' => ''))->setAttribute('rel', 'radioButtonValues');
  80. $this->frm->addCheckbox('radiobutton_required');
  81. $this->frm->addText('radiobutton_required_error_message');
  82. // checkbox dialog
  83. $this->frm->addText('checkbox_label');
  84. $this->frm->addText('checkbox_values');
  85. $this->frm->addDropdown('checkbox_default_value', array('' => ''))->setAttribute('rel', 'checkBoxValues');
  86. $this->frm->addCheckbox('checkbox_required');
  87. $this->frm->addText('checkbox_required_error_message');
  88. // heading dialog
  89. $this->frm->addText('heading');
  90. // paragraph dialog
  91. $this->frm->addEditor('paragraph');
  92. $this->frm->getField('paragraph')->setAttribute('cols', 30);
  93. // submit dialog
  94. $this->frm->addText('submit');
  95. }
  96. /**
  97. * Parse the form
  98. */
  99. protected function parse()
  100. {
  101. $this->parseFields();
  102. parent::parse();
  103. $this->tpl->assign('id', $this->record['id']);
  104. $this->tpl->assign('name', $this->record['name']);
  105. // parse error messages
  106. $this->parseErrorMessages();
  107. }
  108. /**
  109. * Parse the default error messages
  110. */
  111. private function parseErrorMessages()
  112. {
  113. // set frontend locale
  114. FL::setLocale(BL::getWorkingLanguage());
  115. // assign error messages
  116. $this->tpl->assign('errors', BackendFormBuilderModel::getErrors());
  117. }
  118. /**
  119. * Parse the fields
  120. */
  121. private function parseFields()
  122. {
  123. $fieldsHTML = array();
  124. // get fields
  125. $fields = BackendFormBuilderModel::getFields($this->id);
  126. // loop fields
  127. foreach($fields as $field)
  128. {
  129. // submit button
  130. if($field['type'] == 'submit')
  131. {
  132. // assign
  133. $this->tpl->assign('submitId', $field['id']);
  134. // add field
  135. $btn = $this->frm->addButton('submit_field', SpoonFilter::htmlspecialcharsDecode($field['settings']['values']), 'button');
  136. $btn->setAttribute('disabled', 'disabled');
  137. // skip
  138. continue;
  139. }
  140. // parse field to html
  141. $fieldsHTML[]['field'] = FormBuilderHelper::parseField($field);
  142. }
  143. // assign iteration
  144. $this->tpl->assign('fields', $fieldsHTML);
  145. }
  146. /**
  147. * Validate the form
  148. */
  149. private function validateForm()
  150. {
  151. if($this->frm->isSubmitted())
  152. {
  153. $this->frm->cleanupFields();
  154. // shorten the fields
  155. $txtName = $this->frm->getField('name');
  156. $txtEmail = $this->frm->getField('email');
  157. $ddmMethod = $this->frm->getField('method');
  158. $txtSuccessMessage = $this->frm->getField('success_message');
  159. $txtIdentifier = $this->frm->getField('identifier');
  160. $emailAddresses = (array) explode(',', $txtEmail->getValue());
  161. // validate fields
  162. $txtName->isFilled(BL::getError('NameIsRequired'));
  163. $txtSuccessMessage->isFilled(BL::getError('SuccessMessageIsRequired'));
  164. if($ddmMethod->isFilled(BL::getError('NameIsRequired')) && $ddmMethod->getValue() == 'database_email')
  165. {
  166. $error = false;
  167. // check the addresses
  168. foreach($emailAddresses as $address)
  169. {
  170. $address = trim($address);
  171. if(!SpoonFilter::isEmail($address))
  172. {
  173. $error = true;
  174. break;
  175. }
  176. }
  177. // add error
  178. if($error) $txtEmail->addError(BL::getError('EmailIsInvalid'));
  179. }
  180. // identifier
  181. if($txtIdentifier->isFilled())
  182. {
  183. // invalid characters
  184. if(!SpoonFilter::isValidAgainstRegexp('/^[a-zA-Z0-9\.\_\-]+$/', $txtIdentifier->getValue())) $txtIdentifier->setError(BL::getError('InvalidIdentifier'));
  185. // unique identifier
  186. elseif(BackendFormBuilderModel::existsIdentifier($txtIdentifier->getValue(), $this->id)) $txtIdentifier->setError(BL::getError('UniqueIdentifier'));
  187. }
  188. if($this->frm->isCorrect())
  189. {
  190. // build array
  191. $values['name'] = $txtName->getValue();
  192. $values['method'] = $ddmMethod->getValue();
  193. $values['email'] = ($ddmMethod->getValue() == 'database_email') ? serialize($emailAddresses) : null;
  194. $values['success_message'] = $txtSuccessMessage->getValue(true);
  195. $values['identifier'] = ($txtIdentifier->isFilled() ? $txtIdentifier->getValue() : BackendFormBuilderModel::createIdentifier());
  196. $values['edited_on'] = BackendModel::getUTCDate();
  197. // insert the item
  198. $id = (int) BackendFormBuilderModel::update($this->id, $values);
  199. // trigger event
  200. BackendModel::triggerEvent($this->getModule(), 'after_edit', array('item' => $values));
  201. // everything is saved, so redirect to the overview
  202. $this->redirect(BackendModel::createURLForAction('index') . '&report=edited&var=' . urlencode($values['name']) . '&highlight=row-' . $id);
  203. }
  204. }
  205. }
  206. }