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