Bug 7067 QA Followup
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 use warnings;
23
24 use CGI;
25 use C4::Output;
26 use C4::Auth;
27 use C4::Context;
28 use C4::AuthoritiesMarc;
29 use C4::Acquisition;
30 use C4::Koha;    # XXX subfield_is_koha_internal_p
31
32 my $query        = new CGI;
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 $resultstring = $query->param('result');
38 my $relationship = $query->param('relationship');
39 my $dbh          = C4::Context->dbh;
40
41 my $startfrom = $query->param('startfrom');
42 $startfrom = 0 if ( !defined $startfrom );
43 my ( $template, $loggedinuser, $cookie );
44 my $resultsperpage;
45
46 my $authtypes = getauthtypes;
47 my @authtypesloop;
48 foreach my $thisauthtype ( keys %$authtypes ) {
49     my %row = (
50         value        => $thisauthtype,
51         selected     => ($thisauthtype eq $authtypecode),
52         authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
53         index        => $index,
54     );
55     push @authtypesloop, \%row;
56 }
57
58 $op ||= q{};
59 if ( $op eq "do_search" ) {
60     my @marclist  = $query->param('marclist');
61     my @and_or    = $query->param('and_or');
62     my @excluding = $query->param('excluding');
63     my @operator  = $query->param('operator');
64     my @value     = ($query->param('value_mainstr')||undef, $query->param('value_main')||undef, $query->param('value_any')||undef, $query->param('value_match')||undef);
65     my $orderby   = $query->param('orderby');
66
67     $resultsperpage = $query->param('resultsperpage');
68     $resultsperpage = 20 if ( !defined $resultsperpage );
69
70     my ( $results, $total ) =
71       SearchAuthorities( \@marclist, \@and_or, \@excluding, \@operator, \@value,
72         $startfrom * $resultsperpage,
73         $resultsperpage, $authtypecode, $orderby);
74
75     # If an authority heading is repeated, add an arrayref to those repetions
76     # First heading -- Second heading
77     for my $heading ( @$results ) {
78         my @repets = split / -- /, $heading->{summary};
79         if ( @repets > 1 ) {
80             my @repets_loop;
81             for (my $i = 0; $i < @repets; $i++) {
82                 push @repets_loop,
83                     { index => $index, repet => $i+1, value => $repets[$i] };
84             }
85             $heading->{repets} = \@repets_loop;
86         }
87     }
88     # multi page display gestion
89     my $displaynext = 0;
90     my $displayprev = $startfrom;
91     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
92         $displaynext = 1;
93     }
94
95     my @field_data = ();
96
97     my @marclist_ini =
98       $query->param('marclist')
99       ; # get marclist again, as the previous one has been modified by catalogsearch (mainentry replaced by field name
100     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
101         push @field_data, { term => "marclist",  val => $marclist_ini[$i] };
102         push @field_data, { term => "and_or",    val => $and_or[$i] };
103         push @field_data, { term => "excluding", val => $excluding[$i] };
104         push @field_data, { term => "operator",  val => $operator[$i] };
105     }
106
107     push @field_data, { term => "value_mainstr", val => $query->param('value_mainstr') || "" };
108     push @field_data, { term => "value_main",    val => $query->param('value_main')    || "" };
109     push @field_data, { term => "value_any",     val => $query->param('value_any')     || ""};
110     push @field_data, { term => "value_match",   val => $query->param('value_match')   || ""};
111
112     my @numbers = ();
113
114     if ( $total > $resultsperpage ) {
115         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
116             if ( $i < 16 ) {
117                 my $highlight = 0;
118                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
119                 push @numbers,
120                   {
121                     number     => $i,
122                     highlight  => $highlight,
123                     searchdata => \@field_data,
124                     startfrom  => ( $i - 1 )
125                   };
126             }
127         }
128     }
129
130     my $from = $startfrom * $resultsperpage + 1;
131     my $to;
132
133     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
134         $to = $total;
135     }
136     else {
137         $to = ( ( $startfrom + 1 ) * $resultsperpage );
138     }
139     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
140         {
141             template_name   => "authorities/searchresultlist-auth.tt",
142             query           => $query,
143             type            => 'intranet',
144             authnotrequired => 0,
145             flagsrequired   => { catalogue => 1 },
146         }
147     );
148
149     $template->param( result => $results ) if $results;
150     $template->param(
151         orderby      => $orderby,
152         startfrom      => $startfrom,
153     displaynext    => $displaynext,
154     displayprev    => $displayprev,
155     resultsperpage => $resultsperpage,
156     startfromnext  => $startfrom + 1,
157     startfromprev  => $startfrom - 1,
158         searchdata     => \@field_data,
159         total          => $total,
160         from           => $from,
161         to             => $to,
162         numbers        => \@numbers,
163         authtypecode   => $authtypecode,
164         value_mainstr  => $query->param('value_mainstr') || "", 
165         value_main     => $query->param('value_main') || "",
166         value_any      => $query->param('value_any') || "",
167         value_match    => $query->param('value_match') || "",
168     );
169 } else {
170     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
171         {
172             template_name   => "authorities/auth_finder.tt",
173             query           => $query,
174             type            => 'intranet',
175             authnotrequired => 0,
176             flagsrequired   => { catalogue => 1 },
177         }
178     );
179
180     $template->param(
181         resultstring => $resultstring,
182     );
183 }
184
185 $template->param(
186     value_mainstr => $query->param('value_mainstr') || "", 
187     value_main    => $query->param('value_main') || "",
188     value_any     => $query->param('value_any') || "",
189     value_match   => $query->param('value_match') || "",
190     tagid         => $tagid,
191     index         => $index,
192     authtypesloop => \@authtypesloop,
193     authtypecode  => $authtypecode,
194 );
195
196 $template->{VARS}->{source} = $query->param('source') || '';
197 $template->{VARS}->{relationship} = $query->param('relationship') || '';
198
199 # Print the page
200 output_html_with_http_headers $query, $cookie, $template->output;
201
202 # Local Variables:
203 # tab-width: 4
204 # End: