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