diff options
Diffstat (limited to 'modules/search')
| -rw-r--r-- | modules/search/controllers/search.php | 21 | ||||
| -rw-r--r-- | modules/search/helpers/search.php | 45 | ||||
| -rw-r--r-- | modules/search/libraries/Search_Display_Context.php | 52 | 
3 files changed, 108 insertions, 10 deletions
| diff --git a/modules/search/controllers/search.php b/modules/search/controllers/search.php index 5db63ab0..1d5a55bf 100644 --- a/modules/search/controllers/search.php +++ b/modules/search/controllers/search.php @@ -21,6 +21,18 @@ class Search_Controller extends Controller {    public function index() {      $page_size = module::get_var("gallery", "page_size", 9);      $q = Input::instance()->get("q"); +    $q_with_more_terms = search::add_query_terms($q); +    $show = Input::instance()->get("show"); + +    if ($show) { +      $child = ORM::factory("item", $show); +      $index = search::get_position($child, $q_with_more_terms); +      if ($index) { +        $page = ceil($index / $page_size); +        url::redirect( url::abs_site("search?q=" . urlencode($q) . ($page == 1 ? "" : "&page=$page"))); +      } +    } +      $page = Input::instance()->get("page", 1);      // Make sure that the page references a valid offset @@ -30,11 +42,18 @@ class Search_Controller extends Controller {      $offset = ($page - 1) * $page_size; -    $q_with_more_terms = search::add_query_terms($q);      list ($count, $result) = search::search($q_with_more_terms, $page_size, $offset); +    $title = t("Search: %q", array("q" => $q_with_more_terms)); +      $max_pages = max(ceil($count / $page_size), 1); +    Display_Context::factory("search") +      ->set(array("title" => $title, +                  "query_terms" => $q_with_more_terms, +                  "q" => $q)) +      ->save(); +      $template = new Theme_View("page.html", "collection", "search");      $root = item::root();      $template->set_global( 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; +  }  } diff --git a/modules/search/libraries/Search_Display_Context.php b/modules/search/libraries/Search_Display_Context.php new file mode 100644 index 00000000..aed3a125 --- /dev/null +++ b/modules/search/libraries/Search_Display_Context.php @@ -0,0 +1,52 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2011 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 Search_Display_Context_Core extends Display_Context { +  protected function __construct() { +    parent::__construct("search"); +  } + +  function display_context($item) { +    $position = search::get_position($item, $this->get("query_terms")); + +    if ($position > 1) { +      list ($count, $result_data) = +        search::search($this->get("query_terms"), 3, $position - 2); +      list ($previous_item, $ignore, $next_item) = $result_data; +    } else { +      $previous_item = null; +      list ($count, $result_data) = search::search($this->get("query_terms"), 1, $position); +      list ($next_item) = $result_data; +    } + +    $q = $this->get("q"); +    $search_url = url::abs_site("search?q=" . urlencode($q) . "&show={$item->id}"); +    $root = item::root(); + +    return array("position" =>$position, +                 "previous_item" => $previous_item, +                 "next_item" =>$next_item, +                 "sibling_count" => $count, +                 "breadcrumbs" => array( +                                  Breadcrumb::instance($root->title, "/", $root->id), +                                  Breadcrumb::instance($q, $search_url), +                                  Breadcrumb::instance($item->title, $item->url()))); +  } +} | 
