Bug 36845: Do not loop over the content attribute
[koha.git] / authorities / 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 POSIX qw( ceil );
25
26 use C4::Context;
27 use C4::Auth qw( get_template_and_user );
28 use C4::Output qw( output_and_exit pagination_bar output_html_with_http_headers );
29 use C4::AuthoritiesMarc qw( DelAuthority );
30 use C4::Search::History;
31 use C4::Languages;
32
33 use Koha::Authority::Types;
34 use Koha::SearchEngine::Search;
35 use Koha::SearchEngine::QueryBuilder;
36 use Koha::XSLT::Base;
37 use Koha::Z3950Servers;
38
39 my $query = CGI->new;
40 my $dbh   = C4::Context->dbh;
41 my $op           = $query->param('op')           || '';
42 my $authtypecode = $query->param('authtypecode') || '';
43 my $authid       = $query->param('authid')       || '';
44
45 my ( $template, $loggedinuser, $cookie );
46
47 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
48 my $pending_deletion_authid;
49
50 if ( $op eq "cud-delete" ) {
51     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
52         {
53             template_name   => "authorities/authorities-home.tt",
54             query           => $query,
55             type            => 'intranet',
56             flagsrequired   => { catalogue => 1 },
57         }
58     );
59
60     DelAuthority({ authid => $authid });
61     # FIXME No error handling here, DelAuthority needs adjustments
62     $pending_deletion_authid = $authid;
63
64     if ( $query->param('operator') ) {
65         # query contains search params so perform search
66         $op = "do_search";
67     }
68     else {
69         $op = '';
70     }
71 }
72 if ( $op eq "do_search" ) {
73     my $marclist  = $query->param('marclist')  || '';
74     my $and_or    = $query->param('and_or')    || '';
75     my $excluding = $query->param('excluding') || '';
76     my $operator  = $query->param('operator')  || '';
77     my $orderby   = $query->param('orderby')   || '';
78     my $value     = $query->param('value')     || '';
79
80     my $startfrom      = $query->param('startfrom')      || 1;
81     my $resultsperpage = $query->param('resultsperpage') || 20;
82     my $offset = ( $startfrom - 1 ) * $resultsperpage + 1;
83
84     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
85         {
86             template_name   => "authorities/searchresultlist.tt",
87             query           => $query,
88             type            => 'intranet',
89             flagsrequired   => { catalogue => 1 },
90         }
91     );
92
93     my $builder  = Koha::SearchEngine::QueryBuilder->new( { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
94     my $searcher = Koha::SearchEngine::Search->new( { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
95
96     my $search_query = $builder->build_authorities_query_compat(
97         [$marclist], [$and_or], [$excluding], [$operator],
98         [$value], $authtypecode, $orderby
99     );
100     my ( $results, $total );
101     eval { ( $results, $total ) = $searcher->search_auth_compat( $search_query, $offset, $resultsperpage ); };
102     if ($@) {
103         my $query_error = q{};
104         $query_error .= $@ if $@;
105         $template->param( query_error => $query_error );
106     }
107
108     $template->param( search_query => $search_query ) if C4::Context->preference('DumpSearchQueryTemplate');
109
110     # search history
111     if (C4::Context->preference('EnableSearchHistory')) {
112         if ( $startfrom == 1) {
113             my $path_info = $query->url(-path_info=>1);
114             my $query_cgi_history = $query->url(-query=>1);
115             $query_cgi_history =~ s/^$path_info\?//;
116             $query_cgi_history =~ s/;/&/g;
117
118             C4::Search::History::add({
119                 userid => $loggedinuser,
120                 sessionid => $query->cookie("CGISESSID"),
121                 query_desc => $value,
122                 query_cgi => $query_cgi_history,
123                 total => $total,
124                 type => "authority",
125             });
126         }
127     }
128
129     $template->param(
130         marclist       => $marclist,
131         and_or         => $and_or,
132         excluding      => $excluding,
133         operator       => $operator,
134         orderby        => $orderby,
135         value          => $value,
136         authtypecode   => $authtypecode,
137         startfrom      => $startfrom,
138         resultsperpage => $resultsperpage,
139     );
140
141     # we must get parameters once again. Because if there is a mainentry, it
142     # has been replaced by something else during the search, thus the links
143     # next/previous would not work anymore
144
145     # construction of the url of each page
146     my $value_url = uri_escape_utf8($value);
147     my $base_url = "authorities-home.pl?"
148       ."marclist=$marclist"
149       ."&amp;and_or=$and_or"
150       ."&amp;excluding=$excluding"
151       ."&amp;operator=$operator"
152       ."&amp;value=$value_url"
153       ."&amp;resultsperpage=$resultsperpage"
154       ."&amp;type=intranet"
155       ."&amp;op=do_search"
156       ."&amp;authtypecode=$authtypecode"
157       ."&amp;orderby=$orderby";
158
159     my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
160     my $to;
161     if ( !defined $total ) {
162         $total = 0;
163     }
164
165     if ( $total < $startfrom * $resultsperpage ) {
166         $to = $total;
167     }
168     else {
169         $to = $startfrom * $resultsperpage;
170     }
171
172     my $AuthorityXSLTResultsDisplay = C4::Context->preference('AuthorityXSLTResultsDisplay');
173     if ($results && $AuthorityXSLTResultsDisplay) {
174         my $lang = C4::Languages::getlanguage();
175         foreach my $result (@$results) {
176             my $authority = Koha::Authorities->find($result->{authid});
177             next unless $authority;
178
179             my $authtypecode = $authority->authtypecode;
180             my $xsl = $AuthorityXSLTResultsDisplay;
181             $xsl =~ s/\{langcode\}/$lang/g;
182             $xsl =~ s/\{authtypecode\}/$authtypecode/g;
183
184             my $xslt_engine = Koha::XSLT::Base->new;
185             my $output = $xslt_engine->transform({ xml => $authority->marcxml, file => $xsl });
186             if ($xslt_engine->err) {
187                 warn "XSL transformation failed ($xsl): " . $xslt_engine->err;
188                 next;
189             }
190
191             $result->{html} = $output;
192         }
193     }
194
195     if( $pending_deletion_authid && $results ) {
196         $results = [ grep { $_->{authid} != $pending_deletion_authid } @$results ];
197     }
198
199     $template->param( result => $results ) if $results;
200
201     my $max_result_window = $searcher->max_result_window;
202     my $hits_to_paginate = ($max_result_window && $max_result_window < $total) ? $max_result_window : $total;
203
204     $template->param(
205         pagination_bar => pagination_bar(
206             $base_url,  ceil( $hits_to_paginate / $resultsperpage ),
207             $startfrom, 'startfrom'
208         ),
209         total            => $total,
210         hits_to_paginate => $hits_to_paginate,
211         from             => $from,
212         to               => $to,
213         isEDITORS        => $authtypecode eq 'EDITORS',
214     );
215
216 }
217 if ( $op eq '' ) {
218     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
219         {
220             template_name   => "authorities/authorities-home.tt",
221             query           => $query,
222             type            => 'intranet',
223             flagsrequired   => { catalogue => 1 },
224         }
225     );
226
227 }
228
229 my $servers = Koha::Z3950Servers->search(
230     {
231         recordtype => 'authority',
232         servertype => ['zed', 'sru'],
233     },
234 );
235
236 $template->param(
237     servers => $servers,
238     authority_types => $authority_types,
239     op            => $op,
240 );
241
242 $template->{VARS}->{marcflavour} = C4::Context->preference("marcflavour");
243
244 # Print the page
245 output_html_with_http_headers $query, $cookie, $template->output;