PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/application/controllers/admin/manage/scheduler.php

http://github.com/ushahidi/Ushahidi_Web
PHP | 225 lines | 166 code | 30 blank | 29 comment | 18 complexity | 611285f475853245d3b3822463170864 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Scheduler
  4. * This controller is used to manage the scheduled tasks
  5. *
  6. * PHP version 5
  7. * LICENSE: This source file is subject to LGPL license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.gnu.org/copyleft/lesser.html
  10. * @author Ushahidi Team <team@ushahidi.com>
  11. * @package Ushahidi - http://source.ushahididev.com
  12. * @subpackage Admin
  13. * @copyright Ushahidi - http://www.ushahidi.com
  14. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
  15. */
  16. class Scheduler_Controller extends Admin_Controller
  17. {
  18. function __construct()
  19. {
  20. parent::__construct();
  21. $this->template->this_page = 'manage';
  22. // If user doesn't have access, redirect to dashboard
  23. if ( ! $this->auth->has_permission("manage"))
  24. {
  25. url::redirect(url::site().'admin/dashboard');
  26. }
  27. }
  28. function index()
  29. {
  30. $this->template->content = new View('admin/manage/scheduler/main');
  31. // Check if we should be running the scheduler and then do it
  32. if (isset($_GET['run_scheduler'])){
  33. // Get all active scheduled items
  34. foreach (ORM::factory('scheduler')
  35. ->where('scheduler_active','1')
  36. ->find_all() as $scheduler)
  37. {
  38. $s_controller = $scheduler->scheduler_controller;
  39. try {
  40. $dispatch = Dispatch::controller($s_controller, "scheduler/");
  41. if ($dispatch instanceof Dispatch) $run = $dispatch->method('index', '');
  42. }
  43. catch (Exception $e)
  44. {
  45. $run = FALSE;
  46. }
  47. if ($run !== FALSE)
  48. {
  49. // Set last time of last execution
  50. $schedule_time = time();
  51. $scheduler->scheduler_last = $schedule_time;
  52. $scheduler->save();
  53. // Record Action to Log
  54. $scheduler_log = new Scheduler_Log_Model();
  55. $scheduler_log->scheduler_id = $scheduler->id;
  56. $scheduler_log->scheduler_status = "200";
  57. $scheduler_log->scheduler_date = $schedule_time;
  58. $scheduler_log->save();
  59. }
  60. }
  61. }
  62. // setup and initialize form field names
  63. $form = array(
  64. 'action' => '',
  65. 'schedule_id' => '',
  66. 'scheduler_weekday' => '',
  67. 'scheduler_day' => '',
  68. 'scheduler_hour' => '',
  69. 'scheduler_minute' => '',
  70. 'scheduler_active' => ''
  71. );
  72. // copy the form as errors, so the errors will be stored with keys corresponding to the form field names
  73. $errors = $form;
  74. $form_error = FALSE;
  75. $form_saved = FALSE;
  76. $form_action = "";
  77. if ($_POST)
  78. {
  79. //print_r($_POST);
  80. $post = Validation::factory( $_POST );
  81. // Add some filters
  82. $post->pre_filter('trim', TRUE);
  83. if ($post->action == 'a') // Add Action
  84. {
  85. // Add some rules, the input field, followed by a list of checks, carried out in order
  86. $post->add_rules('scheduler_weekday','required', 'between[1,7]');
  87. $post->add_rules('scheduler_day','required', 'between[-1,31]');
  88. $post->add_rules('scheduler_hour','required', 'between[-1,23]');
  89. $post->add_rules('scheduler_minute','required', 'between[-1,59]');
  90. }
  91. if ( $post->validate() )
  92. {
  93. $scheduler_id = $post->scheduler_id;
  94. $scheduler = new Scheduler_Model($scheduler_id);
  95. if ($post->action == 'v')
  96. { // Active/Inactive Action
  97. if ($scheduler->loaded == TRUE)
  98. {
  99. $scheduler->scheduler_active = ($scheduler->scheduler_active == 1) ? 0 : 1;
  100. $scheduler->save();
  101. $form_saved = TRUE;
  102. $form_action = utf8::strtoupper(Kohana::lang('ui_admin.modified'));
  103. }
  104. }
  105. else
  106. { // SAVE Schedule
  107. $scheduler->scheduler_weekday = $post->scheduler_weekday;
  108. $scheduler->scheduler_day = $post->scheduler_day;
  109. $scheduler->scheduler_hour = $post->scheduler_hour;
  110. $scheduler->scheduler_minute = $post->scheduler_minute;
  111. $scheduler->save();
  112. $form_saved = TRUE;
  113. $form_action = utf8::strtoupper(Kohana::lang('ui_admin.edited'));
  114. }
  115. } else {
  116. // repopulate the form fields
  117. $form = arr::overwrite($form, $post->as_array());
  118. // populate the error fields, if any
  119. $errors = arr::overwrite($errors, $post->errors('scheduler'));
  120. $form_error = TRUE;
  121. }
  122. }
  123. // Pagination
  124. $pagination = new Pagination(array(
  125. 'query_string' => 'page',
  126. 'items_per_page' => $this->items_per_page,
  127. 'total_items' => ORM::factory('scheduler')->count_all()
  128. ));
  129. $schedules = ORM::factory('scheduler')
  130. ->orderby('scheduler_name', 'asc')
  131. ->find_all($this->items_per_page, $pagination->sql_offset);
  132. $this->template->content->weekday_array = array(
  133. "-1"=>"ALL",
  134. "0"=>"Sunday",
  135. "1"=>"Monday",
  136. "2"=>"Tuesday",
  137. "3"=>"Wednesday",
  138. "4"=>"Thursday",
  139. "5"=>"Friday",
  140. "6"=>"Saturday",
  141. );
  142. for ($i=0; $i <= 31 ; $i++)
  143. {
  144. $day_array = $i;
  145. }
  146. $this->template->content->day_array = $day_array;
  147. $day_array = array();
  148. $day_array[-1] = "ALL";
  149. for ($i=1; $i <= 31 ; $i++)
  150. {
  151. $day_array[] = $i;
  152. }
  153. $this->template->content->day_array = $day_array;
  154. $hour_array = array();
  155. $hour_array[-1] = "ALL";
  156. for ($i=0; $i <= 23 ; $i++)
  157. {
  158. $hour_array[] = $i;
  159. }
  160. $this->template->content->hour_array = $hour_array;
  161. $minute_array = array();
  162. $minute_array[-1] = "ALL";
  163. for ($i=0; $i <= 59 ; $i++)
  164. {
  165. $minute_array[] = $i;
  166. }
  167. $this->template->content->minute_array = $minute_array;
  168. $this->template->content->form_error = $form_error;
  169. $this->template->content->form_saved = $form_saved;
  170. $this->template->content->form_action = $form_action;
  171. $this->template->content->pagination = $pagination;
  172. $this->template->content->total_items = $pagination->total_items;
  173. $this->template->content->schedules = $schedules;
  174. $this->template->content->errors = $errors;
  175. // Javascript Header
  176. $this->themes->js = new View('admin/manage/scheduler/scheduler_js');
  177. }
  178. public function log()
  179. {
  180. $this->template->content = new View('admin/manage/scheduler/log');
  181. // Pagination
  182. $pagination = new Pagination(array(
  183. 'query_string' => 'page',
  184. 'items_per_page' => $this->items_per_page,
  185. 'total_items' => ORM::factory('scheduler_log')
  186. ->count_all()
  187. ));
  188. $scheduler_logs = ORM::factory('scheduler_log')
  189. ->orderby('scheduler_date','desc')
  190. ->find_all($this->items_per_page, $pagination->sql_offset);
  191. $this->template->content->scheduler_logs = $scheduler_logs;
  192. $this->template->content->pagination = $pagination;
  193. $this->template->content->total_items = $pagination->total_items;
  194. }
  195. }