Example #1
0
 /**
  * To retrieve a collection of items, you can specify the following query parameters to specify
  * the type of the collection.  If both are specified, then the url parameter is used and the
  * ancestors_for is ignored.  Specifying the "type" parameter with the urls parameter, will
  * filter the results based on the specified type.  Using the type parameter with the
  * ancestors_for parameter makes no sense and will be ignored.
  *
  *   urls=["url1","url2","url3"]
  *     Return items that match the specified urls.  Typically used to return the member detail
  *
  *   ancestors_for=url
  *     Return the ancestors of the specified item
  *
  *   type=<comma separate list of photo, movie or album>
  *     Limit the type to types in this list, eg: "type=photo,movie".
  *     Also limits the types returned in the member collections (same behaviour as item_rest).
  *     Ignored if ancestors_for is set.
  */
 static function get($request)
 {
     $items = array();
     $types = array();
     if (isset($request->params->urls)) {
         if (isset($request->params->type)) {
             $types = explode(",", $request->params->type);
         }
         foreach (json_decode($request->params->urls) as $url) {
             $item = rest::resolve($url);
             if (!access::can("view", $item)) {
                 continue;
             }
             if (empty($types) || in_array($item->type, $types)) {
                 $items[] = items_rest::_format_restful_item($item, $types);
             }
         }
     } else {
         if (isset($request->params->ancestors_for)) {
             $item = rest::resolve($request->params->ancestors_for);
             if (!access::can("view", $item)) {
                 throw new Kohana_404_Exception();
             }
             $items[] = items_rest::_format_restful_item($item, $types);
             while (($item = $item->parent()) != null) {
                 array_unshift($items, items_rest::_format_restful_item($item, $types));
             }
         }
     }
     return $items;
 }
 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));
 }