viewable() ->select("COUNT(*) AS C") ->find()->C; if (empty($viewable_items)) { return ""; } $block = new Block(); $block->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) { $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; } }