diff options
author | Bharat Mediratta <bharat@menalto.com> | 2012-12-14 09:46:53 -0800 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2012-12-14 09:46:53 -0800 |
commit | 5905dc554ae79c6249768d323d73e5acf74916ef (patch) | |
tree | ec89c8645b7d40d27dab7b7d0e8ef6f96a73820a | |
parent | b0618aab7a72eb0e5495e3c5f4d2a57f343c0076 (diff) | |
parent | f5420b4eab76b9846528dfee507cacce93a29ecb (diff) |
Merge pull request #76 from chilversc/search-filter-album
Search within current album. Resolves #1402
-rw-r--r-- | modules/search/controllers/search.php | 35 | ||||
-rw-r--r-- | modules/search/helpers/search.php | 32 | ||||
-rw-r--r-- | modules/search/views/search.html.php | 12 | ||||
-rw-r--r-- | modules/search/views/search_link.html.php | 7 |
4 files changed, 71 insertions, 15 deletions
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 d4444158..32b36e73 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 @@ <ul> <li> <label for="q"><?= t("Search the gallery") ?></label> + <input name="album" type="hidden" value="<?= html::clean_attribute($album->id) ?>" /> <input name="q" id="q" type="text" value="<?= html::clean_attribute($q) ?>" class="text" /> </li> <li> @@ -20,6 +21,17 @@ <div id="g-search-results"> <h1><?= t("Search results") ?></h1> + <? if ($album->id == item::root()->id): ?> + <div> + <?= t("Searched the whole gallery.") ?> + </div> + <? else: ?> + <div> + <?= t("Searched within album <b>%album</b>.", array("album" => html::purify($album->title))) ?> + <a href="<?= url::site(url::merge(array("album" => item::root()->id))) ?>"><?= t("Search whole gallery") ?></a> + </div> + <? endif; ?> + <? if (count($items)): ?> <ul id="g-album-grid" class="ui-helper-clearfix"> <? foreach ($items as $item): ?> diff --git a/modules/search/views/search_link.html.php b/modules/search/views/search_link.html.php index dd3a76a4..b2eacd4a 100644 --- a/modules/search/views/search_link.html.php +++ b/modules/search/views/search_link.html.php @@ -9,4 +9,11 @@ <input type="submit" value="<?= t("Go")->for_html_attr() ?>" class="submit" /> </li> </ul> + <? if (isset($item) && $item instanceof Item_Model_Core): ?> + <? if ($item->is_album ()): ?> + <input type="hidden" name="album" value="<?= $item->id ?>" /> + <? else: ?> + <input type="hidden" name="album" value="<?= $item->parent_id ?>" /> + <? endif; ?> + <? endif; ?> </form> |