Bug 11944: use CGI( -utf8 ) everywhere
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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->param('marclist'));
63     my @and_or = ($query->param('and_or'));
64     my @excluding = ($query->param('excluding'),);
65     my @operator = ($query->param('operator'));
66     my $orderby = $query->param('orderby');
67     my @value = ($query->param('value') || "",);
68
69     $resultsperpage = $query->param('resultsperpage');
70     $resultsperpage = 20 if ( !defined $resultsperpage );
71     my @tags;
72     my ( $results, $total, @fields ) =
73       SearchAuthorities( \@marclist, \@and_or, \@excluding, \@operator,
74         \@value, $startfrom * $resultsperpage,
75         $resultsperpage, $authtypecode, $orderby );
76     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
77         {
78             template_name   => "opac-authoritiessearchresultlist.tt",
79             query           => $query,
80             type            => 'opac',
81             authnotrequired => 1,
82             debug           => 1,
83         }
84     );
85
86     # multi page display gestion
87     my $displaynext = 0;
88     my $displayprev = $startfrom;
89     $total ||= 0;
90     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
91         $displaynext = 1;
92     }
93
94     my @field_data = (
95         { term => "marclist",  val => $marclist[0] },
96         { term => "and_or",    val => $and_or[0] },
97         { term => "excluding", val => $excluding[0] },
98         { term => "operator",  val => $operator[0] },
99         { term => "value",     val => $value[0] },
100     );
101
102     my @numbers = ();
103
104     if ( $total > $resultsperpage ) {
105         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
106             if ( $i < 16 ) {
107                 my $highlight = 0;
108                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
109                 push @numbers,
110                   {
111                     number     => $i,
112                     highlight  => $highlight,
113                     searchdata => \@field_data,
114                     startfrom  => ( $i - 1 )
115                   };
116             }
117         }
118     }
119
120     my $from = $startfrom * $resultsperpage + 1;
121     my $to;
122
123     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
124         $to = $total;
125     }
126     else {
127         $to = ( ( $startfrom + 1 ) * $resultsperpage );
128     }
129     unless (C4::Context->preference('OPACShowUnusedAuthorities')) {
130         my @usedauths = grep { $_->{used} > 0 } @$results;
131         $results = \@usedauths;
132     }
133
134     # Opac search history
135     if (C4::Context->preference('EnableOpacSearchHistory')) {
136         unless ( $startfrom ) {
137             my $path_info = $query->url(-path_info=>1);
138             my $query_cgi_history = $query->url(-query=>1);
139             $query_cgi_history =~ s/^$path_info\?//;
140             $query_cgi_history =~ s/;/&/g;
141
142             unless ( $loggedinuser ) {
143                 my $new_search = C4::Search::History::add_to_session({
144                         cgi => $query,
145                         query_desc => $value[0],
146                         query_cgi => $query_cgi_history,
147                         total => $total,
148                         type => "authority",
149                 });
150             } else {
151                 # To the session (the user is logged in)
152                 C4::Search::History::add({
153                     userid => $loggedinuser,
154                     sessionid => $query->cookie("CGISESSID"),
155                     query_desc => $value[0],
156                     query_cgi => $query_cgi_history,
157                     total => $total,
158                     type => "authority",
159                 });
160             }
161         }
162     }
163
164     $template->param( result => $results ) if $results;
165     $template->param( FIELDS => \@fields );
166     $template->param( orderby => $orderby );
167     $template->param(
168         startfrom      => $startfrom,
169         displaynext    => $displaynext,
170         displayprev    => $displayprev,
171         resultsperpage => $resultsperpage,
172         startfromnext  => $startfrom + 1,
173         startfromprev  => $startfrom - 1,
174         searchdata     => \@field_data,
175         countfuzzy     => !(C4::Context->preference('OPACShowUnusedAuthorities')),
176         total          => $total,
177         from           => $from,
178         to             => $to,
179         resultcount    => scalar @$results,
180         numbers        => \@numbers,
181         authtypecode   => $authtypecode,
182         authtypetext   => $authtypes->{$authtypecode}{'authtypetext'},
183         isEDITORS      => $authtypecode eq 'EDITORS',
184     );
185
186 }
187 else {
188     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
189         {
190             template_name   => "opac-authorities-home.tt",
191             query           => $query,
192             type            => 'opac',
193             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
194             debug           => 1,
195         }
196     );
197
198 }
199
200 $template->param( authtypesloop => \@authtypesloop );
201
202 # Print the page
203 output_html_with_http_headers $query, $cookie, $template->output;
204
205 # Local Variables:
206 # tab-width: 4
207 # End: