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 Array::Utils qw(intersect);
26 use C4::Auth qw( get_template_and_user );
29 use C4::Output qw( pagination_bar output_html_with_http_headers );
31 use C4::Search::History;
35 use Koha::Authority::Types;
36 use Koha::SearchEngine::Search;
37 use Koha::SearchEngine::QueryBuilder;
40 my $op = $query->param('op') || '';
41 my $authtypecode = $query->param('authtypecode') || '';
42 my $dbh = C4::Context->dbh;
44 my $startfrom = $query->param('startfrom') || 1;
45 my $resultsperpage = $query->param('resultsperpage') || 20;
46 my $authid = $query->param('authid');
47 my ( $template, $loggedinuser, $cookie );
49 my $authority_types = Koha::Authority::Types->search({}, { order_by => ['authtypetext']});
51 if ( $op eq "do_search" ) {
52 my @input_marclist = $query->multi_param('marclist');
53 my @and_or = $query->multi_param('and_or');
54 my @excluding = $query->multi_param('excluding');
55 my @operator = $query->multi_param('operator');
56 my $orderby = $query->param('orderby');
57 my @value = $query->multi_param('value');
60 # validation of "Where"
61 my @valid_marc_list = qw( all match mainentry );
62 my @marclist = intersect( @input_marclist, @valid_marc_list );
63 @marclist = ('all') unless @marclist;
65 my $builder = Koha::SearchEngine::QueryBuilder->new(
66 { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
67 my $searcher = Koha::SearchEngine::Search->new(
68 { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
69 my $search_query = $builder->build_authorities_query_compat( \@marclist, \@and_or,
70 \@excluding, \@operator, \@value, $authtypecode, $orderby );
71 my $offset = ( $startfrom - 1 ) * $resultsperpage + 1;
72 my ( $results, $total ) =
73 $searcher->search_auth_compat( $search_query, $offset, $resultsperpage );
74 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
76 template_name => "opac-authoritiessearchresultlist.tt",
82 $template->param( search_query => $search_query ) if C4::Context->preference('DumpSearchQueryTemplate');
84 # multi page display gestion
85 my $value_url = uri_escape_utf8($value[0]);
86 my $base_url = "opac-authorities-home.pl?"
87 ."marclist=$marclist[0]"
88 ."&and_or=$and_or[0]"
89 ."&excluding=$excluding[0]"
90 ."&operator=$operator[0]"
91 ."&value=$value_url"
92 ."&resultsperpage=$resultsperpage"
95 ."&authtypecode=$authtypecode"
96 ."&orderby=$orderby";
98 my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
100 if ( !defined $total ) {
104 if ( $total < $startfrom * $resultsperpage ) {
108 $to = $startfrom * $resultsperpage;
111 my $AuthorityXSLTOpacResultsDisplay = C4::Context->preference('AuthorityXSLTOpacResultsDisplay');
112 if ($results && $AuthorityXSLTOpacResultsDisplay) {
113 my $lang = C4::Languages::getlanguage();
114 foreach my $result (@$results) {
115 my $authority = Koha::Authorities->find($result->{authid});
116 next unless $authority;
117 my $authtypecode = $authority->authtypecode;
118 my $xsl = $AuthorityXSLTOpacResultsDisplay;
120 $xsl =~ s/\{langcode\}/$lang/g;
121 $xsl =~ s/\{authtypecode\}/$authtypecode/g;
122 my $xslt_engine = Koha::XSLT::Base->new;
123 my $output = $xslt_engine->transform({ xml => $authority->marcxml, file => $xsl });
124 if ($xslt_engine->err) {
125 warn "XSL transformation failed ($xsl): " . $xslt_engine->err;
128 $result->{html} = $output;
132 $template->param( result => $results ) if $results;
135 pagination_bar => pagination_bar(
136 $base_url, int( $total / $resultsperpage ) + 1,
137 $startfrom, 'startfrom'
144 unless (C4::Context->preference('OPACShowUnusedAuthorities')) {
145 # TODO implement usage counts
146 # my @usedauths = grep { $_->{used} > 0 } @$results;
147 # $results = \@usedauths;
150 # Opac search history
151 if (C4::Context->preference('EnableOpacSearchHistory')) {
152 if ( $startfrom == 1) {
153 my $path_info = $query->url(-path_info=>1);
154 my $query_cgi_history = $query->url(-query=>1);
155 $query_cgi_history =~ s/^$path_info\?//;
156 $query_cgi_history =~ s/;/&/g;
158 unless ( $loggedinuser ) {
159 my $new_search = C4::Search::History::add_to_session({
161 query_desc => $value[0],
162 query_cgi => $query_cgi_history,
167 # To the session (the user is logged in)
168 C4::Search::History::add({
169 userid => $loggedinuser,
170 sessionid => $query->cookie("CGISESSID"),
171 query_desc => $value[0],
172 query_cgi => $query_cgi_history,
180 $template->param( orderby => $orderby );
182 startfrom => $startfrom,
183 resultsperpage => $resultsperpage,
184 countfuzzy => !(C4::Context->preference('OPACShowUnusedAuthorities')),
185 resultcount => scalar @$results,
186 authtypecode => $authtypecode,
187 authtypetext => $authority_types->find($authtypecode)->authtypetext,
188 isEDITORS => $authtypecode eq 'EDITORS',
193 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
195 template_name => "opac-authorities-home.tt",
198 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
205 authority_types => $authority_types,
206 authtypecode => $authtypecode,
210 output_html_with_http_headers $query, $cookie, $template->output;