SproutCMS

This is the code documentation for the SproutCMS project

source of /modules/Demo/Controllers/Admin/DemoItemAdminController.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 SproutModules\Karmabunny\Demo\Controllers\Admin;
  15.  
  16. use InvalidArgumentException;
  17.  
  18. use Sprout\Controllers\Admin\HasCategoriesAdminController;
  19. use Sprout\Helpers\AdminAuth;
  20. use Sprout\Helpers\ColModifierBinary;
  21. use Sprout\Helpers\Enc;
  22. use Sprout\Helpers\Json;
  23. use Sprout\Helpers\Pdb;
  24. use Sprout\Helpers\Url;
  25. use Sprout\Helpers\WorkerCtrl;
  26.  
  27.  
  28. /**
  29.  * Handles admin processing for Demo items
  30.  */
  31. class DemoItemAdminController extends HasCategoriesAdminController
  32. {
  33. protected $controller_name = 'demo_item';
  34. protected $friendly_name = 'Demo items';
  35. protected $add_defaults = [
  36. 'active' => 1,
  37. ];
  38. protected $main_columns = [];
  39. protected $main_delete = true;
  40.  
  41.  
  42. /**
  43.   * Constructor
  44.   **/
  45. public function __construct()
  46. {
  47. $this->main_columns = [
  48. 'Name' => 'name',
  49. 'Active' => [new ColModifierBinary(), 'active'],
  50. 'Email' => 'email',
  51. ];
  52.  
  53. $this->initRefineBar();
  54.  
  55. parent::__construct();
  56. }
  57.  
  58.  
  59. public function _getTools()
  60. {
  61. $tools = parent::_getTools();
  62.  
  63. $url = "admin/call/{$this->controller_name}/runWorker";
  64. $tools['worker'] = '<li class="worker"><a href="' . Enc::html($url) . '">Run demo worker</a></li>';
  65.  
  66. return $tools;
  67. }
  68.  
  69.  
  70. /**
  71.   * Pre-render hook for adding
  72.   **/
  73. protected function _addPreRender($view)
  74. {
  75. parent::_addPreRender($view);
  76. }
  77.  
  78.  
  79. /**
  80.   * Return the sub-actions for adding; for spec {@see AdminController::renderSubActions}
  81.   * @return array
  82.   */
  83. public function _getAddSubActions()
  84. {
  85. $actions = parent::_getAddSubActions();
  86. // Add your actions here, like this: $actions[] = [ ... ];
  87. return $actions;
  88. }
  89.  
  90.  
  91. /**
  92.   * Saves the provided POST data into a new record in the database
  93.   *
  94.   * @param int $item_id After saving, the new record id will be returned in this parameter
  95.   * @param bool True on success, false on failure
  96.   */
  97. public function _addSave(&$item_id)
  98. {
  99. return parent::_addSave($item_id);
  100. }
  101.  
  102.  
  103.  
  104. /**
  105.   * Pre-render hook for editing
  106.   **/
  107. protected function _editPreRender($view, $item_id)
  108. {
  109. parent::_editPreRender($view, $item_id);
  110. }
  111.  
  112.  
  113. /**
  114.   * Return the sub-actions for editing; for spec {@see AdminController::renderSubActions}
  115.   * @return array
  116.   */
  117. public function _getEditSubActions($item_id)
  118. {
  119. $actions = parent::_getEditSubActions($item_id);
  120. // Add your actions here, like this: $actions[] = [ ... ];
  121. return $actions;
  122. }
  123.  
  124.  
  125. /**
  126.   * Process the saving of a record.
  127.   *
  128.   * @param int $item_id The record to save
  129.   * @return boolean True on success, false on failure
  130.   */
  131. public function _editSave($item_id)
  132. {
  133. $item_id = (int) $item_id;
  134. if ($item_id <= 0) throw new InvalidArgumentException('$item_id must be greater than 0');
  135.  
  136. return parent::_editSave($item_id);
  137. }
  138.  
  139.  
  140. /**
  141.   * AJAX lookup of the words table for the 'word' field
  142.   * @return string AJAX array containing required 'value' key for each item
  143.   */
  144. public function ajaxLookup() {
  145. AdminAuth::checkLogin();
  146.  
  147. if (!empty($_GET['id'])) {
  148. $q = "SELECT name AS label FROM ~words WHERE id = ?";
  149. $res = Pdb::q($q, [$_GET['id']], 'arr');
  150. Json::out($res);
  151. }
  152.  
  153. $q = "SELECT id, name AS value
  154. FROM ~words
  155. WHERE name LIKE CONCAT(?, '%')";
  156. $res = Pdb::q($q, [Pdb::likeEscape($_GET['term'])], 'arr');
  157. Json::out($res);
  158. }
  159.  
  160.  
  161. /**
  162.   * AJAX lookup of the words table for the autofill list
  163.   * @return string AJAX array containing required 'value' key for each item
  164.   */
  165. public function autofillLookup() {
  166. AdminAuth::checkLogin();
  167.  
  168. $q = "SELECT id, CONCAT('#', id, ' - ', name) AS value
  169. FROM ~words
  170. WHERE name LIKE CONCAT(?, '%')";
  171. $res = Pdb::q($q, [Pdb::likeEscape($_GET['term'])], 'arr');
  172. Json::out($res);
  173. }
  174.  
  175.  
  176. public function runWorker()
  177. {
  178. $worker = WorkerCtrl::start('SproutModules\\Karmabunny\\Demo\\Helpers\\DemoWorker', 100, 500, 1000);
  179. Url::redirect($worker['log_url']);
  180. }
  181. }
  182.  
  183.  
  184.