diff options
author | Nathan Kinkade <nkinkade@nkinka.de> | 2010-01-03 15:51:24 +0000 |
---|---|---|
committer | Nathan Kinkade <nkinkade@nkinka.de> | 2010-01-03 15:51:24 +0000 |
commit | 399abbc3a754cf5fdcfdff113446e1bc264091e2 (patch) | |
tree | 592188568e15325d59e51bf19cfdf667fae8d86d /modules/tag/helpers | |
parent | 925a6a2220760cb7daacee1ab80a07b61b3a30a1 (diff) | |
parent | 64e5efd57ba1479179c202e1b76b6eeb42d2924c (diff) |
Merge branch 'master' of git://github.com/gallery/gallery3
Diffstat (limited to 'modules/tag/helpers')
-rw-r--r-- | modules/tag/helpers/tag.php | 28 | ||||
-rw-r--r-- | modules/tag/helpers/tag_event.php | 2 | ||||
-rw-r--r-- | modules/tag/helpers/tag_rest.php | 160 | ||||
-rw-r--r-- | modules/tag/helpers/tag_rss.php | 4 |
4 files changed, 180 insertions, 14 deletions
diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php index 89a27034..8075afe4 100644 --- a/modules/tag/helpers/tag.php +++ b/modules/tag/helpers/tag.php @@ -33,8 +33,8 @@ class tag_Core { throw new exception("@todo MISSING_TAG_NAME"); } - $tag = ORM::factory("tag")->where("name", $tag_name)->find(); - if (!$tag->loaded) { + $tag = ORM::factory("tag")->where("name", "=", $tag_name)->find(); + if (!$tag->loaded()) { $tag->name = $tag_name; $tag->count = 0; $tag->save(); @@ -57,7 +57,7 @@ class tag_Core { */ static function popular_tags($count) { return ORM::factory("tag") - ->orderby("count", "DESC") + ->order_by("count", "DESC") ->limit($count) ->find_all(); } @@ -89,12 +89,12 @@ class tag_Core { */ static function item_tags($item) { $tags = array(); - foreach (Database::instance() + foreach (db::build() ->select("name") ->from("tags") ->join("items_tags", "tags.id", "items_tags.tag_id", "left") - ->where("items_tags.item_id", $item->id) - ->get() as $row) { + ->where("items_tags.item_id", "=", $item->id) + ->execute() as $row) { $tags[] = $row->name; } return $tags; @@ -125,10 +125,16 @@ class tag_Core { * Delete all tags associated with an item */ static function clear_all($item) { - $db = Database::instance(); - $db->query("UPDATE {tags} SET `count` = `count` - 1 WHERE `count` > 0 " . - "AND `id` IN (SELECT `tag_id` from {items_tags} WHERE `item_id` = $item->id)"); - $db->delete("items_tags", array("item_id" => "$item->id")); + db::build() + ->update("tags") + ->set("count", new Database_Expression("`count` - 1")) + ->where("count", ">", 0) + ->where("id", "IN", db::build()->select("tag_id")->from("items_tags")->where("item_id", "=", $item->id)) + ->execute(); + db::build() + ->delete("items_tags") + ->where("item_id", "=", $item->id) + ->execute(); } /** @@ -138,6 +144,6 @@ class tag_Core { // @todo There's a potential race condition here which we can solve by adding a lock around // this and all the cases where we create/update tags. I'm loathe to do that since it's an // extremely rare case. - Database::instance() ->delete("tags", array("count" => 0)); + db::build()->delete("tags")->where("count", "=", 0)->execute(); } }
\ No newline at end of file diff --git a/modules/tag/helpers/tag_event.php b/modules/tag/helpers/tag_event.php index a857a99d..6ee8e708 100644 --- a/modules/tag/helpers/tag_event.php +++ b/modules/tag/helpers/tag_event.php @@ -51,7 +51,7 @@ class tag_event_Core { try { tag::add($photo, $tag); } catch (Exception $e) { - Kohana::log("error", "Error adding tag: $tag\n" . + Kohana_Log::add("error", "Error adding tag: $tag\n" . $e->getMessage() . "\n" . $e->getTraceAsString()); } } diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php new file mode 100644 index 00000000..cd1ca6c6 --- /dev/null +++ b/modules/tag/helpers/tag_rest.php @@ -0,0 +1,160 @@ +<?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 { + // 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::success($resources); + } + + static function post($request) { + if (empty($request->arguments) || count($request->arguments) != 1 || empty($request->path)) { + throw new Rest_Exception(400, "Bad request"); + } + $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(); + } + + if (!access::can("edit", $item)) { + throw new Kohana_404_Exception(); + } + + foreach ($tags as $tag) { + tag::add($item, $tag); + } + return rest::success(); + } + + static function put($request) { + if (empty($request->arguments[0]) || empty($request->new_name)) { + throw new Rest_Exception(400, "Bad request"); + } + + $name = $request->arguments[0]; + + $tag = ORM::factory("tag") + ->where("name", "=", $name) + ->find(); + if (!$tag->loaded()) { + throw new Kohana_404_Exception(); + } + + $tag->name = $request->new_name; + $tag->save(); + + return rest::success(); + } + + static function delete($request) { + if (empty($request->arguments[0])) { + throw new Rest_Exception(400, "Bad 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") + ->where("tags.name", "IN", $tags) + ->where("relative_url_cache", "=", $request->path) + ->viewable() + ->find_all(); + } else { + $tag_list = ORM::factory("tag") + ->where("name", "IN", $tags) + ->find_all(); + } + + foreach ($tag_list as $row) { + $row->delete(); + }; + + tag::compact(); + return rest::success(); + } + + private static function _get_items($request) { + $tags = explode(",", $request->arguments[0]); + $items = ORM::factory("item") + ->select_distinct("*") + ->join("items_tags", "items.id", "items_tags.item_id") + ->join("tags", "tags.id", "items_tags.tag_id") + ->where("tags.name", "IN", $tags); + if (!empty($request->limit)) { + $items->limit($request->limit); + } + if (!empty($request->offset)) { + $items->offset($request->offset); + } + $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; + } +} diff --git a/modules/tag/helpers/tag_rss.php b/modules/tag/helpers/tag_rss.php index de5d6c72..f09a4530 100644 --- a/modules/tag/helpers/tag_rss.php +++ b/modules/tag/helpers/tag_rss.php @@ -31,8 +31,8 @@ class tag_rss_Core { static function feed($feed_id, $offset, $limit, $id) { if ($feed_id == "tag") { $tag = ORM::factory("tag", $id); - if (!$tag->loaded) { - Kohana::show_404(); + if (!$tag->loaded()) { + throw new Kohana_404_Exception(); } $feed->children = $tag->items($limit, $offset, "photo"); $feed->max_pages = ceil($tag->count / $limit); |