Bug 33036: Use Koha::Objects
[koha.git] / opac / opac-authorities-home.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23 use URI::Escape qw( uri_escape_utf8 );
24 use Array::Utils qw(intersect);
25
26 use C4::Auth qw( get_template_and_user );
27
28 use C4::Context;
29 use C4::Output qw( pagination_bar output_html_with_http_headers );
30 use C4::Koha;
31 use C4::Search::History;
32 use C4::Languages;
33 use Koha::XSLT::Base;
34
35 use Koha::Authority::Types;
36 use Koha::SearchEngine::Search;
37 use Koha::SearchEngine::QueryBuilder;
38
39 my $query        = CGI->new;
40 my $op           = $query->param('op') || '';
41 my $authtypecode = $query->param('authtypecode') || '';
42 my $dbh          = C4::Context->dbh;
43
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 );
48
49 my $authority_types = Koha::Authority::Types->search({}, { order_by => ['authtypetext']});
50
51 if ( $op eq "cud-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');
58     $value[0] ||= q||;
59
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;
64
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(
75         {
76             template_name   => "opac-authoritiessearchresultlist.tt",
77             query           => $query,
78             type            => 'opac',
79             authnotrequired => 1,
80         }
81     );
82     $template->param( search_query => $search_query ) if C4::Context->preference('DumpSearchQueryTemplate');
83
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       ."&amp;and_or=$and_or[0]"
89       ."&amp;excluding=$excluding[0]"
90       ."&amp;operator=$operator[0]"
91       ."&amp;value=$value_url"
92       ."&amp;resultsperpage=$resultsperpage"
93       ."&amp;type=opac"
94       ."&amp;op=do_search"
95       ."&amp;authtypecode=$authtypecode"
96       ."&amp;orderby=$orderby";
97
98     my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
99     my $to;
100     if ( !defined $total ) {
101         $total = 0;
102     }
103
104     if ( $total < $startfrom * $resultsperpage ) {
105         $to = $total;
106     }
107     else {
108         $to = $startfrom * $resultsperpage;
109     }
110
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;
119
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;
126                 next;
127             }
128             $result->{html} = $output;
129         }
130     }
131
132     $template->param( result => $results ) if $results;
133
134     $template->param(
135         pagination_bar => pagination_bar(
136             $base_url,  int( $total / $resultsperpage ) + 1,
137             $startfrom, 'startfrom'
138         ),
139         total     => $total,
140         from      => $from,
141         to        => $to,
142     );
143
144     unless (C4::Context->preference('OPACShowUnusedAuthorities')) {
145 #        TODO implement usage counts
146 #        my @usedauths = grep { $_->{used} > 0 } @$results;
147 #        $results = \@usedauths;
148     }
149
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;
157
158             unless ( $loggedinuser ) {
159                 my $new_search = C4::Search::History::add_to_session({
160                         cgi => $query,
161                         query_desc => $value[0],
162                         query_cgi => $query_cgi_history,
163                         total => $total,
164                         type => "authority",
165                 });
166             } else {
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,
173                     total => $total,
174                     type => "authority",
175                 });
176             }
177         }
178     }
179
180     $template->param( orderby => $orderby );
181     $template->param(
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',
189     );
190
191 }
192 else {
193     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
194         {
195             template_name   => "opac-authorities-home.tt",
196             query           => $query,
197             type            => 'opac',
198             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
199         }
200     );
201
202 }
203
204 $template->param(
205     authority_types => $authority_types,
206     authtypecode    => $authtypecode,
207 );
208
209 # Print the page
210 output_html_with_http_headers $query, $cookie, $template->output;