Bug 4198 - deleting an authority refreshes the page
[srvgit] / 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 ( sort { $authtypes->{$a}{'authtypetext'} cmp $authtypes->{$b}{'authtypetext'} }
47     keys %$authtypes )
48 {
49     my %row = (
50         value        => $thisauthtype,
51         selected     => $thisauthtype eq $authtypecode,
52         authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
53     );
54     push @authtypesloop, \%row;
55 }
56
57 if ( $op eq "delete" ) {
58     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
59         {
60             template_name   => "authorities/authorities-home.tmpl",
61             query           => $query,
62             type            => 'intranet',
63             authnotrequired => 0,
64             flagsrequired   => { catalogue => 1 },
65             debug           => 1,
66         }
67     );
68     &DelAuthority( $authid, 1 );
69
70     $op = "do_search";
71 }
72 if ( $op eq "do_search" ) {
73     my @marclist  = $query->param('marclist');
74     my @and_or    = $query->param('and_or');
75     my @excluding = $query->param('excluding');
76     my @operator  = $query->param('operator');
77     my $orderby   = $query->param('orderby');
78     my @value     = $query->param('value');
79
80     my $startfrom      = $query->param('startfrom')      || 1;
81     my $resultsperpage = $query->param('resultsperpage') || 20;
82
83     my ( $results, $total ) =
84       SearchAuthorities( \@marclist, \@and_or, \@excluding, \@operator, \@value,
85         ( $startfrom - 1 ) * $resultsperpage,
86         $resultsperpage, $authtypecode, $orderby );
87
88     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
89         {
90             template_name   => "authorities/searchresultlist.tmpl",
91             query           => $query,
92             type            => 'intranet',
93             authnotrequired => 0,
94             flagsrequired   => { catalogue => 1 },
95             debug           => 1,
96         }
97     );
98
99     $template->param(
100         marclist       => $query->param('marclist'),
101         and_or         => $query->param('and_or'),
102         excluding      => $query->param('excluding'),
103         operator       => $query->param('operator'),
104         orderby        => $query->param('orderby'),
105         value          => $query->param('value'),
106         authtypecode   => $query->param('authtypecode'),
107         startfrom      => $startfrom,
108         resultsperpage => $resultsperpage,
109     );
110
111     my @field_data = ();
112
113     # we must get parameters once again. Because if there is a mainentry, it
114     # has been replaced by something else during the search, thus the links
115     # next/previous would not work anymore
116     my @marclist_ini = $query->param('marclist');
117     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
118         if ($value[$i]){   
119           push @field_data, { term => "marclist",  val => $marclist_ini[$i] };
120           if (!defined $and_or[$i]) {
121               $and_or[$i] = q{};
122           }
123           push @field_data, { term => "and_or",    val => $and_or[$i] };
124           if (!defined $excluding[$i]) {
125               $excluding[$i] = q{};
126           }
127           push @field_data, { term => "excluding", val => $excluding[$i] };
128           push @field_data, { term => "operator",  val => $operator[$i] };
129           push @field_data, { term => "value",     val => $value[$i] };
130         }    
131     }
132
133     # construction of the url of each page
134     my $base_url =
135         'authorities-home.pl?'
136       . join( '&amp;', map { $_->{term} . '=' . $_->{val} } @field_data )
137       . '&amp;'
138       . join(
139         '&amp;',
140         map { $_->{term} . '=' . $_->{val} } (
141             { term => 'resultsperpage', val => $resultsperpage },
142             { term => 'type',           val => 'intranet' },
143             { term => 'op',             val => 'do_search' },
144             { term => 'authtypecode',   val => $authtypecode },
145             { term => 'orderby',        val => $orderby },
146         )
147       );
148
149     my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
150     my $to;
151     if (!defined $total) {
152         $total = 0;
153     }
154
155     if ( $total < $startfrom * $resultsperpage ) {
156         $to = $total;
157     }
158     else {
159         $to = $startfrom * $resultsperpage;
160     }
161
162     $template->param( result => $results ) if $results;
163
164     $template->param(
165         pagination_bar => pagination_bar(
166             $base_url,  int( $total / $resultsperpage ) + 1,
167             $startfrom, 'startfrom'
168         ),
169         total     => $total,
170         from      => $from,
171         to        => $to,
172         isEDITORS => $authtypecode eq 'EDITORS',
173     );
174
175 }
176 if ( $op eq '' ) {
177     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
178         {
179             template_name   => "authorities/authorities-home.tmpl",
180             query           => $query,
181             type            => 'intranet',
182             authnotrequired => 0,
183             flagsrequired   => { catalogue => 1 },
184             debug           => 1,
185         }
186     );
187
188 }
189
190 $template->param(
191     authtypesloop => \@authtypesloop,
192 );
193
194 $template->{VARS}->{marcflavour} = C4::Context->preference("marcflavour");
195
196 # Print the page
197 output_html_with_http_headers $query, $cookie, $template->output;