From 93eab24c4a858ac0121540e7d06acf37c79c8de9 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 1 Apr 2010 21:00:13 -0700 Subject: Add a new resource for managing child item orders. --- .../gallery/helpers/item_ordered_members_rest.php | 80 ++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 modules/gallery/helpers/item_ordered_members_rest.php (limited to 'modules/gallery/helpers') diff --git a/modules/gallery/helpers/item_ordered_members_rest.php b/modules/gallery/helpers/item_ordered_members_rest.php new file mode 100644 index 00000000..a2bcc034 --- /dev/null +++ b/modules/gallery/helpers/item_ordered_members_rest.php @@ -0,0 +1,80 @@ +url); + $ordered_members = array(); + foreach ($item->children() as $child) { + $ordered_members[] = rest::url("item", $child); + } + + return array( + "url" => $request->url, + "entity" => array("ordered_members" => $ordered_members)); + } + + static function put($request) { + $item = rest::resolve($request->url); + access::required("edit", $item); + + // Verify that we're not adding or removing members this way + if (count($request->params->ordered_members) != $item->children_count()) { + throw new Rest_Exception("Bad Request", 400); + } + + $ordered_members = array(); + foreach ($request->params->ordered_members as $url) { + $member = rest::resolve($url); + if ($member->parent_id != $item->id) { + throw new Rest_Exception("Bad Request", 400); + } + $ordered_members[] = $member; + } + + // Update all the weights. This is a pretty inefficient way to do this if we're just changing + // one or two elements, but it's easy. We could optimize this by looking at the current order + // and figuring out which elements have moved and then only changing those values. + $i = 0; + foreach ($ordered_members as $member) { + $member->weight = $i++; + $member->save(); + } + } + + static function relationships($resource_type, $resource) { + if ($resource_type == "item" && $resource->is_album()) { + return array( + "item_ordered_members" => array( + "url" => rest::url("item_ordered_members", $resource))); + } + } + + static function resolve($id) { + $item = ORM::factory("item", $id); + if (!access::can("view", $item) || !$item->is_album()) { + throw new Kohana_404_Exception(); + } + return $item; + } + + static function url($item) { + return url::abs_site("rest/item_ordered_members/{$item->id}"); + } +} -- cgit v1.2.3 From 4e450b5a9664d2a24b74050f7b410cd581fc41cb Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Fri, 2 Apr 2010 11:53:24 -0700 Subject: Always return an array from item_ordered_members_rest::relationships() --- modules/gallery/helpers/item_ordered_members_rest.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'modules/gallery/helpers') diff --git a/modules/gallery/helpers/item_ordered_members_rest.php b/modules/gallery/helpers/item_ordered_members_rest.php index a2bcc034..5ae331bc 100644 --- a/modules/gallery/helpers/item_ordered_members_rest.php +++ b/modules/gallery/helpers/item_ordered_members_rest.php @@ -64,6 +64,8 @@ class item_ordered_members_rest_Core { "item_ordered_members" => array( "url" => rest::url("item_ordered_members", $resource))); } + + return array(); } static function resolve($id) { -- cgit v1.2.3 From 5679e30ef6c68272cce295bd593bc23c9ec1e1b3 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 4 Apr 2010 11:55:54 -0700 Subject: REST changes: Allow PUT and POST requests to modify members, not just entity. TESTS ARE NOT UPDATED YET. - Fix item_rest::get() to maintain the proper sort order, which requires duplicating some Item_Model code. - Elide "weight" from the REST version of item - Adjust the weight of members according to the order they're returned from the client. You can't add or remove members here, you can only reorder them. - Changed the wire protocol to handle more complex values. Now "entity" and "members" are JSON encoded. The Gallery3 helper does this correctly. - Changed the wire protocol for tag_item -- now it stores the tag and item urls in the entity, not as members. This is more consistent. - Added missing security for renaming and deleting tags. - Got rid of vestigial tag_rest::post(). We add/remove tags via the relationship. --- .../gallery/helpers/item_ordered_members_rest.php | 82 ------------------- modules/gallery/helpers/item_rest.php | 93 +++++++++++++--------- modules/gallery/models/item.php | 2 +- modules/rest/controllers/rest.php | 7 ++ modules/tag/helpers/item_tags_rest.php | 5 +- modules/tag/helpers/tag_item_rest.php | 2 +- modules/tag/helpers/tag_items_rest.php | 4 +- modules/tag/helpers/tag_rest.php | 27 +++---- modules/tag/helpers/tags_rest.php | 6 +- 9 files changed, 85 insertions(+), 143 deletions(-) delete mode 100644 modules/gallery/helpers/item_ordered_members_rest.php (limited to 'modules/gallery/helpers') diff --git a/modules/gallery/helpers/item_ordered_members_rest.php b/modules/gallery/helpers/item_ordered_members_rest.php deleted file mode 100644 index 5ae331bc..00000000 --- a/modules/gallery/helpers/item_ordered_members_rest.php +++ /dev/null @@ -1,82 +0,0 @@ -url); - $ordered_members = array(); - foreach ($item->children() as $child) { - $ordered_members[] = rest::url("item", $child); - } - - return array( - "url" => $request->url, - "entity" => array("ordered_members" => $ordered_members)); - } - - static function put($request) { - $item = rest::resolve($request->url); - access::required("edit", $item); - - // Verify that we're not adding or removing members this way - if (count($request->params->ordered_members) != $item->children_count()) { - throw new Rest_Exception("Bad Request", 400); - } - - $ordered_members = array(); - foreach ($request->params->ordered_members as $url) { - $member = rest::resolve($url); - if ($member->parent_id != $item->id) { - throw new Rest_Exception("Bad Request", 400); - } - $ordered_members[] = $member; - } - - // Update all the weights. This is a pretty inefficient way to do this if we're just changing - // one or two elements, but it's easy. We could optimize this by looking at the current order - // and figuring out which elements have moved and then only changing those values. - $i = 0; - foreach ($ordered_members as $member) { - $member->weight = $i++; - $member->save(); - } - } - - static function relationships($resource_type, $resource) { - if ($resource_type == "item" && $resource->is_album()) { - return array( - "item_ordered_members" => array( - "url" => rest::url("item_ordered_members", $resource))); - } - - return array(); - } - - static function resolve($id) { - $item = ORM::factory("item", $id); - if (!access::can("view", $item) || !$item->is_album()) { - throw new Kohana_404_Exception(); - } - return $item; - } - - static function url($item) { - return url::abs_site("rest/item_ordered_members/{$item->id}"); - } -} diff --git a/modules/gallery/helpers/item_rest.php b/modules/gallery/helpers/item_rest.php index 16abec5a..f52713b8 100644 --- a/modules/gallery/helpers/item_rest.php +++ b/modules/gallery/helpers/item_rest.php @@ -70,6 +70,14 @@ class item_rest_Core { $orm->where("type", "IN", explode(",", $p->type)); } + // 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); + $members = array(); foreach ($orm->find_all() as $child) { $members[] = rest::url("item", $child); @@ -86,33 +94,44 @@ class item_rest_Core { $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 +142,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/models/item.php b/modules/gallery/models/item.php index 6ede5109..4a87a2ab 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -941,7 +941,7 @@ class Item_Model extends ORM_MPTT { // Elide some internal-only data that is going to cause confusion in the client. foreach (array("relative_path_cache", "relative_url_cache", "left_ptr", "right_ptr", - "thumb_dirty", "resize_dirty") as $key) { + "thumb_dirty", "resize_dirty", "weight") as $key) { unset($data[$key]); } return $data; diff --git a/modules/rest/controllers/rest.php b/modules/rest/controllers/rest.php index 29334cea..dab54976 100644 --- a/modules/rest/controllers/rest.php +++ b/modules/rest/controllers/rest.php @@ -54,6 +54,13 @@ class Rest_Controller extends Controller { break; } + if (isset($request->params->entity)) { + $request->params->entity = json_decode($request->params->entity); + } + if (isset($request->params->members)) { + $request->params->members = json_decode($request->params->members); + } + $request->method = strtolower($input->server("HTTP_X_GALLERY_REQUEST_METHOD", $method)); $request->access_key = $input->server("HTTP_X_GALLERY_REQUEST_KEY"); $request->url = url::abs_current(true); diff --git a/modules/tag/helpers/item_tags_rest.php b/modules/tag/helpers/item_tags_rest.php index 8a1b1e8b..02c79e5d 100644 --- a/modules/tag/helpers/item_tags_rest.php +++ b/modules/tag/helpers/item_tags_rest.php @@ -31,8 +31,8 @@ class item_tags_rest_Core { } static function post($request) { - $tag = rest::resolve($request->params->tag); - $item = rest::resolve($request->params->item); + $tag = rest::resolve($request->params->entity->tag); + $item = rest::resolve($request->params->entity->item); access::required("view", $item); tag::add($item, $tag->name); @@ -45,6 +45,7 @@ class item_tags_rest_Core { static function delete($request) { list ($tag, $item) = rest::resolve($request->url); + access::required("edit", $item); $tag->remove($item); $tag->save(); } diff --git a/modules/tag/helpers/tag_item_rest.php b/modules/tag/helpers/tag_item_rest.php index bce00a9f..17cb726e 100644 --- a/modules/tag/helpers/tag_item_rest.php +++ b/modules/tag/helpers/tag_item_rest.php @@ -22,7 +22,7 @@ class tag_item_rest_Core { list ($tag, $item) = rest::resolve($request->url); return array( "url" => $request->url, - "members" => array( + "entity" => array( "tag" => rest::url("tag", $tag), "item" => rest::url("item", $item))); } diff --git a/modules/tag/helpers/tag_items_rest.php b/modules/tag/helpers/tag_items_rest.php index 003c7c95..848c2cd3 100644 --- a/modules/tag/helpers/tag_items_rest.php +++ b/modules/tag/helpers/tag_items_rest.php @@ -33,8 +33,8 @@ class tag_items_rest_Core { } static function post($request) { - $tag = rest::resolve($request->params->tag); - $item = rest::resolve($request->params->item); + $tag = rest::resolve($request->params->entity->tag); + $item = rest::resolve($request->params->entity->item); access::required("view", $item); if (!$tag->loaded()) { diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php index f30706bd..e0b7bd87 100644 --- a/modules/tag/helpers/tag_rest.php +++ b/modules/tag/helpers/tag_rest.php @@ -36,28 +36,25 @@ class tag_rest_Core { "members" => $tag_items))); } - static function post($request) { - if (empty($request->params->url)) { - throw new Rest_Exception("Bad request", 400); - } - - $tag = rest::resolve($request->url); - $item = rest::resolve($request->params->url); - access::required("edit", $item); - - tag::add($item, $tag->name); - return array("url" => rest::url("tag_item", $tag, $item)); - } - static function put($request) { + // Who can we allow to edit a tag name? If we allow anybody to do it then any logged in + // user can rename all your tags to something offensive. Right now limit renaming to admins. + if (!identity::active_user()->admin) { + access::forbidden(); + } $tag = rest::resolve($request->url); - if (isset($request->params->name)) { - $tag->name = $request->params->name; + if (isset($request->params->entity->name)) { + $tag->name = $request->params->entity->name; $tag->save(); } } static function delete($request) { + // Restrict deleting tags to admins. Otherwise, a logged in user can do great harm to an + // install. + if (!identity::active_user()->admin) { + access::forbidden(); + } $tag = rest::resolve($request->url); $tag->delete(); } diff --git a/modules/tag/helpers/tags_rest.php b/modules/tag/helpers/tags_rest.php index 82826d8e..434e774a 100644 --- a/modules/tag/helpers/tags_rest.php +++ b/modules/tag/helpers/tags_rest.php @@ -40,13 +40,13 @@ class tags_rest_Core { } } - if (empty($request->params->name)) { + if (empty($request->params->entity->name)) { throw new Rest_Exception("Bad Request", 400); } - $tag = ORM::factory("tag")->where("name", "=", $request->params->name)->find(); + $tag = ORM::factory("tag")->where("name", "=", $request->params->entity->name)->find(); if (!$tag->loaded()) { - $tag->name = $request->params->name; + $tag->name = $request->params->entity->name; $tag->count = 0; $tag->save(); } -- cgit v1.2.3 From 2657d085863782f57279d507c1c2c0a15ccccc24 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 4 Apr 2010 14:46:44 -0700 Subject: Move the "Fix MPTT" task from the rescue module into the main Gallery module. --- modules/gallery/helpers/gallery_task.php | 86 ++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) (limited to 'modules/gallery/helpers') 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 -- cgit v1.2.3 From 886337e1dd1380b3382eebe38ee789a14123c69f Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 10 Apr 2010 20:45:09 -0700 Subject: Add error messages to theme::get_edit_form_admin() --- modules/gallery/helpers/theme.php | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'modules/gallery/helpers') 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")); -- cgit v1.2.3 From 8d8aefc5b83b4b814e3f6bd14866b817c1dbb7d0 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 10 Apr 2010 20:58:05 -0700 Subject: Add album_add_form event to album::get_add_form(). --- modules/gallery/helpers/album.php | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'modules/gallery/helpers') 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; } -- cgit v1.2.3 From a2e0472f83b7f3151c4b4ca51472d18258110561 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Thu, 15 Apr 2010 06:18:47 -0700 Subject: Only return the members element to the rest client if the item is an album. This makes it consistent to the rest client that collections will have a members element. --- modules/gallery/helpers/item_rest.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'modules/gallery/helpers') diff --git a/modules/gallery/helpers/item_rest.php b/modules/gallery/helpers/item_rest.php index f52713b8..36d2ca62 100644 --- a/modules/gallery/helpers/item_rest.php +++ b/modules/gallery/helpers/item_rest.php @@ -78,16 +78,18 @@ class item_rest_Core { } $orm->order_by($order_by); - $members = array(); - foreach ($orm->find_all() as $child) { - $members[] = rest::url("item", $child); - } - - 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) { -- cgit v1.2.3 From 2257776f9b4c231033656db1eb23246035aa42fd Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 17 Apr 2010 15:36:46 -0700 Subject: Don't show the member element for non-collection elements in the response. Having a members sidebar element indicates to the rest clients that this is a resource collection. Conflicts: modules/gallery/helpers/items_rest.php --- modules/gallery/helpers/items_rest.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'modules/gallery/helpers') 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; } } } -- cgit v1.2.3 From 9affa8ebbd539396d71f19003b91af577a8a183e Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 10 May 2010 22:11:59 -0700 Subject: Pick a new album cover when the photo that's the current cover is deleted. Fixes ticket #1083. --- modules/gallery/helpers/gallery_event.php | 11 +++++++++++ modules/gallery/tests/Item_Helper_Test.php | 13 ++++++++++++- modules/gallery/tests/Item_Model_Test.php | 8 ++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) (limited to 'modules/gallery/helpers') 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/tests/Item_Helper_Test.php b/modules/gallery/tests/Item_Helper_Test.php index 295871a5..4771b11a 100644 --- a/modules/gallery/tests/Item_Helper_Test.php +++ b/modules/gallery/tests/Item_Helper_Test.php @@ -54,7 +54,6 @@ class Item_Helper_Test extends Gallery_Unit_Test_Case { $this->assert_same($dst_album->id, $photo->parent_id); } - public function move_updates_album_covers_test() { // 2 photos in the source album $src_album = test::random_album(); @@ -106,4 +105,16 @@ class Item_Helper_Test extends Gallery_Unit_Test_Case { $this->assert_not_same("{$rand}.jpg", $photo2->name); $this->assert_not_same($rand, $photo2->slug); } + + public function delete_cover_photo_picks_new_album_cover() { + $album = test::random_album(); + $photo1 = test::random_photo($album); + // At this point, $photo1 is the album cover. We verify this in + // Item_Model_Test::first_photo_becomes_album_cover + $photo2 = test::random_photo($album); + $photo1->delete(); + $album->reload(); + + $this->assert_same($photo2->id, $album->album_cover_item_id); + } } diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index 28d6fba7..15aa2d8c 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -363,4 +363,12 @@ class Item_Model_Test extends Gallery_Unit_Test_Case { $this->assert_true(!array_key_exists("parent_id", $result)); $this->assert_true(!array_key_exists("album_cover_item_id", $result)); } + + public function first_photo_becomes_album_cover() { + $album = test::random_album(); + $photo = test::random_photo($album); + $album->reload(); + + $this->assert_same($photo->id, $album->album_cover_item_id); + } } -- cgit v1.2.3