View | Details | Raw Unified | Return to bug 23763
Collapse All | Expand All

(-)a/Koha/SearchEngine/Search.pm (+79 lines)
Lines 46-51 Creates a new C<Search> of whatever the relevant type is. Link Here
46
use Modern::Perl;
46
use Modern::Perl;
47
use C4::Context;
47
use C4::Context;
48
use C4::Biblio qw//;
48
use C4::Biblio qw//;
49
use POSIX qw(ceil floor);
49
50
50
sub new {
51
sub new {
51
    my $engine = C4::Context->preference("SearchEngine") // 'Zebra';
52
    my $engine = C4::Context->preference("SearchEngine") // 'Zebra';
Lines 76-79 sub extract_biblionumber { Link Here
76
    return $record->subfield( $biblionumbertagfield, $biblionumbertagsubfield );
77
    return $record->subfield( $biblionumbertagfield, $biblionumbertagsubfield );
77
}
78
}
78
79
80
=head2 pagination_bar
81
82
   my (@PAGE_NUMBERS, $hits_to_paginate, $pages, $current_page_number, $previous_page_offset, $next_page_offset, $last_page_offset) = 
83
       Koha::SearchEngine::Search->pagination_bar({
84
           hits => $hits,
85
           max_result_window => $max_result_window,
86
           results_per_page => $results_per_page,
87
           offset => $offset,
88
           sort_by => \@sort_by
89
       });
90
91
Returns the variables needed for the page-nubers.inc to build search results
92
93
=cut
94
95
sub pagination_bar{
96
    my ($self, $params) = @_;
97
    my $hits             = $params->{hits};
98
    my $results_per_page = $params->{results_per_page};
99
    my $offset           = $params->{offset};
100
    my $sort_by          = $params->{sort_by};
101
    my @page_numbers;
102
    my $max_result_window = $params->{max_result_window};
103
    my $hits_to_paginate = ($max_result_window && $max_result_window < $hits) ? $max_result_window : $hits;
104
    # total number of pages there will be
105
    my $pages = ceil($hits_to_paginate / $results_per_page);
106
    my $last_page_offset = ( $pages -1 ) * $results_per_page;
107
    # default page number
108
    my $current_page_number = 1;
109
    $current_page_number = ($offset / $results_per_page + 1) if $offset;
110
    my $previous_page_offset;
111
    if ( $offset >= $results_per_page ) {
112
        $previous_page_offset = $offset - $results_per_page;
113
    }
114
    my $next_page_offset = $offset + $results_per_page;
115
    # If we're within the first 10 pages, keep it simple
116
    if ($current_page_number < 10) {
117
        # just show the first 10 pages
118
        # Loop through the pages
119
        my $pages_to_show = 10;
120
        $pages_to_show = $pages if $pages<10;
121
        for (my $i=1; $i<=$pages_to_show;$i++) {
122
            # the offset for this page
123
            my $this_offset = (($i*$results_per_page)-$results_per_page);
124
            # the page number for this page
125
            my $this_page_number = $i;
126
            # put it in the array
127
            push @page_numbers,
128
              { offset    => $this_offset,
129
                pg        => $this_page_number,
130
                # it should only be highlighted if it's the current page
131
                highlight => $this_page_number == $current_page_number,
132
                sort_by   => join ' ', @$sort_by
133
              };
134
        }
135
    }
136
137
    # now, show up to twenty pages, with the current one smack in the middle
138
    # near the end of search results we will show 10 below and as many remaining above
139
    else {
140
        for (my $i=$current_page_number; $i<=($current_page_number + 19 );$i++) {
141
            my $this_offset = ((($i-9)*$results_per_page)-$results_per_page);
142
            my $this_page_number = $i-9;
143
            if ( $this_page_number <= $pages ) {
144
                push @page_numbers,
145
                  { offset    => $this_offset,
146
                    pg        => $this_page_number,
147
                    highlight => $this_page_number == $current_page_number,
148
                    sort_by   => join ' ', @$sort_by
149
                  };
150
            }
151
        }
152
    }
153
154
    return (\@page_numbers, $hits_to_paginate, $pages, $current_page_number, $previous_page_offset, $next_page_offset, $last_page_offset);
155
156
}
157
79
1;
158
1;
(-)a/t/Koha/SearchEngine/Search.t (-1 / +70 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
#
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 5;
21
use Test::Exception;
22
23
use t::lib::Mocks;
24
25
use Test::MockModule;
26
27
use MARC::Record;
28
use Try::Tiny;
29
30
use Koha::SearchEngine::Search;
31
32
subtest "pagination_bar tests" => sub {
33
    plan tests=> 14;
34
35
   my @sort_by = ('relevance_dsc');
36
37
   my ($PAGE_NUMBERS, $hits_to_paginate, $pages, $current_page_number, $previous_page_offset, $next_page_offset, $last_page_offset) = 
38
       Koha::SearchEngine::Search->pagination_bar({
39
           hits => 500,
40
           max_result_window => 1000,
41
           results_per_page => 20,
42
           offset => 160,
43
           sort_by => \@sort_by
44
       });
45
   is ($hits_to_paginate, 500, "We paginate all hits if less than max_result_window");
46
   is ($pages, 25, "We have hits/hits_to_paginate pages");
47
   is ($current_page_number, 9, "We calculate current page by offset/results_per_page plus 1");
48
   is ($previous_page_offset, 140, "Previous page is current offset minus reults per page");
49
   is ($next_page_offset, 180, "Next page is current offset plus reults per page");
50
   is ($last_page_offset, 480, "Last page is pages minus 1 times reults per page");
51
   is (@$PAGE_NUMBERS, 10, "If on first ten pages we only show 10 pages");
52
53
   ($PAGE_NUMBERS, $hits_to_paginate, $pages, $current_page_number, $previous_page_offset, $next_page_offset, $last_page_offset) = 
54
       Koha::SearchEngine::Search->pagination_bar({
55
           hits => 500,
56
           max_result_window => 480,
57
           results_per_page => 20,
58
           offset => 240,
59
           sort_by => \@sort_by
60
       });
61
   is ($hits_to_paginate, 480, "We paginate all hits if less than max_result_window");
62
   is ($pages, 24, "We have hits/hits_to_paginate pages");
63
   is ($current_page_number, 13, "We calculate current page by offset/results_per_page plus 1");
64
   is ($previous_page_offset, 220, "Previous page is current offset minus reults per page");
65
   is ($next_page_offset, 260, "Next page is current offset plus reults per page");
66
   is ($last_page_offset, 460, "Last page is pages minus 1 times reults per page");
67
   is (@$PAGE_NUMBERS, 20, "If past first ten pages we show 20 pages");
68
69
};
70

Return to bug 23763