diff options
Diffstat (limited to 'modules/gallery/helpers')
-rw-r--r-- | modules/gallery/helpers/album.php | 4 | ||||
-rw-r--r-- | modules/gallery/helpers/gallery_event.php | 11 | ||||
-rw-r--r-- | modules/gallery/helpers/gallery_task.php | 86 | ||||
-rw-r--r-- | modules/gallery/helpers/item_rest.php | 105 | ||||
-rw-r--r-- | modules/gallery/helpers/items_rest.php | 13 | ||||
-rw-r--r-- | modules/gallery/helpers/theme.php | 6 |
6 files changed, 177 insertions, 48 deletions
diff --git a/modules/gallery/helpers/album.php b/modules/gallery/helpers/album.php index 521806a0..0baae631 100644 --- a/modules/gallery/helpers/album.php +++ b/modules/gallery/helpers/album.php @@ -45,9 +45,13 @@ class album_Core { ->error_messages("required", t("You must provide an internet address")) ->error_messages("length", t("Your internet address is too long")); $group->hidden("type")->value("album"); + + module::event("album_add_form", $parent, $form); + $group->submit("")->value(t("Create")); $form->script("") ->url(url::abs_file("modules/gallery/js/albums_form_add.js")); + return $form; } diff --git a/modules/gallery/helpers/gallery_event.php b/modules/gallery/helpers/gallery_event.php index d723cc1b..2416f2e5 100644 --- a/modules/gallery/helpers/gallery_event.php +++ b/modules/gallery/helpers/gallery_event.php @@ -97,6 +97,17 @@ class gallery_event_Core { static function item_deleted($item) { access::delete_item($item); + + $parent = $item->parent(); + if (!$parent->album_cover_item_id) { + // Assume we deleted the album cover and pick a new one. Choosing the first photo in the + // album is logical, but it's not the most efficient in the case where we're deleting all + // the photos in the album one at a time since we'll probably delete them in order which + // means that we'll be resetting the album cover each time. + if ($child = $parent->children(1)->current()) { + item::make_album_cover($child); + } + } } static function item_moved($item, $old_parent) { diff --git a/modules/gallery/helpers/gallery_task.php b/modules/gallery/helpers/gallery_task.php index fa95c612..bc128b3e 100644 --- a/modules/gallery/helpers/gallery_task.php +++ b/modules/gallery/helpers/gallery_task.php @@ -18,6 +18,9 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class gallery_task_Core { + const MPTT_LEFT = 0; + const MPTT_RIGHT = 1; + static function available_tasks() { $dirty_count = graphics::find_dirty_images_query()->count_records(); $tasks = array(); @@ -42,6 +45,14 @@ class gallery_task_Core { ->name(t("Remove old files")) ->description(t("Remove expired files from the logs and tmp directory")) ->severity(log::SUCCESS); + + $tasks[] = Task_Definition::factory() + ->callback("gallery_task::fix_mptt") + ->name(t("Fix Album/Photo hierarchy")) + ->description(t("Fix problems where your album/photo breadcrumbs are out of " . + "sync with your actual hierarchy.")) + ->severity(log::SUCCESS); + return $tasks; } @@ -298,4 +309,79 @@ class gallery_task_Core { $task->log($errors); } } + + static function fix_mptt($task) { + $start = microtime(true); + + $total = $task->get("total"); + if (empty($total)) { + $task->set("total", $total = db::build()->count_records("items")); + $task->set("stack", "1:" . self::MPTT_LEFT); + $task->set("ptr", 1); + $task->set("completed", 0); + } + + $ptr = $task->get("ptr"); + $stack = explode(" ", $task->get("stack")); + $completed = $task->get("completed"); + + // Implement a depth-first tree walk using a stack. Not the most efficient, but it's simple. + while ($stack && microtime(true) - $start < 1.5) { + list($id, $state) = explode(":", array_pop($stack)); + switch ($state) { + case self::MPTT_LEFT: + self::fix_mptt_set_left($id, $ptr++); + $item = ORM::factory("item", $id); + array_push($stack, $id . ":" . self::MPTT_RIGHT); + foreach (self::fix_mptt_children($id) as $child) { + array_push($stack, $child->id . ":" . self::MPTT_LEFT); + } + break; + + case self::MPTT_RIGHT: + self::fix_mptt_set_right($id, $ptr++); + $completed++; + break; + } + } + + $task->set("stack", implode(" ", $stack)); + $task->set("ptr", $ptr); + $task->set("completed", $completed); + + if ($total == $completed) { + $task->done = true; + $task->state = "success"; + $task->percent_complete = 100; + } else { + $task->percent_complete = round(100 * $completed / $total); + } + $task->status = t2("One row updated", "%count / %total rows updated", $completed, + array("total" => $total)); + } + + static function fix_mptt_children($parent_id) { + return db::build() + ->select("id") + ->from("items") + ->where("parent_id", "=", $parent_id) + ->order_by("left_ptr", "ASC") + ->execute(); + } + + static function fix_mptt_set_left($id, $value) { + db::build() + ->update("items") + ->set("left_ptr", $value) + ->where("id", "=", $id) + ->execute(); + } + + static function fix_mptt_set_right($id, $value) { + db::build() + ->update("items") + ->set("right_ptr", $value) + ->where("id", "=", $id) + ->execute(); + } }
\ No newline at end of file diff --git a/modules/gallery/helpers/item_rest.php b/modules/gallery/helpers/item_rest.php index 16abec5a..36d2ca62 100644 --- a/modules/gallery/helpers/item_rest.php +++ b/modules/gallery/helpers/item_rest.php @@ -70,49 +70,70 @@ class item_rest_Core { $orm->where("type", "IN", explode(",", $p->type)); } - $members = array(); - foreach ($orm->find_all() as $child) { - $members[] = rest::url("item", $child); + // Apply the item's sort order, using id as the tie breaker. + // See Item_Model::children() + $order_by = array($item->sort_column => $item->sort_order); + if ($item->sort_column != "id") { + $order_by["id"] = "ASC"; } + $orm->order_by($order_by); - return array( + $result = array( "url" => $request->url, "entity" => $item->as_restful_array(), - "members" => $members, "relationships" => rest::relationships("item", $item)); + if ($item->is_album()) { + $result["members"] = array(); + foreach ($orm->find_all() as $child) { + $result["members"][] = rest::url("item", $child); + } + } + + return $result; } static function put($request) { $item = rest::resolve($request->url); access::required("edit", $item); - $params = $request->params; - - // Only change fields from a whitelist. - foreach (array("album_cover", "captured", "description", - "height", "mime_type", "name", "parent", "rand_key", "resize_dirty", - "resize_height", "resize_width", "slug", "sort_column", "sort_order", - "thumb_dirty", "thumb_height", "thumb_width", "title", "view_count", - "weight", "width") as $key) { - switch ($key) { - case "album_cover": - if (property_exists($request->params, "album_cover")) { - $album_cover_item = rest::resolve($request->params->album_cover); - access::required("view", $album_cover_item); - $item->album_cover_item_id = $album_cover_item->id; + if ($entity = $request->params->entity) { + // Only change fields from a whitelist. + foreach (array("album_cover", "captured", "description", + "height", "mime_type", "name", "parent", "rand_key", "resize_dirty", + "resize_height", "resize_width", "slug", "sort_column", "sort_order", + "thumb_dirty", "thumb_height", "thumb_width", "title", "view_count", + "width") as $key) { + switch ($key) { + case "album_cover": + if (property_exists($entity, "album_cover")) { + $album_cover_item = rest::resolve($entity->album_cover); + access::required("view", $album_cover_item); + $item->album_cover_item_id = $album_cover_item->id; + } + break; + + case "parent": + if (property_exists($entity, "parent")) { + $parent = rest::resolve($entity->parent); + access::required("edit", $parent); + $item->parent_id = $parent->id; + } + break; + default: + if (property_exists($entity, $key)) { + $item->$key = $entity->$key; + } } - break; + } + } - case "parent": - if (property_exists($request->params, "parent")) { - $parent = rest::resolve($request->params->parent); - access::required("edit", $parent); - $item->parent_id = $parent->id; - } - break; - default: - if (property_exists($request->params, $key)) { - $item->$key = $request->params->$key; + $weight = 0; + if (isset($request->params->members)) { + foreach ($request->params->members as $url) { + $child = rest::resolve($url); + if ($child->parent_id == $item->id && $child->weight != $weight) { + $child->weight = $weight++; + $child->save(); } } } @@ -123,33 +144,33 @@ class item_rest_Core { $parent = rest::resolve($request->url); access::required("edit", $parent); - $params = $request->params; + $entity = $request->params->entity; $item = ORM::factory("item"); - switch ($params->type) { + switch ($entity->type) { case "album": $item->type = "album"; $item->parent_id = $parent->id; - $item->name = $params->name; - $item->title = isset($params->title) ? $params->title : $name; - $item->description = isset($params->description) ? $params->description : null; - $item->slug = isset($params->slug) ? $params->slug : null; + $item->name = $entity->name; + $item->title = isset($entity->title) ? $entity->title : $name; + $item->description = isset($entity->description) ? $entity->description : null; + $item->slug = isset($entity->slug) ? $entity->slug : null; $item->save(); break; case "photo": case "movie": - $item->type = $params->type; + $item->type = $entity->type; $item->parent_id = $parent->id; $item->set_data_file($request->file); - $item->name = $params->name; - $item->title = isset($params->title) ? $params->title : $params->name; - $item->description = isset($params->description) ? $params->description : null; - $item->slug = isset($params->slug) ? $params->slug : null; + $item->name = $entity->name; + $item->title = isset($entity->title) ? $entity->title : $entity->name; + $item->description = isset($entity->description) ? $entity->description : null; + $item->slug = isset($entity->slug) ? $entity->slug : null; $item->save(); break; default: - throw new Rest_Exception("Invalid type: $params->type", 400); + throw new Rest_Exception("Invalid type: $entity->type", 400); } return array("url" => rest::url("item", $item)); diff --git a/modules/gallery/helpers/items_rest.php b/modules/gallery/helpers/items_rest.php index 05ca65cf..5d8e80b2 100644 --- a/modules/gallery/helpers/items_rest.php +++ b/modules/gallery/helpers/items_rest.php @@ -22,19 +22,20 @@ class items_rest_Core { $items = array(); if (isset($request->params->url)) { - foreach($request->params->url as $url) { + foreach (json_decode($request->params->url) as $url) { $item = rest::resolve($url); if (access::can("view", $item)) { - $members = array(); + $item_rest = array("url" => $url, + "entity" => $item->as_restful_array(), + "relationship" => rest::relationships("item", $item)); if ($item->type == "album") { + $members = array(); foreach ($item->children() as $child) { $members[] = rest::url("item", $child); } + $item_rest["members"] = $members; } - $items[] = array("url" => $url, - "entity" => $item->as_restful_array(), - "members" => $members, - "relationship" => rest::relationships("item", $item)); + $items[] = $item_rest; } } } diff --git a/modules/gallery/helpers/theme.php b/modules/gallery/helpers/theme.php index ae5f030c..980ee11a 100644 --- a/modules/gallery/helpers/theme.php +++ b/modules/gallery/helpers/theme.php @@ -73,12 +73,18 @@ class theme_Core { $group = $form->group("edit_theme"); $group->input("page_size")->label(t("Items per page"))->id("g-page-size") ->rules("required|valid_digit") + ->error_messages("required", t("You must enter a number")) + ->error_messages("valid_digit", t("You must enter a number")) ->value(module::get_var("gallery", "page_size")); $group->input("thumb_size")->label(t("Thumbnail size (in pixels)"))->id("g-thumb-size") ->rules("required|valid_digit") + ->error_messages("required", t("You must enter a number")) + ->error_messages("valid_digit", t("You must enter a number")) ->value(module::get_var("gallery", "thumb_size")); $group->input("resize_size")->label(t("Resized image size (in pixels)"))->id("g-resize-size") ->rules("required|valid_digit") + ->error_messages("required", t("You must enter a number")) + ->error_messages("valid_digit", t("You must enter a number")) ->value(module::get_var("gallery", "resize_size")); $group->textarea("header_text")->label(t("Header text"))->id("g-header-text") ->value(module::get_var("gallery", "header_text")); |