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 @input_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 $valid_marc_list = {
64 foreach my $entry (@input_marclist) {
65 if ( $valid_marc_list->{$entry} ) {
66 push( @marclist, $entry );
70 push( @marclist, 'all' );
73 my $builder = Koha::SearchEngine::QueryBuilder->new(
74 { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
75 my $searcher = Koha::SearchEngine::Search->new(
76 { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
77 my $search_query = $builder->build_authorities_query_compat( \@marclist, \@and_or,
78 \@excluding, \@operator, \@value, $authtypecode, $orderby );
79 my $offset = ( $startfrom - 1 ) * $resultsperpage + 1;
80 my ( $results, $total ) =
81 $searcher->search_auth_compat( $search_query, $offset, $resultsperpage );
82 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
84 template_name => "opac-authoritiessearchresultlist.tt",
90 $template->param( search_query => $search_query ) if C4::Context->preference('DumpSearchQueryTemplate');
92 # multi page display gestion
93 my $value_url = uri_escape_utf8($value[0]);
94 my $base_url = "opac-authorities-home.pl?"
95 ."marclist=$marclist[0]"
96 ."&and_or=$and_or[0]"
97 ."&excluding=$excluding[0]"
98 ."&operator=$operator[0]"
99 ."&value=$value_url"
100 ."&resultsperpage=$resultsperpage"
103 ."&authtypecode=$authtypecode"
104 ."&orderby=$orderby";
106 my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
108 if ( !defined $total ) {
112 if ( $total < $startfrom * $resultsperpage ) {
116 $to = $startfrom * $resultsperpage;
119 my $AuthorityXSLTOpacResultsDisplay = C4::Context->preference('AuthorityXSLTOpacResultsDisplay');
120 if ($results && $AuthorityXSLTOpacResultsDisplay) {
121 my $lang = C4::Languages::getlanguage();
122 foreach my $result (@$results) {
123 my $authority = Koha::Authorities->find($result->{authid});
124 next unless $authority;
125 my $authtypecode = $authority->authtypecode;
126 my $xsl = $AuthorityXSLTOpacResultsDisplay;
128 $xsl =~ s/\{langcode\}/$lang/g;
129 $xsl =~ s/\{authtypecode\}/$authtypecode/g;
130 my $xslt_engine = Koha::XSLT::Base->new;
131 my $output = $xslt_engine->transform({ xml => $authority->marcxml, file => $xsl });
132 if ($xslt_engine->err) {
133 warn "XSL transformation failed ($xsl): " . $xslt_engine->err;
136 $result->{html} = $output;
140 $template->param( result => $results ) if $results;
143 pagination_bar => pagination_bar(
144 $base_url, int( $total / $resultsperpage ) + 1,
145 $startfrom, 'startfrom'
152 unless (C4::Context->preference('OPACShowUnusedAuthorities')) {
153 # TODO implement usage counts
154 # my @usedauths = grep { $_->{used} > 0 } @$results;
155 # $results = \@usedauths;
158 # Opac search history
159 if (C4::Context->preference('EnableOpacSearchHistory')) {
160 if ( $startfrom == 1) {
161 my $path_info = $query->url(-path_info=>1);
162 my $query_cgi_history = $query->url(-query=>1);
163 $query_cgi_history =~ s/^$path_info\?//;
164 $query_cgi_history =~ s/;/&/g;
166 unless ( $loggedinuser ) {
167 my $new_search = C4::Search::History::add_to_session({
169 query_desc => $value[0],
170 query_cgi => $query_cgi_history,
175 # To the session (the user is logged in)
176 C4::Search::History::add({
177 userid => $loggedinuser,
178 sessionid => $query->cookie("CGISESSID"),
179 query_desc => $value[0],
180 query_cgi => $query_cgi_history,
188 $template->param( orderby => $orderby );
190 startfrom => $startfrom,
191 resultsperpage => $resultsperpage,
192 countfuzzy => !(C4::Context->preference('OPACShowUnusedAuthorities')),
193 resultcount => scalar @$results,
194 authtypecode => $authtypecode,
195 authtypetext => $authority_types->find($authtypecode)->authtypetext,
196 isEDITORS => $authtypecode eq 'EDITORS',
201 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
203 template_name => "opac-authorities-home.tt",
206 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
213 authority_types => $authority_types,
214 authtypecode => $authtypecode,
218 output_html_with_http_headers $query, $cookie, $template->output;