From 37e82a433cd09995988bc52e5cc23f35860fcd69 Mon Sep 17 00:00:00 2001 From: Nathan Kinkade Date: Sun, 9 Jan 2011 17:37:23 +0000 Subject: Overhaul of module, with a good many changes suggested by danneh3826 on the gallery forum. --- controllers/randimg.php | 14 +++++++------- helpers/randimg.php | 23 +++++++---------------- module.info | 2 +- views/randimg.html.php | 46 +++++++++++++++++++++++++++++++++------------- 4 files changed, 48 insertions(+), 37 deletions(-) diff --git a/controllers/randimg.php b/controllers/randimg.php index 00f95a7..04ee1d1 100644 --- a/controllers/randimg.php +++ b/controllers/randimg.php @@ -17,13 +17,13 @@ class randimg_Controller extends Controller { - protected $resource_type = "randimg"; + protected $resource_type = "randimg"; - public function index() { - // Far from perfection, but at least require view permission for the root album - $album = ORM::factory("item", 1); - access::required("view", $album); - print randimg::randimg_link(); - } + public function index() { + // Far from perfection, but at least require view permission for the root album + $album = ORM::factory("item", 1); + access::required("view", $album); + print randimg::randimg_link(); + } } diff --git a/helpers/randimg.php b/helpers/randimg.php index 91bf5cb..af56564 100644 --- a/helpers/randimg.php +++ b/helpers/randimg.php @@ -19,23 +19,14 @@ class randimg_Core { static function randimg_link() { - $db = Database::instance(); - $table_prefix = $db->table_prefix; - $query = " - SELECT i1.name, i1.title, i1.description, i1.relative_path_cache, i1.relative_url_cache, i1.slug AS imgslug, i2.slug - FROM {$table_prefix}items i1 JOIN {$table_prefix}items i2 - ON i1.parent_id = i2.id - WHERE i1.type != 'album' AND i1.relative_path_cache IS NOT NULL - ORDER BY RAND() - LIMIT 1; - "; - $result = $db->query($query)->current(); - - $randimg = new View("randimg.html"); - $randimg->imgpath = $result->relative_path_cache; - $randimg->linkpath = $result->relative_url_cache; - $randimg->img_title = $result->title; + $attempts=0; + do { + $item = item::random_query()->where("type", "!=", "album")->find_all(1)->current(); + } while ( !$item && $attempts++ < 5 ); + $randimg = new View("randimg.html"); + $randimg->item = $item; + return $randimg; } diff --git a/module.info b/module.info index 386907d..37a4c4c 100644 --- a/module.info +++ b/module.info @@ -1,3 +1,3 @@ name = "RandImg" description = "Returns an link of a random image." -version = 1 +version = 1.1 diff --git a/views/randimg.html.php b/views/randimg.html.php index 52d4d78..59817a6 100644 --- a/views/randimg.html.php +++ b/views/randimg.html.php @@ -1,21 +1,41 @@ - $img_title - + // URL to Gallery3 + $g3path = "{$_SERVER['HTTP_HOST']}{$_SERVER['SCRIPT_NAME']}"; + $link_path = $item->relative_url(); + $img_title = $item->title; + switch (Input::instance()->get("size")) { + case "fullsize": + $thumb_url = $item->file_url(true); + break; + case "resize": + $thumb_url = $item->resize_url(true); + break; + case "thumb": + default: + $thumb_url = $item->thumb_url(true); + } + + $width = Input::instance()->get("width"); + $width = $width ? "width: {$width}px;" : ""; + + echo << + + $img_title + + HTML; -?> +} -- cgit v1.2.3