From 586f2d78b85cbe964a625b3c83896c1c8a991702 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Thu, 24 Dec 2009 19:25:14 -0800 Subject: Add the functionality to add, delete and rename tags via the Gallery3 remote interface. --- modules/tag/helpers/tag_rest.php | 104 +++++++++++++++++++++++++++++++++------ 1 file changed, 90 insertions(+), 14 deletions(-) (limited to 'modules/tag/helpers') diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php index eec90971..d62c0231 100644 --- a/modules/tag/helpers/tag_rest.php +++ b/modules/tag/helpers/tag_rest.php @@ -34,12 +34,18 @@ class tag_rest_Core { $response["tags"][] = array("name" => $row->name, "count" => $row->count); } } else { - $item = ORM::factory("item") - ->where("relative_url_cache", implode("/", $request->arguments)) - ->viewable() - ->find(); - if ($item->loaded) { - $response = array("tags" => tag::item_tags($item)); + $path = implode("/", $request->arguments); + if (strpos($path, ",") === false) { + $item = ORM::factory("item") + ->where("relative_url_cache", $path) + ->viewable() + ->find(); + // If we didn't find it and there was only one argument, retry as a tag not a path + if ($item->loaded || count($request->arguments) != 1) { + $response = array("tags" => $item->loaded ? tag::item_tags($item) : array()); + } else { + $response = array("resources" => tag_rest::_get_items($request)); + } } else { $response = array("resources" => tag_rest::_get_items($request)); } @@ -48,20 +54,90 @@ class tag_rest_Core { return rest::success($response); } + static function post($request) { + if (empty($request->arguments) || count($request->arguments) != 1 || empty($request->path)) { + return rest::invalid_request(); + } + $path = $request->path; + $tags = explode(",", $request->arguments[0]); + + $item = ORM::factory("item") + ->where("relative_url_cache", $path) + ->viewable() + ->find(); + if (!$item->loaded) { + return rest::not_found("Resource: {$path} missing."); + } + + if (!access::can("edit", $item)) { + return rest::not_found("Resource: {$path} permission denied."); + } + + foreach ($tags as $tag) { + tag::add($item, $tag); + } + return rest::success(); + } + + static function put($request) { + if (empty($request->arguments[0]) || empty($request->new_name)) { + return rest::invalid_request(); + } + + $name = $request->arguments[0]; + + $tag = ORM::factory("tag") + ->where("name", $name) + ->find(); + if (!$tag->loaded) { + return rest::not_found("Tag: {$name} not found."); + } + + $tag->name = $request->new_name; + $tag->save(); + + return rest::success(); + } + + static function delete($request) { + if (empty($request->arguments[0])) { + return rest::invalid_request(); + } + $tags = explode(",", $request->arguments[0]); + if (!empty($request->path)) { + $tag_list = ORM::factory("tag") + ->join("items_tags", "tags.id", "items_tags.tag_id") + ->join("items", "items.id", "items_tags.item_id") + ->in("tags.name", $tags) + ->where("relative_url_cache", $request->path) + ->viewable() + ->find_all(); + } else { + $tag_list = ORM::factory("tag") + ->in("name", $tags) + ->find_all(); + } + + foreach ($tag_list as $row) { + $row->delete(); + }; + + tag::compact(); + return rest::success(); + } + private static function _get_items($request) { - $tags = $request->arguments; + $tags = explode(",", $request->arguments[0]); $items = ORM::factory("item") - ->join("items_tags", "items.id", "items_tags.item_id", "left") - ->join("tags", "tags.id", "items_tags.tag_id", "left") - ->where("tags.name", array_shift($tags)); + ->select("distinct *") + ->join("items_tags", "items.id", "items_tags.item_id") + ->join("tags", "tags.id", "items_tags.tag_id") + ->in("tags.name", $tags); if (!empty($request->limit)) { $items->limit($request->limit); } if (!empty($request->offset)) { - $tags->offset($request->offset); - } - foreach ($tags as $tag) { - $items->orWhere("tags.name", $tag); + $items->offset($request->offset); } $resources = array(); foreach ($items->find_all() as $item) { -- cgit v1.2.3