summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Almdal <tnalmdal@shaw.ca>2009-03-03 05:59:38 +0000
committerTim Almdal <tnalmdal@shaw.ca>2009-03-03 05:59:38 +0000
commit7cadb4b21b90864b4151481da9aba244d7083788 (patch)
treef8df3ce866f69a6bc11790d3b2cb4c18a4ba297b
parent1d5cca34efcd25473f00ed5d6594ea15c569622f (diff)
Refactored the batch Api: 1) created a small batch helper class:
Starting a batch call batch::operation(name, item). In the case of adding photos name = add and item is the parent of the new items. When the operation is finished the batch::end_operation(name) is called. operation and end_operation events are called. Handlers (i.e. item_created) can call batch::in_progress(name) to determine if a batch is being processed.
-rw-r--r--core/controllers/scaffold.php12
-rw-r--r--core/controllers/simple_uploader.php4
-rw-r--r--core/helpers/batch.php40
-rw-r--r--modules/local_import/controllers/local_import.php6
4 files changed, 51 insertions, 11 deletions
diff --git a/core/controllers/scaffold.php b/core/controllers/scaffold.php
index 45242b8c..c7f336df 100644
--- a/core/controllers/scaffold.php
+++ b/core/controllers/scaffold.php
@@ -218,8 +218,8 @@ class Scaffold_Controller extends Template_Controller {
throw new Exception("@todo BAD_ALBUM");
}
- $batch_id = mt_rand();
- module::event("start_batch");
+ batch::operation("add", $parent);
+
cookie::set("add_photos_path", $path);
$photo_count = 0;
foreach (glob("$path/*.[Jj][Pp][Gg]") as $file) {
@@ -227,7 +227,7 @@ class Scaffold_Controller extends Template_Controller {
photo::create($parent, $file, basename($file), basename($file));
$photo_count++;
}
- module::event("end_batch");
+ batch::end_operation("add");
if ($photo_count > 0) {
log::success("content", "(scaffold) Added $photo_count photos",
@@ -244,8 +244,8 @@ class Scaffold_Controller extends Template_Controller {
$test_images = glob(APPPATH . "tests/images/*.[Jj][Pp][Gg]");
- $batch_id = mt_rand();
- module::event("start_batch");
+ batch::operation("add", null);
+
$album_count = $photo_count = 0;
for ($i = 0; $i < $count; $i++) {
set_time_limit(30);
@@ -269,7 +269,7 @@ class Scaffold_Controller extends Template_Controller {
$photo_count++;
}
}
- module::event("end_batch");
+ batch::end_operation("add");
if ($photo_count > 0) {
log::success("content", "(scaffold) Added $photo_count photos");
diff --git a/core/controllers/simple_uploader.php b/core/controllers/simple_uploader.php
index 5ae4bb2a..38eb5c38 100644
--- a/core/controllers/simple_uploader.php
+++ b/core/controllers/simple_uploader.php
@@ -48,7 +48,7 @@ class Simple_Uploader_Controller extends Controller {
$file_validation = new Validation($_FILES);
$file_validation->add_rules("file", "upload::valid", "upload::type[gif,jpg,png,flv,mp4]");
if ($file_validation->validate()) {
- module::event("start_batch");
+ batch::operation("add", $album);
$temp_filename = upload::save("file");
$title = substr(basename($temp_filename), 10); // Skip unique identifier Kohana adds
$path_info = pathinfo($temp_filename);
@@ -65,7 +65,7 @@ class Simple_Uploader_Controller extends Controller {
}
public function finish() {
- module::event("end_batch");
+ batch::end_operation("add");
print json_encode(array("result" => "success"));
}
diff --git a/core/helpers/batch.php b/core/helpers/batch.php
new file mode 100644
index 00000000..4411b1b0
--- /dev/null
+++ b/core/helpers/batch.php
@@ -0,0 +1,40 @@
+<?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 batch_Core {
+ static function operation($name, $item) {
+ if (self::in_progress($name) === false) {
+ Session::instance()->set("operation_$name", "1");
+ module::event("operation", $name, $item);
+ }
+ }
+
+ static function end_operation($name) {
+ if (self::in_progress($name) === true) {
+ module::event("end_operation", $name);
+ Session::instance()->set("operation_$name", null);
+ }
+ }
+
+ static function in_progress($name) {
+ $value = Session::instance()->get("operation_$name", null);
+ return !empty($value);
+ }
+}
diff --git a/modules/local_import/controllers/local_import.php b/modules/local_import/controllers/local_import.php
index 8fc183b7..ee4a4f1a 100644
--- a/modules/local_import/controllers/local_import.php
+++ b/modules/local_import/controllers/local_import.php
@@ -65,8 +65,8 @@ class Local_Import_Controller extends Controller {
}
$path = $this->input->post("path");
- module::event("start_batch");
-
+ batch::operation("add", $parent);
+
$source_path = $path[0];
for ($i = 1; $i < count($path); $i++) { // skip the first path
$source_path .= "/$path[$i]";
@@ -92,7 +92,7 @@ class Local_Import_Controller extends Controller {
}
public function finish() {
- module::event("end_batch");
+ batch::end_operation("add");
print json_encode(array("result" => "success"));
}