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