summaryrefslogtreecommitdiff
path: root/modules/tag
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2010-01-04 21:48:21 -0800
committerBharat Mediratta <bharat@menalto.com>2010-01-04 21:48:21 -0800
commit3fffa18e650189e7f846592c9d4c3e7bbfe71c62 (patch)
tree4800f553d249fd2908a457aa6bd1dcd394223fec /modules/tag
parent0e3327bca70623175791ee41085d55d0cb13fe5b (diff)
Further progress on refining the REST server side code.
1) Deal in fully qualified URL resources through the rest interface. All rest methods are now passed the complete url in request->url. 2) Create rest::resolve() which lets individual resource definition code convert a full url into the appropriate matching resource. Implement gallery_rest::resolve() and tag_rest::resolve() 3) Reimplement tag_rest's get() and post() methods. They're much simpler now. 4) Implement the tags_rest helper which supports working with the entire tags collection.
Diffstat (limited to 'modules/tag')
-rw-r--r--modules/tag/helpers/tag.php2
-rw-r--r--modules/tag/helpers/tag_rest.php69
-rw-r--r--modules/tag/helpers/tags_rest.php48
3 files changed, 66 insertions, 53 deletions
diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php
index 8075afe4..d895e08f 100644
--- a/modules/tag/helpers/tag.php
+++ b/modules/tag/helpers/tag.php
@@ -41,7 +41,7 @@ class tag_Core {
}
if (!$tag->has($item)) {
- if (!$tag->add($item, $tag)) {
+ if (!$tag->add($item)) {
throw new Exception("@todo {$tag->name} WAS_NOT_ADDED_TO {$item->id}");
}
$tag->count++;
diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php
index 0c06587b..4b5103ef 100644
--- a/modules/tag/helpers/tag_rest.php
+++ b/modules/tag/helpers/tag_rest.php
@@ -18,71 +18,36 @@
* 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) {
- $resources = array();
- switch (count($request->arguments)) {
- case 0:
- $tags = ORM::factory("tag")
- ->select("name", "count")
- ->order_by("count", "DESC");
- if (!empty($request->limit)) {
- $tags->limit($request->limit);
- }
- if (!empty($request->offset)) {
- $tags->offset($request->offset);
- }
- $resources = array("tags" => array());
- foreach ($tags->find_all() as $row) {
- $resources["tags"][] = array("name" => $row->name, "count" => $row->count);
- }
- 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::reply($resources);
+ return rest::reply(rest::resolve($request->url)->as_array());
}
static function post($request) {
- if (empty($request->arguments) || count($request->arguments) != 1 || empty($request->path)) {
+ $tag = rest::resolve($request->url);
+
+ if (empty($request->params->url)) {
throw new Rest_Exception("Bad request", 400);
}
- $path = $request->path;
- $tags = explode(",", $request->arguments[0]);
- $item = ORM::factory("item")
- ->where("relative_url_cache", "=", $path)
- ->viewable()
- ->find();
- if (!$item->loaded()) {
- throw new Kohana_404_Exception();
- }
+ $item = rest::resolve($request->params->url);
- if (!access::can("edit", $item)) {
+ access::required("edit", $item);
+ tag::add($item, $tag->name);
+
+ return rest::reply();
+ }
+
+ static function resolve($tag_name) {
+ $tag = ORM::factory("tag")->where("name", "=", $tag_name)->find();
+ if (!$tag->loaded()) {
throw new Kohana_404_Exception();
}
- foreach ($tags as $tag) {
- tag::add($item, $tag);
- }
- return rest::reply();
+ return $tag;
}
+ // ------------------------------------------------------------
+
static function put($request) {
if (empty($request->arguments[0]) || empty($request->new_name)) {
throw new Rest_Exception("Bad request", 400);
diff --git a/modules/tag/helpers/tags_rest.php b/modules/tag/helpers/tags_rest.php
new file mode 100644
index 00000000..d2bd28b0
--- /dev/null
+++ b/modules/tag/helpers/tags_rest.php
@@ -0,0 +1,48 @@
+<?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 tags_rest_Core {
+ static function get($request) {
+ $data = array();
+ foreach (ORM::factory("tag")->find_all() as $tag) {
+ $data[$tag->name] = url::abs_site("rest/tags/" . rawurlencode($tag->name));
+ }
+ return rest::reply($data);
+ }
+
+ static function post($request) {
+ // @todo: what permission should be required to create a tag here?
+ // for now, require edit at the top level. Perhaps later, just require any edit perms,
+ // anywhere in the gallery?
+ access::required("edit", item::root());
+
+ if (empty($request->params->name)) {
+ throw new Rest_Exception("Bad Request", 400);
+ }
+
+ $tag = ORM::factory("tag")->where("name", "=", $request->params->name)->find();
+ if (!$tag->loaded()) {
+ $tag->name = $request->params->name;
+ $tag->count = 0;
+ $tag->save();
+ }
+
+ return rest::reply(array("url" => url::abs_site("rest/tag/" . rawurlencode($tag->name))));
+ }
+}