Bug 14076: Do not use CGI->param in list context - opac-authorities-home.pl
[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 C4::Auth;
26
27 use C4::Context;
28 use C4::Auth;
29 use C4::Output;
30 use C4::AuthoritiesMarc;
31 use C4::Koha;    # XXX subfield_is_koha_internal_p
32 use C4::Search::History;
33
34 my $query        = new CGI;
35 my $op           = $query->param('op') || '';
36 my $authtypecode = $query->param('authtypecode') || '';
37 my $dbh          = C4::Context->dbh;
38
39 my $startfrom = $query->param('startfrom');
40 my $authid    = $query->param('authid');
41 $startfrom = 0 if ( !defined $startfrom );
42 my ( $template, $loggedinuser, $cookie );
43 my $resultsperpage;
44
45 my $authtypes     = getauthtypes();
46 my @authtypesloop = ();
47 foreach my $thisauthtype (
48     sort {
49         $authtypes->{$a}->{'authtypetext'}
50           cmp $authtypes->{$b}->{'authtypetext'}
51     }
52     keys %{$authtypes}
53   ) {
54     push @authtypesloop,
55       { value        => $thisauthtype,
56         selected     => $thisauthtype eq $authtypecode,
57         authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
58       };
59 }
60
61 if ( $op eq "do_search" ) {
62     my @marclist = $query->multi_param('marclist');
63     my @and_or = $query->multi_param('and_or');
64     my @excluding = $query->multi_param('excluding');
65     my @operator = $query->multi_param('operator');
66     my $orderby = $query->param('orderby');
67     my @value = $query->multi_param('value');
68     $value[0] ||= q||;
69
70     $resultsperpage = $query->param('resultsperpage');
71     $resultsperpage = 20 if ( !defined $resultsperpage );
72     my @tags;
73     my ( $results, $total, @fields ) =
74       SearchAuthorities( \@marclist, \@and_or, \@excluding, \@operator,
75         \@value, $startfrom * $resultsperpage,
76         $resultsperpage, $authtypecode, $orderby );
77     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
78         {
79             template_name   => "opac-authoritiessearchresultlist.tt",
80             query           => $query,
81             type            => 'opac',
82             authnotrequired => 1,
83             debug           => 1,
84         }
85     );
86
87     # multi page display gestion
88     my $displaynext = 0;
89     my $displayprev = $startfrom;
90     $total ||= 0;
91     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
92         $displaynext = 1;
93     }
94
95     my @field_data = (
96         { term => "marclist",  val => $marclist[0] },
97         { term => "and_or",    val => $and_or[0] },
98         { term => "excluding", val => $excluding[0] },
99         { term => "operator",  val => $operator[0] },
100         { term => "value",     val => $value[0] },
101     );
102
103     my @numbers = ();
104
105     if ( $total > $resultsperpage ) {
106         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
107             if ( $i < 16 ) {
108                 my $highlight = 0;
109                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
110                 push @numbers,
111                   {
112                     number     => $i,
113                     highlight  => $highlight,
114                     searchdata => \@field_data,
115                     startfrom  => ( $i - 1 )
116                   };
117             }
118         }
119     }
120
121     my $from = $startfrom * $resultsperpage + 1;
122     my $to;
123
124     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
125         $to = $total;
126     }
127     else {
128         $to = ( ( $startfrom + 1 ) * $resultsperpage );
129     }
130     unless (C4::Context->preference('OPACShowUnusedAuthorities')) {
131         my @usedauths = grep { $_->{used} > 0 } @$results;
132         $results = \@usedauths;
133     }
134
135     # Opac search history
136     if (C4::Context->preference('EnableOpacSearchHistory')) {
137         unless ( $startfrom ) {
138             my $path_info = $query->url(-path_info=>1);
139             my $query_cgi_history = $query->url(-query=>1);
140             $query_cgi_history =~ s/^$path_info\?//;
141             $query_cgi_history =~ s/;/&/g;
142
143             unless ( $loggedinuser ) {
144                 my $new_search = C4::Search::History::add_to_session({
145                         cgi => $query,
146                         query_desc => $value[0],
147                         query_cgi => $query_cgi_history,
148                         total => $total,
149                         type => "authority",
150                 });
151             } else {
152                 # To the session (the user is logged in)
153                 C4::Search::History::add({
154                     userid => $loggedinuser,
155                     sessionid => $query->cookie("CGISESSID"),
156                     query_desc => $value[0],
157                     query_cgi => $query_cgi_history,
158                     total => $total,
159                     type => "authority",
160                 });
161             }
162         }
163     }
164
165     $template->param( result => $results ) if $results;
166     $template->param( FIELDS => \@fields );
167     $template->param( orderby => $orderby );
168     $template->param(
169         startfrom      => $startfrom,
170         displaynext    => $displaynext,
171         displayprev    => $displayprev,
172         resultsperpage => $resultsperpage,
173         startfromnext  => $startfrom + 1,
174         startfromprev  => $startfrom - 1,
175         searchdata     => \@field_data,
176         countfuzzy     => !(C4::Context->preference('OPACShowUnusedAuthorities')),
177         total          => $total,
178         from           => $from,
179         to             => $to,
180         resultcount    => scalar @$results,
181         numbers        => \@numbers,
182         authtypecode   => $authtypecode,
183         authtypetext   => $authtypes->{$authtypecode}{'authtypetext'},
184         isEDITORS      => $authtypecode eq 'EDITORS',
185     );
186
187 }
188 else {
189     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
190         {
191             template_name   => "opac-authorities-home.tt",
192             query           => $query,
193             type            => 'opac',
194             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
195             debug           => 1,
196         }
197     );
198
199 }
200
201 $template->param( authtypesloop => \@authtypesloop );
202
203 # Print the page
204 output_html_with_http_headers $query, $cookie, $template->output;
205
206 # Local Variables:
207 # tab-width: 4
208 # End: