SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Controllers/Admin/ExtraPageAdminController.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Controllers\Admin;
  15.  
  16. use Sprout\Helpers\AdminAuth;
  17. use Sprout\Helpers\ColModifierLookupArray;
  18. use Sprout\Helpers\Pdb;
  19. use Sprout\Helpers\RefineBar;
  20. use Sprout\Helpers\RefineWidgetSelect;
  21. use Sprout\Helpers\Register;
  22. use Sprout\Helpers\Validator;
  23. use Sprout\Helpers\View;
  24.  
  25.  
  26. /**
  27. * Handles most processing for Extra pages
  28. **/
  29. class ExtraPageAdminController extends ManagedAdminController
  30. {
  31. protected $controller_name = 'extra_page';
  32. protected $friendly_name = 'Snippet pages';
  33. protected $add_defaults = array(
  34. 'active' => 1,
  35. );
  36.  
  37.  
  38. /**
  39.   * Constructor
  40.   **/
  41. public function __construct()
  42. {
  43. parent::__construct();
  44.  
  45. $this->extra_page_types = Register::getExtraPages();
  46.  
  47. $this->main_order = 'item.type';
  48. $this->main_columns = array(
  49. 'Type' => array(new ColModifierLookupArray($this->extra_page_types), 'type'),
  50. );
  51. $this->main_where = array(
  52. 'subsite_id = ' . ((int)$_SESSION['admin']['active_subsite']),
  53. );
  54.  
  55. $this->refine_bar = new RefineBar();
  56. $this->refine_bar->addWidget(new RefineWidgetSelect('type', 'Type', $this->extra_page_types));
  57. }
  58.  
  59.  
  60. /**
  61.   * No tools
  62.   **/
  63. public function _getTools()
  64. {
  65. return null;
  66. }
  67.  
  68.  
  69. /**
  70.   * Returns the contents of the navigation pane for the list
  71.   **/
  72. public function _getNavigation()
  73. {
  74. $q = "SELECT id, type
  75. FROM ~extra_pages
  76. WHERE subsite_id = ?
  77. ORDER BY type";
  78. $res = Pdb::query($q, [$_SESSION['admin']['active_subsite']], 'pdo');
  79.  
  80. $snippets = [];
  81. foreach ($res as $row) {
  82. if (isset($this->extra_page_types[$row['type']])) {
  83. $snippets[$row['id']] = $this->extra_page_types[$row['type']];
  84. }
  85. }
  86.  
  87. $view = new View('sprout/admin/extra_page_sidebar');
  88. $view->snippets = $snippets;
  89. return $view->render();
  90. }
  91.  
  92.  
  93. /**
  94.   * Return the fields to show in the sidebar when adding or editing a record.
  95.   * These fields are shown under a heading of "Visibility"
  96.   *
  97.   * Key is the field name, value is the field label
  98.   *
  99.   * @return array
  100.   */
  101. public function _getVisibilityFields()
  102. {
  103. return [];
  104. }
  105.  
  106.  
  107. /**
  108.   * Validates incoming POST data.
  109.   *
  110.   * @param bool True on success, false on failure
  111.   **/
  112. private function validate()
  113. {
  114. $_SESSION['admin']['field_values'] = Validator::trim($_POST);
  115. $result = true;
  116.  
  117. $valid = new Validator($_POST);
  118. $valid->required(['type', 'text']);
  119.  
  120. if ($valid->hasErrors()) {
  121. $_SESSION['admin']['field_errors'] = $valid->getFieldErrors();
  122. $valid->createNotifications();
  123. $result = false;
  124. }
  125.  
  126. return $result;
  127. }
  128.  
  129.  
  130. /**
  131.   * Pre-render hook for adding
  132.   **/
  133. protected function _addPreRender($view)
  134. {
  135. $types = Register::getExtraPages();
  136.  
  137. $q = "SELECT type
  138. FROM ~extra_pages
  139. WHERE subsite_id = ?";
  140. $extant_types = Pdb::q($q, [$_SESSION['admin']['active_subsite']], 'col');
  141. foreach ($extant_types as $type_id) {
  142. unset($types[$type_id]);
  143. }
  144.  
  145. $view->types = $types;
  146. }
  147.  
  148.  
  149. /**
  150.   * Return the sub-actions for adding; for spec {@see AdminController::renderSubActions}
  151.   * @return array
  152.   */
  153. public function _getAddSubActions()
  154. {
  155. $actions = parent::_getAddSubActions();
  156. // Add your actions here, like this: $actions[] = [ ... ];
  157. return $actions;
  158. }
  159.  
  160.  
  161. /**
  162.   * Saves the provided POST data into a new record in the database
  163.   *
  164.   * @param int $item_id After saving, the new record id will be returned in this parameter
  165.   * @param bool True on success, false on failure
  166.   **/
  167. public function _addSave(&$item_id)
  168. {
  169. $res = $this->validate();
  170. if (! $res) return false;
  171.  
  172. // Start transaction
  173. Pdb::transact();
  174.  
  175. // Main insert
  176. $update_fields = [];
  177. $update_fields['type'] = $_POST['type'];
  178. $update_fields['text'] = $_POST['text'];
  179. $update_fields['subsite_id'] = $_SESSION['admin']['active_subsite'];
  180. $update_fields['date_added'] = Pdb::now();
  181. $update_fields['date_modified'] = Pdb::now();
  182.  
  183. $item_id = Pdb::insert('extra_pages', $update_fields);
  184.  
  185. $res = $this->logAdd('extra_pages', $item_id);
  186. if (! $res) return false;
  187.  
  188. // Commit
  189. Pdb::commit();
  190.  
  191. return true;
  192. }
  193.  
  194.  
  195. /**
  196.   * Pre-render hook for editing
  197.   **/
  198. protected function _editPreRender($view, $item_id)
  199. {
  200. parent::_editPreRender($view, $item_id);
  201. }
  202.  
  203.  
  204. /**
  205.   * Return the sub-actions for editing; for spec {@see AdminController::renderSubActions}
  206.   * @return array
  207.   */
  208. public function _getEditSubActions($item_id)
  209. {
  210. $actions = parent::_getEditSubActions($item_id);
  211.  
  212. if (!AdminAuth::isSuper()) {
  213. unset($actions['delete']);
  214. }
  215.  
  216. // Add your actions here, like this: $actions[] = [ ... ];
  217. return $actions;
  218. }
  219.  
  220.  
  221. /**
  222.   * Saves the provided POST data the specified record
  223.   *
  224.   * @param int $item_id The record to update
  225.   * @param bool True on success, false on failure
  226.   **/
  227. public function _editSave($item_id)
  228. {
  229. $item_id = (int) $item_id;
  230.  
  231. $res = $this->validate();
  232. if (! $res) return false;
  233.  
  234. // Start transaction
  235. Pdb::transact();
  236.  
  237. // Update item
  238. $update_fields = [];
  239. $update_fields['type'] = $_POST['type'];
  240. $update_fields['text'] = $_POST['text'];
  241. $update_fields['date_modified'] = Pdb::now();
  242.  
  243. $logdata = $this->loadRecord('extra_pages', $item_id);
  244.  
  245. Pdb::update('extra_pages', $update_fields, ['id' => $item_id]);
  246.  
  247. $res = $this->logEdit('extra_pages', $item_id, $logdata);
  248. if (! $res) return false;
  249.  
  250. // Commit
  251. Pdb::commit();
  252.  
  253. return true;
  254. }
  255.  
  256. /**
  257.   * Creates the identifier used in the heading.
  258.   *
  259.   * @return string
  260.   **/
  261. public function _identifier(array $item)
  262. {
  263. $labels = Register::getExtraPages();
  264. return $labels[$item['type']];
  265. }
  266. }
  267.  
  268.  
  269.