* only return items where the name contains this substring * * random=true * return a single random item * * type= * limit the type to types in this list. eg, "type=photo,movie" */ static function get($request) { $item = rest::resolve($request->url); access::required("view", $item); $p = $request->params; if (isset($p->random)) { $orm = item::random_query()->offset(0)->limit(1); } else { $orm = ORM::factory("item")->viewable(); } if (!empty($p->scope) && !in_array($p->scope, array("direct", "all"))) { throw new Exception("Bad Request", 400); } if (!empty($p->scope)) { if ($p->scope == "direct") { $orm->where("parent_id", "=", $item->id); } else { $orm->where("left_ptr", ">=", $item->left_ptr); $orm->where("right_ptr", "<=", $item->left_ptr); $orm->where("id", "<>", $item->id); } } if (isset($p->name)) { $orm->where("name", "LIKE", "%{$p->name}%"); } if (isset($p->type)) { $orm->where("type", "IN", explode(",", $p->type)); } $members = array(); foreach ($orm->find_all() as $child) { $members[] = url::abs_site("rest/gallery/" . $child->relative_url()); } return rest::reply(array("resource" => $item->as_array(), "members" => $members)); } static function put($request) { $item = rest::resolve($request->url); access::required("edit", $item); $params = $request->params; foreach (array("captured", "description", "slug", "sort_column", "sort_order", "title", "view_count", "weight") as $key) { if (isset($params->$key)) { $item->$key = $params->$key; } } $item->save(); return rest::reply(array("url" => url::abs_site("/rest/gallery/" . $item->relative_url()))); } static function post($request) { $parent = rest::resolve($request->url); access::required("edit", $parent); $params = $request->params; switch ($params->type) { case "album": $item = album::create( $parent, $params->name, isset($params->title) ? $params->title : $name, isset($params->description) ? $params->description : null); break; case "photo": $item = photo::create( $parent, $request->file, $params->name, isset($params->title) ? $params->title : $name, isset($params->description) ? $params->description : null); break; default: throw new Rest_Exception("Invalid type: $args->type", 400); } return rest::reply(array("url" => url::abs_site("/rest/gallery/" . $item->relative_url()))); } static function delete($request) { $item = rest::resolve($request->url); access::required("edit", $item); $item->delete(); return rest::reply(); } static function resolve($path) { return url::get_item_from_uri($path); } }