Bug 3662: Invalid XHTML in update-child.tmpl - resubmission.
[koha.git] / admin / categorie.pl
blobcb3cd7b446f884af80af484dd7b67ad2bdd4b259
1 #!/usr/bin/perl
3 #script to administer the categories table
4 #written 20/02/2002 by paul.poulain@free.fr
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
22 # Copyright 2000-2002 Katipo Communications
24 # This file is part of Koha.
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.
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.
35 # You should have received a copy of the GNU General Public License along with
36 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
37 # Suite 330, Boston, MA 02111-1307 USA
39 use strict;
40 use CGI;
41 use C4::Context;
42 use C4::Auth;
43 use C4::Output;
44 use C4::Dates;
45 use C4::Form::MessagingPreferences;
47 sub StringSearch {
48 my ($searchstring,$type)=@_;
49 my $dbh = C4::Context->dbh;
50 $searchstring=~ s/\'/\\\'/g;
51 my @data=split(' ',$searchstring);
52 my $count=@data;
53 my $sth=$dbh->prepare("Select * from categories where (description like ?) order by category_type,description,categorycode");
54 $sth->execute("$data[0]%");
55 my @results;
56 while (my $data=$sth->fetchrow_hashref){
57 push(@results,$data);
59 # $sth->execute;
60 $sth->finish;
61 return (scalar(@results),\@results);
64 my $input = new CGI;
65 my $searchfield=$input->param('description');
66 my $script_name="/cgi-bin/koha/admin/categorie.pl";
67 my $categorycode=$input->param('categorycode');
68 my $op = $input->param('op');
70 my ($template, $loggedinuser, $cookie)
71 = get_template_and_user({template_name => "admin/categorie.tmpl",
72 query => $input,
73 type => "intranet",
74 authnotrequired => 0,
75 flagsrequired => {parameters => 1},
76 debug => 1,
77 });
80 $template->param(script_name => $script_name,
81 categorycode => $categorycode,
82 searchfield => $searchfield);
85 ################## ADD_FORM ##################################
86 # called by default. Used to create form to add or modify a record
87 if ($op eq 'add_form') {
88 $template->param(add_form => 1);
90 #---- if primkey exists, it's a modify action, so read values to modify...
91 my $data;
92 if ($categorycode) {
93 my $dbh = C4::Context->dbh;
94 my $sth=$dbh->prepare("select categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,overduenoticerequired,category_type from categories where categorycode=?");
95 $sth->execute($categorycode);
96 $data=$sth->fetchrow_hashref;
97 $sth->finish;
100 $data->{'enrolmentperioddate'} = undef if ($data->{'enrolmentperioddate'} eq '0000-00-00');
102 $template->param(description => $data->{'description'},
103 enrolmentperiod => $data->{'enrolmentperiod'},
104 enrolmentperioddate => C4::Dates::format_date($data->{'enrolmentperioddate'}),
105 upperagelimit => $data->{'upperagelimit'},
106 dateofbirthrequired => $data->{'dateofbirthrequired'},
107 enrolmentfee => sprintf("%.2f",$data->{'enrolmentfee'}),
108 overduenoticerequired => $data->{'overduenoticerequired'},
109 issuelimit => $data->{'issuelimit'},
110 reservefee => sprintf("%.2f",$data->{'reservefee'}),
111 category_type => $data->{'category_type'},
112 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
113 "type_".$data->{'category_type'} => 1,
115 if (C4::Context->preference('EnhancedMessagingPreferences')) {
116 C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode } , $template);
118 # END $OP eq ADD_FORM
119 ################## ADD_VALIDATE ##################################
120 # called by add_form, used to insert/modify data in DB
121 } elsif ($op eq 'add_validate') {
122 $template->param(add_validate => 1);
123 my $is_a_modif = $input->param("is_a_modif");
124 my $dbh = C4::Context->dbh;
125 if($input->param('enrolmentperioddate')){
126 $input->param('enrolmentperioddate' => C4::Dates::format_date_in_iso($input->param('enrolmentperioddate')) );
129 if ($is_a_modif) {
130 my $sth=$dbh->prepare("UPDATE categories SET description=?,enrolmentperiod=?, enrolmentperioddate=?,upperagelimit=?,dateofbirthrequired=?,enrolmentfee=?,reservefee=?,overduenoticerequired=?,category_type=? WHERE categorycode=?");
131 $sth->execute(map { $input->param($_) } ('description','enrolmentperiod','enrolmentperioddate','upperagelimit','dateofbirthrequired','enrolmentfee','reservefee','overduenoticerequired','category_type','categorycode'));
132 $sth->finish;
133 } else {
134 my $sth=$dbh->prepare("INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,enrolmentfee,reservefee,overduenoticerequired,category_type) values (?,?,?,?,?,?,?,?,?,?)");
135 $sth->execute(map { $input->param($_) } ('categorycode','description','enrolmentperiod','enrolmentperioddate','upperagelimit','dateofbirthrequired','enrolmentfee','reservefee','overduenoticerequired','category_type'));
136 $sth->finish;
138 if (C4::Context->preference('EnhancedMessagingPreferences')) {
139 C4::Form::MessagingPreferences::handle_form_action($input,
140 { categorycode => $input->param('categorycode') }, $template);
142 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
143 exit;
145 # END $OP eq ADD_VALIDATE
146 ################## DELETE_CONFIRM ##################################
147 # called by default form, used to confirm deletion of data in DB
148 } elsif ($op eq 'delete_confirm') {
149 $template->param(delete_confirm => 1);
151 my $dbh = C4::Context->dbh;
152 my $sth=$dbh->prepare("select count(*) as total from borrowers where categorycode=?");
153 $sth->execute($categorycode);
154 my $total = $sth->fetchrow_hashref;
155 $sth->finish;
156 $template->param(total => $total->{'total'});
158 my $sth2=$dbh->prepare("select categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,overduenoticerequired,category_type from categories where categorycode=?");
159 $sth2->execute($categorycode);
160 my $data=$sth2->fetchrow_hashref;
161 $sth2->finish;
162 if ($total->{'total'} >0) {
163 $template->param(totalgtzero => 1);
166 $template->param( description => $data->{'description'},
167 enrolmentperiod => $data->{'enrolmentperiod'},
168 enrolmentperioddate => C4::Dates::format_date($data->{'enrolmentperioddate'}),
169 upperagelimit => $data->{'upperagelimit'},
170 dateofbirthrequired => $data->{'dateofbirthrequired'},
171 enrolmentfee => sprintf("%.2f",$data->{'enrolmentfee'}),
172 overduenoticerequired => $data->{'overduenoticerequired'},
173 issuelimit => $data->{'issuelimit'},
174 reservefee => sprintf("%.2f",$data->{'reservefee'}),
175 category_type => $data->{'category_type'},
177 # END $OP eq DELETE_CONFIRM
178 ################## DELETE_CONFIRMED ##################################
179 # called by delete_confirm, used to effectively confirm deletion of data in DB
180 } elsif ($op eq 'delete_confirmed') {
181 $template->param(delete_confirmed => 1);
182 my $dbh = C4::Context->dbh;
183 my $categorycode=uc($input->param('categorycode'));
184 my $sth=$dbh->prepare("delete from categories where categorycode=?");
185 $sth->execute($categorycode);
186 $sth->finish;
187 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
188 exit;
190 # END $OP eq DELETE_CONFIRMED
191 } else { # DEFAULT
192 $template->param(else => 1);
193 my @loop;
194 my ($count,$results)=StringSearch($searchfield,'web');
195 for (my $i=0; $i < $count; $i++){
196 my %row = (
197 categorycode => $results->[$i]{'categorycode'},
198 description => $results->[$i]{'description'},
199 enrolmentperiod => $results->[$i]{'enrolmentperiod'},
200 enrolmentperioddate => C4::Dates::format_date($results->[$i]{'enrolmentperioddate'}),
201 upperagelimit => $results->[$i]{'upperagelimit'},
202 dateofbirthrequired => $results->[$i]{'dateofbirthrequired'},
203 enrolmentfee => sprintf("%.2f",$results->[$i]{'enrolmentfee'}),
204 overduenoticerequired => $results->[$i]{'overduenoticerequired'},
205 issuelimit => $results->[$i]{'issuelimit'},
206 reservefee => sprintf("%.2f",$results->[$i]{'reservefee'}),
207 category_type => $results->[$i]{'category_type'},
208 "type_".$results->[$i]{'category_type'} => 1);
209 if (C4::Context->preference('EnhancedMessagingPreferences')) {
210 my $brief_prefs = _get_brief_messaging_prefs($results->[$i]{'categorycode'});
211 $row{messaging_prefs} = $brief_prefs if @$brief_prefs;
213 push @loop, \%row;
215 $template->param(loop => \@loop);
216 # check that I (institution) and C (child) exists. otherwise => warning to the user
217 my $dbh = C4::Context->dbh;
218 my $sth=$dbh->prepare("select category_type from categories where category_type='C'");
219 $sth->execute;
220 my ($categoryChild) = $sth->fetchrow;
221 $template->param(categoryChild => $categoryChild);
222 $sth=$dbh->prepare("select category_type from categories where category_type='I'");
223 $sth->execute;
224 my ($categoryInstitution) = $sth->fetchrow;
225 $template->param(categoryInstitution => $categoryInstitution);
226 $sth->finish;
229 } #---- END $OP eq DEFAULT
230 output_html_with_http_headers $input, $cookie, $template->output;
232 exit 0;
234 sub _get_brief_messaging_prefs {
235 my $categorycode = shift;
236 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
237 my $results = [];
238 PREF: foreach my $option ( @$messaging_options ) {
239 my $pref = C4::Members::Messaging::GetMessagingPreferences( { categorycode => $categorycode,
240 message_name => $option->{'message_name'} } );
241 next unless @{$pref->{'transports'}};
242 my $brief_pref = { message_attribute_id => $option->{'message_attribute_id'},
243 message_name => $option->{'message_name'},
245 foreach my $transport ( @{$pref->{'transports'}} ) {
246 push @{ $brief_pref->{'transports'} }, { transport => $transport };
248 push @$results, $brief_pref;
250 return $results;