SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Controllers/Admin/CronJobAdminController.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 Kohana;
  17.  
  18. use Sprout\Helpers\AdminAuth;
  19. use Sprout\Helpers\Csrf;
  20. use Sprout\Helpers\Notification;
  21. use Sprout\Helpers\RefineBar;
  22. use Sprout\Helpers\RefineWidgetSelect;
  23. use Sprout\Helpers\RefineWidgetTextbox;
  24. use Sprout\Helpers\Register;
  25. use Sprout\Helpers\Sprout;
  26. use Sprout\Helpers\Url;
  27. use Sprout\Helpers\View;
  28.  
  29.  
  30. /**
  31. * Handles most processing for Cron Jobs
  32. **/
  33. class CronJobAdminController extends ListAdminController
  34. {
  35. protected $controller_name = 'cron_job';
  36. protected $friendly_name = 'Cron Jobs';
  37. protected $navigation_name = 'Dev tools';
  38. protected $add_defaults = array(
  39. 'active' => 1,
  40. );
  41. protected $main_order = 'item.date_added DESC';
  42. protected $main_delete = false;
  43.  
  44.  
  45. /**
  46.   * Constructor
  47.   **/
  48. public function __construct()
  49. {
  50. $this->main_columns = [
  51. 'Name' => 'name',
  52. 'Status' => 'status',
  53. 'Date' => 'date_added',
  54. ];
  55.  
  56. $this->refine_bar = new RefineBar();
  57. $this->refine_bar->setGroup('Job');
  58. $this->refine_bar->addWidget(new RefineWidgetTextbox('name', 'Name'));
  59. $this->refine_bar->addWidget(new RefineWidgetSelect('status', 'Status', array('Incomplete', 'Complete')));
  60.  
  61. parent::__construct();
  62. }
  63.  
  64.  
  65. /**
  66.   * Returns the contents of the navigation pane for the list
  67.   **/
  68. public function _getTools()
  69. {
  70. return null;
  71. }
  72.  
  73. public function _getNavigation()
  74. {
  75. $nav = new View('sprout/dbtools/navigation');
  76. return $nav->render();
  77. }
  78.  
  79.  
  80. /**
  81.   * Return the fields to show in the sidebar when adding or editing a record.
  82.   * These fields are shown under a heading of "Visibility"
  83.   *
  84.   * Key is the field name, value is the field label
  85.   *
  86.   * @return array
  87.   */
  88. public function _getVisibilityFields()
  89. {
  90. return [];
  91. }
  92.  
  93.  
  94. public function _getAddForm() { return "<p><i>You can't add these!</i></p>"; }
  95. public function _addSave(&$item_id) { return false; }
  96. public function _editSave($item_id) { return false; }
  97.  
  98.  
  99. /**
  100.   * UI for running cron jobs manually
  101.   */
  102. public function _extraManualRun()
  103. {
  104. $jobs = Register::getAllCronJobs();
  105.  
  106. $job_list = [];
  107. foreach ($jobs as $sched => $sj) {
  108. $job_list[$sched] = [];
  109. foreach ($sj as $job) {
  110. $key = $sched . '__' . $job[0] . '__' . $job[1];
  111.  
  112. $ns_hunks = explode('\\', $job[0]);
  113. $class = array_pop($ns_hunks);
  114. $val = "{$class}::{$job[1]}";
  115.  
  116. $job_list[$sched][$key] = $val;
  117. }
  118. }
  119.  
  120. $view = new View('sprout/admin/cron_job_manual_run');
  121. $view->jobs = $job_list;
  122.  
  123. return [
  124. 'title' => 'Cron job manual run',
  125. 'content' => $view->render(),
  126. ];
  127. }
  128.  
  129.  
  130. /**
  131.   * Manually runs a cron job
  132.   */
  133. public function manualRunAction()
  134. {
  135. AdminAuth::checkLogin();
  136. Csrf::checkOrDie();
  137.  
  138. if (empty($_POST['job'])) {
  139. Notification::error('You must select a cron job');
  140. Url::redirect('admin/extra/cron_job/manual_run');
  141. }
  142.  
  143. list($sched, $class, $func) = explode('__', $_POST['job']);
  144.  
  145. // Check the job is registered and reject any which are not
  146. $jobs = Register::getCronJobs($sched);
  147. $found = false;
  148. foreach ($jobs as $j) {
  149. if ($j[0] === $class and $j[1] === $func) {
  150. $found = true;
  151. break;
  152. }
  153. }
  154. if (!$found) {
  155. Notification::error('Specified cron job is not registered');
  156. Url::redirect('admin/extra/cron_job/manual_run');
  157. }
  158.  
  159. // Instance class - this may throw an exception if class not found or invalid
  160. $inst = Sprout::instance(
  161. $class,
  162. ['Sprout\\Controllers\\Controller']
  163. );
  164.  
  165. // Set up cron environment
  166. header('Content-type: text/plain');
  167. Kohana::closeBuffers();
  168.  
  169. echo 'Class: ', $class, PHP_EOL;
  170. echo 'Func: ', $func, PHP_EOL;
  171. echo str_repeat('-', 80), PHP_EOL;
  172.  
  173. call_user_func([$inst, $func]);
  174. }
  175.  
  176. }
  177.  
  178.  
  179.