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