Bug 36845: Do not loop over the content attribute
[koha.git] / authorities / auth_finder.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 C4::Output qw( output_html_with_http_headers );
24 use C4::Auth qw( get_template_and_user );
25 use C4::Context;
26 use C4::Languages;
27 use Koha::SearchEngine::Search;
28 use Koha::SearchEngine::QueryBuilder;
29
30 use Koha::Authority::Types;
31 use Koha::Authorities;
32 use Koha::XSLT::Base;
33
34 my $query        = CGI->new;
35 my $op           = $query->param('op') || '';
36 my $authtypecode = $query->param('authtypecode') || '';
37 my $index        = $query->param('index') || '';
38 my $tagid        = $query->param('tagid') || '';
39 my $source       = $query->param('source') || '';
40 my $relationship = $query->param('relationship') || '';
41
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43     {
44         template_name => ( $op eq 'do_search' )
45         ? 'authorities/searchresultlist-auth.tt'
46         : 'authorities/auth_finder.tt',
47         query           => $query,
48         type            => 'intranet',
49         flagsrequired   => { catalogue => 1 },
50     }
51 );
52
53 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
54
55 # If search form posted
56 if ( $op eq "do_search" ) {
57     my @marclist  = $query->multi_param('marclist');
58     my @and_or    = $query->multi_param('and_or');
59     my @excluding = $query->multi_param('excluding');
60     my @operator  = $query->multi_param('operator');
61     my @value     = (
62         $query->param('value_mainstr') || undef,
63         $query->param('value_main')    || undef,
64         $query->param('value_match')   || undef,
65         $query->param('value_any')     || undef,
66     );
67     my $orderby        = $query->param('orderby')        || '';
68     my $startfrom      = $query->param('startfrom')      || 0;
69     my $resultsperpage = $query->param('resultsperpage') || 20;
70
71     my $builder = Koha::SearchEngine::QueryBuilder->new(
72         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
73     my $searcher = Koha::SearchEngine::Search->new(
74         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
75     my $search_query = $builder->build_authorities_query_compat(
76         \@marclist, \@and_or, \@excluding, \@operator,
77         \@value, $authtypecode, $orderby
78     );
79     $template->param( search_query => $search_query ) if C4::Context->preference('DumpSearchQueryTemplate');
80     my $offset = $startfrom * $resultsperpage;
81     my ( $results, $total ) =
82         $searcher->search_auth_compat( $search_query, $offset,
83         $resultsperpage );
84
85     # multi page display gestion
86     my $displaynext = 0;
87     my $displayprev = $startfrom;
88     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
89         $displaynext = 1;
90     }
91
92     my @field_data = ();
93
94 # get marclist again, as the previous one has been modified by catalogsearch (mainentry replaced by field name)
95     my @marclist_ini = $query->multi_param('marclist');
96     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
97         push @field_data, { term => "marclist",  val => $marclist_ini[$i] };
98         push @field_data, { term => "and_or",    val => $and_or[$i] };
99         push @field_data, { term => "excluding", val => $excluding[$i] };
100         push @field_data, { term => "operator",  val => $operator[$i] };
101     }
102
103     push @field_data,
104       { term => "value_mainstr", val => scalar $query->param('value_mainstr') || "" };
105     push @field_data,
106       { term => "value_main", val => scalar $query->param('value_main') || "" };
107     push @field_data,
108       { term => "value_match", val => scalar $query->param('value_match') || "" };
109     push @field_data,
110       { term => "value_any", val => scalar $query->param('value_any') || "" };
111
112     my @numbers = ();
113     if ( $total > $resultsperpage ) {
114         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
115             if ( $i < 16 ) {
116                 my $highlight = 0;
117                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
118                 push @numbers,
119                   {
120                     number     => $i,
121                     highlight  => $highlight,
122                     searchdata => \@field_data,
123                     startfrom  => ( $i - 1 )
124                   };
125             }
126         }
127     }
128
129     my $from = $startfrom * $resultsperpage + 1;
130     my $to;
131     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
132         $to = $total;
133     }
134     else {
135         $to = ( ( $startfrom + 1 ) * $resultsperpage );
136     }
137
138     my $AuthorityXSLTResultsDisplay = C4::Context->preference('AuthorityXSLTResultsDisplay');
139     if ( $results && $AuthorityXSLTResultsDisplay ) {
140         my $lang = C4::Languages::getlanguage();
141         foreach my $result (@$results) {
142             my $authority = Koha::Authorities->find( $result->{authid} );
143             next unless $authority;
144
145             my $authtypecode = $authority->authtypecode;
146             my $xsl          = $AuthorityXSLTResultsDisplay;
147             $xsl =~ s/\{langcode\}/$lang/g;
148             $xsl =~ s/\{authtypecode\}/$authtypecode/g;
149
150             my $xslt_engine = Koha::XSLT::Base->new;
151             my $output      = $xslt_engine->transform( { xml => $authority->marcxml, file => $xsl } );
152             if ( $xslt_engine->err ) {
153                 warn "XSL transformation failed ($xsl): " . $xslt_engine->err;
154                 next;
155             }
156
157             $result->{html} = $output;
158         }
159     }
160
161     $template->param( result => $results ) if $results;
162     $template->param(
163         orderby          => $orderby,
164         startfrom        => $startfrom,
165         displaynext      => $displaynext,
166         displayprev      => $displayprev,
167         resultsperpage   => $resultsperpage,
168         startfromnext    => $startfrom + 1,
169         startfromprev    => $startfrom - 1,
170         searchdata       => \@field_data,
171         total            => $total,
172         from             => $from,
173         to               => $to,
174         numbers          => \@numbers,
175         operator_mainstr => ( @operator > 0 && $operator[0] ) ? $operator[0] : '',
176         operator_main    => ( @operator > 1 && $operator[1] ) ? $operator[1] : '',
177         operator_match   => ( @operator > 2 && $operator[2] ) ? $operator[2] : '',
178         operator_any     => ( @operator > 3 && $operator[3] ) ? $operator[3] : '',
179     );
180 }
181 else {
182
183     # special case for UNIMARC field 210c builder
184     my $resultstring = $query->param('result') || '';
185     $template->param( resultstring => $resultstring, );
186 }
187
188 $template->param(
189     op            => $op,
190     value_mainstr => scalar $query->param('value_mainstr') || '',
191     value_main    => scalar $query->param('value_main') || '',
192     value_any     => scalar $query->param('value_any') || '',
193     value_match   => scalar $query->param('value_match') || '',
194     tagid         => $tagid,
195     index         => $index,
196     authority_types  => $authority_types,
197     authtypecode  => $authtypecode,
198     source        => $source,
199     relationship  => $relationship,
200 );
201
202 # Print the page
203 output_html_with_http_headers $query, $cookie, $template->output;