From cf6de01f1e51a8dca97347600a844a07b4f80f8b Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Wed, 23 Dec 2009 14:20:23 -0800 Subject: Provide a REST interface to retrieve tags or tagged items. GET /tag?limit=nn,offset=nn Retrieve all tags sorted by count descending GET /tag/uri/uri/uri Retrieve all the tags for the resource specified by path GET /tag/t1/t2/t3 Retrieve all the items that have the specified tags Can specifiy limit and offset to limit response size --- modules/tag/helpers/tag_rest.php | 80 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 modules/tag/helpers/tag_rest.php (limited to 'modules') diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php new file mode 100644 index 00000000..eec90971 --- /dev/null +++ b/modules/tag/helpers/tag_rest.php @@ -0,0 +1,80 @@ +arguments)) { + $tags = ORM::factory("tag") + ->select("name", "count") + ->orderby("count", "DESC"); + if (!empty($request->limit)) { + $tags->limit($request->limit); + } + if (!empty($request->offset)) { + $tags->offset($request->offset); + } + $response = array("tags" => array()); + foreach ($tags->find_all() as $row) { + $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)); + } else { + $response = array("resources" => tag_rest::_get_items($request)); + } + } + + return rest::success($response); + } + + private static function _get_items($request) { + $tags = $request->arguments; + $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)); + 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); + } + $resources = array(); + foreach ($items->find_all() as $item) { + $resources[] = array("type" => $item->type, + "has_children" => $item->children_count() > 0, + "path" => $item->relative_url(), + "thumb_url" => $item->thumb_url(true), + "thumb_dimensions" => array("width" => $item->thumb_width, + "height" => $item->thumb_height), + "has_thumb" => $item->has_thumb(), + "title" => $item->title); + } + + return $resources; + } +} -- cgit v1.2.3