From a529a55e0b365309b4796edd291af3de4f80c110 Mon Sep 17 00:00:00 2001 From: Chris Chilvers Date: Fri, 14 Dec 2012 17:34:26 +0000 Subject: search within the current album by default When searching, limit search results to the current album. In the search results screen, display which album was searched and provide a link to search the whole gallery. --- modules/search/controllers/search.php | 35 +++++++++++++++++++++++-------- modules/search/helpers/search.php | 32 ++++++++++++++++++++++------ modules/search/views/search.html.php | 12 +++++++++++ modules/search/views/search_link.html.php | 7 +++++++ 4 files changed, 71 insertions(+), 15 deletions(-) (limited to 'modules') diff --git a/modules/search/controllers/search.php b/modules/search/controllers/search.php index 235cc90e..52eb973c 100644 --- a/modules/search/controllers/search.php +++ b/modules/search/controllers/search.php @@ -24,12 +24,21 @@ class Search_Controller extends Controller { $q_with_more_terms = search::add_query_terms($q); $show = Input::instance()->get("show"); + $album_id = Input::instance()->get("album", item::root()->id); + $album = ORM::factory("item", $album_id); + if (!access::can("view", $album) || !$album->is_album()) { + $album = item::root(); + } + if ($show) { $child = ORM::factory("item", $show); - $index = search::get_position($child, $q_with_more_terms); + $index = search::get_position_within_album($child, $q_with_more_terms, $album); if ($index) { $page = ceil($index / $page_size); - url::redirect(url::abs_site("search?q=" . urlencode($q) . ($page == 1 ? "" : "&page=$page"))); + url::redirect(url::abs_site("search" . + "?q=" . urlencode($q) . + "&album=" . urlencode($album->id) . + ($page == 1 ? "" : "&page=$page"))); } } @@ -42,7 +51,8 @@ class Search_Controller extends Controller { $offset = ($page - 1) * $page_size; - list ($count, $result) = search::search($q_with_more_terms, $page_size, $offset); + list ($count, $result) = + search::search_within_album($q_with_more_terms, $album, $page_size, $offset); $title = t("Search: %q", array("q" => $q_with_more_terms)); @@ -61,28 +71,35 @@ class Search_Controller extends Controller { "children_count" => $count)); $template->content = new View("search.html"); + $template->content->album = $album; $template->content->items = $result; $template->content->q = $q; print $template; item::set_display_context_callback( - "Search_Controller::get_display_context", $title, $q_with_more_terms, $q); + "Search_Controller::get_display_context", $album->id, $title, $q_with_more_terms, $q); } - static function get_display_context($item, $title, $query_terms, $q) { - $position = search::get_position($item, $query_terms); + static function get_display_context($item, $album_id, $title, $query_terms, $q) { + $album = ORM::factory("item", $album_id); + $position = search::get_position_within_album($item, $query_terms, $album); if ($position > 1) { - list ($count, $result_data) = search::search($query_terms, 3, $position - 2); + list ($count, $result_data) = + search::search_within_album($query_terms, $album, 3, $position - 2); list ($previous_item, $ignore, $next_item) = $result_data; } else { $previous_item = null; - list ($count, $result_data) = search::search($query_terms, 1, $position); + list ($count, $result_data) = + search::search_within_album($query_terms, $album, 1, $position); list ($next_item) = $result_data; } - $search_url = url::abs_site("search?q=" . urlencode($q) . "&show={$item->id}"); + $search_url = url::abs_site("search" . + "?q=" . urlencode($q) . + "&album=" . urlencode($album_id) . + "&show={$item->id}"); $root = item::root(); return array("position" => $position, diff --git a/modules/search/helpers/search.php b/modules/search/helpers/search.php index 64a13825..64bd3d31 100644 --- a/modules/search/helpers/search.php +++ b/modules/search/helpers/search.php @@ -35,9 +35,13 @@ class search_Core { } static function search($q, $limit, $offset) { + return search::search_within_album($q, item::root(), $limit, $offset); + } + + static function search_within_album($q, $album, $limit, $offset) { $db = Database::instance(); - $query = self::_build_query_base($q) . + $query = self::_build_query_base($q, $album) . "ORDER BY `score` DESC " . "LIMIT $limit OFFSET " . (int)$offset; @@ -47,8 +51,10 @@ class search_Core { return array($count, new ORM_Iterator(ORM::factory("item"), $data)); } - private static function _build_query_base($q, $where=array()) { - $q = Database::instance()->escape($q); + private static function _build_query_base($q, $album, $where=array()) { + $db = Database::instance(); + $q = $db->escape($q); + if (!identity::active_user()->admin) { foreach (identity::group_ids_for_active_user() as $id) { $fields[] = "`view_$id` = TRUE"; // access::ALLOW @@ -58,13 +64,23 @@ class search_Core { $access_sql = ""; } + if ($album->id == item::root()->id) { + $album_sql = ""; + } else { + $album_sql = + " AND {items}.left_ptr > " .$db->escape($album->left_ptr) . + " AND {items}.right_ptr <= " . $db->escape($album->right_ptr); + } + 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) " . + $album_sql . (empty($where) ? "" : " AND " . join(" AND ", $where)) . - $access_sql; + $access_sql . + " "; } /** @@ -111,8 +127,12 @@ class search_Core { } static function get_position($item, $q) { + return search::get_position_within_album($item, $q, item::root()); + } + + static function get_position_within_album($item, $q, $album) { $page_size = module::get_var("gallery", "page_size", 9); - $query = self::_build_query_base($q, array("{items}.id = " . $item->id)); + $query = self::_build_query_base($q, $album, array("{items}.id = " . $item->id)); $db = Database::instance(); // Truncate the score by two decimal places as this resolves the issues @@ -129,7 +149,7 @@ class search_Core { $score = substr($score, 0, strlen($score) - 2); } - $data = $db->query(self::_build_query_base($q) . " HAVING `score` >= " . $score); + $data = $db->query(self::_build_query_base($q, $album) . " HAVING `score` >= " . $score); $data->seek($data->count() - 1); while ($data->get("id") != $item->id && $data->prev()->valid()) { diff --git a/modules/search/views/search.html.php b/modules/search/views/search.html.php index 4279cbab..f1906744 100644 --- a/modules/search/views/search.html.php +++ b/modules/search/views/search.html.php @@ -8,6 +8,7 @@