-
Notifications
You must be signed in to change notification settings - Fork 260
/
TagItems.php
148 lines (133 loc) · 5.04 KB
/
TagItems.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2013 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_Controller_Rest_TagItems extends Controller_Rest {
/**
* This resource represents a collection of item resources that all have a specified tag.
*
* GET can accept the following query parameters:
* name=<substring>
* Only return items where the name contains this substring.
* type=<comma-separated list of photo, movie or album>
* Limit the type to types in this list (e.g. "type=photo,movie").
* Also limits the types returned in the member collections (i.e. sub-albums).
* @see Controller_Rest_TagItems::get_members()
*
* PUT can accept the following post parameters:
* members
* Replace the collection of items on the tag with this list
* @see Controller_Rest_TagItems::put_members()
*
* POST can accept the following post parameters:
* members
* Add the tag to the items in the list. Unlike PUT, this only *adds* items.
* Since there is no entity function, this is only accessible using relationships.
* @see Controller_Rest_TagItems::post_members()
*
* DELETE removes all items from the tag, which deletes the tag entirely (no parameters accepted).
* @see Controller_Rest_TagItems::delete()
*
* RELATIONSHIPS: "tag_items" is the "items" relationship of a "tag" resource.
*
* Note: similar to the standard UI, only admins can PUT or DELETE tag_items.
*/
/**
* GET the item members of the tag_items resource.
* @see Controller_Rest_Items::get_members().
*/
static function get_members($id, $params) {
$tag = ORM::factory("Tag", $id);
if (!$tag->loaded()) {
throw Rest_Exception::factory(404);
}
$members = $tag->items->viewable()
->limit(Arr::get($params, "num", static::$default_params["num"]))
->offset(Arr::get($params, "start", static::$default_params["start"]));
if (isset($params["type"])) {
$members->where("type", "IN", $params["type"]);
}
if (isset($params["name"])) {
$members->where("name", "LIKE", "%" . Database::escape_for_like($params["name"]) . "%");
}
$data = array();
foreach ($members->find_all() as $member) {
$data[] = array("item", $member->id);
}
return $data;
}
/**
* PUT the item members of the tag_items resource. This replaces the list of items with
* the specified tag, and is only for admins.
*/
static function put_members($id, $params) {
if (!Identity::active_user()->admin) {
throw Rest_Exception::factory(403);
}
$tag = ORM::factory("Tag", $id);
if (!$tag->loaded()) {
throw Rest_Exception::factory(404);
}
// Resolve our members list into an array of item models.
$items = Rest::resolve_members($params["members"],
function($type, $id, $params) {
$item = ORM::factory("Item", $id);
return (($type == "item") && $item->loaded()) ? $item : false;
});
// Clear all items from the tag, then add the new set.
Tag::remove_items($tag);
foreach ($items as $item) {
Tag::add($item, $tag->name);
}
Tag::compact();
}
/**
* POST item members of the tag_items resource. Unlike PUT, this only *adds* the tag
* to the items, and is not admin-only.
*/
static function post_members($id, $params) {
$tag = ORM::factory("Tag", $id);
if (!$tag->loaded()) {
throw Rest_Exception::factory(404);
}
// Resolve our members list into an array of item models.
$items = Rest::resolve_members($params["members"],
function($type, $id, $params) {
$item = ORM::factory("Item", $id);
return (($type == "item") && $item->loaded()) ? $item : false;
});
// Add the tag to the items.
foreach ($items as $item) {
Tag::add($item, $tag->name);
}
}
/**
* DELETE the tag. This is only for admins.
* @see Controller_Rest_Tag::delete()
*/
static function delete($id, $params) {
return Rest::delete("tag", $id, $params);
}
/**
* Return the relationship established by tag_items. This adds "items"
* as a relationship of a "tag" resource.
*/
static function relationships($type, $id, $params) {
return ($type == "tag") ? array("items" => array("tag_items", $id, $params)) : null;
}
}