Bug 12110: (follow-up) improve whitespace
[koha.git] / admin / categorie.pl
1 #!/usr/bin/perl
2
3 #script to administer the categories table
4 #written 20/02/2002 by paul.poulain@free.fr
5
6 # ALGO :
7 # this script use an $op to know what to do.
8 # if $op is empty or none of the above values,
9 #       - the default screen is build (with all records, or filtered datas).
10 #       - the   user can clic on add, modify or delete record.
11 # if $op=add_form
12 #       - if primkey exists, this is a modification,so we read the $primkey record
13 #       - builds the add/modify form
14 # if $op=add_validate
15 #       - the user has just send datas, so we create/modify the record
16 # if $op=delete_form
17 #       - we show the record having primkey=$primkey and ask for deletion validation form
18 # if $op=delete_confirm
19 #       - we delete the record having primkey=$primkey
20
21
22 # Copyright 2000-2002 Katipo Communications
23 #
24 # This file is part of Koha.
25 #
26 # Koha is free software; you can redistribute it and/or modify it under the
27 # terms of the GNU General Public License as published by the Free Software
28 # Foundation; either version 2 of the License, or (at your option) any later
29 # version.
30 #
31 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
32 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
33 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
34 #
35 # You should have received a copy of the GNU General Public License along
36 # with Koha; if not, write to the Free Software Foundation, Inc.,
37 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
38
39 use Modern::Perl;
40
41 use CGI;
42 use C4::Context;
43 use C4::Auth;
44 use C4::Branch;
45 use C4::Output;
46 use C4::Dates;
47 use C4::Form::MessagingPreferences;
48
49 sub StringSearch  {
50         my ($searchstring,$type)=@_;
51         my $dbh = C4::Context->dbh;
52         $searchstring //= '';
53         $searchstring=~ s/\'/\\\'/g;
54         my @data=split(' ',$searchstring);
55         push @data,q{} if $#data==-1;
56         my $count=@data;
57         my $sth=$dbh->prepare("Select * from categories where (description like ?) order by category_type,description,categorycode");
58         $sth->execute("$data[0]%");
59         my @results;
60         while (my $data=$sth->fetchrow_hashref){
61         push(@results,$data);
62         }
63         #  $sth->execute;
64         $sth->finish;
65         return (scalar(@results),\@results);
66 }
67
68 my $input = new CGI;
69 my $searchfield=$input->param('description');
70 my $script_name="/cgi-bin/koha/admin/categorie.pl";
71 my $categorycode=$input->param('categorycode');
72 my $op = $input->param('op') // '';
73 my $block_expired = $input->param("block_expired");
74
75 my ($template, $loggedinuser, $cookie)
76     = get_template_and_user({template_name => "admin/categorie.tmpl",
77                              query => $input,
78                              type => "intranet",
79                              authnotrequired => 0,
80                  flagsrequired => {parameters => 'parameters_remaining_permissions'},
81                              debug => 1,
82                              });
83
84
85 $template->param(script_name => $script_name,
86                  categorycode => $categorycode,
87                  searchfield => $searchfield);
88
89
90 ################## ADD_FORM ##################################
91 # called by default. Used to create form to add or  modify a record
92 if ($op eq 'add_form') {
93         $template->param(add_form => 1);
94         
95         #---- if primkey exists, it's a modify action, so read values to modify...
96         my $data;
97     my @selected_branches;
98         if ($categorycode) {
99                 my $dbh = C4::Context->dbh;
100          my $sth=$dbh->prepare("SELECT * FROM categories WHERE categorycode=?");
101                 $sth->execute($categorycode);
102                 $data=$sth->fetchrow_hashref;
103
104         $sth = $dbh->prepare("SELECT b.branchcode, b.branchname FROM categories_branches AS cb, branches AS b WHERE cb.branchcode = b.branchcode AND cb.categorycode = ?");
105         $sth->execute( $categorycode );
106         while ( my $branch = $sth->fetchrow_hashref ) {
107             push @selected_branches, $branch;
108         }
109         $sth->finish;
110     }
111
112     if ($data->{'enrolmentperioddate'} && $data->{'enrolmentperioddate'} eq '0000-00-00') {
113         $data->{'enrolmentperioddate'} = undef;
114     }
115     $data->{'category_type'} //= '';
116
117     my $branches = GetBranches;
118     my @branches_loop;
119     foreach my $branch (sort keys %$branches) {
120         my $selected = ( grep {$$_{branchcode} eq $branch} @selected_branches ) ? 1 : 0;
121         push @branches_loop, {
122             branchcode => $$branches{$branch}{branchcode},
123             branchname => $$branches{$branch}{branchname},
124             selected => $selected,
125         };
126     }
127
128         $template->param(description        => $data->{'description'},
129                                 enrolmentperiod         => $data->{'enrolmentperiod'},
130                          enrolmentperioddate     => $data->{'enrolmentperioddate'},
131                                 upperagelimit           => $data->{'upperagelimit'},
132                                 dateofbirthrequired     => $data->{'dateofbirthrequired'},
133                          enrolmentfee            => sprintf("%.2f",$data->{'enrolmentfee'} || 0),
134                                 overduenoticerequired   => $data->{'overduenoticerequired'},
135                                 issuelimit              => $data->{'issuelimit'},
136                          reservefee              => sprintf("%.2f",$data->{'reservefee'} || 0),
137                                 hidelostitems           => $data->{'hidelostitems'},
138                                 category_type           => $data->{'category_type'},
139                 SMSSendDriver => C4::Context->preference("SMSSendDriver"),
140                 TalkingTechItivaPhone => C4::Context->preference("TalkingTechItivaPhoneNotification"),
141                                 "type_".$data->{'category_type'} => 1,
142                 branches_loop           => \@branches_loop,
143                 BlockExpiredPatronOpacActions => $data->{'BlockExpiredPatronOpacActions'},
144                                 );
145     if (C4::Context->preference('EnhancedMessagingPreferences')) {
146         C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode } , $template);
147     }
148                                                                                                         # END $OP eq ADD_FORM
149 ################## ADD_VALIDATE ##################################
150 # called by add_form, used to insert/modify data in DB
151 } elsif ($op eq 'add_validate') {
152         $template->param(add_validate => 1);
153         my $is_a_modif = $input->param("is_a_modif");
154         my $dbh = C4::Context->dbh;
155         if($input->param('enrolmentperioddate')){
156             $input->param('enrolmentperioddate' => C4::Dates::format_date_in_iso($input->param('enrolmentperioddate')) );
157         }
158         
159         if ($is_a_modif) {
160             my $sth=$dbh->prepare("
161                 UPDATE categories
162                 SET description=?,
163                     enrolmentperiod=?,
164                     enrolmentperioddate=?,
165                     upperagelimit=?,
166                     dateofbirthrequired=?,
167                     enrolmentfee=?,
168                     reservefee=?,
169                     hidelostitems=?,
170                     overduenoticerequired=?,
171                     category_type=?,
172                     BlockExpiredPatronOpacActions=?
173                 WHERE categorycode=?"
174             );
175             $sth->execute(
176                 map { $input->param($_) } (
177                     'description',
178                     'enrolmentperiod',
179                     'enrolmentperioddate',
180                     'upperagelimit',
181                     'dateofbirthrequired',
182                     'enrolmentfee',
183                     'reservefee',
184                     'hidelostitems',
185                     'overduenoticerequired',
186                     'category_type',
187                     'block_expired',
188                     'categorycode'
189                 )
190             );
191             my @branches = $input->param("branches");
192             if ( @branches ) {
193                 $sth = $dbh->prepare("DELETE FROM categories_branches WHERE categorycode = ?");
194                 $sth->execute( $input->param( "categorycode" ) );
195                 $sth = $dbh->prepare(
196                     "INSERT INTO categories_branches
197                                 ( categorycode, branchcode )
198                                 VALUES ( ?, ? )"
199                 );
200                 for my $branchcode ( @branches ) {
201                     next if not $branchcode;
202                     $sth->bind_param( 1, $input->param( "categorycode" ) );
203                     $sth->bind_param( 2, $branchcode );
204                     $sth->execute;
205                 }
206             }
207             $sth->finish;
208     } else {
209         my $sth=$dbh->prepare("
210             INSERT INTO categories (
211                 categorycode,
212                 description,
213                 enrolmentperiod,
214                 enrolmentperioddate,
215                 upperagelimit,
216                 dateofbirthrequired,
217                 enrolmentfee,
218                 reservefee,
219                 hidelostitems,
220                 overduenoticerequired,
221                 category_type,
222                 BlockExpiredPatronOpacActions
223             )
224             VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
225         $sth->execute(
226             map { $input->param($_) } (
227                 'categorycode',
228                 'description',
229                 'enrolmentperiod',
230                 'enrolmentperioddate',
231                 'upperagelimit',
232                 'dateofbirthrequired',
233                 'enrolmentfee',
234                 'reservefee',
235                 'hidelostitems',
236                 'overduenoticerequired',
237                 'category_type',
238                 'block_expired'
239             )
240         );
241         $sth->finish;
242     }
243
244     if (C4::Context->preference('EnhancedMessagingPreferences')) {
245         C4::Form::MessagingPreferences::handle_form_action($input, 
246                                                            { categorycode => $input->param('categorycode') }, $template);
247     }
248         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
249         exit;
250
251                                                                                                         # END $OP eq ADD_VALIDATE
252 ################## DELETE_CONFIRM ##################################
253 # called by default form, used to confirm deletion of data in DB
254 } elsif ($op eq 'delete_confirm') {
255         $template->param(delete_confirm => 1);
256
257         my $dbh = C4::Context->dbh;
258         my $sth=$dbh->prepare("select count(*) as total from borrowers where categorycode=?");
259         $sth->execute($categorycode);
260         my $total = $sth->fetchrow_hashref;
261         $sth->finish;
262         $template->param(total => $total->{'total'});
263         
264         my $sth2=$dbh->prepare("SELECT * FROM categories WHERE categorycode=?");
265         $sth2->execute($categorycode);
266         my $data=$sth2->fetchrow_hashref;
267         $sth2->finish;
268         if ($total->{'total'} >0) {
269                 $template->param(totalgtzero => 1);
270         }
271
272     if ($data->{'enrolmentperioddate'} && $data->{'enrolmentperioddate'} eq '0000-00-00') {
273         $data->{'enrolmentperioddate'} = undef;
274     }
275         $template->param(       description             => $data->{'description'},
276                                 enrolmentperiod         => $data->{'enrolmentperiod'},
277                                 enrolmentperioddate     => $data->{'enrolmentperioddate'},
278                                 upperagelimit           => $data->{'upperagelimit'},
279                                 dateofbirthrequired     => $data->{'dateofbirthrequired'},
280                                 enrolmentfee            =>  sprintf("%.2f",$data->{'enrolmentfee'} || 0),
281                                 overduenoticerequired   => $data->{'overduenoticerequired'},
282                                 issuelimit              => $data->{'issuelimit'},
283                                 reservefee              =>  sprintf("%.2f",$data->{'reservefee'} || 0),
284                                 hidelostitems           => $data->{'hidelostitems'},
285                                 category_type           => $data->{'category_type'},
286                                 BlockExpiredPatronOpacActions => $data->{'BlockExpiredPatronOpacActions'},
287                                 );
288                                                                                                         # END $OP eq DELETE_CONFIRM
289 ################## DELETE_CONFIRMED ##################################
290 # called by delete_confirm, used to effectively confirm deletion of data in DB
291 } elsif ($op eq 'delete_confirmed') {
292         $template->param(delete_confirmed => 1);
293         my $dbh = C4::Context->dbh;
294         my $categorycode=uc($input->param('categorycode'));
295         my $sth=$dbh->prepare("delete from categories where categorycode=?");
296         $sth->execute($categorycode);
297         $sth->finish;
298         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
299         exit;
300
301                                                                                                         # END $OP eq DELETE_CONFIRMED
302 } else { # DEFAULT
303         $template->param(else => 1);
304         my @loop;
305         my ($count,$results)=StringSearch($searchfield,'web');
306     my $dbh = C4::Context->dbh;
307     my $sth = $dbh->prepare("SELECT b.branchcode, b.branchname FROM categories_branches AS cb, branches AS b WHERE cb.branchcode = b.branchcode AND cb.categorycode = ?");
308         for (my $i=0; $i < $count; $i++){
309         $sth->execute( $results->[$i]{'categorycode'} );
310         my @selected_branches;
311         while ( my $branch = $sth->fetchrow_hashref ) {
312             push @selected_branches, $branch;
313         }
314         my $enrolmentperioddate = $results->[$i]{'enrolmentperioddate'};
315     if ($enrolmentperioddate && $enrolmentperioddate eq '0000-00-00') {
316         $enrolmentperioddate = undef;
317     }
318     $results->[$i]{'category_type'} //= '';
319                 my %row = (
320                         categorycode            => $results->[$i]{'categorycode'},
321                                 description             => $results->[$i]{'description'},
322                                 enrolmentperiod         => $results->[$i]{'enrolmentperiod'},
323                         enrolmentperioddate     => $enrolmentperioddate,
324                                 upperagelimit           => $results->[$i]{'upperagelimit'},
325                                 dateofbirthrequired     => $results->[$i]{'dateofbirthrequired'},
326                         enrolmentfee            => sprintf("%.2f",$results->[$i]{'enrolmentfee'} || 0),
327                                 overduenoticerequired   => $results->[$i]{'overduenoticerequired'},
328                                 issuelimit              => $results->[$i]{'issuelimit'},
329                         reservefee              => sprintf("%.2f",$results->[$i]{'reservefee'} || 0),
330                                 hidelostitems           => $results->[$i]{'hidelostitems'},
331                                 category_type           => $results->[$i]{'category_type'},
332                 "type_".$results->[$i]{'category_type'} => 1,
333                 branches                => \@selected_branches,
334         );
335         if (C4::Context->preference('EnhancedMessagingPreferences')) {
336             my $brief_prefs = _get_brief_messaging_prefs($results->[$i]{'categorycode'});
337             $row{messaging_prefs} = $brief_prefs if @$brief_prefs;
338         }
339                 push @loop, \%row;
340         }
341         $template->param(loop => \@loop);
342         # check that I (institution) and C (child) exists. otherwise => warning to the user
343     $sth=$dbh->prepare("select category_type from categories where category_type='C'");
344         $sth->execute;
345         my ($categoryChild) = $sth->fetchrow;
346         $template->param(categoryChild => $categoryChild);
347         $sth=$dbh->prepare("select category_type from categories where category_type='I'");
348         $sth->execute;
349         my ($categoryInstitution) = $sth->fetchrow;
350         $template->param(categoryInstitution => $categoryInstitution);
351         $sth->finish;
352
353
354 } #---- END $OP eq DEFAULT
355 output_html_with_http_headers $input, $cookie, $template->output;
356
357 exit 0;
358
359 sub _get_brief_messaging_prefs {
360     my $categorycode = shift;
361     my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
362     my $results = [];
363     PREF: foreach my $option ( @$messaging_options ) {
364         my $pref = C4::Members::Messaging::GetMessagingPreferences( { categorycode => $categorycode,
365                                                                     message_name       => $option->{'message_name'} } );
366         next unless  $pref->{'transports'};
367         my $brief_pref = {
368             message_attribute_id    => $option->{'message_attribute_id'},
369             message_name            => $option->{'message_name'},
370             $option->{'message_name'} => 1
371         };
372         foreach my $transport ( keys %{$pref->{'transports'}} ) {
373             push @{ $brief_pref->{'transports'} }, { transport => $transport };
374         }
375         push @$results, $brief_pref;
376     }
377     return $results;
378 }