From b8ee6f25bdd9372dccd4552ca29cf6f2857e8164 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Tue, 22 Dec 2009 18:55:03 -0800 Subject: Add a restful get random image block --- modules/image_block/helpers/image_block_rest.php | 61 ++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 modules/image_block/helpers/image_block_rest.php (limited to 'modules/image_block') diff --git a/modules/image_block/helpers/image_block_rest.php b/modules/image_block/helpers/image_block_rest.php new file mode 100644 index 00000000..eca74941 --- /dev/null +++ b/modules/image_block/helpers/image_block_rest.php @@ -0,0 +1,61 @@ +path) { + case "random": + $random = ((float)mt_rand()) / (float)mt_getrandmax(); + + $items = ORM::factory("item") + ->viewable() + ->where("type !=", "album") + ->where("rand_key < ", $random) + ->orderby(array("rand_key" => "DESC")) + ->find_all(1); + + if ($items->count() == 0) { + // Try once more. If this fails, just ditch the block altogether + $items = ORM::factory("item") + ->viewable() + ->where("type !=", "album") + ->where("rand_key >= ", $random) + ->orderby(array("rand_key" => "DESC")) + ->find_all(1); + } + break; + default: + return rest::fail("Unsupported block type: '{$request->path}'"); + } + + if ($items->count() > 0) { + $item = $items->current(); + $response_data = array("name" => $item->name, + "path" => $item->relative_url(), + "title" => $item->title, + "thumb_url" => $item->thumb_url(true), + "thumb_size" => array("height" => $item->thumb_height, + "width" => $item->thumb_width)); + + return rest::success(array("resource" => $response_data)); + } else { + return rest::fail("No Image found"); + } + } +} -- cgit v1.2.3