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