type) ? "random" : $request->type; switch ($type) { case "random": $random = ((float)mt_rand()) / (float)mt_getrandmax(); $items = ORM::factory("item") ->viewable() ->where("type", "!=", "album") ->where("rand_key", "<", $random) ->order_by(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) ->order_by(array("rand_key" => "DESC")) ->find_all(1); } break; default: return rest::fail("Unsupported image block type: '{$type}'"); } 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"); } } }