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 );
24 use C4::Auth qw( get_template_and_user );
27 use C4::Output qw( pagination_bar output_html_with_http_headers );
29 use C4::Search::History;
33 use Koha::Authority::Types;
34 use Koha::SearchEngine::Search;
35 use Koha::SearchEngine::QueryBuilder;
38 my $op = $query->param('op') || '';
39 my $authtypecode = $query->param('authtypecode') || '';
40 my $dbh = C4::Context->dbh;
42 my $startfrom = $query->param('startfrom') || 1;
43 my $resultsperpage = $query->param('resultsperpage') || 20;
44 my $authid = $query->param('authid');
45 my ( $template, $loggedinuser, $cookie );
47 my $authority_types = Koha::Authority::Types->search({}, { order_by => ['authtypetext']});
49 if ( $op eq "do_search" ) {
50 my @marclist = $query->multi_param('marclist');
51 my @and_or = $query->multi_param('and_or');
52 my @excluding = $query->multi_param('excluding');
53 my @operator = $query->multi_param('operator');
54 my $orderby = $query->param('orderby');
55 my @value = $query->multi_param('value');
58 my $builder = Koha::SearchEngine::QueryBuilder->new(
59 { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
60 my $searcher = Koha::SearchEngine::Search->new(
61 { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
62 my $search_query = $builder->build_authorities_query_compat( \@marclist, \@and_or,
63 \@excluding, \@operator, \@value, $authtypecode, $orderby );
64 my $offset = ( $startfrom - 1 ) * $resultsperpage + 1;
65 my ( $results, $total ) =
66 $searcher->search_auth_compat( $search_query, $offset, $resultsperpage );
67 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
69 template_name => "opac-authoritiessearchresultlist.tt",
75 $template->param( search_query => $search_query ) if C4::Context->preference('DumpSearchQueryTemplate');
77 # multi page display gestion
78 my $value_url = uri_escape_utf8($value[0]);
79 my $base_url = "opac-authorities-home.pl?"
80 ."marclist=$marclist[0]"
81 ."&and_or=$and_or[0]"
82 ."&excluding=$excluding[0]"
83 ."&operator=$operator[0]"
84 ."&value=$value_url"
85 ."&resultsperpage=$resultsperpage"
88 ."&authtypecode=$authtypecode"
89 ."&orderby=$orderby";
91 my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
93 if ( !defined $total ) {
97 if ( $total < $startfrom * $resultsperpage ) {
101 $to = $startfrom * $resultsperpage;
104 my $AuthorityXSLTOpacResultsDisplay = C4::Context->preference('AuthorityXSLTOpacResultsDisplay');
105 if ($results && $AuthorityXSLTOpacResultsDisplay) {
106 my $lang = C4::Languages::getlanguage();
107 foreach my $result (@$results) {
108 my $authority = Koha::Authorities->find($result->{authid});
109 next unless $authority;
110 my $authtypecode = $authority->authtypecode;
111 my $xsl = $AuthorityXSLTOpacResultsDisplay;
113 $xsl =~ s/\{langcode\}/$lang/g;
114 $xsl =~ s/\{authtypecode\}/$authtypecode/g;
115 my $xslt_engine = Koha::XSLT::Base->new;
116 my $output = $xslt_engine->transform({ xml => $authority->marcxml, file => $xsl });
117 if ($xslt_engine->err) {
118 warn "XSL transformation failed ($xsl): " . $xslt_engine->err;
121 $result->{html} = $output;
125 $template->param( result => $results ) if $results;
128 pagination_bar => pagination_bar(
129 $base_url, int( $total / $resultsperpage ) + 1,
130 $startfrom, 'startfrom'
137 unless (C4::Context->preference('OPACShowUnusedAuthorities')) {
138 # TODO implement usage counts
139 # my @usedauths = grep { $_->{used} > 0 } @$results;
140 # $results = \@usedauths;
143 # Opac search history
144 if (C4::Context->preference('EnableOpacSearchHistory')) {
145 if ( $startfrom == 1) {
146 my $path_info = $query->url(-path_info=>1);
147 my $query_cgi_history = $query->url(-query=>1);
148 $query_cgi_history =~ s/^$path_info\?//;
149 $query_cgi_history =~ s/;/&/g;
151 unless ( $loggedinuser ) {
152 my $new_search = C4::Search::History::add_to_session({
154 query_desc => $value[0],
155 query_cgi => $query_cgi_history,
160 # To the session (the user is logged in)
161 C4::Search::History::add({
162 userid => $loggedinuser,
163 sessionid => $query->cookie("CGISESSID"),
164 query_desc => $value[0],
165 query_cgi => $query_cgi_history,
173 $template->param( orderby => $orderby );
175 startfrom => $startfrom,
176 resultsperpage => $resultsperpage,
177 countfuzzy => !(C4::Context->preference('OPACShowUnusedAuthorities')),
178 resultcount => scalar @$results,
179 authtypecode => $authtypecode,
180 authtypetext => $authority_types->find($authtypecode)->authtypetext,
181 isEDITORS => $authtypecode eq 'EDITORS',
186 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
188 template_name => "opac-authorities-home.tt",
191 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
198 authority_types => $authority_types,
199 authtypecode => $authtypecode,
203 output_html_with_http_headers $query, $cookie, $template->output;