3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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.
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.
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>.
23 use URI::Escape qw( uri_escape_utf8 );
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;
33 use Koha::Authority::Types;
34 use Koha::SearchEngine::Search;
35 use Koha::SearchEngine::QueryBuilder;
38 use Koha::Z3950Servers;
41 my $dbh = C4::Context->dbh;
42 my $op = $query->param('op') || '';
43 my $authtypecode = $query->param('authtypecode') || '';
44 my $authid = $query->param('authid') || '';
46 my ( $template, $loggedinuser, $cookie );
48 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
50 if ( $op eq "delete" ) {
51 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
53 template_name => "authorities/authorities-home.tt",
56 flagsrequired => { catalogue => 1 },
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'),
66 DelAuthority({ authid => $authid });
68 if ( $query->param('operator') ) {
69 # query contains search params so perform search
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') || '';
84 my $startfrom = $query->param('startfrom') || 1;
85 my $resultsperpage = $query->param('resultsperpage') || 20;
86 my $offset = ( $startfrom - 1 ) * $resultsperpage + 1;
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
96 my ( $results, $total ) = $searcher->search_auth_compat(
97 $search_query, $offset, $resultsperpage
100 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
102 template_name => "authorities/searchresultlist.tt",
105 flagsrequired => { catalogue => 1 },
109 $template->param( search_query => $search_query ) if C4::Context->preference('DumpSearchQueryTemplate');
111 csrf_token => Koha::Token->new->generate_csrf({
112 session_id => scalar $query->cookie('CGISESSID'),
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;
124 C4::Search::History::add({
125 userid => $loggedinuser,
126 sessionid => $query->cookie("CGISESSID"),
127 query_desc => $value,
128 query_cgi => $query_cgi_history,
136 marclist => $marclist,
138 excluding => $excluding,
139 operator => $operator,
142 authtypecode => $authtypecode,
143 startfrom => $startfrom,
144 resultsperpage => $resultsperpage,
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
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 ."&and_or=$and_or"
156 ."&excluding=$excluding"
157 ."&operator=$operator"
158 ."&value=$value_url"
159 ."&resultsperpage=$resultsperpage"
160 ."&type=intranet"
162 ."&authtypecode=$authtypecode"
163 ."&orderby=$orderby";
165 my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
167 if ( !defined $total ) {
171 if ( $total < $startfrom * $resultsperpage ) {
175 $to = $startfrom * $resultsperpage;
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;
185 my $authtypecode = $authority->authtypecode;
186 my $xsl = $AuthorityXSLTResultsDisplay;
187 $xsl =~ s/\{langcode\}/$lang/g;
188 $xsl =~ s/\{authtypecode\}/$authtypecode/g;
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;
197 $result->{html} = $output;
201 $template->param( result => $results ) if $results;
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;
207 pagination_bar => pagination_bar(
208 $base_url, ceil( $hits_to_paginate / $resultsperpage ),
209 $startfrom, 'startfrom'
212 hits_to_paginate => $hits_to_paginate,
215 isEDITORS => $authtypecode eq 'EDITORS',
220 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
222 template_name => "authorities/authorities-home.tt",
225 flagsrequired => { catalogue => 1 },
231 my $servers = Koha::Z3950Servers->search(
233 recordtype => 'authority',
234 servertype => ['zed', 'sru'],
240 authority_types => $authority_types,
244 $template->{VARS}->{marcflavour} = C4::Context->preference("marcflavour");
247 output_html_with_http_headers $query, $cookie, $template->output;