Bug 32482: (follow-up) Add markup comments
[koha.git] / opac / opac-authorities-home.pl
1 #!/usr/bin/perl
2 # WARNING: 4-character tab stops here
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22
23 use CGI qw ( -utf8 );
24 use URI::Escape qw( uri_escape_utf8 );
25 use C4::Auth qw( get_template_and_user );
26
27 use C4::Context;
28 use C4::Output qw( pagination_bar output_html_with_http_headers );
29 use C4::Koha;
30 use C4::Search::History;
31 use C4::Languages;
32 use Koha::XSLT::Base;
33
34 use Koha::Authority::Types;
35 use Koha::SearchEngine::Search;
36 use Koha::SearchEngine::QueryBuilder;
37
38 my $query        = CGI->new;
39 my $op           = $query->param('op') || '';
40 my $authtypecode = $query->param('authtypecode') || '';
41 my $dbh          = C4::Context->dbh;
42
43 my $startfrom = $query->param('startfrom') || 1;
44 my $resultsperpage = $query->param('resultsperpage') || 20;
45 my $authid    = $query->param('authid');
46 my ( $template, $loggedinuser, $cookie );
47
48 my $authority_types = Koha::Authority::Types->search({}, { order_by => ['authtypetext']});
49
50 if ( $op eq "do_search" ) {
51     my @marclist = $query->multi_param('marclist');
52     my @and_or = $query->multi_param('and_or');
53     my @excluding = $query->multi_param('excluding');
54     my @operator = $query->multi_param('operator');
55     my $orderby = $query->param('orderby');
56     my @value = $query->multi_param('value');
57     $value[0] ||= q||;
58
59     my $builder = Koha::SearchEngine::QueryBuilder->new(
60         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
61     my $searcher = Koha::SearchEngine::Search->new(
62         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
63     my $search_query = $builder->build_authorities_query_compat( \@marclist, \@and_or,
64         \@excluding, \@operator, \@value, $authtypecode, $orderby );
65     my $offset = ( $startfrom - 1 ) * $resultsperpage + 1;
66     my ( $results, $total ) =
67       $searcher->search_auth_compat( $search_query, $offset, $resultsperpage );
68     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
69         {
70             template_name   => "opac-authoritiessearchresultlist.tt",
71             query           => $query,
72             type            => 'opac',
73             authnotrequired => 1,
74         }
75     );
76     $template->param( search_query => $search_query ) if C4::Context->preference('DumpSearchQueryTemplate');
77
78     # multi page display gestion
79     my $value_url = uri_escape_utf8($value[0]);
80     my $base_url = "opac-authorities-home.pl?"
81       ."marclist=$marclist[0]"
82       ."&amp;and_or=$and_or[0]"
83       ."&amp;excluding=$excluding[0]"
84       ."&amp;operator=$operator[0]"
85       ."&amp;value=$value_url"
86       ."&amp;resultsperpage=$resultsperpage"
87       ."&amp;type=opac"
88       ."&amp;op=do_search"
89       ."&amp;authtypecode=$authtypecode"
90       ."&amp;orderby=$orderby";
91
92     my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
93     my $to;
94     if ( !defined $total ) {
95         $total = 0;
96     }
97
98     if ( $total < $startfrom * $resultsperpage ) {
99         $to = $total;
100     }
101     else {
102         $to = $startfrom * $resultsperpage;
103     }
104
105     my $AuthorityXSLTOpacResultsDisplay = C4::Context->preference('AuthorityXSLTOpacResultsDisplay');
106     if ($results && $AuthorityXSLTOpacResultsDisplay) {
107         my $lang = C4::Languages::getlanguage();
108         foreach my $result (@$results) {
109             my $authority = Koha::Authorities->find($result->{authid});
110             next unless $authority;
111             my $authtypecode = $authority->authtypecode;
112             my $xsl = $AuthorityXSLTOpacResultsDisplay;
113
114             $xsl =~ s/\{langcode\}/$lang/g;
115             $xsl =~ s/\{authtypecode\}/$authtypecode/g;
116             my $xslt_engine = Koha::XSLT::Base->new;
117             my $output = $xslt_engine->transform({ xml => $authority->marcxml, file => $xsl });
118             if ($xslt_engine->err) {
119                 warn "XSL transformation failed ($xsl): " . $xslt_engine->err;
120                 next;
121             }
122             $result->{html} = $output;
123         }
124     }
125
126     $template->param( result => $results ) if $results;
127
128     $template->param(
129         pagination_bar => pagination_bar(
130             $base_url,  int( $total / $resultsperpage ) + 1,
131             $startfrom, 'startfrom'
132         ),
133         total     => $total,
134         from      => $from,
135         to        => $to,
136     );
137
138     unless (C4::Context->preference('OPACShowUnusedAuthorities')) {
139 #        TODO implement usage counts
140 #        my @usedauths = grep { $_->{used} > 0 } @$results;
141 #        $results = \@usedauths;
142     }
143
144     # Opac search history
145     if (C4::Context->preference('EnableOpacSearchHistory')) {
146         if ( $startfrom == 1) {
147             my $path_info = $query->url(-path_info=>1);
148             my $query_cgi_history = $query->url(-query=>1);
149             $query_cgi_history =~ s/^$path_info\?//;
150             $query_cgi_history =~ s/;/&/g;
151
152             unless ( $loggedinuser ) {
153                 my $new_search = C4::Search::History::add_to_session({
154                         cgi => $query,
155                         query_desc => $value[0],
156                         query_cgi => $query_cgi_history,
157                         total => $total,
158                         type => "authority",
159                 });
160             } else {
161                 # To the session (the user is logged in)
162                 C4::Search::History::add({
163                     userid => $loggedinuser,
164                     sessionid => $query->cookie("CGISESSID"),
165                     query_desc => $value[0],
166                     query_cgi => $query_cgi_history,
167                     total => $total,
168                     type => "authority",
169                 });
170             }
171         }
172     }
173
174     $template->param( orderby => $orderby );
175     $template->param(
176         startfrom      => $startfrom,
177         resultsperpage => $resultsperpage,
178         countfuzzy     => !(C4::Context->preference('OPACShowUnusedAuthorities')),
179         resultcount    => scalar @$results,
180         authtypecode   => $authtypecode,
181         authtypetext   => $authority_types->find($authtypecode)->authtypetext,
182         isEDITORS      => $authtypecode eq 'EDITORS',
183     );
184
185 }
186 else {
187     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
188         {
189             template_name   => "opac-authorities-home.tt",
190             query           => $query,
191             type            => 'opac',
192             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
193         }
194     );
195
196 }
197
198 $template->param(
199     authority_types => $authority_types,
200     authtypecode    => $authtypecode,
201 );
202
203 # Print the page
204 output_html_with_http_headers $query, $cookie, $template->output;
205
206 # Local Variables:
207 # tab-width: 4
208 # End: