summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modules/gallery/helpers/item_rest.php14
-rw-r--r--modules/gallery/models/item.php41
2 files changed, 38 insertions, 17 deletions
diff --git a/modules/gallery/helpers/item_rest.php b/modules/gallery/helpers/item_rest.php
index c0fc422a..72230d8b 100644
--- a/modules/gallery/helpers/item_rest.php
+++ b/modules/gallery/helpers/item_rest.php
@@ -30,6 +30,9 @@ class item_rest_Core {
* name=<substring>
* only return items where the name contains this substring
*
+ * depth=<number>
+ * return the children to the depth specified.
+ *
* random=true
* return a single random item
*
@@ -70,16 +73,7 @@ class item_rest_Core {
$orm->where("type", "IN", explode(",", $p->type));
}
- $members = array();
- foreach ($orm->find_all() as $child) {
- $members[] = rest::url("item", $child);
- }
-
- return array(
- "url" => $request->url,
- "entity" => $item->as_restful_array(),
- "members" => $members,
- "relationships" => rest::relationships("item", $item));
+ return $item->as_restful_array(isset($p->depth) ? $p->depth : 0);
}
static function put($request) {
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;
}
}