summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Almdal <tnalmdal@shaw.ca>2009-12-23 14:20:23 -0800
committerTim Almdal <tnalmdal@shaw.ca>2009-12-23 14:20:23 -0800
commitcf6de01f1e51a8dca97347600a844a07b4f80f8b (patch)
treeb878c3181725906ec7a096b9c25ff195730244d0
parentd0dd6650bea15a30a2da66f996980a269a5f9c44 (diff)
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
-rw-r--r--modules/tag/helpers/tag_rest.php80
1 files changed, 80 insertions, 0 deletions
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 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class tag_rest_Core {
+ static function get($request) {
+ if (empty($request->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;
+ }
+}