PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/content/content.sets.php

https://github.com/carsten/globalparamloader
PHP | 455 lines | 334 code | 104 blank | 17 comment | 35 complexity | f4357cad96dd8605251a8615aedc9dad MD5 | raw file
  1. <?php
  2. require_once(TOOLKIT . '/class.administrationpage.php');
  3. class contentExtensionGlobalParamLoaderSets extends AdministrationPage {
  4. protected $_action = '';
  5. protected $_conditions = array();
  6. protected $_driver = null;
  7. protected $_editing = false;
  8. protected $_errors = array();
  9. protected $_fields = array();
  10. protected $_pages = array();
  11. protected $_prepared = false;
  12. protected $_status = '';
  13. protected $_sets = array();
  14. protected $_uri = null;
  15. protected $_valid = true;
  16. public function __construct(&$parent){
  17. parent::__construct($parent);
  18. $this->_uri = URL . '/symphony/extension/globalparamloader';
  19. $this->_driver = $this->_Parent->ExtensionManager->create('globalparamloader');
  20. }
  21. public function build($context) {
  22. if (@$context[0] == 'edit' or @$context[0] == 'new') {
  23. if ($this->_editing = $context[0] == 'edit') {
  24. $this->_fields = $this->_driver->getSet((integer)$context[1]);
  25. $this->_params = $this->_driver->getParameters((integer)$context[1]);
  26. }
  27. $this->_fields = (isset($_POST['fields']) ? $_POST['fields'] : $this->_fields);
  28. $this->_params = (isset($_POST['params']) ? $_POST['params'] : $this->_params);
  29. $this->_status = $context[2];
  30. $this->_pages = $this->_driver->getPages();
  31. } else {
  32. $this->_sets = $this->_driver->getSets();
  33. }
  34. parent::build($context);
  35. }
  36. public function __actionNew() {
  37. $this->__actionEdit();
  38. }
  39. public function __actionEdit() {
  40. if (@array_key_exists('delete', $_POST['action'])) {
  41. $this->__actionEditDelete();
  42. } else {
  43. $this->__actionEditNormal();
  44. }
  45. }
  46. public function __actionEditDelete() {
  47. $this->_Parent->Database->delete('tbl_gpl_sets', " `id` = '{$this->_fields['id']}'");
  48. $this->_Parent->Database->delete('tbl_gpl_params', " `set_id` = '{$this->_fields['id']}'");
  49. redirect("{$this->_uri}/sets/");
  50. }
  51. public function __actionEditNormal() {
  52. // Validate: ----------------------------------------------------------
  53. if (empty($this->_fields['name'])) {
  54. $this->_errors['name'] = 'Name must not be empty.';
  55. }
  56. if (empty($this->_params)) {
  57. $this->_errors['params'] = 'Parameters must not be empty.';
  58. }
  59. foreach ($this->_params as $sortorder => $param) {
  60. if (empty($param['param'])) {
  61. $this->_errors["{$sortorder}:param"] = 'Parameter must not be empty.';
  62. }
  63. }
  64. if (!empty($this->_errors)) {
  65. $this->_valid = false;
  66. return;
  67. }
  68. // Save: --------------------------------------------------------------
  69. $this->_fields['params'] = (integer)count($this->_params);
  70. $this->_fields['exclude_page'] = implode(',', $this->_fields['exclude_page']);
  71. $this->_Parent->Database->insert($this->_fields, 'tbl_gpl_sets', true);
  72. $this->_Parent->Database->update($this->_fields, 'tbl_gpl_sets', "`id` = '".$this->_fields['id']."'");
  73. if (!$this->_editing) {
  74. $redirect_mode = 'created';
  75. $set_id = $this->_Parent->Database->fetchVar('id', 0, "
  76. SELECT
  77. s.id
  78. FROM
  79. `tbl_gpl_sets` AS s
  80. ORDER BY
  81. s.id DESC
  82. LIMIT 1
  83. ");
  84. } else {
  85. $redirect_mode = 'saved';
  86. $set_id = $this->_fields['id'];
  87. }
  88. // Remove all parameters before adding existing ones
  89. $this->_Parent->Database->delete('tbl_gpl_params', " `set_id` = '{$this->_fields['id']}'");
  90. foreach ($this->_params as $param) {
  91. $param['set_id'] = $set_id;
  92. $this->_Parent->Database->insert($param, 'tbl_gpl_params', true);
  93. }
  94. redirect("{$this->_uri}/sets/edit/{$set_id}/{$redirect_mode}/");
  95. }
  96. public function __viewNew() {
  97. $this->__viewEdit();
  98. }
  99. public function __viewEdit() {
  100. // Status: -----------------------------------------------------------
  101. if (!$this->_valid) $this->pageAlert('
  102. An error occurred while processing this form.
  103. <a href="#error">See below for details.</a>',
  104. AdministrationPage::PAGE_ALERT_ERROR
  105. );
  106. // Status message:
  107. if ($this->_status) {
  108. $action = null;
  109. switch($this->_status) {
  110. case 'saved': $action = 'updated'; break;
  111. case 'created': $action = 'created'; break;
  112. case 'removed': $action = 'removed'; break;
  113. }
  114. if ($action) $this->pageAlert(
  115. "Set {$this->_status} successfully. <a href=\"{$this->_uri}/sets/new/\">Create another?</a>",
  116. AdministrationPage::PAGE_ALERT_NOTICE, array(
  117. $action, URL, 'extension/globalparamloader/sets/new/'
  118. )
  119. );
  120. }
  121. // Edit:
  122. if ($this->_action == 'edit') {
  123. if ($this->_set > 0) {
  124. $row = $this->_Parent->Database->fetchRow(0, "
  125. SELECT
  126. s.*
  127. FROM
  128. `tbl_gpl_sets` AS s
  129. WHERE
  130. s.id = {$this->_set}
  131. ");
  132. if (!empty($row)) {
  133. $this->_fields = $row;
  134. } else {
  135. $this->_editing = false;
  136. }
  137. }
  138. }
  139. // Header: ------------------------------------------------------------
  140. $this->setPageType('form');
  141. $this->setTitle('Symphony &ndash; Global Parameter Sets' . (
  142. $this->_editing ? ' &ndash; ' . $this->_fields['name'] : null
  143. ));
  144. $this->appendSubheading("<a href=\"{$this->_uri}/sets/\">Parameter Sets</a> &mdash; " . (
  145. $this->_editing ? $this->_fields['name'] : 'Untitled'
  146. ));
  147. // Form: --------------------------------------------------------------
  148. $fieldset = new XMLElement('fieldset');
  149. $fieldset->setAttribute('class', 'settings');
  150. $fieldset->appendChild(new XMLElement('legend', 'Essentials'));
  151. if (!empty($this->_fields['id'])) {
  152. $fieldset->appendChild(Widget::Input("fields[id]", $this->_fields['id'], 'hidden'));
  153. }
  154. $label = Widget::Label('Name');
  155. $label->appendChild(Widget::Input(
  156. 'fields[name]',
  157. General::sanitize(@$this->_fields['name'])
  158. ));
  159. if (isset($this->_errors['name'])) {
  160. $label = Widget::wrapFormElementWithError($label, $this->_errors['name']);
  161. }
  162. $fieldset->appendChild($label);
  163. $this->Form->appendChild($fieldset);
  164. $fieldset = new XMLElement('fieldset');
  165. $fieldset->setAttribute('class', 'settings');
  166. $fieldset->appendChild(new XMLElement('legend', 'Parameters'));
  167. $div = new XMLElement('div');
  168. $div->setAttribute('class', 'subsection');
  169. $div->appendChild(new XMLElement('h3', 'Parameters'));
  170. $ol = new XMLElement('ol');
  171. // Add existing parameters:
  172. if(isset($this->_params)) {
  173. foreach ($this->_params as $sortorder => $param) {
  174. $wrapper = new XMLElement('li');
  175. $this->displayParameter($wrapper, $sortorder, $param);
  176. $ol->appendChild($wrapper);
  177. }
  178. }
  179. // Add parameter set:
  180. $wrapper = new XMLElement('li');
  181. $wrapper->setAttribute('class', 'template');
  182. $this->displayParameter($wrapper, '-1', array(
  183. 'type' => 'Parameter definition'
  184. ));
  185. $ol->appendChild($wrapper);
  186. $div->appendChild($ol);
  187. $fieldset->appendChild($div);
  188. $this->Form->appendChild($fieldset);
  189. // Excluded Pages --------------------------------------------------------
  190. $fieldset = new XMLElement('fieldset');
  191. $fieldset->setAttribute('class', 'settings');
  192. $fieldset->appendChild(new XMLElement('legend', 'Pages'));
  193. $group = new XMLElement('div');
  194. $group->setAttribute('class', 'group');
  195. $this->viewIndexPages($group, $this->_fields['id']);
  196. $fieldset->appendChild($group);
  197. $this->Form->appendChild($fieldset);
  198. // Footer: ------------------------------------------------------------
  199. $div = new XMLElement('div');
  200. $div->setAttribute('class', 'actions');
  201. $div->appendChild(
  202. Widget::Input('action[save]',
  203. ($this->_editing ? 'Save Changes' : 'Create Set'),
  204. 'submit', array(
  205. 'accesskey' => 's'
  206. )
  207. )
  208. );
  209. if ($this->_editing) {
  210. $button = new XMLElement('button', 'Delete');
  211. $button->setAttributeArray(array(
  212. 'name' => 'action[delete]',
  213. 'class' => 'confirm delete',
  214. 'title' => 'Delete this set'
  215. ));
  216. $div->appendChild($button);
  217. }
  218. $this->Form->appendChild($div);
  219. }
  220. protected function displayParameter(&$wrapper, $sortorder, $param) {
  221. $wrapper->appendChild(new XMLElement('h4', ucwords($param['type'])));
  222. $wrapper->appendChild(Widget::Input("params[{$sortorder}][type]", $param['type'], 'hidden'));
  223. if (!empty($param['id'])) {
  224. $wrapper->appendChild(Widget::Input("params[{$sortorder}][id]", $param['id'], 'hidden'));
  225. }
  226. // Parameter name ------------------------------------------------------------
  227. $div = new XMLElement('div');
  228. $div->setAttribute('class', 'group');
  229. $label = Widget::Label('Parameter name');
  230. $label->appendChild(Widget::Input(
  231. "params[{$sortorder}][param]",
  232. General::sanitize($param['param'])
  233. ));
  234. if (isset($this->_errors["{$sortorder}:param"])) {
  235. $label = Widget::wrapFormElementWithError($label, $this->_errors["{$sortorder}:param"]);
  236. }
  237. $div->appendChild($label);
  238. // Parameter Value --------------------------------------------------------
  239. $label = Widget::Label('Parameter value');
  240. $label->appendChild(Widget::Input(
  241. "params[{$sortorder}][value]",
  242. General::sanitize($param['value'])
  243. ));
  244. if (isset($this->_errors["{$sortorder}:value"])) {
  245. $label = Widget::wrapFormElementWithError($label, $this->_errors["{$sortorder}:value"]);
  246. }
  247. $div->appendChild($label);
  248. $wrapper->appendChild($div);
  249. }
  250. public function viewIndexPages($context, $set_id = Null) {
  251. $pages = $this->_Parent->Database->fetch("
  252. SELECT
  253. p.*
  254. FROM
  255. tbl_pages AS p
  256. ORDER BY
  257. p.sortorder ASC
  258. ");
  259. $options = array();
  260. foreach ($pages as $page) {
  261. $selected = $this->_driver->isPageSelected($page['id'], $set_id);
  262. $options[] = array(
  263. $page['id'], $selected, '/' . $this->_Parent->resolvePagePath($page['id'])
  264. );
  265. }
  266. $section = Widget::Label('Excluded Pages');
  267. $section->appendChild(Widget::Select(
  268. 'fields[exclude_page][]', $options, array(
  269. 'multiple' => 'multiple'
  270. )
  271. ));
  272. $context->appendChild($section);
  273. }
  274. /*-------------------------------------------------------------------------
  275. Index
  276. -------------------------------------------------------------------------*/
  277. public function __actionIndex() {
  278. $checked = @array_keys($_POST['items']);
  279. if (is_array($checked) and !empty($checked)) {
  280. switch ($_POST['with-selected']) {
  281. case 'delete':
  282. foreach ($checked as $set_id) {
  283. $this->_Parent->Database->query("
  284. DELETE FROM
  285. `tbl_gpl_sets`
  286. WHERE
  287. `id` = {$set_id}
  288. ");
  289. $this->_Parent->Database->query("
  290. DELETE FROM
  291. `tbl_gpl_params`
  292. WHERE
  293. `set_id` = {$set_id}
  294. ");
  295. }
  296. redirect("{$this->_uri}/sets/");
  297. break;
  298. }
  299. }
  300. }
  301. public function __viewIndex() {
  302. $this->setPageType('table');
  303. $this->setTitle('Symphony &ndash; Global Parameter Sets');
  304. $this->appendSubheading('Parameter Sets', Widget::Anchor(
  305. 'Create New', "{$this->_uri}/sets/new/",
  306. 'Create a new parameter set', 'create button'
  307. ));
  308. $tableHead = array(
  309. array('Parameter Set Name', 'col'),
  310. array('Parameters', 'col')
  311. );
  312. $tableBody = array();
  313. if (!is_array($this->_sets) or empty($this->_sets)) {
  314. $tableBody = array(
  315. Widget::TableRow(array(Widget::TableData(__('None Found.'), 'inactive', null, count($tableHead))))
  316. );
  317. } else {
  318. foreach ($this->_sets as $set) {
  319. $set = (object)$set;
  320. $col_name = Widget::TableData(
  321. Widget::Anchor(
  322. $set->name,
  323. "{$this->_uri}/sets/edit/{$set->id}/"
  324. )
  325. );
  326. $col_name->appendChild(Widget::Input("items[{$set->id}]", null, 'checkbox'));
  327. if (!empty($set->params)) {
  328. $col_params = Widget::TableData($set->params);
  329. } else {
  330. $col_params = Widget::TableData('None', 'inactive');
  331. }
  332. $tableBody[] = Widget::TableRow(array($col_name, $col_params), null);
  333. }
  334. }
  335. $table = Widget::Table(
  336. Widget::TableHead($tableHead), null,
  337. Widget::TableBody($tableBody)
  338. );
  339. $this->Form->appendChild($table);
  340. $actions = new XMLElement('div');
  341. $actions->setAttribute('class', 'actions');
  342. $options = array(
  343. array(null, false, 'With Selected...'),
  344. array('delete', false, 'Delete')
  345. );
  346. $actions->appendChild(Widget::Select('with-selected', $options));
  347. $actions->appendChild(Widget::Input('action[apply]', 'Apply', 'submit'));
  348. $this->Form->appendChild($actions);
  349. }
  350. }
  351. ?>