SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Controllers/Admin/SubsiteAdminController.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 Exception;
  17.  
  18. use Sprout\Helpers\Notification;
  19. use Sprout\Helpers\Pdb;
  20. use Sprout\Helpers\Subsites;
  21. use Sprout\Helpers\Url;
  22. use Sprout\Helpers\Validator;
  23.  
  24.  
  25. /**
  26. * Handles most processing for Subsites
  27. **/
  28. class SubsiteAdminController extends ListAdminController
  29. {
  30. protected $controller_name = 'subsite';
  31. protected $friendly_name = 'Subsites';
  32. protected $action_log = true;
  33.  
  34. /**
  35.   * Constructor
  36.   **/
  37. public function __construct()
  38. {
  39. $this->main_columns = [
  40. 'Name' => 'name',
  41. 'Code' => 'code',
  42. ];
  43.  
  44. parent::__construct();
  45. }
  46.  
  47.  
  48. /**
  49.   * Validates incoming POST data.
  50.   *
  51.   * @param bool True on success, false on failure
  52.   **/
  53. private function validate()
  54. {
  55. $_SESSION['admin']['field_values'] = Validator::trim($_POST);
  56. $result = true;
  57.  
  58. $valid = new Validator($_POST);
  59. $valid->required(['name', 'code']);
  60. $valid->check('name', 'Validity::length', 0, 50);
  61. $valid->check('code', 'Validity::length', 0, 15);
  62. $valid->check('cond_directory', 'Validity::length', 0, 150);
  63. $valid->check('override_site_title', 'Validity::length', 0, 150);
  64.  
  65. if ($valid->hasErrors()) {
  66. $_SESSION['admin']['field_errors'] = $valid->getFieldErrors();
  67. $valid->createNotifications();
  68. $result = false;
  69. }
  70.  
  71. return $result;
  72. }
  73.  
  74.  
  75. public function _addPreRender($view)
  76. {
  77. parent::_addPreRender($view);
  78.  
  79. $view->codes = Subsites::getCodes();
  80. $view->subsites = Pdb::lookup('subsites', ['content_id' => 0]);
  81. }
  82.  
  83.  
  84. /**
  85.   * Return the sub-actions for adding; for spec {@see AdminController::renderSubActions}
  86.   * @return array
  87.   */
  88. public function _getAddSubActions()
  89. {
  90. $actions = parent::_getAddSubActions();
  91. // Add your actions here, like this: $actions[] = [ ... ];
  92. return $actions;
  93. }
  94.  
  95.  
  96. /**
  97.   * Saves the provided POST data into a new record in the database
  98.   *
  99.   * @param int $item_id After saving, the new record id will be returned in this parameter
  100.   * @param bool True on success, false on failure
  101.   **/
  102. public function _addSave(&$item_id)
  103. {
  104. $res = $this->validate();
  105. if (! $res) return false;
  106.  
  107. Pdb::transact();
  108.  
  109. // Main insert
  110. $update_fields = [];
  111. $update_fields['name'] = $_POST['name'];
  112. $update_fields['code'] = $_POST['code'];
  113. $update_fields['cond_domain'] = $_POST['cond_domain'];
  114. $update_fields['cond_directory'] = $_POST['cond_directory'];
  115. $update_fields['mobile'] = (int) (bool) @$_POST['mobile'];
  116. $update_fields['content_id'] = $_POST['content_id'];
  117. $update_fields['require_admin'] = (int) (bool) @$_POST['require_admin'];
  118. $update_fields['require_user'] = (int) (bool) @$_POST['require_user'];
  119. $update_fields['active'] = $_POST['active'];
  120. $update_fields['date_added'] = Pdb::now();
  121. $update_fields['date_modified'] = Pdb::now();
  122. $item_id = Pdb::insert('subsites', $update_fields);
  123.  
  124. $res = $this->logAdd('subsites', $item_id);
  125.  
  126. // Create homepage record
  127. $update_fields = array();
  128. $update_fields['subsite_id'] = $item_id;
  129. $update_fields['date_added'] = Pdb::now();
  130. $update_fields['date_modified'] = Pdb::now();
  131. Pdb::insert('homepages', $update_fields);
  132.  
  133. Pdb::commit();
  134.  
  135. return true;
  136. }
  137.  
  138.  
  139. public function _editPreRender($view, $item_id)
  140. {
  141. parent::_editPreRender($view, $item_id);
  142.  
  143. $view->codes = Subsites::getCodes();
  144. $view->subsites = Pdb::lookup('subsites', ['content_id' => 0, ['id', '!=', $item_id]]);
  145. }
  146.  
  147.  
  148. /**
  149.   * Return the sub-actions for editing; for spec {@see AdminController::renderSubActions}
  150.   * @return array
  151.   */
  152. public function _getEditSubActions($item_id)
  153. {
  154. $actions = parent::_getEditSubActions($item_id);
  155. // Add your actions here, like this: $actions[] = [ ... ];
  156. return $actions;
  157. }
  158.  
  159.  
  160. /**
  161.   * Saves the provided POST data the specified record
  162.   *
  163.   * @param int $item_id The record to update
  164.   * @param bool True on success, false on failure
  165.   **/
  166. public function _editSave($item_id)
  167. {
  168. $item_id = (int) $item_id;
  169.  
  170. $res = $this->validate();
  171. if (! $res) return false;
  172.  
  173. // Start transaction
  174. $res = Pdb::transact();
  175.  
  176. // Update item
  177. $update_fields = [];
  178. $update_fields['name'] = $_POST['name'];
  179. $update_fields['code'] = $_POST['code'];
  180. $update_fields['cond_domain'] = $_POST['cond_domain'];
  181. $update_fields['cond_directory'] = $_POST['cond_directory'];
  182. $update_fields['mobile'] = (int) (bool) @$_POST['mobile'];
  183. $update_fields['content_id'] = (int) @$_POST['content_id'];
  184. $update_fields['require_admin'] = (int) (bool) @$_POST['require_admin'];
  185. $update_fields['require_user'] = (int) (bool) @$_POST['require_user'];
  186. $update_fields['active'] = $_POST['active'];
  187. $update_fields['date_modified'] = Pdb::now();
  188.  
  189. $logdata = $this->loadRecord('subsites', $item_id);
  190.  
  191. Pdb::update('subsites', $update_fields, ['id' => $item_id]);
  192.  
  193. $this->logEdit('subsites', $item_id, $logdata);
  194.  
  195. // Commit
  196. Pdb::commit();
  197.  
  198. return true;
  199. }
  200.  
  201.  
  202. /**
  203.   * Return HTML which represents the form for deleting a record
  204.   *
  205.   * @param int $id The record to show the delete form for
  206.   * @return string The HTML code which represents the edit form
  207.   **/
  208. public function _getDeleteForm($id)
  209. {
  210. $q = "SELECT COUNT(id) FROM ~subsites";
  211. $count = Pdb::q($q, [], 'val');
  212. if ($count == 1) {
  213. Notification::error('You cannot delete the only subsite in the system; must have at least one subsite at all times');
  214. Url::redirect('admin/contents/subsite');
  215. }
  216.  
  217. return parent::_getDeleteForm($id);
  218. }
  219.  
  220.  
  221. /**
  222.   * Prevents deletion if there's only subsite
  223.   * @param int $item_id The record to delete
  224.   * @return void
  225.   * @throws Exception if the deletion shouldn't proceed for some reason
  226.   */
  227. public function _deletePreSave($item_id)
  228. {
  229. $q = "SELECT COUNT(id) FROM ~subsites";
  230. $count = Pdb::q($q, [], 'val');
  231. if ($count == 1) {
  232. throw new Exception('You cannot delete the only subsite in the system; must have at least one subsite at all times');
  233. }
  234. }
  235.  
  236. }
  237.