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