summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2010-01-08 14:56:08 -0800
committerBharat Mediratta <bharat@menalto.com>2010-01-08 14:56:08 -0800
commit9864ab4b2708ec54c39092a21828403cbbd25e2e (patch)
treee3542b02adbb8dc7da3c3185a35f1d46d944ef59
parentfb65a0a5854812a9837c770dfbb27a23bee49e3d (diff)
Move the random image functionality into the gallery REST helper since
choosing a random image is essentially a function on an item collection. Also implemented a bunch of other query filters for item collections. Created item::random_query() as a way of generating a reasonable starting point for random queries.
-rw-r--r--modules/gallery/helpers/gallery_rest.php55
-rw-r--r--modules/gallery/helpers/item.php25
-rw-r--r--modules/image_block/helpers/image_block_block.php2
-rw-r--r--modules/image_block/helpers/image_block_rest.php62
4 files changed, 55 insertions, 89 deletions
diff --git a/modules/gallery/helpers/gallery_rest.php b/modules/gallery/helpers/gallery_rest.php
index fd18d59a..0de5da2b 100644
--- a/modules/gallery/helpers/gallery_rest.php
+++ b/modules/gallery/helpers/gallery_rest.php
@@ -36,22 +36,63 @@
// just like #1 except in a helper instead of in the model?
class gallery_rest_Core {
+
+ /**
+ * For items that are collections, you can specify the following additional query parameters to
+ * query the collection. You can specify them in any combination.
+ *
+ * scope=direct
+ * only return items that are immediately under this one
+ * scope=all
+ * return items anywhere under this one
+ *
+ * name=<substring>
+ * only return items where the name contains this substring
+ *
+ * random=true
+ * return a single random item
+ *
+ * type=<comma separate list of photo, movie or album>
+ * limit the type to types in this list. eg, "type=photo,movie"
+ */
static function get($request) {
$item = rest::resolve($request->url);
access::required("view", $item);
- if (isset($request->params->name)) {
- $where[] = array("name", "=", $request->params->name);
+ $p = $request->params;
+ if (isset($p->random)) {
+ $orm = item::random_query()->offset(0)->limit(1);
} else {
- $where = array();
+ $orm = ORM::factory("item")->viewable();
+ }
+
+ if (!empty($p->scope) && !in_array($p->scope, array("direct", "all"))) {
+ throw new Exception("Bad Request", 400);
+ }
+ if (!empty($p->scope)) {
+ if ($p->scope == "direct") {
+ $orm->where("parent_id", "=", $item->id);
+ } else {
+ $orm->where("left_ptr", ">=", $item->left_ptr);
+ $orm->where("right_ptr", "<=", $item->left_ptr);
+ $orm->where("id", "<>", $item->id);
+ }
+ }
+
+ if (isset($p->name)) {
+ $orm->where("name", "LIKE", "%{$p->name}%");
+ }
+
+ if (isset($p->type)) {
+ $orm->where("type", "IN", explode(",", $p->type));
}
- $children = array();
- foreach ($item->children($where) as $child) {
- $children[] = url::abs_site("rest/gallery/" . $child->relative_url());
+ $members = array();
+ foreach ($orm->find_all() as $child) {
+ $members[] = url::abs_site("rest/gallery/" . $child->relative_url());
}
- return rest::reply(array("resource" => $item->as_array(), "members" => $children));
+ return rest::reply(array("resource" => $item->as_array(), "members" => $members));
}
static function put($request) {
diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php
index eb528f8f..1fd9ef16 100644
--- a/modules/gallery/helpers/item.php
+++ b/modules/gallery/helpers/item.php
@@ -175,31 +175,18 @@ class item_Core {
}
/**
- * Return a random Item_Model, with optional filters
+ * Return a query to get a random Item_Model, with optional filters
*
* @param array (optional) where tuple
*/
- static function random($where=null) {
- $random = ((float)mt_rand()) / (float)mt_getrandmax();
-
- // Pick a random number and find the item that's got nearest smaller number. In the rare case
- // that we chose the smallest number in the system, choose the item with the smallest number.
+ static function random_query($where=null) {
+ // Pick a random number and find the item that's got nearest smaller number.
// This approach works best when the random numbers in the system are roughly evenly
// distributed so this is going to be more efficient with larger data sets.
- $items = ORM::factory("item")
+ return ORM::factory("item")
->viewable()
- ->where("rand_key", "<", $random)
+ ->where("rand_key", "<", ((float)mt_rand()) / (float)mt_getrandmax())
->merge_where($where)
- ->order_by("rand_key", "DESC")
- ->find_all(1);
-
- if ($items->count() == 0) {
- $items = ORM::factory("item")
- ->viewable()
- ->merge_where($where)
- ->order_by("rand_key", "ASC")
- ->find_all(1);
- }
- return $items;
+ ->order_by("rand_key", "DESC");
}
} \ No newline at end of file
diff --git a/modules/image_block/helpers/image_block_block.php b/modules/image_block/helpers/image_block_block.php
index 5f2bbcb7..f28e775f 100644
--- a/modules/image_block/helpers/image_block_block.php
+++ b/modules/image_block/helpers/image_block_block.php
@@ -30,7 +30,7 @@ class image_block_block_Core {
$block->css_id = "g-image-block";
$block->title = t("Random image");
$block->content = new View("image_block_block.html");
- $block->content->items = item::random(array(array("type", "!=", "album")));
+ $block->content->items = item::random_query(array(array("type", "!=", "album")))->find_all(1);
if ($block->content->items->count() == 0) {
$block = "";
diff --git a/modules/image_block/helpers/image_block_rest.php b/modules/image_block/helpers/image_block_rest.php
deleted file mode 100644
index 65eefb21..00000000
--- a/modules/image_block/helpers/image_block_rest.php
+++ /dev/null
@@ -1,62 +0,0 @@
-<?php defined("SYSPATH") or die("No direct script access.");
-/**
- * Gallery - a web based photo album viewer and editor
- * Copyright (C) 2000-2009 Bharat Mediratta
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or (at
- * your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
- */
-class image_block_rest_Core {
- static function get($request) {
- $type = empty($request->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::reply(array("resource" => $response_data));
- } else {
- return rest::reply();
- }
- }
-}