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