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;
37 use Koha::Z3950Servers;
40 my $dbh = C4::Context->dbh;
41 my $op = $query->param('op') || '';
42 my $authtypecode = $query->param('authtypecode') || '';
43 my $authid = $query->param('authid') || '';
45 my ( $template, $loggedinuser, $cookie );
47 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
48 my $pending_deletion_authid;
50 if ( $op eq "cud-delete" ) {
51 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
53 template_name => "authorities/authorities-home.tt",
56 flagsrequired => { catalogue => 1 },
60 DelAuthority({ authid => $authid });
61 # FIXME No error handling here, DelAuthority needs adjustments
62 $pending_deletion_authid = $authid;
64 if ( $query->param('operator') ) {
65 # query contains search params so perform search
72 if ( $op eq "do_search" ) {
73 my $marclist = $query->param('marclist') || '';
74 my $and_or = $query->param('and_or') || '';
75 my $excluding = $query->param('excluding') || '';
76 my $operator = $query->param('operator') || '';
77 my $orderby = $query->param('orderby') || '';
78 my $value = $query->param('value') || '';
80 my $startfrom = $query->param('startfrom') || 1;
81 my $resultsperpage = $query->param('resultsperpage') || 20;
82 my $offset = ( $startfrom - 1 ) * $resultsperpage + 1;
84 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
86 template_name => "authorities/searchresultlist.tt",
89 flagsrequired => { catalogue => 1 },
93 my $builder = Koha::SearchEngine::QueryBuilder->new( { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
94 my $searcher = Koha::SearchEngine::Search->new( { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
96 my $search_query = $builder->build_authorities_query_compat(
97 [$marclist], [$and_or], [$excluding], [$operator],
98 [$value], $authtypecode, $orderby
100 my ( $results, $total );
101 eval { ( $results, $total ) = $searcher->search_auth_compat( $search_query, $offset, $resultsperpage ); };
103 my $query_error = q{};
104 $query_error .= $@ if $@;
105 $template->param( query_error => $query_error );
108 $template->param( search_query => $search_query ) if C4::Context->preference('DumpSearchQueryTemplate');
111 if (C4::Context->preference('EnableSearchHistory')) {
112 if ( $startfrom == 1) {
113 my $path_info = $query->url(-path_info=>1);
114 my $query_cgi_history = $query->url(-query=>1);
115 $query_cgi_history =~ s/^$path_info\?//;
116 $query_cgi_history =~ s/;/&/g;
118 C4::Search::History::add({
119 userid => $loggedinuser,
120 sessionid => $query->cookie("CGISESSID"),
121 query_desc => $value,
122 query_cgi => $query_cgi_history,
130 marclist => $marclist,
132 excluding => $excluding,
133 operator => $operator,
136 authtypecode => $authtypecode,
137 startfrom => $startfrom,
138 resultsperpage => $resultsperpage,
141 # we must get parameters once again. Because if there is a mainentry, it
142 # has been replaced by something else during the search, thus the links
143 # next/previous would not work anymore
145 # construction of the url of each page
146 my $value_url = uri_escape_utf8($value);
147 my $base_url = "authorities-home.pl?"
148 ."marclist=$marclist"
149 ."&and_or=$and_or"
150 ."&excluding=$excluding"
151 ."&operator=$operator"
152 ."&value=$value_url"
153 ."&resultsperpage=$resultsperpage"
154 ."&type=intranet"
156 ."&authtypecode=$authtypecode"
157 ."&orderby=$orderby";
159 my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
161 if ( !defined $total ) {
165 if ( $total < $startfrom * $resultsperpage ) {
169 $to = $startfrom * $resultsperpage;
172 my $AuthorityXSLTResultsDisplay = C4::Context->preference('AuthorityXSLTResultsDisplay');
173 if ($results && $AuthorityXSLTResultsDisplay) {
174 my $lang = C4::Languages::getlanguage();
175 foreach my $result (@$results) {
176 my $authority = Koha::Authorities->find($result->{authid});
177 next unless $authority;
179 my $authtypecode = $authority->authtypecode;
180 my $xsl = $AuthorityXSLTResultsDisplay;
181 $xsl =~ s/\{langcode\}/$lang/g;
182 $xsl =~ s/\{authtypecode\}/$authtypecode/g;
184 my $xslt_engine = Koha::XSLT::Base->new;
185 my $output = $xslt_engine->transform({ xml => $authority->marcxml, file => $xsl });
186 if ($xslt_engine->err) {
187 warn "XSL transformation failed ($xsl): " . $xslt_engine->err;
191 $result->{html} = $output;
195 if( $pending_deletion_authid && $results ) {
196 $results = [ grep { $_->{authid} != $pending_deletion_authid } @$results ];
199 $template->param( result => $results ) if $results;
201 my $max_result_window = $searcher->max_result_window;
202 my $hits_to_paginate = ($max_result_window && $max_result_window < $total) ? $max_result_window : $total;
205 pagination_bar => pagination_bar(
206 $base_url, ceil( $hits_to_paginate / $resultsperpage ),
207 $startfrom, 'startfrom'
210 hits_to_paginate => $hits_to_paginate,
213 isEDITORS => $authtypecode eq 'EDITORS',
218 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
220 template_name => "authorities/authorities-home.tt",
223 flagsrequired => { catalogue => 1 },
229 my $servers = Koha::Z3950Servers->search(
231 recordtype => 'authority',
232 servertype => ['zed', 'sru'],
238 authority_types => $authority_types,
242 $template->{VARS}->{marcflavour} = C4::Context->preference("marcflavour");
245 output_html_with_http_headers $query, $cookie, $template->output;