Bug 2735: Fix authority search pagination at the OPAC
[koha.git] / opac / opac-authorities-home.pl
1 #!/usr/bin/perl
2 # WARNING: 4-character tab stops here
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use strict;
22 use warnings;
23
24 use CGI qw ( -utf8 );
25 use URI::Escape;
26 use C4::Auth;
27
28 use C4::Context;
29 use C4::Auth;
30 use C4::Output;
31 use C4::AuthoritiesMarc;
32 use C4::Koha;
33 use C4::Search::History;
34
35 use Koha::Authority::Types;
36 use Koha::SearchEngine::Search;
37 use Koha::SearchEngine::QueryBuilder;
38
39 my $query        = new CGI;
40 my $op           = $query->param('op') || '';
41 my $authtypecode = $query->param('authtypecode') || '';
42 my $dbh          = C4::Context->dbh;
43
44 my $startfrom = $query->param('startfrom') || 1;
45 my $resultsperpage = $query->param('resultsperpage') || 20;
46 my $authid    = $query->param('authid');
47 my ( $template, $loggedinuser, $cookie );
48
49 my $authority_types = Koha::Authority::Types->search({}, { order_by => ['authtypetext']});
50
51 if ( $op eq "do_search" ) {
52     my @marclist = $query->multi_param('marclist');
53     my @and_or = $query->multi_param('and_or');
54     my @excluding = $query->multi_param('excluding');
55     my @operator = $query->multi_param('operator');
56     my $orderby = $query->param('orderby');
57     my @value = $query->multi_param('value');
58     $value[0] ||= q||;
59
60     my @tags;
61     my $builder = Koha::SearchEngine::QueryBuilder->new(
62         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
63     my $searcher = Koha::SearchEngine::Search->new(
64         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
65     my $search_query = $builder->build_authorities_query_compat( \@marclist, \@and_or,
66         \@excluding, \@operator, \@value, $authtypecode, $orderby );
67     my ( $results, $total ) =
68       $searcher->search_auth_compat( $search_query, $startfrom, $resultsperpage );
69     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
70         {
71             template_name   => "opac-authoritiessearchresultlist.tt",
72             query           => $query,
73             type            => 'opac',
74             authnotrequired => 1,
75             debug           => 1,
76         }
77     );
78
79     # multi page display gestion
80     my $value_url = uri_escape_utf8($value[0]);
81     my $base_url = "opac-authorities-home.pl?"
82       ."marclist=$marclist[0]"
83       ."&amp;and_or=$and_or[0]"
84       ."&amp;excluding=$excluding[0]"
85       ."&amp;operator=$operator[0]"
86       ."&amp;value=$value_url"
87       ."&amp;resultsperpage=$resultsperpage"
88       ."&amp;type=opac"
89       ."&amp;op=do_search"
90       ."&amp;authtypecode=$authtypecode"
91       ."&amp;orderby=$orderby";
92
93     my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
94     my $to;
95     if ( !defined $total ) {
96         $total = 0;
97     }
98
99     if ( $total < $startfrom * $resultsperpage ) {
100         $to = $total;
101     }
102     else {
103         $to = $startfrom * $resultsperpage;
104     }
105
106     $template->param( result => $results ) if $results;
107
108     $template->param(
109         pagination_bar => pagination_bar(
110             $base_url,  int( $total / $resultsperpage ) + 1,
111             $startfrom, 'startfrom'
112         ),
113         total     => $total,
114         from      => $from,
115         to        => $to,
116     );
117
118     unless (C4::Context->preference('OPACShowUnusedAuthorities')) {
119 #        TODO implement usage counts
120 #        my @usedauths = grep { $_->{used} > 0 } @$results;
121 #        $results = \@usedauths;
122     }
123
124     # Opac search history
125     if (C4::Context->preference('EnableOpacSearchHistory')) {
126         unless ( $startfrom ) {
127             my $path_info = $query->url(-path_info=>1);
128             my $query_cgi_history = $query->url(-query=>1);
129             $query_cgi_history =~ s/^$path_info\?//;
130             $query_cgi_history =~ s/;/&/g;
131
132             unless ( $loggedinuser ) {
133                 my $new_search = C4::Search::History::add_to_session({
134                         cgi => $query,
135                         query_desc => $value[0],
136                         query_cgi => $query_cgi_history,
137                         total => $total,
138                         type => "authority",
139                 });
140             } else {
141                 # To the session (the user is logged in)
142                 C4::Search::History::add({
143                     userid => $loggedinuser,
144                     sessionid => $query->cookie("CGISESSID"),
145                     query_desc => $value[0],
146                     query_cgi => $query_cgi_history,
147                     total => $total,
148                     type => "authority",
149                 });
150             }
151         }
152     }
153
154     $template->param( orderby => $orderby );
155     $template->param(
156         startfrom      => $startfrom,
157         resultsperpage => $resultsperpage,
158         countfuzzy     => !(C4::Context->preference('OPACShowUnusedAuthorities')),
159         resultcount    => scalar @$results,
160         authtypecode   => $authtypecode,
161         authtypetext   => $authority_types->find($authtypecode)->authtypetext,
162         isEDITORS      => $authtypecode eq 'EDITORS',
163     );
164
165 }
166 else {
167     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
168         {
169             template_name   => "opac-authorities-home.tt",
170             query           => $query,
171             type            => 'opac',
172             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
173             debug           => 1,
174         }
175     );
176
177 }
178
179 $template->param(
180     authority_types => $authority_types,
181     authtypecode    => $authtypecode,
182 );
183
184 # Print the page
185 output_html_with_http_headers $query, $cookie, $template->output;
186
187 # Local Variables:
188 # tab-width: 4
189 # End: