summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2009-08-29 10:48:23 -0700
committerBharat Mediratta <bharat@menalto.com>2009-08-29 10:48:23 -0700
commitb833cb607323000b987428c94995e18a9d07df65 (patch)
treecaba6f8fb4abe617af2a5caef2bbd16f7719a5ba
parentf257cd3d69a771ce245c432e37c9f9e7e53ad03d (diff)
Get rid of the task infrastructure. The multiple requests greatly
slow down simple operations. We may run into problems with more complex operations, but let's only add tasks into the mix when it's clear that we need them.
-rw-r--r--modules/organize/controllers/organize.php205
-rw-r--r--modules/organize/js/organize.js72
-rw-r--r--modules/organize/views/organize_dialog.html.php3
3 files changed, 67 insertions, 213 deletions
diff --git a/modules/organize/controllers/organize.php b/modules/organize/controllers/organize.php
index f25a41ca..de53c4ed 100644
--- a/modules/organize/controllers/organize.php
+++ b/modules/organize/controllers/organize.php
@@ -44,38 +44,63 @@ class Organize_Controller extends Controller {
function move_to($album_id) {
access::verify_csrf();
- $task_def = Task_Definition::factory()
- ->callback("Organize_Controller::move_task_handler");
- $task = task::create($task_def, array("album_id" => $album_id,
- "source_ids" => $this->input->post("source_ids")));
+ $album = ORM::factory("item", $album_id);
+ foreach ($this->input->post("source_ids") as $source_id) {
+ item::move(ORM::factory("item", $source_id), $album);
+ }
print json_encode(
- array("result" => "started",
- "status" => $task->status,
- "url" => url::site("organize/run/$task->id?csrf=" . access::csrf_token())));
+ array("tree" => self::_tree($album)->__toString(),
+ "grid" => self::_get_micro_thumb_grid($album, 0)->__toString()));
}
function rearrange($target_id, $before_or_after) {
access::verify_csrf();
$target = ORM::factory("item", $target_id);
- $parent = $target->parent();
- access::required("view", $parent);
- access::required("edit", $parent);
-
- $task_def = Task_Definition::factory()
- ->callback("Organize_Controller::rearrange_task_handler");
- $task = task::create(
- $task_def,
- array("target_id" => $target_id,
- "album_id" => $parent->id,
- "before_or_after" => $before_or_after,
- "source_ids" => $this->input->post("source_ids")));
+ $album = $target->parent();
+ access::required("view", $album);
+ access::required("edit", $album);
+
+ $source_ids = $this->input->post("source_ids", array());
+
+ if ($album->sort_column != "weight") {
+ $i = 0;
+ foreach ($album->children() as $child) {
+ // Do this directly in the database to avoid sending notifications
+ Database::Instance()->update("items", array("weight" => ++$i), array("id" => $child->id));
+ }
+ $album->sort_column = "weight";
+ $album->sort_order = "ASC";
+ $album->save();
+ $target->reload();
+ }
+
+ // Find the insertion point
+ $target_weight = $target->weight;
+ if ($before_or_after == "after") {
+ $target_weight++;
+ }
+
+ // Make a hole
+ $count = count($source_ids);
+ Database::Instance()->query(
+ "UPDATE {items} " .
+ "SET `weight` = `weight` + $count " .
+ "WHERE `weight` >= $target_weight AND `parent_id` = {$album->id}");
+
+ // Insert source items into the hole
+ foreach ($source_ids as $source_id) {
+ Database::Instance()->update(
+ "items", array("weight" => $target_weight++), array("id" => $source_id));
+ }
+
+ module::event("album_rearrange", $album);
print json_encode(
- array("result" => "started",
- "status" => $task->status,
- "url" => url::site("organize/run/$task->id?csrf=" . access::csrf_token())));
+ array("grid" => self::_get_micro_thumb_grid($album, 0)->__toString(),
+ "sort_column" => $album->sort_column,
+ "sort_order" => $album->sort_order));
}
function sort_order($album_id, $col, $dir) {
@@ -107,146 +132,10 @@ class Organize_Controller extends Controller {
return $v;
}
- /**
- * Run the task
- */
- function run($task_id) {
- access::verify_csrf();
-
- $task = ORM::factory("task", $task_id);
- if (!$task->loaded || $task->owner_id != user::active()->id) {
- access::forbidden();
- }
-
- $task = task::run($task_id);
- $results = array("done" => $task->done, "status" => $task->status,
- "percent_complete" => $task->percent_complete);
-
- $album = ORM::factory("item", $task->get("album_id"));
- $result = array(
- "done" => $task->done,
- "status" => $task->status,
- "percent_complete" => $task->percent_complete);
- if ($task->done) {
- $result["tree"] = self::_tree($album)->__toString();
- $result["grid"] = self::_get_micro_thumb_grid($album, 0)->__toString();
- }
- print json_encode($result);
- }
-
private static function _tree($album) {
$v = new View("organize_tree.html");
$v->parents = $album->parents();
$v->album = $album;
return $v;
}
-
- static function move_task_handler($task) {
- $start = microtime(true);
- if ($task->percent_complete == 0) {
- batch::start();
- }
-
- $target_album = ORM::factory("item", $task->get("album_id"));
- $source_ids = $task->get("source_ids", array());
- $idx = $task->get("current", 0);
- $count = 0;
- for (; $idx < count($source_ids) && microtime(true) - $start < 0.5; $idx++) {
- item::move(ORM::factory("item", $source_ids[$idx]), $target_album);
- $count++;
- }
- $task->set("current", $idx);
- $task->percent_complete = (int)($idx / count($source_ids) * 100);
- if ($task->percent_complete == 100) {
- batch::stop();
- $task->done = true;
- $task->state = "success";
- $parents = array();
- foreach ($target_album->parents() as $parent) {
- $parents[$parent->id] = 1;
- }
- $parents[$target_album->id] = 1;
- }
- }
-
- static function rearrange_task_handler($task) {
- $start = microtime(true);
- $mode = $task->get("mode", "init");
-
- if ($task->percent_complete == 0) {
- batch::start();
- }
- while (microtime(true) - $start < 1.5) {
- switch ($mode) {
- case "init":
- $album = ORM::factory("item", $task->get("album_id"));
- if ($album->sort_column != "weight") {
- $mode = "convert-to-weight-order";
- } else {
- $mode = "find-insertion-point";
- }
- break;
-
- case "convert-to-weight-order":
- $i = 0;
- $album = ORM::factory("item", $task->get("album_id"));
- foreach ($album->children() as $child) {
- // Do this directly in the database to avoid sending notifications
- Database::Instance()->update("items", array("weight" => ++$i), array("id" => $child->id));
- }
- $album->sort_column = "weight";
- $album->sort_order = "ASC";
- $album->save();
- $mode = "find-insertion-point";
- $task->percent_complete = 25;
- break;
-
- case "find-insertion-point":
- $album = ORM::factory("item", $task->get("album_id"));
- $target_weight = $album->weight;
-
- if ($task->get("before_or_after") == "after") {
- $target_weight++;
- }
- $task->set("target_weight", $target_weight);
- $task->percent_complete = 40;
- $mode = "make-a-hole";
- break;
-
- case "make-a-hole":
- $target_weight = $task->get("target_weight");
- $source_ids = $task->get("source_ids");
- $count = count($source_ids);
- $album_id = $task->get("album_id");
- Database::Instance()->query(
- "UPDATE {items} " .
- "SET `weight` = `weight` + $count " .
- "WHERE `weight` >= $target_weight AND `parent_id` = {$album_id}");
-
- $mode = "insert-source-items";
- $task->percent_complete = 80;
- break;
-
- case "insert-source-items":
- $target_weight = $task->get("target_weight");
- foreach ($source_ids as $source_id) {
- Database::Instance()->update(
- "items", array("weight" => $target_weight++), array("id" => $source_id));
- }
- $mode = "done";
- break;
-
- case "done":
- $album = ORM::factory("item", $task->get("album_id"));
- module::event("album_rearrange", $album);
- batch::stop();
- $task->done = true;
- $task->state = "success";
- $task->percent_complete = 100;
- break;
- }
- }
-
- $task->set("mode", $mode);
- }
}
diff --git a/modules/organize/js/organize.js b/modules/organize/js/organize.js
index 5e7bd47c..04e14a2f 100644
--- a/modules/organize/js/organize.js
+++ b/modules/organize/js/organize.js
@@ -56,11 +56,12 @@
tolerance: "pointer",
greedy: true,
drop: function(event, ui) {
+ var before_or_after = $(".currentDropTarget").css("borderLeftStyle") == "solid" ? "before" : "after";
$.organize.do_drop({
url: rearrange_url
.replace("__TARGET_ID__", $(".currentDropTarget").attr("ref"))
.replace("__ALBUM_ID__", $(".currentDropTarget").attr("ref"))
- .replace("__BEFORE__", $(".currentDropTarget").css("borderLeftStyle") == "solid" ? "before" : "after"),
+ .replace("__BEFORE__", before_or_after),
source: $(ui.helper).children("img")
});
}
@@ -91,56 +92,23 @@
});
if (source_ids.length) {
- $("#gOrganize .gProgressBar").progressbar().progressbar("value", 0);
- $("#gOrganizeProgress").animate(
- { height: "toggle", display: "toggle" },
- {
- duration: "fast",
- step: function() {
- },
- complete: function() {
- $("#gMicroThumbPanel").height($("#gMicroThumbPanel").height() - $(this).height());
- $.ajax({
- url: options.url,
- type: "POST",
- async: false,
- data: { "source_ids[]": source_ids },
- dataType: "json",
- success: function(data, textStatus) {
- $("#gOrganize .gProgressBar").progressbar("value", data.percent_complete);
- setTimeout(function() { $.organize._run_task(data.url); }, 0);
- }
- });
- }
- });
+ $.post(options.url,
+ { "source_ids[]": source_ids },
+ function(data) { $.organize._refresh(data); },
+ "json");
}
},
- _run_task: function(url) {
- $.ajax({
- url: url,
- async: false,
- dataType: "json",
- success: function(data, textStatus) {
- $("#gOrganize .gProgressBar").progressbar("value", data.percent_complete);
- if (data.done) {
- var height = $("#gOrganizeProgress").height();
- $("#gOrganizeProgress").slideUp();
- $("#gMicroThumbPanel").height($("#gDialog").innerHeight() - 90);
- if (data.tree) {
- $("#gOrganizeAlbumTree").html(data.tree);
- }
- if (data.grid) {
- $("#gMicroThumbGrid").html(data.grid);
- $("#gOrganizeSortColumn").attr("value", data.sort_column);
- $("#gOrganizeSortOrder").attr("value", data.sort_order);
- }
- $.organize.set_handlers();
- } else {
- setTimeout(function() { $.organize._run_task(url); }, 0);
- }
- }
- });
+ _refresh: function(data) {
+ if (data.tree) {
+ $("#gOrganizeAlbumTree").html(data.tree);
+ }
+ if (data.grid) {
+ $("#gMicroThumbGrid").html(data.grid);
+ $("#gOrganizeSortColumn").attr("value", data.sort_column);
+ $("#gOrganizeSortOrder").attr("value", data.sort_order);
+ }
+ $.organize.set_handlers();
},
mouse_move_handler: function(event) {
@@ -211,13 +179,13 @@
$("#gOrganizeAlbumTree .selected").removeClass("selected");
$(".gAlbumText[ref=" + id + "]").addClass("selected");
var url = $("#gMicroThumbPanel").attr("ref").replace("__ITEM_ID__", id).replace("__OFFSET__", 0);
- $.get(url, function(data) {
+ $.get(url, {},
+ function(data) {
$("#gMicroThumbGrid").html(data.grid);
$("#gOrganizeSortColumn").attr("value", data.sort_column);
$("#gOrganizeSortOrder").attr("value", data.sort_order);
$.organize.set_handlers();
},
- function() { },
"json");
},
@@ -229,13 +197,13 @@
.replace("__ALBUM_ID__", $("#gOrganizeAlbumTree .selected").attr("ref"))
.replace("__COL__", column)
.replace("__DIR__", dir);
- $.get(url, function(data) {
+ $.get(url, {},
+ function(data) {
$("#gMicroThumbGrid").html(data.grid);
$("#gOrganizeSortColumn").attr("value", data.sort_column);
$("#gOrganizeSortOrder").attr("value", data.sort_order);
$.organize.set_handlers();
},
- function() { },
"json");
}
};
diff --git a/modules/organize/views/organize_dialog.html.php b/modules/organize/views/organize_dialog.html.php
index 7ade91a2..b03c066c 100644
--- a/modules/organize/views/organize_dialog.html.php
+++ b/modules/organize/views/organize_dialog.html.php
@@ -37,9 +37,6 @@
<?= form::dropdown(array("id" => "gOrganizeSortOrder"), array("ASC" => "Ascending", "DESC" => "Descending"), $album->sort_order) ?>
</form>
</div>
- <div id="gOrganizeProgress" style="display: none">
- <div class="gProgressBar"></div>
- </div>
</div>
</div>
</div>