Bug 7359 - Begin migration to a new "Koha" namespace from the old "C4" namespace
[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 strict;
40 #use warnings; FIXME - Bug 2505
41 use CGI;
42 use C4::Context;
43 use C4::Auth;
44 use C4::Output;
45 use C4::Dates;
46 use C4::Form::MessagingPreferences;
47
48 sub StringSearch  {
49         my ($searchstring,$type)=@_;
50         my $dbh = C4::Context->dbh;
51         $searchstring=~ s/\'/\\\'/g;
52         my @data=split(' ',$searchstring);
53         my $count=@data;
54         my $sth=$dbh->prepare("Select * from categories where (description like ?) order by category_type,description,categorycode");
55         $sth->execute("$data[0]%");
56         my @results;
57         while (my $data=$sth->fetchrow_hashref){
58         push(@results,$data);
59         }
60         #  $sth->execute;
61         $sth->finish;
62         return (scalar(@results),\@results);
63 }
64
65 my $input = new CGI;
66 my $searchfield=$input->param('description');
67 my $script_name="/cgi-bin/koha/admin/categorie.pl";
68 my $categorycode=$input->param('categorycode');
69 my $op = $input->param('op');
70
71 my ($template, $loggedinuser, $cookie)
72     = get_template_and_user({template_name => "admin/categorie.tmpl",
73                              query => $input,
74                              type => "intranet",
75                              authnotrequired => 0,
76                              flagsrequired => {parameters => 1},
77                              debug => 1,
78                              });
79
80
81 $template->param(script_name => $script_name,
82                  categorycode => $categorycode,
83                  searchfield => $searchfield);
84
85
86 ################## ADD_FORM ##################################
87 # called by default. Used to create form to add or  modify a record
88 if ($op eq 'add_form') {
89         $template->param(add_form => 1);
90         
91         #---- if primkey exists, it's a modify action, so read values to modify...
92         my $data;
93         if ($categorycode) {
94                 my $dbh = C4::Context->dbh;
95                 my $sth=$dbh->prepare("select categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,hidelostitems,overduenoticerequired,category_type from categories where categorycode=?");
96                 $sth->execute($categorycode);
97                 $data=$sth->fetchrow_hashref;
98                 $sth->finish;
99         }
100
101     $data->{'enrolmentperioddate'} = undef if ($data->{'enrolmentperioddate'} eq '0000-00-00');
102
103         $template->param(description        => $data->{'description'},
104                                 enrolmentperiod         => $data->{'enrolmentperiod'},
105                                 enrolmentperioddate     => C4::Dates::format_date($data->{'enrolmentperioddate'}),
106                                 upperagelimit           => $data->{'upperagelimit'},
107                                 dateofbirthrequired     => $data->{'dateofbirthrequired'},
108                                 enrolmentfee            => sprintf("%.2f",$data->{'enrolmentfee'}),
109                                 overduenoticerequired   => $data->{'overduenoticerequired'},
110                                 issuelimit              => $data->{'issuelimit'},
111                                 reservefee              => sprintf("%.2f",$data->{'reservefee'}),
112                                 hidelostitems           => $data->{'hidelostitems'},
113                                 category_type           => $data->{'category_type'},
114                                 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
115                                 "type_".$data->{'category_type'} => 1,
116                                 SMSSendDriver => C4::Context->preference("SMSSendDriver")
117                                 );
118     if (C4::Context->preference('EnhancedMessagingPreferences')) {
119         C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode } , $template);
120     }
121                                                                                                         # END $OP eq ADD_FORM
122 ################## ADD_VALIDATE ##################################
123 # called by add_form, used to insert/modify data in DB
124 } elsif ($op eq 'add_validate') {
125         $template->param(add_validate => 1);
126         my $is_a_modif = $input->param("is_a_modif");
127         my $dbh = C4::Context->dbh;
128         if($input->param('enrolmentperioddate')){
129             $input->param('enrolmentperioddate' => C4::Dates::format_date_in_iso($input->param('enrolmentperioddate')) );
130         }
131         
132         if ($is_a_modif) {
133             my $sth=$dbh->prepare("UPDATE categories SET description=?,enrolmentperiod=?, enrolmentperioddate=?,upperagelimit=?,dateofbirthrequired=?,enrolmentfee=?,reservefee=?,hidelostitems=?,overduenoticerequired=?,category_type=? WHERE categorycode=?");
134             $sth->execute(map { $input->param($_) } ('description','enrolmentperiod','enrolmentperioddate','upperagelimit','dateofbirthrequired','enrolmentfee','reservefee','hidelostitems','overduenoticerequired','category_type','categorycode'));
135             $sth->finish;
136         } else {
137             my $sth=$dbh->prepare("INSERT INTO categories  (categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,enrolmentfee,reservefee,hidelostitems,overduenoticerequired,category_type) values (?,?,?,?,?,?,?,?,?,?,?)");
138             $sth->execute(map { $input->param($_) } ('categorycode','description','enrolmentperiod','enrolmentperioddate','upperagelimit','dateofbirthrequired','enrolmentfee','reservefee','hidelostitems','overduenoticerequired','category_type'));
139             $sth->finish;
140         }
141     if (C4::Context->preference('EnhancedMessagingPreferences')) {
142         C4::Form::MessagingPreferences::handle_form_action($input, 
143                                                            { categorycode => $input->param('categorycode') }, $template);
144     }
145         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
146         exit;
147
148                                                                                                         # END $OP eq ADD_VALIDATE
149 ################## DELETE_CONFIRM ##################################
150 # called by default form, used to confirm deletion of data in DB
151 } elsif ($op eq 'delete_confirm') {
152         $template->param(delete_confirm => 1);
153
154         my $dbh = C4::Context->dbh;
155         my $sth=$dbh->prepare("select count(*) as total from borrowers where categorycode=?");
156         $sth->execute($categorycode);
157         my $total = $sth->fetchrow_hashref;
158         $sth->finish;
159         $template->param(total => $total->{'total'});
160         
161         my $sth2=$dbh->prepare("select categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,hidelostitems,overduenoticerequired,category_type from categories where categorycode=?");
162         $sth2->execute($categorycode);
163         my $data=$sth2->fetchrow_hashref;
164         $sth2->finish;
165         if ($total->{'total'} >0) {
166                 $template->param(totalgtzero => 1);
167         }
168
169         $template->param(       description             => $data->{'description'},
170                                 enrolmentperiod         => $data->{'enrolmentperiod'},
171                                 enrolmentperioddate     => C4::Dates::format_date($data->{'enrolmentperioddate'}),
172                                 upperagelimit           => $data->{'upperagelimit'},
173                                 dateofbirthrequired     => $data->{'dateofbirthrequired'},
174                                 enrolmentfee            =>  sprintf("%.2f",$data->{'enrolmentfee'}),
175                                 overduenoticerequired   => $data->{'overduenoticerequired'},
176                                 issuelimit              => $data->{'issuelimit'},
177                                 reservefee              =>  sprintf("%.2f",$data->{'reservefee'}),
178                                 hidelostitems           => $data->{'hidelostitems'},
179                                 category_type           => $data->{'category_type'},
180                                 );
181                                                                                                         # END $OP eq DELETE_CONFIRM
182 ################## DELETE_CONFIRMED ##################################
183 # called by delete_confirm, used to effectively confirm deletion of data in DB
184 } elsif ($op eq 'delete_confirmed') {
185         $template->param(delete_confirmed => 1);
186         my $dbh = C4::Context->dbh;
187         my $categorycode=uc($input->param('categorycode'));
188         my $sth=$dbh->prepare("delete from categories where categorycode=?");
189         $sth->execute($categorycode);
190         $sth->finish;
191         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
192         exit;
193
194                                                                                                         # END $OP eq DELETE_CONFIRMED
195 } else { # DEFAULT
196         $template->param(else => 1);
197         my @loop;
198         my ($count,$results)=StringSearch($searchfield,'web');
199         for (my $i=0; $i < $count; $i++){
200                 my %row = (
201                         categorycode            => $results->[$i]{'categorycode'},
202                                 description             => $results->[$i]{'description'},
203                                 enrolmentperiod         => $results->[$i]{'enrolmentperiod'},
204                                 enrolmentperioddate     => C4::Dates::format_date($results->[$i]{'enrolmentperioddate'}),
205                                 upperagelimit           => $results->[$i]{'upperagelimit'},
206                                 dateofbirthrequired     => $results->[$i]{'dateofbirthrequired'},
207                                 enrolmentfee            => sprintf("%.2f",$results->[$i]{'enrolmentfee'}),
208                                 overduenoticerequired   => $results->[$i]{'overduenoticerequired'},
209                                 issuelimit              => $results->[$i]{'issuelimit'},
210                                 reservefee              => sprintf("%.2f",$results->[$i]{'reservefee'}),
211                                 hidelostitems           => $results->[$i]{'hidelostitems'},
212                                 category_type           => $results->[$i]{'category_type'},
213                                 "type_".$results->[$i]{'category_type'} => 1);
214         if (C4::Context->preference('EnhancedMessagingPreferences')) {
215             my $brief_prefs = _get_brief_messaging_prefs($results->[$i]{'categorycode'});
216             $row{messaging_prefs} = $brief_prefs if @$brief_prefs;
217         }
218                 push @loop, \%row;
219         }
220         $template->param(loop => \@loop);
221         # check that I (institution) and C (child) exists. otherwise => warning to the user
222         my $dbh = C4::Context->dbh;
223         my $sth=$dbh->prepare("select category_type from categories where category_type='C'");
224         $sth->execute;
225         my ($categoryChild) = $sth->fetchrow;
226         $template->param(categoryChild => $categoryChild);
227         $sth=$dbh->prepare("select category_type from categories where category_type='I'");
228         $sth->execute;
229         my ($categoryInstitution) = $sth->fetchrow;
230         $template->param(categoryInstitution => $categoryInstitution);
231         $sth->finish;
232
233
234 } #---- END $OP eq DEFAULT
235 output_html_with_http_headers $input, $cookie, $template->output;
236
237 exit 0;
238
239 sub _get_brief_messaging_prefs {
240     my $categorycode = shift;
241     my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
242     my $results = [];
243     PREF: foreach my $option ( @$messaging_options ) {
244         my $pref = C4::Members::Messaging::GetMessagingPreferences( { categorycode => $categorycode,
245                                                                     message_name       => $option->{'message_name'} } );
246         next unless  $pref->{'transports'};
247         my $brief_pref = {
248             message_attribute_id    => $option->{'message_attribute_id'},
249             message_name            => $option->{'message_name'},
250             $option->{'message_name'} => 1
251         };
252         foreach my $transport ( @{$pref->{'transports'}} ) {
253             push @{ $brief_pref->{'transports'} }, { transport => $transport };
254         }
255         push @$results, $brief_pref;
256     }
257     return $results;
258 }