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