summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Kinkade <nkinkade@nkinka.de>2011-01-09 17:37:23 +0000
committerNathan Kinkade <nkinkade@nkinka.de>2011-01-09 17:37:23 +0000
commit37e82a433cd09995988bc52e5cc23f35860fcd69 (patch)
tree7ed02c364b0b0bdce4e0eda296a202d32cfd99b2
parent2cdc3b76dc51d4d7a7675117974ce3c6b004cb63 (diff)
Overhaul of module, with a good many changes suggested by danneh3826 on the gallery forum.
-rw-r--r--controllers/randimg.php14
-rw-r--r--helpers/randimg.php23
-rw-r--r--module.info2
-rw-r--r--views/randimg.html.php46
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 <img> 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 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<?php
-/**
- * In some cases the proper URL path to the images on the disc
- * will be virtualized via an Apache Alias definition, so grab
- * that path here.
- */
-$virtpath = $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']);
+if ( ! empty($item) ) {
-// URL to Gallery3
-$g3path = "{$_SERVER['HTTP_HOST']}{$_SERVER['SCRIPT_NAME']}";
+ /**
+ * In some cases the proper URL path to the images on the disc
+ * will be virtualized via an Apache Alias definition, so grab
+ * that path here.
+ */
+ $virtpath = $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']);
-echo <<<HTML
-<a href="http://$g3path/$linkpath" title="$img_title">
- <img src="http://$virtpath/var/resizes/$imgpath" style="border: thin solid black;" alt="$img_title" />
-</a>
+ // 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 <<<HTML
+<div class="g-randimg" style="$width">
+ <a href="http://$g3path/$link_path" title="$img_title">
+ <img src="$thumb_url" style="border: thin solid black;" alt="$img_title" />
+ </a>
+</div>
HTML;
-?>
+}