Bug 35291: Don't allow symlinks for link files in cover image ZIP
[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 @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 $builder = Koha::SearchEngine::QueryBuilder->new(
59         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
60     my $searcher = Koha::SearchEngine::Search->new(
61         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
62     my $search_query = $builder->build_authorities_query_compat( \@marclist, \@and_or,
63         \@excluding, \@operator, \@value, $authtypecode, $orderby );
64     my $offset = ( $startfrom - 1 ) * $resultsperpage + 1;
65     my ( $results, $total ) =
66       $searcher->search_auth_compat( $search_query, $offset, $resultsperpage );
67     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
68         {
69             template_name   => "opac-authoritiessearchresultlist.tt",
70             query           => $query,
71             type            => 'opac',
72             authnotrequired => 1,
73         }
74     );
75     $template->param( search_query => $search_query ) if C4::Context->preference('DumpSearchQueryTemplate');
76
77     # multi page display gestion
78     my $value_url = uri_escape_utf8($value[0]);
79     my $base_url = "opac-authorities-home.pl?"
80       ."marclist=$marclist[0]"
81       ."&amp;and_or=$and_or[0]"
82       ."&amp;excluding=$excluding[0]"
83       ."&amp;operator=$operator[0]"
84       ."&amp;value=$value_url"
85       ."&amp;resultsperpage=$resultsperpage"
86       ."&amp;type=opac"
87       ."&amp;op=do_search"
88       ."&amp;authtypecode=$authtypecode"
89       ."&amp;orderby=$orderby";
90
91     my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
92     my $to;
93     if ( !defined $total ) {
94         $total = 0;
95     }
96
97     if ( $total < $startfrom * $resultsperpage ) {
98         $to = $total;
99     }
100     else {
101         $to = $startfrom * $resultsperpage;
102     }
103
104     my $AuthorityXSLTOpacResultsDisplay = C4::Context->preference('AuthorityXSLTOpacResultsDisplay');
105     if ($results && $AuthorityXSLTOpacResultsDisplay) {
106         my $lang = C4::Languages::getlanguage();
107         foreach my $result (@$results) {
108             my $authority = Koha::Authorities->find($result->{authid});
109             next unless $authority;
110             my $authtypecode = $authority->authtypecode;
111             my $xsl = $AuthorityXSLTOpacResultsDisplay;
112
113             $xsl =~ s/\{langcode\}/$lang/g;
114             $xsl =~ s/\{authtypecode\}/$authtypecode/g;
115             my $xslt_engine = Koha::XSLT::Base->new;
116             my $output = $xslt_engine->transform({ xml => $authority->marcxml, file => $xsl });
117             if ($xslt_engine->err) {
118                 warn "XSL transformation failed ($xsl): " . $xslt_engine->err;
119                 next;
120             }
121             $result->{html} = $output;
122         }
123     }
124
125     $template->param( result => $results ) if $results;
126
127     $template->param(
128         pagination_bar => pagination_bar(
129             $base_url,  int( $total / $resultsperpage ) + 1,
130             $startfrom, 'startfrom'
131         ),
132         total     => $total,
133         from      => $from,
134         to        => $to,
135     );
136
137     unless (C4::Context->preference('OPACShowUnusedAuthorities')) {
138 #        TODO implement usage counts
139 #        my @usedauths = grep { $_->{used} > 0 } @$results;
140 #        $results = \@usedauths;
141     }
142
143     # Opac search history
144     if (C4::Context->preference('EnableOpacSearchHistory')) {
145         if ( $startfrom == 1) {
146             my $path_info = $query->url(-path_info=>1);
147             my $query_cgi_history = $query->url(-query=>1);
148             $query_cgi_history =~ s/^$path_info\?//;
149             $query_cgi_history =~ s/;/&/g;
150
151             unless ( $loggedinuser ) {
152                 my $new_search = C4::Search::History::add_to_session({
153                         cgi => $query,
154                         query_desc => $value[0],
155                         query_cgi => $query_cgi_history,
156                         total => $total,
157                         type => "authority",
158                 });
159             } else {
160                 # To the session (the user is logged in)
161                 C4::Search::History::add({
162                     userid => $loggedinuser,
163                     sessionid => $query->cookie("CGISESSID"),
164                     query_desc => $value[0],
165                     query_cgi => $query_cgi_history,
166                     total => $total,
167                     type => "authority",
168                 });
169             }
170         }
171     }
172
173     $template->param( orderby => $orderby );
174     $template->param(
175         startfrom      => $startfrom,
176         resultsperpage => $resultsperpage,
177         countfuzzy     => !(C4::Context->preference('OPACShowUnusedAuthorities')),
178         resultcount    => scalar @$results,
179         authtypecode   => $authtypecode,
180         authtypetext   => $authority_types->find($authtypecode)->authtypetext,
181         isEDITORS      => $authtypecode eq 'EDITORS',
182     );
183
184 }
185 else {
186     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
187         {
188             template_name   => "opac-authorities-home.tt",
189             query           => $query,
190             type            => 'opac',
191             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
192         }
193     );
194
195 }
196
197 $template->param(
198     authority_types => $authority_types,
199     authtypecode    => $authtypecode,
200 );
201
202 # Print the page
203 output_html_with_http_headers $query, $cookie, $template->output;