Skip to content

Commit

Permalink
Merge builds 261 and 262 from origin.
Browse files Browse the repository at this point in the history
  • Loading branch information
shadlaws committed Dec 16, 2012
2 parents 94b26e5 + 989c24e commit 768e6fa
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .build_number
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
; process. You don't need to edit it. In fact..
;
; DO NOT EDIT THIS FILE BY HAND!
build_number=260
build_number=262
35 changes: 26 additions & 9 deletions modules/search/controllers/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,21 @@ public function index() {
$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")));
}
}

Expand All @@ -42,7 +51,8 @@ public function index() {

$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));

Expand All @@ -61,28 +71,35 @@ public function index() {
"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,
Expand Down
34 changes: 27 additions & 7 deletions modules/search/helpers/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ static function add_query_terms($q) {
}

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;

Expand All @@ -47,8 +51,10 @@ static function search($q, $limit, $offset) {
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
Expand All @@ -58,13 +64,23 @@ private static function _build_query_base($q, $where=array()) {
$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 .
" ";
}

/**
Expand Down Expand Up @@ -111,8 +127,12 @@ static function stats() {
}

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
Expand All @@ -124,12 +144,12 @@ static function get_position($item, $q) {
item::clear_display_context_callback();
url::redirect(url::current());
}
$score = $current->score();
$score = $current->score;
if (strlen($score) > 7) {
$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()) {
Expand Down
12 changes: 12 additions & 0 deletions modules/search/views/search.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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): ?>
Expand Down
7 changes: 7 additions & 0 deletions modules/search/views/search_link.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>

0 comments on commit 768e6fa

Please sign in to comment.