/** * The tree is rooted in a single item and can have modifiers which adjust what data is shown * for items inside the given tree, up to the depth that you want. The entity for this resource * is a series of items. * * depth=<number> * Only traverse this far down into the tree. If there are more albums * below this depth, provide RESTful urls to other tree resources in * the members section. Default is infinite. * * type=<album|photo|movie> * Restrict the items displayed to the given type. Default is all types. * * fields=<comma separated list of field names> * In the entity section only return these fields for each item. * Default is all fields. */ static function get($request) { $item = rest::resolve($request->url); access::required("view", $item); $query_params = array(); $p = $request->params; $where = array(); if (isset($p->type)) { $where[] = array("type", "=", $p->type); $query_params[] = "type={$p->type}"; } if (isset($p->depth)) { $lowest_depth = $item->level + $p->depth; $where[] = array("level", "<=", $lowest_depth); $query_params[] = "depth={$p->depth}"; } $fields = array(); if (isset($p->fields)) { $fields = explode(",", $p->fields); $query_params[] = "fields={$p->fields}"; } $entity = array(array("url" => rest::url("item", $item), "entity" => $item->as_restful_array($fields))); $members = array(); foreach ($item->viewable()->descendants(null, null, $where) as $child) { $entity[] = array("url" => rest::url("item", $child), "entity" => $child->as_restful_array($fields)); if (isset($lowest_depth) && $child->level == $lowest_depth) { $members[] = url::merge_querystring(rest::url("tree", $child), $query_params); } } $result = array("url" => $request->url, "entity" => $entity, "members" => $members, "relationships" => rest::relationships("tree", $item)); return $result; }
public function get_test() { $t1 = tag::add(item::root(), "t1"); $t2 = tag::add(item::root(), "t2"); $request = new stdClass(); $this->assert_equal_array(array("url" => rest::url("tags"), "members" => array(rest::url("tag", $t1), rest::url("tag", $t2))), tags_rest::get($request)); }
static function get($request) { $item = rest::resolve($request->url); access::required("view", $item); $checksums = array(rest::url("itemchecksum_md5", $item), rest::url("itemchecksum_sha1", $item)); return array("url" => $request->url, "members" => $checksums); }
static function relationships($resource_type, $resource) { switch ($resource_type) { case "item": return array("comments" => array("url" => rest::url("item_comments", $resource))); } }
public function resolve_test() { $album = test::random_album(); $tag = tag::add($album, "tag1")->reload(); $tuple = rest::resolve(rest::url("tag_item", $tag, $album)); $this->assert_equal_array($tag->as_array(), $tuple[0]->as_array()); $this->assert_equal_array($album->as_array(), $tuple[1]->as_array()); }
static function post($request) { $tag = rest::resolve($request->params->entity->tag); $item = rest::resolve($request->params->entity->item); access::required("view", $item); tag::add($item, $tag->name); return array("url" => rest::url("tag_item", $tag, $item), "members" => array(rest::url("tag", $tag), rest::url("item", $item))); }
static function get($request) { $item = rest::resolve($request->url); access::required("view", $item); $comments = array(); foreach (ORM::factory("comment")->viewable()->where("item_id", "=", $item->id)->order_by("created", "DESC")->find_all() as $comment) { $comments[] = rest::url("comment", $comment); } return array("url" => $request->url, "members" => $comments); }
static function post($request) { $tag = rest::resolve($request->params->entity->tag); $item = rest::resolve($request->params->entity->item); access::required("view", $item); if (!$tag->loaded()) { throw new Kohana_404_Exception(); } tag::add($item, $tag->name); return array("url" => rest::url("tag_item", $tag, $item), "members" => array("tag" => rest::url("tag", $tag), "item" => rest::url("item", $item))); }
static function relationships($resource_type, $resource) { switch ($resource_type) { case "item": $tags = array(); foreach (tag::item_tags($resource) as $tag) { $tags[] = rest::url("tag_item", $tag, $resource); } return array("tags" => array("url" => rest::url("item_tags", $resource), "members" => $tags)); } }
static function post($request) { $entity = $request->params->entity; $item = rest::resolve($entity->item); access::required("edit", $item); $comment = ORM::factory("comment"); $comment->author_id = identity::active_user()->id; $comment->item_id = $item->id; $comment->text = $entity->text; $comment->save(); return array("url" => rest::url("comment", $comment)); }
public function missing_file_test() { $photo = test::random_photo(); $request = new stdClass(); $request->url = rest::url("data", $photo, "thumb"); $request->params = new stdClass(); $request->params->size = "thumb"; unlink($photo->thumb_path()); // oops! try { data_rest::get($request); $this->assert_true(false); } catch (Kohana_404_Exception $e) { // pass } }
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 array("url" => rest::url("tag", $tag)); }
static function get($request) { $items = array(); if (isset($request->params->url)) { foreach ($request->params->url as $url) { $item = rest::resolve($url); if (access::can("view", $item)) { $members = array(); if ($item->type == "album") { foreach ($item->children() as $child) { $members[] = rest::url("item", $child); } } $items[] = array("url" => $url, "entity" => $item->as_restful_array(), "members" => $members, "relationship" => rest::relationships("item", $item)); } } } return $items; }
static function post($request) { // The user must have some edit permission somewhere to create a tag. if (!identity::active_user()->admin) { $query = db::build()->from("access_caches")->and_open(); foreach (identity::active_user()->groups() as $group) { $query->or_where("edit_{$group->id}", "=", access::ALLOW); } $has_any_edit_perm = $query->close()->count_records(); if (!$has_any_edit_perm) { access::forbidden(); } } if (empty($request->params->entity->name)) { throw new Rest_Exception("Bad Request", 400); } $tag = ORM::factory("tag")->where("name", "=", $request->params->entity->name)->find(); if (!$tag->loaded()) { $tag->name = $request->params->entity->name; $tag->count = 0; $tag->save(); } return array("url" => rest::url("tag", $tag)); }
private static function _format_restful_item($item, $types) { $item_rest = array("url" => rest::url("item", $item), "entity" => $item->as_restful_array(), "relationships" => rest::relationships("item", $item)); if ($item->type == "album") { $members = array(); foreach ($item->viewable()->children() as $child) { if (empty($types) || in_array($child->type, $types)) { $members[] = rest::url("item", $child); } } $item_rest["members"] = $members; } return $item_rest; }
static function post($request) { $parent = rest::resolve($request->url); access::required("edit", $parent); $params = $request->params; $item = ORM::factory("item"); switch ($params->type) { case "album": $item->type = "album"; $item->parent_id = $parent->id; $item->name = $params->name; $item->title = isset($params->title) ? $params->title : $name; $item->description = isset($params->description) ? $params->description : null; $item->slug = isset($params->slug) ? $params->slug : null; $item->save(); break; case "photo": case "movie": $item->type = $params->type; $item->parent_id = $parent->id; $item->set_data_file($request->file); $item->name = $params->name; $item->title = isset($params->title) ? $params->title : $params->name; $item->description = isset($params->description) ? $params->description : null; $item->slug = isset($params->slug) ? $params->slug : null; $item->save(); break; default: throw new Rest_Exception("Invalid type: {$params->type}", 400); } return array("url" => rest::url("item", $item)); }
/** * Same as ORM::as_array() but convert id fields into their RESTful form. */ public function as_restful_array() { // Convert item ids to rest URLs for consistency $data = $this->as_array(); if ($tmp = $this->parent()) { $data["parent"] = rest::url("item", $tmp); } unset($data["parent_id"]); if ($tmp = $this->album_cover()) { $data["album_cover"] = rest::url("item", $tmp); } unset($data["album_cover_item_id"]); if (access::can("view_full", $this) && $this->is_photo()) { $data["file_url"] = $this->file_url(true); } if (($tmp = $this->resize_url(true)) && $this->is_photo()) { $data["resize_url"] = $tmp; } $data["thumb_url"] = $this->thumb_url(true); // Elide some internal-only data that is going to cause confusion in the client. foreach (array("relative_path_cache", "relative_url_cache", "left_ptr", "right_ptr", "thumb_dirty", "resize_dirty") as $key) { unset($data[$key]); } return $data; }
public function delete_album_fails_without_permission_test() { $album1 = test::random_album(); access::deny(identity::everybody(), "edit", $album1); identity::set_active_user(identity::guest()); $request->url = rest::url("item", $album1); try { item_rest::delete($request); } catch (Exception $e) { $this->assert_equal("@todo FORBIDDEN", $e->getMessage()); return; } $this->assert_true(false, "Shouldn't get here"); }
public function as_restful_array_test() { $album = test::random_album(); $photo = test::random_photo($album); $album->reload(); $result = $album->as_restful_array(); $this->assert_same(rest::url("item", item::root()), $result["parent"]); $this->assert_same(rest::url("item", $photo), $result["album_cover"]); $this->assert_true(!array_key_exists("parent_id", $result)); $this->assert_true(!array_key_exists("album_cover_item_id", $result)); }
static function get($request) { list($tag, $item) = rest::resolve($request->url); return array("url" => $request->url, "entity" => array("tag" => rest::url("tag", $tag), "item" => rest::url("item", $item))); }
static function get($request) { list($tag, $item) = rest::resolve($request->url); return array("url" => $request->url, "members" => array(rest::url("tag", $tag), rest::url("item", $item))); }
/** * Same as ORM::as_array() but convert id fields into their RESTful form. * * @param array if specified, only return the named fields */ public function as_restful_array($fields = array()) { if ($fields) { $data = array(); foreach ($fields as $field) { if (isset($this->object[$field])) { $data[$field] = $this->__get($field); } } $fields = array_flip($fields); } else { $data = $this->as_array(); } // Convert item ids to rest URLs for consistency if (empty($fields) || isset($fields["parent"])) { if ($tmp = $this->parent()) { $data["parent"] = rest::url("item", $tmp); } unset($data["parent_id"]); } if (empty($fields) || isset($fields["album_cover"])) { if ($tmp = $this->album_cover()) { $data["album_cover"] = rest::url("item", $tmp); } unset($data["album_cover_item_id"]); } if (empty($fields) || isset($fields["web_url"])) { $data["web_url"] = $this->abs_url(); } if (!$this->is_album()) { if (access::can("view_full", $this)) { if (empty($fields) || isset($fields["file_url"])) { $data["file_url"] = rest::url("data", $this, "full"); } if (empty($fields) || isset($fields["file_size"])) { $data["file_size"] = filesize($this->file_path()); } if (access::user_can(identity::guest(), "view_full", $this)) { if (empty($fields) || isset($fields["file_url_public"])) { $data["file_url_public"] = $this->file_url(true); } } } } if ($this->is_photo()) { if (empty($fields) || isset($fields["resize_url"])) { $data["resize_url"] = rest::url("data", $this, "resize"); } if (empty($fields) || isset($fields["resize_size"])) { $data["resize_size"] = filesize($this->resize_path()); } if (access::user_can(identity::guest(), "view", $this)) { if (empty($fields) || isset($fields["resize_url_public"])) { $data["resize_url_public"] = $this->resize_url(true); } } } if ($this->has_thumb()) { if (empty($fields) || isset($fields["thumb_url"])) { $data["thumb_url"] = rest::url("data", $this, "thumb"); } if (empty($fields) || isset($fields["thumb_size"])) { $data["thumb_size"] = filesize($this->thumb_path()); } if (access::user_can(identity::guest(), "view", $this)) { if (empty($fields) || isset($fields["thumb_url_public"])) { $data["thumb_url_public"] = $this->thumb_url(true); } } } if (empty($fields) || isset($fields["can_edit"])) { $data["can_edit"] = access::can("edit", $this); } // Elide some internal-only data that is going to cause confusion in the client. foreach (array("relative_path_cache", "relative_url_cache", "left_ptr", "right_ptr", "thumb_dirty", "resize_dirty", "weight") as $key) { unset($data[$key]); } return $data; }
static function post($request) { $parent = rest::resolve($request->url); access::required("add", $parent); $entity = $request->params->entity; $item = ORM::factory("item"); switch ($entity->type) { case "album": $item->type = "album"; $item->parent_id = $parent->id; $item->name = $entity->name; $item->title = isset($entity->title) ? $entity->title : $entity->name; $item->description = isset($entity->description) ? $entity->description : null; $item->slug = isset($entity->slug) ? $entity->slug : null; $item->save(); break; case "photo": case "movie": if (empty($request->file)) { throw new Rest_Exception("Bad Request", 400, array("errors" => array("file" => t("Upload failed")))); } $item->type = $entity->type; $item->parent_id = $parent->id; $item->set_data_file($request->file); $item->name = $entity->name; $item->title = isset($entity->title) ? $entity->title : $entity->name; $item->description = isset($entity->description) ? $entity->description : null; $item->slug = isset($entity->slug) ? $entity->slug : null; $item->save(); break; default: throw new Rest_Exception("Bad Request", 400, array("errors" => array("type" => "invalid"))); } return array("url" => rest::url("item", $item)); }
public function get_ancestors_test() { $album1 = test::random_album(); $photo1 = test::random_photo($album1); $album2 = test::random_album($album1); $photo2 = test::random_photo($album2); $album1->reload(); $album2->reload(); $root = ORM::factory("item", 1); $restful_root = array("url" => rest::url("item", $root), "entity" => $root->as_restful_array(), "relationships" => rest::relationships("item", $root)); $restful_root["members"] = array(); foreach ($root->children() as $child) { $restful_root["members"][] = rest::url("item", $child); } $request = new stdClass(); $request->params = new stdClass(); $request->params->ancestors_for = rest::url("item", $photo2); $this->assert_equal_array(array($restful_root, array("url" => rest::url("item", $album1), "entity" => $album1->as_restful_array(), "relationships" => array("comments" => array("url" => rest::url("item_comments", $album1)), "tags" => array("url" => rest::url("item_tags", $album1), "members" => array())), "members" => array(rest::url("item", $photo1), rest::url("item", $album2))), array("url" => rest::url("item", $album2), "entity" => $album2->as_restful_array(), "relationships" => array("comments" => array("url" => rest::url("item_comments", $album2)), "tags" => array("url" => rest::url("item_tags", $album2), "members" => array())), "members" => array(rest::url("item", $photo2))), array("url" => rest::url("item", $photo2), "entity" => $photo2->as_restful_array(), "relationships" => array("comments" => array("url" => rest::url("item_comments", $photo2)), "tags" => array("url" => rest::url("item_tags", $photo2), "members" => array())))), items_rest::get($request)); }
/** * Same as ORM::as_array() but convert id fields into their RESTful form. */ public function as_restful_array() { $data = array(); foreach ($this->as_array() as $key => $value) { if (strncmp($key, "server_", 7)) { $data[$key] = $value; } } $data["item"] = rest::url("item", $this->item()); unset($data["item_id"]); return $data; }
public function resolve_test() { $tag = test::random_tag(); $this->assert_equal($tag->as_array(), rest::resolve(rest::url("tag", $tag))->as_array()); }