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