PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/pages_editor_minimal/content/content.edit.php

https://github.com/bauhouse/sym-extensions
PHP | 379 lines | 216 code | 107 blank | 56 comment | 50 complexity | b584071d92bd7415581156336e65abd1 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. require_once(TOOLKIT . '/class.administrationpage.php');
  3. require_once(TOOLKIT . '/class.eventmanager.php');
  4. require_once(TOOLKIT . '/class.datasourcemanager.php');
  5. require_once(TOOLKIT . '/class.xsltprocess.php');
  6. class contentExtensionPages_editor_minimalEdit extends AdministrationPage {
  7. private $_errors;
  8. private $_driver;
  9. function __construct(&$parent){
  10. parent::__construct($parent);
  11. $this->_driver = Administration::instance()->ExtensionManager->create('pages_editor_minimal');
  12. }
  13. function view(){
  14. $this->setPageType('form');
  15. $this->addStylesheetToHead(URL . '/extensions/pages_editor_minimal/assets/screen.css', 'screen', 1200);
  16. $fields = array();
  17. if(!$page_id = $this->_context[0]) redirect(URL . '/symphony/blueprints/pages/');
  18. if(!$existing = $this->_Parent->Database->fetchRow(0, "SELECT * FROM `tbl_pages` WHERE `id` = '$page_id' LIMIT 1"))
  19. $this->_Parent->customError(E_USER_ERROR, __('Page not found'), __('The page you requested to edit does not exist.'), false, true, 'error', array('header' => 'HTTP/1.0 404 Not Found'));
  20. if(isset($this->_context[1])){
  21. switch($this->_context[1]){
  22. case 'saved':
  23. $this->pageAlert(
  24. __(
  25. 'Page updated at %1$s. <a href="%2$s">Create another?</a> <a href="%3$s">View all Pages</a>',
  26. array(
  27. DateTimeObj::get(__SYM_TIME_FORMAT__),
  28. URL . '/symphony/blueprints/pages/new/',
  29. URL . '/symphony/blueprints/pages/'
  30. )
  31. ),
  32. Alert::SUCCESS);
  33. break;
  34. case 'created':
  35. $this->pageAlert(
  36. __(
  37. 'Page created at %1$s. <a href="%2$s">Create another?</a> <a href="%3$s">View all Pages</a>',
  38. array(
  39. DateTimeObj::get(__SYM_TIME_FORMAT__),
  40. URL . '/symphony/blueprints/pages/new/',
  41. URL . '/symphony/blueprints/pages/'
  42. )
  43. ),
  44. Alert::SUCCESS);
  45. break;
  46. }
  47. }
  48. if(isset($_POST['fields'])){
  49. $fields = $_POST['fields'];
  50. }
  51. else{
  52. $fields = $existing;
  53. $types = $this->_Parent->Database->fetchCol('type', "SELECT `type` FROM `tbl_pages_types` WHERE page_id = '$page_id' ORDER BY `type` ASC");
  54. $fields['type'] = @implode(', ', $types);
  55. $fields['data_sources'] = preg_split('/,/i', $fields['data_sources'], -1, PREG_SPLIT_NO_EMPTY);
  56. $fields['events'] = preg_split('/,/i', $fields['events'], -1, PREG_SPLIT_NO_EMPTY);
  57. $fields['body'] = @file_get_contents(PAGES . '/' . trim(str_replace('/', '_', $fields['path'] . '_' . $fields['handle']), '_') . ".xsl");
  58. }
  59. $title = $fields['title'];
  60. if(trim($title) == '') $title = $existing['title'];
  61. $this->setTitle(__(($title ? '%1$s &ndash; %2$s &ndash; %3$s' : '%1$s &ndash; %2$s'), array(__('Symphony'), __('Pages'), $title)));
  62. # $this->appendSubheading(($title ? $title : __('Untitled')));
  63. $label = Widget::Label(__('Title'));
  64. $label->appendChild(Widget::Input('fields[title]', General::sanitize($fields['title'])));
  65. $this->Form->appendChild((isset($this->_errors['title']) ? $this->wrapFormElementWithError($label, $this->_errors['title']) : $label));
  66. $fieldset = new XMLElement('fieldset');
  67. $fieldset->setAttribute('class', 'primary');
  68. $group = new XMLElement('div');
  69. $group->setAttribute('class', 'group');
  70. $label = Widget::Label(__('Events'));
  71. $EventManager = new EventManager($this->_Parent);
  72. $events = $EventManager->listAll();
  73. $options = array();
  74. if(is_array($events) && !empty($events)){
  75. foreach($events as $name => $about) $options[] = array($name, @in_array($name, $fields['events']), $about['name']);
  76. }
  77. $label->appendChild(Widget::Select('fields[events][]', $options, array('multiple' => 'multiple')));
  78. $group->appendChild($label);
  79. $label = Widget::Label(__('Data Sources'));
  80. $DSManager = new DatasourceManager($this->_Parent);
  81. $datasources = $DSManager->listAll();
  82. $options = array();
  83. if(is_array($datasources) && !empty($datasources)){
  84. foreach($datasources as $name => $about) $options[] = array($name, @in_array($name, $fields['data_sources']), $about['name']);
  85. }
  86. $label->appendChild(Widget::Select('fields[data_sources][]', $options, array('multiple' => 'multiple')));
  87. $group->appendChild($label);
  88. $fieldset->appendChild($group);
  89. $this->Form->appendChild($fieldset);
  90. $fieldset = new XMLElement('fieldset');
  91. $fieldset->setAttribute('class', 'secondary');
  92. $label = Widget::Label(__('URL Handle'));
  93. $label->appendChild(Widget::Input('fields[handle]', $fields['handle']));
  94. $fieldset->appendChild((isset($this->_errors['handle']) ? $this->wrapFormElementWithError($label, $this->_errors['handle']) : $label));
  95. $pages = $this->_Parent->Database->fetch("SELECT * FROM `tbl_pages` WHERE `id` != '{$page_id}' ORDER BY `title` ASC");
  96. $label = Widget::Label(__('Parent Page'));
  97. $options = array(
  98. array('', false, '/')
  99. );
  100. if (is_array($pages) and !empty($pages)) {
  101. if (!function_exists('__compare_pages')) {
  102. function __compare_pages($a, $b) {
  103. return strnatcasecmp($a[2], $b[2]);
  104. }
  105. }
  106. foreach ($pages as $page) {
  107. $options[] = array(
  108. $page['id'], $fields['parent'] == $page['id'],
  109. '/' . $this->_Parent->resolvePagePath($page['id'])
  110. );
  111. }
  112. usort($options, '__compare_pages');
  113. }
  114. $label->appendChild(Widget::Select('fields[parent]', $options));
  115. $fieldset->appendChild($label);
  116. $label = Widget::Label(__('URL Parameters'));
  117. $label->appendChild(Widget::Input('fields[params]', $fields['params']));
  118. $fieldset->appendChild($label);
  119. $div3 = new XMLElement('div');
  120. $label = Widget::Label(__('Page Type'));
  121. $label->appendChild(Widget::Input('fields[type]', $fields['type']));
  122. $div3->appendChild((isset($this->_errors['type']) ? $this->wrapFormElementWithError($label, $this->_errors['type']) : $label));
  123. $ul = new XMLElement('ul');
  124. $ul->setAttribute('class', 'tags');
  125. if($types = $this->__fetchAvailablePageTypes()) foreach($types as $type) $ul->appendChild(new XMLElement('li', $type));
  126. $div3->appendChild($ul);
  127. $fieldset->appendChild($div3);
  128. $this->Form->appendChild($fieldset);
  129. /*$utilities = General::listStructure(UTILITIES, array('xsl'), false, 'asc', UTILITIES);
  130. $utilities = $utilities['filelist'];
  131. if(is_array($utilities) && !empty($utilities)){
  132. $div = new XMLElement('div');
  133. $div->setAttribute('class', 'secondary');
  134. $h3 = new XMLElement('h3', __('Utilities'));
  135. $h3->setAttribute('class', 'label');
  136. $div->appendChild($h3);
  137. $ul = new XMLElement('ul');
  138. $ul->setAttribute('id', 'utilities');
  139. $i = 0;
  140. foreach($utilities as $util){
  141. $li = new XMLElement('li');
  142. if ($i++ % 2 != 1) {
  143. $li->setAttribute('class', 'odd');
  144. }
  145. $li->appendChild(Widget::Anchor($util, URL . '/symphony/blueprints/utilities/edit/' . str_replace('.xsl', '', $util) . '/', NULL));
  146. $ul->appendChild($li);
  147. }
  148. $div->appendChild($ul);
  149. $this->Form->appendChild($div);
  150. }*/
  151. $div = new XMLElement('div');
  152. $div->setAttribute('class', 'actions');
  153. $div->appendChild(Widget::Input('action[save]', __('Save Changes'), 'submit', array('accesskey' => 's')));
  154. $button = new XMLElement('button', __('Delete'));
  155. $button->setAttributeArray(array('name' => 'action[delete]', 'class' => 'confirm delete', 'title' => __('Delete this page')));
  156. $div->appendChild($button);
  157. $this->Form->appendChild($div);
  158. }
  159. function action(){
  160. if(!$page_id = $this->_context[0]) redirect(URL . '/symphony/blueprints/pages/');
  161. if(@array_key_exists('delete', $_POST['action'])) {
  162. ## TODO: Fix Me
  163. ###
  164. # Delegate: Delete
  165. # Description: Prior to deletion. Provided with Page's database ID
  166. //$ExtensionManager->notifyMembers('Delete', getCurrentPage(), array('page' => $page_id));
  167. $page = $this->_Parent->Database->fetchRow(0, "SELECT * FROM tbl_pages WHERE `id` = '$page_id'");
  168. $filename = $page['path'] . '_' . $page['handle'];
  169. $filename = trim(str_replace('/', '_', $filename), '_');
  170. $this->_Parent->Database->delete('tbl_pages', " `id` = '$page_id'");
  171. $this->_Parent->Database->delete('tbl_pages_types', " `page_id` = '$page_id'");
  172. $this->_Parent->Database->query("UPDATE tbl_pages SET `sortorder` = (`sortorder` + 1) WHERE `sortorder` < '$page_id'");
  173. General::deleteFile(PAGES . "/$filename.xsl");
  174. redirect(URL . '/symphony/blueprints/pages/');
  175. }
  176. elseif(@array_key_exists('save', $_POST['action'])){
  177. $fields = $_POST['fields'];
  178. $this->_errors = array();
  179. if(!isset($fields['title']) || trim($fields['title']) == '') $this->_errors['title'] = __('Title is a required field');
  180. if(trim($fields['type']) != '' && preg_match('/(index|404|403)/i', $fields['type'])){
  181. $haystack = strtolower($fields['type']);
  182. if(preg_match('/\bindex\b/i', $haystack, $matches) && $row = $this->_Parent->Database->fetchRow(0, "SELECT * FROM `tbl_pages_types` WHERE `page_id` != '$page_id' AND `type` = 'index' LIMIT 1")){
  183. $this->_errors['type'] = __('An index type page already exists.');
  184. }
  185. elseif(preg_match('/\b404\b/i', $haystack, $matches) && $row = $this->_Parent->Database->fetchRow(0, "SELECT * FROM `tbl_pages_types` WHERE `page_id` != '$page_id' AND `type` = '404' LIMIT 1")){
  186. $this->_errors['type'] = __('A 404 type page already exists.');
  187. }
  188. elseif(preg_match('/\b403\b/i', $haystack, $matches) && $row = $this->_Parent->Database->fetchRow(0, "SELECT * FROM `tbl_pages_types` WHERE `page_id` != '$page_id' AND `type` = '403' LIMIT 1")){
  189. $this->_errors['type'] = __('A 403 type page already exists.');
  190. }
  191. }
  192. if(empty($this->_errors)){
  193. ## Manipulate some fields
  194. //$fields['sortorder'] = $this->_Parent->Database->fetchVar('next', 0, "SELECT MAX(sortorder) + 1 as `next` FROM `tbl_pages` LIMIT 1");
  195. //
  196. //if(empty($fields['sortorder']) || !is_numeric($fields['sortorder'])) $fields['sortorder'] = 1;
  197. $autogenerated_handle = false;
  198. if(trim($fields['handle'] ) == ''){
  199. $fields['handle'] = $fields['title'];
  200. $autogenerated_handle = true;
  201. }
  202. $fields['handle'] = Lang::createHandle($fields['handle']);
  203. if($fields['params']) $fields['params'] = trim(preg_replace('@\/{2,}@', '/', $fields['params']), '/');
  204. ## Clean up type list
  205. $types = preg_split('/,\s*/', $fields['type'], -1, PREG_SPLIT_NO_EMPTY);
  206. $types = @array_map('trim', $types);
  207. unset($fields['type']);
  208. //if(trim($fields['type'])) $fields['type'] = preg_replace('/\s*,\s*/i', ', ', $fields['type']);
  209. //else $fields['type'] = NULL;
  210. ## Manipulate some fields
  211. $fields['parent'] = ($fields['parent'] != 'None' ? $fields['parent'] : NULL);
  212. $fields['data_sources'] = @implode(',', $fields['data_sources']);
  213. $fields['events'] = @implode(',', $fields['events']);
  214. $fields['path'] = NULL;
  215. if($fields['parent']) $fields['path'] = $this->_Parent->resolvePagePath(intval($fields['parent']));
  216. $new_filename = trim(str_replace('/', '_', $fields['path'] . '_' . $fields['handle']), '_');
  217. $current = $this->_Parent->Database->fetchRow(0, "SELECT * FROM `tbl_pages` WHERE `id` = '$page_id' LIMIT 1");
  218. $current_filename = $current['path'] . '_' . $current['handle'];
  219. $current_filename = trim(str_replace('/', '_', $current_filename), '_');
  220. ## Duplicate
  221. if($this->_Parent->Database->fetchRow(0, "SELECT * FROM `tbl_pages`
  222. WHERE `handle` = '" . $fields['handle'] . "'
  223. AND `id` != '$page_id'
  224. AND `path` ".($fields['path'] ? " = '".$fields['path']."'" : ' IS NULL')."
  225. LIMIT 1")){
  226. if($autogenerated_handle) $this->_errors['title'] = __('A page with that title %s already exists', array(($fields['parent'] ? __('and parent') : '')));
  227. else $this->_errors['handle'] = __('A page with that handle %s already exists', array(($fields['parent'] ? __('and parent') : '')));
  228. }
  229. else{
  230. $fields['body'] = file_get_contents(PAGES . "/$current_filename.xsl");
  231. ## Write the file
  232. if($new_filename != $current_filename && !$write = General::writeFile(PAGES . "/$new_filename.xsl" , $fields['body'], $this->_Parent->Configuration->get('write_mode', 'file'))){
  233. $this->pageAlert(__('Page could not be written to disk. Please check permissions on <code>/workspace/pages</code>.'), Alert::ERROR);
  234. }
  235. ## Write Successful, add record to the database
  236. else{
  237. if($new_filename != $current_filename) @unlink(PAGES . "/$current_filename.xsl");
  238. ## No longer need the body text
  239. unset($fields['body']);
  240. ## Insert the new data
  241. if(!$this->_Parent->Database->update($fields, 'tbl_pages', "`id` = '$page_id'")) $this->pageAlert(__('Unknown errors occurred while attempting to save. Please check your <a href="%s">activity log</a>.', array(URL.'/symphony/system/log/')), Alert::ERROR);
  242. else{
  243. $this->_Parent->Database->delete('tbl_pages_types', " `page_id` = '$page_id'");
  244. if(is_array($types) && !empty($types)){
  245. foreach($types as $type) $this->_Parent->Database->insert(array('page_id' => $page_id, 'type' => $type), 'tbl_pages_types');
  246. }
  247. ## TODO: Fix Me
  248. ###
  249. # Delegate: Edit
  250. # Description: After saving the page. The Page's database ID is provided.
  251. //$ExtensionManager->notifyMembers('Edit', getCurrentPage(), array('page_id' => $page_id));
  252. redirect(URL . "/symphony/extension/pages_editor_minimal/edit/$page_id/saved/");
  253. }
  254. }
  255. }
  256. }
  257. if(is_array($this->_errors) && !empty($this->_errors)){
  258. $this->pageAlert(__('An error occurred while processing this form. <a href="#error">See below for details.</a>'), Alert::ERROR);
  259. }
  260. }
  261. }
  262. }