diff options
Diffstat (limited to 'modules/gallery/models')
| -rw-r--r-- | modules/gallery/models/item.php | 41 |
1 files changed, 34 insertions, 7 deletions
diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index dbd56fa2..a1be4fbc 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -918,22 +918,49 @@ class Item_Model extends ORM_MPTT { /** * Same as ORM::as_array() but convert id fields into their RESTful form. */ - public function as_restful_array() { + public function as_restful_array($depth=0, $level=0) { // Convert item ids to rest URLs for consistency - $data = $this->as_array(); + $data = array("url" => rest::url("item", $this), + "entity" => $this->as_array(), + "members" => array(), + "relationships" => array()); + if ($tmp = $this->parent()) { - $data["parent"] = rest::url("item", $tmp); + $data["entity"]["parent"] = rest::url("item", $tmp); } - unset($data["parent_id"]); + unset($data["entity"]["parent_id"]); if ($tmp = $this->album_cover()) { - $data["album_cover"] = rest::url("item", $tmp); + $data["entity"]["album_cover"] = rest::url("item", $tmp); } - unset($data["album_cover_item_id"]); + unset($data["entity"]["album_cover_item_id"]); // 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") as $key) { - unset($data[$key]); + unset($data["entity"][$key]); + } + + // check that we have given enough information. At this point we don't + // return relationships and we give enough information to determine how to handle + // the children. + $summarize = $depth < $level; + if (!$summarize) { + $data["relationships"] = rest::relationships("item", $this); + } + + $next_level = $level + 1; + foreach ($this->children() as $child) { + if ($summarize) { + $data["members"][] = array("url" => rest::url("item", $child), + "entity" => array("title" => $child->title, + "type" => $child->type), + "members" => array(), + "summary" => true, + "relationships" => array()); + } else { + $data["members"][] = $child->as_restful_array($depth, $next_level); + } } + $data["summary"] = $summarize; return $data; } } |
