From ed469269694c15f7df837be2ae0bf5fda08907d1 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Mon, 7 Oct 2019 14:20:32 +0000 Subject: [PATCH] Bug 23763: Make search on staff and opac use new routine To test: 1 - Apply patch 2 - Search on staff and opac side 3 - Confirm you can paginate results 4 - Confirm you see 10 pages on pagination if under page 10 5 - Confirm you see 20 pages in pagination if over page 10 6 - Confirm you can go to first or last page --- catalogue/search.pl | 68 ++++++----------------------------------- opac/opac-search.pl | 73 ++++++++------------------------------------ t/Koha/SearchEngine/Search.t | 2 +- 3 files changed, 24 insertions(+), 119 deletions(-) diff --git a/catalogue/search.pl b/catalogue/search.pl index d354a40eeb..ab11d2d233 100755 --- a/catalogue/search.pl +++ b/catalogue/search.pl @@ -598,66 +598,18 @@ for (my $i=0;$i<@servers;$i++) { $line->{'incart'} = 1; } } - $template->param(SEARCH_RESULTS => \@newresults); - ## FIXME: add a global function for this, it's better than the current global one - ## Build the page numbers on the bottom of the page - my @page_numbers; - my $max_result_window = $searcher->max_result_window; - my $hits_to_paginate = ($max_result_window && $max_result_window < $hits) ? $max_result_window : $hits; + my( $page_numbers, $hits_to_paginate, $pages, $current_page_number, $previous_page_offset, $next_page_offset, $last_page_offset ) = + Koha::SearchEngine::Search->pagination_bar({ + hits => $hits, + max_result_window => $searcher->max_result_window, + results_per_page => $results_per_page, + offset => $offset, + sort_by => \@sort_by + }); $template->param( hits_to_paginate => $hits_to_paginate ); - # total number of pages there will be - my $pages = ceil($hits_to_paginate / $results_per_page); - my $last_page_offset = ( $pages -1 ) * $results_per_page; - # default page number - my $current_page_number = 1; - $current_page_number = ($offset / $results_per_page + 1) if $offset; - my $previous_page_offset; - if ( $offset >= $results_per_page ) { - $previous_page_offset = $offset - $results_per_page; - } - my $next_page_offset = $offset + $results_per_page; - # If we're within the first 10 pages, keep it simple - #warn "current page:".$current_page_number; - if ($current_page_number < 10) { - # just show the first 10 pages - # Loop through the pages - my $pages_to_show = 10; - $pages_to_show = $pages if $pages<10; - for (my $i=1; $i<=$pages_to_show;$i++) { - # the offset for this page - my $this_offset = (($i*$results_per_page)-$results_per_page); - # the page number for this page - my $this_page_number = $i; - # put it in the array - push @page_numbers, - { offset => $this_offset, - pg => $this_page_number, - # it should only be highlighted if it's the current page - highlight => $this_page_number == $current_page_number, - sort_by => join ' ', @sort_by - }; - - } - - } - - # now, show twenty pages, with the current one smack in the middle - else { - for (my $i=$current_page_number; $i<=($current_page_number + 20 );$i++) { - my $this_offset = ((($i-9)*$results_per_page)-$results_per_page); - my $this_page_number = $i-9; - if ( $this_page_number <= $pages ) { - push @page_numbers, - { offset => $this_offset, - pg => $this_page_number, - highlight => $this_page_number == $current_page_number, - sort_by => join ' ', @sort_by - }; - } - } - } + $template->param(SEARCH_RESULTS => \@newresults); # FIXME: no previous_page_offset when pages < 2 - $template->param( PAGE_NUMBERS => \@page_numbers, + $template->param( PAGE_NUMBERS => $page_numbers, last_page_offset => $last_page_offset, previous_page_offset => $previous_page_offset) unless $pages < 2; $template->param( next_page_offset => $next_page_offset) unless $pages eq $current_page_number; diff --git a/opac/opac-search.pl b/opac/opac-search.pl index 2d1ac14e0a..821dfa8d68 100755 --- a/opac/opac-search.pl +++ b/opac/opac-search.pl @@ -875,68 +875,21 @@ for (my $i=0;$i<@servers;$i++) { OPACItemsResultsDisplay => (C4::Context->preference("OPACItemsResultsDisplay")), suppress_result_number => $hide, ); - if (C4::Context->preference("OPACLocalCoverImages")){ - $template->param(OPACLocalCoverImages => 1); - $template->param(OPACLocalCoverImagesPriority => C4::Context->preference("OPACLocalCoverImagesPriority")); - } + if (C4::Context->preference("OPACLocalCoverImages")){ + $template->param(OPACLocalCoverImages => 1); + $template->param(OPACLocalCoverImagesPriority => C4::Context->preference("OPACLocalCoverImagesPriority")); + } ## Build the page numbers on the bottom of the page - my @page_numbers; - my $max_result_window = $searcher->max_result_window; - my $hits_to_paginate = ($max_result_window && $max_result_window < $hits) ? $max_result_window : $hits; + my ( $page_numbers, $hits_to_paginate, $pages, $current_page_number, $previous_page_offset, $next_page_offset, $last_page_offset ) = + Koha::SearchEngine::Search->pagination_bar({ + hits => $hits, + max_result_window => $searcher->max_result_window, + results_per_page => $results_per_page, + offset => $offset, + sort_by => \@sort_by + }); $template->param( hits_to_paginate => $hits_to_paginate ); - # total number of pages there will be - my $pages = ceil($hits_to_paginate / $results_per_page); - my $last_page_offset = ( $pages - 1 ) * $results_per_page; - # default page number - my $current_page_number = 1; - if ($offset) { - $current_page_number = ( $offset / $results_per_page + 1 ); - } - my $previous_page_offset; - if ( $offset >= $results_per_page ) { - $previous_page_offset = $offset - $results_per_page; - } - my $next_page_offset = $offset + $results_per_page; - # If we're within the first 10 pages, keep it simple - #warn "current page:".$current_page_number; - if ($current_page_number < 10) { - # just show the first 10 pages - # Loop through the pages - my $pages_to_show = 10; - $pages_to_show = $pages if $pages<10; - for ($i=1; $i<=$pages_to_show;$i++) { - # the offset for this page - my $this_offset = (($i*$results_per_page)-$results_per_page); - # the page number for this page - my $this_page_number = $i; - # put it in the array - push @page_numbers, - { offset => $this_offset, - pg => $this_page_number, - highlight => $this_page_number == $current_page_number, - sort_by => join ' ', @sort_by - }; - - } - - } - # now, show twenty pages, with the current one smack in the middle - else { - for ($i=$current_page_number; $i<=($current_page_number + 20 );$i++) { - my $this_offset = ((($i-9)*$results_per_page)-$results_per_page); - my $this_page_number = $i-9; - if ( $this_page_number <= $pages ) { - push @page_numbers, - { offset => $this_offset, - pg => $this_page_number, - highlight => $this_page_number == $current_page_number, - sort_by => join ' ', @sort_by - }; - } - } - - } - $template->param( PAGE_NUMBERS => \@page_numbers, + $template->param( PAGE_NUMBERS => $page_numbers, last_page_offset => $last_page_offset, previous_page_offset => $previous_page_offset) unless $pages < 2; $template->param(next_page_offset => $next_page_offset) unless $pages eq $current_page_number; diff --git a/t/Koha/SearchEngine/Search.t b/t/Koha/SearchEngine/Search.t index dc1a82a342..da45840d6b 100644 --- a/t/Koha/SearchEngine/Search.t +++ b/t/Koha/SearchEngine/Search.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 1; use Test::Exception; use t::lib::Mocks; -- 2.11.0