SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Controllers/Admin/ListAdminController.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 karmabunny\pdb\Exceptions\QueryException;
  17. use Sprout\Helpers\Admin;
  18. use Sprout\Helpers\AdminAuth;
  19. use Sprout\Helpers\Csrf;
  20. use Sprout\Helpers\Notification;
  21. use Sprout\Helpers\Pdb;
  22. use Sprout\Helpers\Router;
  23. use Sprout\Helpers\Url;
  24. use Sprout\Helpers\View;
  25.  
  26.  
  27. /**
  28. * Any controller which is essentially a short list of items, which are not substantial enough
  29. * to warrant a categories system
  30. *
  31. * Required fields for a list controller table:
  32. * id
  33. * name
  34. * record_order
  35. **/
  36. abstract class ListAdminController extends ManagedAdminController {
  37. protected $main_order = 'item.record_order';
  38.  
  39. /**
  40.   * Constructor
  41.   **/
  42. public function __construct()
  43. {
  44. parent::__construct();
  45. }
  46.  
  47.  
  48. /**
  49.   * Returns the contents of the navigation pane for the list
  50.   **/
  51. public function _getNavigation()
  52. {
  53. $q = "SELECT id, name FROM ~{$this->table_name} ORDER BY record_order";
  54. $res = Pdb::q($q, [], 'arr');
  55.  
  56. $view = new View('sprout/admin/list_navigation');
  57. $view->controller_name = $this->controller_name;
  58. $view->friendly_name = $this->friendly_name;
  59. $view->items = $res;
  60. $view->allow_add = $this->main_add;
  61. $view->record_id = (int) Admin::getRecordId();
  62.  
  63. if (Router::$method == 'contents' or Router::$method == 'export') {
  64. $view->export_refine = '?' . $_SERVER['QUERY_STRING'];
  65. } else {
  66. $view->export_refine = '';
  67. }
  68.  
  69. return $view->render();
  70. }
  71.  
  72.  
  73. /**
  74.   * Returns the tools to show in the left navigation
  75.   **/
  76. public function _getTools()
  77. {
  78. $items = parent::_getTools();
  79.  
  80. $items[] = "<li class=\"reorder\"><a href=\"admin/call/{$this->controller_name}/reorder\" onclick=\"$.facebox({'ajax':this.href}); return false;\">Reorder</a></li>";
  81.  
  82. return $items;
  83. }
  84.  
  85.  
  86. /**
  87.   * Deletes an item and logs the deleted data
  88.   *
  89.   * @param int $item_id The record to delete.
  90.   * @return bool True on success, false on failure
  91.   */
  92. final public function _deleteSave($item_id)
  93. {
  94. $item_id = (int) $item_id;
  95.  
  96. if (!$this->_isDeleteSaved($item_id)) return false;
  97.  
  98. $this->deleteRecord($this->table_name, $item_id);
  99.  
  100. return true;
  101. }
  102.  
  103.  
  104. /**
  105.   * Shows the reorder screen (which is shown in a popup box) for re-ordering the children items
  106.   **/
  107. public function reorder()
  108. {
  109.  
  110. // Get children
  111. $q = "SELECT id, name
  112. FROM ~{$this->table_name}
  113. ORDER BY record_order";
  114. $children = Pdb::q($q, [], 'arr');
  115.  
  116. // If this item only has one child, complain that it's impossible to re-order
  117. if (count($children) == 1) {
  118. echo "<p>This item does not have enough items for ordering.</p>";
  119. return;
  120. }
  121.  
  122. // View
  123. $view = new View('sprout/admin/categories_reorder');
  124. $view->items = $children;
  125. $view->controller_name = $this->controller_name;
  126. $view->friendly_name = $this->friendly_name;
  127.  
  128. echo $view->render();
  129. }
  130.  
  131.  
  132. /**
  133.   * Saves a reorder
  134.   **/
  135. public function reorderSave()
  136. {
  137. AdminAuth::checkLogin();
  138. Csrf::checkOrDie();
  139.  
  140. $record_order = 1;
  141. foreach ($_POST['items'] as $id) {
  142. $id = (int) $id;
  143.  
  144. $q = "UPDATE ~{$this->table_name}
  145. SET record_order = ?
  146. WHERE id = ?";
  147. Pdb::q($q, [$record_order, $id], 'count');
  148.  
  149. $record_order++;
  150. }
  151.  
  152. $this->_invalidateCaches('reorder');
  153.  
  154. Notification::confirm('Re-order was successful');
  155. Url::redirect("admin/intro/" . $this->controller_name);
  156. }
  157.  
  158.  
  159. /**
  160.   * If the specified item needs a record number to be set,
  161.   * Puts this item at the end of the list.
  162.   *
  163.   * @param int $item_id Record-id to update
  164.   */
  165. protected function fixRecordOrder($item_id)
  166. {
  167. $q = "SELECT record_order
  168. FROM ~{$this->table_name}
  169. WHERE id = ?";
  170. try {
  171. $order = Pdb::q($q, [$item_id], 'val');
  172. } catch (QueryException $ex) {
  173. return;
  174. }
  175. if ($order != 0) return;
  176.  
  177. $q = "SELECT MAX(record_order) AS m
  178. FROM ~{$this->table_name}";
  179. try {
  180. $order = Pdb::q($q, [], 'val');
  181. } catch (QueryException $ex) {
  182. return;
  183. }
  184.  
  185. ++$order;
  186. $q = "UPDATE ~{$this->table_name}
  187. SET record_order = ?
  188. WHERE id = ?";
  189. Pdb::q($q, [$order, $item_id], 'count');
  190. }
  191. }
  192.  
  193.  
  194.