PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/3.0/modules/scheduler/helpers/scheduler.php

https://github.com/wrlee/gallery3-contrib
PHP | 129 lines | 99 code | 11 blank | 19 comment | 15 complexity | 0150252de8a546ea5d6743d37efee354 MD5 | raw file
  1. <?php defined("SYSPATH") or die("No direct script access.");
  2. /**
  3. * Gallery - a web based photo album viewer and editor
  4. * Copyright (C) 2000-2011 Bharat Mediratta
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. class scheduler_Core {
  21. static function intervals() {
  22. static $intervals;
  23. if (empty($intervals)) {
  24. $intervals = array("3600" => t("Hourly"), "86400" => t("Daily"),
  25. "604800" => t("Weekly"), "2419200" => t("Monthly"));
  26. }
  27. return $intervals;
  28. }
  29. static function get_form($method, $schedule) {
  30. if ($method == "define") {
  31. $title = t("Create a scheduled event");
  32. $button_text = t("Create");
  33. } else {
  34. $title = t("Update a scheduled event");
  35. $button_text = t("Update");
  36. }
  37. $id = empty($schedule->id) ? "" : "/$schedule->id";
  38. $form = new Forge("admin/schedule/$method{$id}", "", "post",
  39. array("id" => "g-{$method}-schedule"));
  40. $group = $form->group("schedule_group")->label($title);
  41. $group->input("schedule_name")
  42. ->label(t("Description"))
  43. ->id("g-schedule-name")
  44. ->rules("required|length[0, 128]")
  45. ->error_messages("required", t("You must provide a description"))
  46. ->error_messages("length", t("Your description is too long"))
  47. ->value(!empty($schedule->name) ? $schedule->name : "");
  48. list ($dow, $display_time) = scheduler::format_time($schedule->next_run_datetime);
  49. $next = $group->group("run_date")->label(t("Scheduled Date"));
  50. $next->dropdown("dow")
  51. ->label(t("Day"))
  52. ->id("g-schedule-day")
  53. ->rules("required")
  54. ->options(array(t("Sunday"), t("Monday"), t("Tuesday"), t("Wednesday"),
  55. t("Thursday"), t("Friday"), t("Saturday")))
  56. ->selected($dow);
  57. $next->input("time")
  58. ->label(t("Hour"))
  59. ->id("g-schedule-time")
  60. ->rules("required")
  61. ->error_messages("required", t("You must provide a time"))
  62. ->error_messages("time_invalid", t("Invalid time"))
  63. ->callback("scheduler::valid_time")
  64. ->value($display_time);
  65. // need to set the top padding to zero on g-define-schedule li.g-error
  66. $group->dropdown("interval")->label(t("How often"))->id("g-schedule-frequency")
  67. ->options(scheduler::intervals())
  68. ->rules("required")
  69. ->error_messages("required", t("You must provide an interval"))
  70. ->selected(!empty($schedule->interval) ? $schedule->interval : "2419200");
  71. $group->hidden("callback")->value($schedule->task_callback);
  72. $group->submit("")->value($button_text);
  73. return $form;
  74. }
  75. static function format_time($time) {
  76. $local_time = localtime($time);
  77. $display_time = str_pad($local_time[2], 2, "0", STR_PAD_LEFT) . ":" .
  78. str_pad($local_time[1], 2, "0", STR_PAD_LEFT);
  79. return array($local_time[6], $display_time);
  80. }
  81. static function valid_time($field) {
  82. if (preg_match("/([0-9]{1,2}):([0-9]{2})/", $field->value, $matches)) {
  83. $hour = (int)$matches[1];
  84. $minutes = (int)$matches[2];
  85. if (!(0 <= $hour && $hour<= 23 || 0 <= $minutes && $minutes <= 59)) {
  86. $field->add_error("time_invalid", 1);
  87. }
  88. } else {
  89. $field->add_error("time_invalid", 1);
  90. }
  91. }
  92. static function get_definitions() {
  93. $schedule_definitions = array();
  94. $events = ORM::factory("schedule")
  95. ->order_by("next_run_datetime", "asc")
  96. ->find_all();
  97. if ($events->count()) {
  98. foreach ($events as $schedule) {
  99. $run_date = strftime("%A, %b %e, %Y %H:%M ", $schedule->next_run_datetime);
  100. $intervals = scheduler::intervals();
  101. $interval = $intervals[$schedule->interval];
  102. if (!empty($schedule->task_id)) {
  103. $status = t("Running");
  104. } else if ($schedule->next_run_datetime < time()) {
  105. $status = t("Overdue");
  106. } else {
  107. $status = t("Scheduled");
  108. }
  109. $schedule_definitions[] = (object)array("id" => $schedule->id,
  110. "name" => $schedule->name,
  111. "run_date" => $run_date,
  112. "interval" => $interval,
  113. "status" => $status);
  114. }
  115. }
  116. return $schedule_definitions;
  117. }
  118. }