diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2009-12-24 19:25:14 -0800 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2009-12-24 19:25:14 -0800 |
commit | 586f2d78b85cbe964a625b3c83896c1c8a991702 (patch) | |
tree | a2f6a03d6c5560f733669e0c8024e8823bf4e33f | |
parent | 4268889fbf82705867211a6949f6b3411dde0de3 (diff) |
Add the functionality to add, delete and rename tags via the Gallery3 remote interface.
-rw-r--r-- | modules/tag/helpers/tag_rest.php | 104 | ||||
-rw-r--r-- | modules/tag/tests/Tag_Rest_Helper_Test.php | 227 |
2 files changed, 317 insertions, 14 deletions
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) { diff --git a/modules/tag/tests/Tag_Rest_Helper_Test.php b/modules/tag/tests/Tag_Rest_Helper_Test.php new file mode 100644 index 00000000..1c550366 --- /dev/null +++ b/modules/tag/tests/Tag_Rest_Helper_Test.php @@ -0,0 +1,227 @@ +<?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_Helper_Test extends Unit_Test_Case { + public function setup() { + $this->_save = array($_GET, $_POST, $_SERVER, $_FILES); + $this->_saved_active_user = identity::active_user(); + + $this->_user = identity::create_user("access_test", "Access Test", "password"); + $key = ORM::factory("user_access_token"); + $this->_access_key = $key->access_key = md5($this->_user->name . rand()); + $key->user_id = $this->_user->id; + $key->save(); + + $root = ORM::factory("item", 1); + $this->_album = album::create($root, "album", "Test Album", rand()); + tag::add($this->_album, "albums"); + tag::add($this->_album, "A1"); + tag::add($this->_album, "T1"); + $this->_child = album::create($this->_album, "child", "Test Child Album", rand()); + tag::add($this->_child, "albums"); + tag::add($this->_child, "C1"); + tag::add($this->_child, "T1"); + + $filename = MODPATH . "gallery/tests/test.jpg"; + $rand = rand(); + $this->_photo = photo::create($this->_child, $filename, "$rand.jpg", $rand); + tag::add($this->_photo, "photos"); + tag::add($this->_photo, "P1"); + tag::add($this->_photo, "T1"); + + $filename = MODPATH . "gallery/tests/test.jpg"; + $rand = rand(); + $this->_sibling = photo::create($this->_album, $filename, "$rand.jpg", $rand); + tag::add($this->_sibling, "photos"); + tag::add($this->_sibling, "P3"); + } + + public function teardown() { + list($_GET, $_POST, $_SERVER, $_FILES) = $this->_save; + identity::set_active_user($this->_saved_active_user); + + try { + if (!empty($this->_user)) { + $this->_user->delete(); + } + if (!empty($this->_album)) { + $this->_album->delete(); + } + Database::instance()->query("TRUNCATE {tags}"); + Database::instance()->query("TRUNCATE {items_tags}"); + + } catch (Exception $e) { } + } + + public function tag_rest_get_all_test() { + $request = (object)array("arguments" => array(), "limit" => 2, "offset" => 1); + + $this->assert_equal( + json_encode(array("status" => "OK", + "tags" => array(array("name" => "albums", "count" => 2), + array("name" => "photos", "count" => 2)))), + tag_rest::get($request)); + } + + public function tag_rest_get_tags_for_item_test() { + $request = (object)array("arguments" => explode("/", $this->_photo->relative_url())); + + $this->assert_equal( + json_encode(array("status" => "OK", + "tags" => array("photos", "P1", "T1"))), + tag_rest::get($request)); + } + + public function tag_rest_get_items_test() { + $request = (object)array("arguments" => array("albums")); + + $resources = array(); + foreach (array($this->_album, $this->_child) as $resource) { + $resources[] = array("type" => $resource->type, + "has_children" => $resource->children_count() > 0, + "path" => $resource->relative_url(), + "thumb_url" => $resource->thumb_url(), + "thumb_dimensions" => array( + "width" => $resource->thumb_width, + "height" => $resource->thumb_height), + "has_thumb" => $resource->has_thumb(), + "title" => $resource->title); + + } + $this->assert_equal(json_encode(array("status" => "OK", "resources" => $resources)), + tag_rest::get($request)); + } + + public function tag_rest_add_tags_for_item_no_path_test() { + $request = (object)array("arguments" => array("new,one")); + + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Invalid request")), + tag_rest::post($request)); + } + + public function tag_rest_add_tags_for_item_not_found_test() { + $request = (object)array("path" => $this->_photo->relative_url() . "b", + "arguments" => array("new,one")); + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Resource not found")), + tag_rest::post($request)); + } + + public function tag_rest_add_tags_for_item_no_access_test() { + identity::set_active_user($this->_user); + $request = (object)array("path" => $this->_photo->relative_url(), + "arguments" => array("new,one")); + + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Resource not found")), + tag_rest::post($request)); + } + + public function tag_rest_add_tags_for_item_test() { + access::allow(identity::registered_users(), "edit", $this->_child); + identity::set_active_user($this->_user); + $request = (object)array("path" => $this->_photo->relative_url(), + "arguments" => array("new,one")); + + $this->assert_equal( + json_encode(array("status" => "OK")), + tag_rest::post($request)); + $request = (object)array("arguments" => explode("/", $this->_photo->relative_url())); + $this->assert_equal( + json_encode(array("status" => "OK", + "tags" => array("photos", "P1", "T1", "new", "one"))), + tag_rest::get($request)); + } + + public function tag_rest_update_tag_no_arguments_test() { + $request = (object)array("arguments" => array()); + + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Invalid request")), + tag_rest::put($request)); + } + + public function tag_rest_update_tag_one_arguments_test() { + $request = (object)array("arguments" => array("photos")); + + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Invalid request")), + tag_rest::put($request)); + + $request = (object)array("arguments" => array(), "new_name" => "valid"); + + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Invalid request")), + tag_rest::put($request)); + } + + public function tag_rest_update_tags_not_found_test() { + $request = (object)array("arguments" => array("not"), "new_name" => "found"); + + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Resource not found")), + tag_rest::put($request)); + } + + public function tag_rest_update_tags_test() { + $request = (object)array("arguments" => array("albums"), "new_name" => "new name"); + + $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::put($request)); + + $request = (object)array("arguments" => array("new name")); + $resources = array(); + foreach (array($this->_album, $this->_child) as $resource) { + $resources[] = array("type" => $resource->type, + "has_children" => $resource->children_count() > 0, + "path" => $resource->relative_url(), + "thumb_url" => $resource->thumb_url(), + "thumb_dimensions" => array( + "width" => $resource->thumb_width, + "height" => $resource->thumb_height), + "has_thumb" => $resource->has_thumb(), + "title" => $resource->title); + + } + $this->assert_equal( + json_encode(array("status" => "OK", "resources" => $resources)), + tag_rest::get($request)); + } + + public function tag_rest_delete_tag_test() { + $request = (object)array("arguments" => array("T1,P1")); + + $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::delete($request)); + + $request = (object)array("arguments" => array("T1,P1")); + $this->assert_equal(json_encode(array("status" => "OK", "resources" => array())), + tag_rest::get($request)); + } + + public function tag_rest_delete_tag_from_item_test() { + $request = (object)array("arguments" => array("T1,P1"), + $this->_photo->relative_url()); + + $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::delete($request)); + + $request = (object)array("arguments" => explode("/", $this->_photo->relative_url())); + $this->assert_equal(json_encode(array("status" => "OK", "tags" => array("photos"))), + tag_rest::get($request)); + } +} |