diff options
Diffstat (limited to 'modules/tag/helpers')
| -rw-r--r-- | modules/tag/helpers/tag_rest.php | 62 |
1 files changed, 33 insertions, 29 deletions
diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php index d62c0231..cca9a88b 100644 --- a/modules/tag/helpers/tag_rest.php +++ b/modules/tag/helpers/tag_rest.php @@ -18,40 +18,44 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class tag_rest_Core { + // If no arguments just return all the tags. If 2 or more then it is a path then + // return the tags for that item. But if its only 1, then is it a path or a tag? + // Assume a tag first, if nothing is found then try finding the item. static function get($request) { - if (empty($request->arguments)) { + $resources = array(); + switch (count($request->arguments)) { + case 0: $tags = ORM::factory("tag") ->select("name", "count") - ->orderby("count", "DESC"); + ->order_by("count", "DESC"); if (!empty($request->limit)) { $tags->limit($request->limit); } if (!empty($request->offset)) { $tags->offset($request->offset); } - $response = array("tags" => array()); + $resources = array("tags" => array()); foreach ($tags->find_all() as $row) { - $response["tags"][] = array("name" => $row->name, "count" => $row->count); + $resources["tags"][] = array("name" => $row->name, "count" => $row->count); } - } else { - $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)); + break; + case 1: + $resources = tag_rest::_get_items($request); + if (!empty($resources)) { + $resources = array("resources" =>$resources); + break; + } + default: + $item = ORM::factory("item") + ->where("relative_url_cache", "=", implode("/", $request->arguments)) + ->viewable() + ->find(); + if ($item->loaded()) { + $resources = array("tags" => tag::item_tags($item)); } } - return rest::success($response); + return rest::success($resources); } static function post($request) { @@ -62,10 +66,10 @@ class tag_rest_Core { $tags = explode(",", $request->arguments[0]); $item = ORM::factory("item") - ->where("relative_url_cache", $path) + ->where("relative_url_cache", "=", $path) ->viewable() ->find(); - if (!$item->loaded) { + if (!$item->loaded()) { return rest::not_found("Resource: {$path} missing."); } @@ -87,9 +91,9 @@ class tag_rest_Core { $name = $request->arguments[0]; $tag = ORM::factory("tag") - ->where("name", $name) + ->where("name", "=", $name) ->find(); - if (!$tag->loaded) { + if (!$tag->loaded()) { return rest::not_found("Tag: {$name} not found."); } @@ -108,13 +112,13 @@ class tag_rest_Core { $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) + ->where("tags.name", "IN", $tags) + ->where("relative_url_cache", "=", $request->path) ->viewable() ->find_all(); } else { $tag_list = ORM::factory("tag") - ->in("name", $tags) + ->where("name", "IN", $tags) ->find_all(); } @@ -129,10 +133,10 @@ class tag_rest_Core { private static function _get_items($request) { $tags = explode(",", $request->arguments[0]); $items = ORM::factory("item") - ->select("distinct *") + ->select_distinct("*") ->join("items_tags", "items.id", "items_tags.item_id") ->join("tags", "tags.id", "items_tags.tag_id") - ->in("tags.name", $tags); + ->where("tags.name", "IN", $tags); if (!empty($request->limit)) { $items->limit($request->limit); } |
