summaryrefslogtreecommitdiff
path: root/modules/search/helpers
diff options
context:
space:
mode:
authorTim Almdal <tnalmdal@shaw.ca>2011-08-11 22:04:20 -0700
committerTim Almdal <tnalmdal@shaw.ca>2011-08-11 22:04:21 -0700
commit933a34986dbca248f388e8aa3c3aea4a6d71a94b (patch)
tree42251d934b3b257652f8b1658d5343d3f8759fab /modules/search/helpers
parentb78f87cb80c1ee4b8608508cb8a9bf8be71c511d (diff)
Patch for tickets #1428 and #1760
Create the concept of a Photo_Display_Context. If the user is browsing a dynamic album (i.e. tags) and chooses to look at an image in that album. The display of the image happens correctly, but the 'next' and 'previous' buttons are no longer consistent. When one of these is clicked, Gallery will open the adjacent image in the actuall album, not the dynamic album.
Diffstat (limited to 'modules/search/helpers')
-rw-r--r--modules/search/helpers/search.php45
1 files changed, 36 insertions, 9 deletions
diff --git a/modules/search/helpers/search.php b/modules/search/helpers/search.php
index a3fd795a..a77a2433 100644
--- a/modules/search/helpers/search.php
+++ b/modules/search/helpers/search.php
@@ -36,8 +36,19 @@ class search_Core {
static function search($q, $limit, $offset) {
$db = Database::instance();
- $q = $db->escape($q);
+ $query = self::_build_query_base($q) .
+ "ORDER BY `score` DESC " .
+ "LIMIT $limit OFFSET " . (int)$offset;
+
+ $data = $db->query($query);
+ $count = $db->query("SELECT FOUND_ROWS() as c")->current()->c;
+
+ return array($count, new ORM_Iterator(ORM::factory("item"), $data));
+ }
+
+ private static function _build_query_base($q, $where=array()) {
+ $q = Database::instance()->escape($q);
if (!identity::active_user()->admin) {
foreach (identity::group_ids_for_active_user() as $id) {
$fields[] = "`view_$id` = TRUE"; // access::ALLOW
@@ -47,18 +58,13 @@ class search_Core {
$access_sql = "";
}
- $query =
+ return
"SELECT SQL_CALC_FOUND_ROWS {items}.*, " .
" MATCH({search_records}.`data`) AGAINST ('$q') AS `score` " .
"FROM {items} JOIN {search_records} ON ({items}.`id` = {search_records}.`item_id`) " .
"WHERE MATCH({search_records}.`data`) AGAINST ('$q' IN BOOLEAN MODE) " .
- $access_sql .
- "ORDER BY `score` DESC " .
- "LIMIT $limit OFFSET " . (int)$offset;
- $data = $db->query($query);
- $count = $db->query("SELECT FOUND_ROWS() as c")->current()->c;
-
- return array($count, new ORM_Iterator(ORM::factory("item"), $data));
+ (empty($where) ? "" : " AND " . join(" AND ", $where)) .
+ $access_sql;
}
/**
@@ -103,4 +109,25 @@ class search_Core {
return array($remaining, $total, $percent);
}
+
+ static function get_position($item, $q) {
+ $page_size = module::get_var("gallery", "page_size", 9);
+
+ $query = self::_build_query_base($q, array("{items}.id = " . $item->id));
+
+ $db = Database::instance();
+
+ // Truncate the score by two decimal places as this resolves the issues
+ // that arise due to in exact numeric conversions.
+ $score = $db->query($query)->current()->score;
+ $score = substr($score, 0, strlen($score) - 2);
+
+ $data = $db->query(self::_build_query_base($q) . "having `score` >= " . $score);
+
+ $data->seek($data->count() - 1);
+
+ while ($data->get("id") != $item->id && $data->prev()->valid());
+
+ return $data->key() + 1;
+ }
}