Bug 4198 - Followup - PerlTidy authorities-home.pl
[koha.git] / authorities / authorities-home.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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use CGI;
24 use C4::Auth;
25
26 use C4::Context;
27 use C4::Auth;
28 use C4::Output;
29 use C4::AuthoritiesMarc;
30 use C4::Acquisition;
31 use C4::Koha;    # XXX subfield_is_koha_internal_p
32 use C4::Biblio;
33
34 my $query = new CGI;
35 my $op    = $query->param('op');
36 $op ||= q{};
37 my $authtypecode = $query->param('authtypecode');
38 $authtypecode ||= q{};
39 my $dbh = C4::Context->dbh;
40
41 my $authid = $query->param('authid');
42 my ( $template, $loggedinuser, $cookie );
43
44 my $authtypes = getauthtypes;
45 my @authtypesloop;
46 foreach my $thisauthtype (
47     sort {
48         $authtypes->{$a}{'authtypetext'} cmp $authtypes->{$b}{'authtypetext'}
49     }
50     keys %$authtypes
51   )
52 {
53     my %row = (
54         value        => $thisauthtype,
55         selected     => $thisauthtype eq $authtypecode,
56         authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
57     );
58     push @authtypesloop, \%row;
59 }
60
61 if ( $op eq "delete" ) {
62     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
63         {
64             template_name   => "authorities/authorities-home.tmpl",
65             query           => $query,
66             type            => 'intranet',
67             authnotrequired => 0,
68             flagsrequired   => { catalogue => 1 },
69             debug           => 1,
70         }
71     );
72     &DelAuthority( $authid, 1 );
73
74     $op = "do_search";
75 }
76 if ( $op eq "do_search" ) {
77     my @marclist  = $query->param('marclist');
78     my @and_or    = $query->param('and_or');
79     my @excluding = $query->param('excluding');
80     my @operator  = $query->param('operator');
81     my $orderby   = $query->param('orderby');
82     my @value     = $query->param('value');
83
84     my $startfrom      = $query->param('startfrom')      || 1;
85     my $resultsperpage = $query->param('resultsperpage') || 20;
86
87     my ( $results, $total ) =
88       SearchAuthorities( \@marclist, \@and_or, \@excluding, \@operator, \@value,
89         ( $startfrom - 1 ) * $resultsperpage,
90         $resultsperpage, $authtypecode, $orderby );
91
92     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
93         {
94             template_name   => "authorities/searchresultlist.tmpl",
95             query           => $query,
96             type            => 'intranet',
97             authnotrequired => 0,
98             flagsrequired   => { catalogue => 1 },
99             debug           => 1,
100         }
101     );
102
103     $template->param(
104         marclist       => $query->param('marclist'),
105         and_or         => $query->param('and_or'),
106         excluding      => $query->param('excluding'),
107         operator       => $query->param('operator'),
108         orderby        => $query->param('orderby'),
109         value          => $query->param('value'),
110         authtypecode   => $query->param('authtypecode'),
111         startfrom      => $startfrom,
112         resultsperpage => $resultsperpage,
113     );
114
115     my @field_data = ();
116
117     # we must get parameters once again. Because if there is a mainentry, it
118     # has been replaced by something else during the search, thus the links
119     # next/previous would not work anymore
120     my @marclist_ini = $query->param('marclist');
121     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
122         if ( $value[$i] ) {
123             push @field_data, { term => "marclist", val => $marclist_ini[$i] };
124             if ( !defined $and_or[$i] ) {
125                 $and_or[$i] = q{};
126             }
127             push @field_data, { term => "and_or", val => $and_or[$i] };
128             if ( !defined $excluding[$i] ) {
129                 $excluding[$i] = q{};
130             }
131             push @field_data, { term => "excluding", val => $excluding[$i] };
132             push @field_data, { term => "operator",  val => $operator[$i] };
133             push @field_data, { term => "value",     val => $value[$i] };
134         }
135     }
136
137     # construction of the url of each page
138     my $base_url =
139         'authorities-home.pl?'
140       . join( '&amp;', map { $_->{term} . '=' . $_->{val} } @field_data )
141       . '&amp;'
142       . join(
143         '&amp;',
144         map { $_->{term} . '=' . $_->{val} } (
145             { term => 'resultsperpage', val => $resultsperpage },
146             { term => 'type',           val => 'intranet' },
147             { term => 'op',             val => 'do_search' },
148             { term => 'authtypecode',   val => $authtypecode },
149             { term => 'orderby',        val => $orderby },
150         )
151       );
152
153     my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
154     my $to;
155     if ( !defined $total ) {
156         $total = 0;
157     }
158
159     if ( $total < $startfrom * $resultsperpage ) {
160         $to = $total;
161     }
162     else {
163         $to = $startfrom * $resultsperpage;
164     }
165
166     $template->param( result => $results ) if $results;
167
168     $template->param(
169         pagination_bar => pagination_bar(
170             $base_url,  int( $total / $resultsperpage ) + 1,
171             $startfrom, 'startfrom'
172         ),
173         total     => $total,
174         from      => $from,
175         to        => $to,
176         isEDITORS => $authtypecode eq 'EDITORS',
177     );
178
179 }
180 if ( $op eq '' ) {
181     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
182         {
183             template_name   => "authorities/authorities-home.tmpl",
184             query           => $query,
185             type            => 'intranet',
186             authnotrequired => 0,
187             flagsrequired   => { catalogue => 1 },
188             debug           => 1,
189         }
190     );
191
192 }
193
194 $template->param( authtypesloop => \@authtypesloop, );
195
196 $template->{VARS}->{marcflavour} = C4::Context->preference("marcflavour");
197
198 # Print the page
199 output_html_with_http_headers $query, $cookie, $template->output;