css_id = "gImageBlock"; $block->title = t("Random Image"); $block->content = new View("image_block_block.html"); $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); } $block->content->item = $items->current(); return $items->count() == 0 ? "" : $block; } }