summaryrefslogtreecommitdiff
path: root/core/helpers
diff options
context:
space:
mode:
authorTim Almdal <tnalmdal@shaw.ca>2009-02-27 16:28:20 +0000
committerTim Almdal <tnalmdal@shaw.ca>2009-02-27 16:28:20 +0000
commitdc4f784558db725eb555ce9668231b89aabb8954 (patch)
treec66727af638d7f1716d32c94f20547b00afb999c /core/helpers
parentc4cdecc05f128bac3842852f114b96eaa821f2ca (diff)
* Refactor task management methods from admin_maintenance.php to
task.php * Added a owner_id field to the task database * Modified the admin maintenace to show the owner of the task <<**** Requires a reinstallation of core ****>>
Diffstat (limited to 'core/helpers')
-rw-r--r--core/helpers/core_installer.php4
-rw-r--r--core/helpers/task.php101
2 files changed, 104 insertions, 1 deletions
diff --git a/core/helpers/core_installer.php b/core/helpers/core_installer.php
index bf83c339..e1dd34c2 100644
--- a/core/helpers/core_installer.php
+++ b/core/helpers/core_installer.php
@@ -171,7 +171,9 @@ class core_installer {
`percent_complete` int(9) default 0,
`state` varchar(32) default NULL,
`status` varchar(255) default NULL,
- PRIMARY KEY (`id`))
+ `owner_id` int(9) default NULL,
+ PRIMARY KEY (`id`),
+ KEY (`owner_id`))
ENGINE=InnoDB DEFAULT CHARSET=utf8;");
$db->query("CREATE TABLE `vars` (
diff --git a/core/helpers/task.php b/core/helpers/task.php
new file mode 100644
index 00000000..816a5288
--- /dev/null
+++ b/core/helpers/task.php
@@ -0,0 +1,101 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2008 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class task_Core {
+ /**
+ * Get all available tasks
+ */
+ static function get_task_definitions($type=array("admin", "both")) {
+ $tasks = array();
+ foreach (module::installed() as $module_name => $module_info) {
+ $class_name = "{$module_name}_task";
+ if (method_exists($class_name, "available_tasks")) {
+ foreach (call_user_func(array($class_name, "available_tasks")) as $task) {
+ if (in_array($task->type, $type)) {
+ $tasks[$task->callback] = $task;
+ }
+ }
+ }
+ }
+
+ return $tasks;
+ }
+
+ static function create($task_callback) {
+ $task_definitions = self::get_task_definitions(array("admin", "general", "both"));
+
+ $task = ORM::factory("task");
+ $task->callback = $task_callback;
+ $task->name = $task_definitions[$task_callback]->name;
+ $task->percent_complete = 0;
+ $task->status = "";
+ $task->state = "started";
+ $task->owner_id = user::active()->id;
+ $task->context = serialize(array());
+ $task->save();
+
+ return $task;
+ }
+
+ static function cancel($task_id) {
+ $task = ORM::factory("task", $task_id);
+ if (!$task->loaded) {
+ throw new Exception("@todo MISSING_TASK");
+ }
+ $task->done = 1;
+ $task->state = "cancelled";
+ $task->save();
+
+ return $task;
+ }
+
+ static function remove($task_id) {
+ $task = ORM::factory("task", $task_id);
+ if ($task->loaded) {
+ $task->delete();
+ }
+ }
+
+ static function run($task_id) {
+ $task = ORM::factory("task", $task_id);
+ if (!$task->loaded) {
+ throw new Exception("@todo MISSING_TASK");
+ }
+
+ $task->state = "running";
+ call_user_func_array($task->callback, array(&$task));
+ $task->save();
+
+ return $task;
+ }
+
+ static function success($task, $location=null) {
+ $result = array("result" => "success", "task" => $task->as_array());
+ if (!empty($location)) {
+ $result["location"] = $location;
+ }
+ return json_encode($result);
+ }
+
+ static function in_progress($task) {
+ return json_encode(
+ array("result" => "in_progress",
+ "task" => $task->as_array()));
+ }
+} \ No newline at end of file