Bug 11513: fix log warning noise in patron category editor
[koha.git] / admin / categorie.pl
blob6ea7b5aefe524c6e69d1432cb07e08426a325a07
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
36 # with Koha; if not, write to the Free Software Foundation, Inc.,
37 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
39 use Modern::Perl;
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;
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);
63 # $sth->execute;
64 $sth->finish;
65 return (scalar(@results),\@results);
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') // '';
74 my ($template, $loggedinuser, $cookie)
75 = get_template_and_user({template_name => "admin/categorie.tmpl",
76 query => $input,
77 type => "intranet",
78 authnotrequired => 0,
79 flagsrequired => {parameters => 'parameters_remaining_permissions'},
80 debug => 1,
81 });
84 $template->param(script_name => $script_name,
85 categorycode => $categorycode,
86 searchfield => $searchfield);
89 ################## ADD_FORM ##################################
90 # called by default. Used to create form to add or modify a record
91 if ($op eq 'add_form') {
92 $template->param(add_form => 1);
94 #---- if primkey exists, it's a modify action, so read values to modify...
95 my $data;
96 my @selected_branches;
97 if ($categorycode) {
98 my $dbh = C4::Context->dbh;
99 my $sth=$dbh->prepare("select categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,hidelostitems,overduenoticerequired,category_type from categories where categorycode=?");
100 $sth->execute($categorycode);
101 $data=$sth->fetchrow_hashref;
103 $sth = $dbh->prepare("SELECT b.branchcode, b.branchname FROM categories_branches AS cb, branches AS b WHERE cb.branchcode = b.branchcode AND cb.categorycode = ?");
104 $sth->execute( $categorycode );
105 while ( my $branch = $sth->fetchrow_hashref ) {
106 push @selected_branches, $branch;
108 $sth->finish;
111 if ($data->{'enrolmentperioddate'} && $data->{'enrolmentperioddate'} eq '0000-00-00') {
112 $data->{'enrolmentperioddate'} = undef;
114 $data->{'category_type'} //= '';
116 my $branches = GetBranches;
117 my @branches_loop;
118 foreach my $branch (sort keys %$branches) {
119 my $selected = ( grep {$$_{branchcode} eq $branch} @selected_branches ) ? 1 : 0;
120 push @branches_loop, {
121 branchcode => $$branches{$branch}{branchcode},
122 branchname => $$branches{$branch}{branchname},
123 selected => $selected,
127 $template->param(description => $data->{'description'},
128 enrolmentperiod => $data->{'enrolmentperiod'},
129 enrolmentperioddate => $data->{'enrolmentperioddate'},
130 upperagelimit => $data->{'upperagelimit'},
131 dateofbirthrequired => $data->{'dateofbirthrequired'},
132 enrolmentfee => sprintf("%.2f",$data->{'enrolmentfee'} || 0),
133 overduenoticerequired => $data->{'overduenoticerequired'},
134 issuelimit => $data->{'issuelimit'},
135 reservefee => sprintf("%.2f",$data->{'reservefee'} || 0),
136 hidelostitems => $data->{'hidelostitems'},
137 category_type => $data->{'category_type'},
138 SMSSendDriver => C4::Context->preference("SMSSendDriver"),
139 TalkingTechItivaPhone => C4::Context->preference("TalkingTechItivaPhoneNotification"),
140 "type_".$data->{'category_type'} => 1,
141 branches_loop => \@branches_loop,
143 if (C4::Context->preference('EnhancedMessagingPreferences')) {
144 C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode } , $template);
146 # END $OP eq ADD_FORM
147 ################## ADD_VALIDATE ##################################
148 # called by add_form, used to insert/modify data in DB
149 } elsif ($op eq 'add_validate') {
150 $template->param(add_validate => 1);
151 my $is_a_modif = $input->param("is_a_modif");
152 my $dbh = C4::Context->dbh;
153 if($input->param('enrolmentperioddate')){
154 $input->param('enrolmentperioddate' => C4::Dates::format_date_in_iso($input->param('enrolmentperioddate')) );
157 if ($is_a_modif) {
158 my $sth=$dbh->prepare("UPDATE categories SET description=?,enrolmentperiod=?, enrolmentperioddate=?,upperagelimit=?,dateofbirthrequired=?,enrolmentfee=?,reservefee=?,hidelostitems=?,overduenoticerequired=?,category_type=? WHERE categorycode=?");
159 $sth->execute(map { $input->param($_) } ('description','enrolmentperiod','enrolmentperioddate','upperagelimit','dateofbirthrequired','enrolmentfee','reservefee','hidelostitems','overduenoticerequired','category_type','categorycode'));
160 my @branches = $input->param("branches");
161 if ( @branches ) {
162 $sth = $dbh->prepare("DELETE FROM categories_branches WHERE categorycode = ?");
163 $sth->execute( $input->param( "categorycode" ) );
164 $sth = $dbh->prepare(
165 "INSERT INTO categories_branches
166 ( categorycode, branchcode )
167 VALUES ( ?, ? )"
169 for my $branchcode ( @branches ) {
170 next if not $branchcode;
171 $sth->bind_param( 1, $input->param( "categorycode" ) );
172 $sth->bind_param( 2, $branchcode );
173 $sth->execute;
176 $sth->finish;
177 } else {
178 my $sth=$dbh->prepare("INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,enrolmentfee,reservefee,hidelostitems,overduenoticerequired,category_type) values (?,?,?,?,?,?,?,?,?,?,?)");
179 $sth->execute(map { $input->param($_) } ('categorycode','description','enrolmentperiod','enrolmentperioddate','upperagelimit','dateofbirthrequired','enrolmentfee','reservefee','hidelostitems','overduenoticerequired','category_type'));
180 $sth->finish;
182 if (C4::Context->preference('EnhancedMessagingPreferences')) {
183 C4::Form::MessagingPreferences::handle_form_action($input,
184 { categorycode => $input->param('categorycode') }, $template);
186 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
187 exit;
189 # END $OP eq ADD_VALIDATE
190 ################## DELETE_CONFIRM ##################################
191 # called by default form, used to confirm deletion of data in DB
192 } elsif ($op eq 'delete_confirm') {
193 $template->param(delete_confirm => 1);
195 my $dbh = C4::Context->dbh;
196 my $sth=$dbh->prepare("select count(*) as total from borrowers where categorycode=?");
197 $sth->execute($categorycode);
198 my $total = $sth->fetchrow_hashref;
199 $sth->finish;
200 $template->param(total => $total->{'total'});
202 my $sth2=$dbh->prepare("select categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,hidelostitems,overduenoticerequired,category_type from categories where categorycode=?");
203 $sth2->execute($categorycode);
204 my $data=$sth2->fetchrow_hashref;
205 $sth2->finish;
206 if ($total->{'total'} >0) {
207 $template->param(totalgtzero => 1);
210 if ($data->{'enrolmentperioddate'} && $data->{'enrolmentperioddate'} eq '0000-00-00') {
211 $data->{'enrolmentperioddate'} = undef;
213 $template->param( description => $data->{'description'},
214 enrolmentperiod => $data->{'enrolmentperiod'},
215 enrolmentperioddate => $data->{'enrolmentperioddate'},
216 upperagelimit => $data->{'upperagelimit'},
217 dateofbirthrequired => $data->{'dateofbirthrequired'},
218 enrolmentfee => sprintf("%.2f",$data->{'enrolmentfee'} || 0),
219 overduenoticerequired => $data->{'overduenoticerequired'},
220 issuelimit => $data->{'issuelimit'},
221 reservefee => sprintf("%.2f",$data->{'reservefee'} || 0),
222 hidelostitems => $data->{'hidelostitems'},
223 category_type => $data->{'category_type'},
225 # END $OP eq DELETE_CONFIRM
226 ################## DELETE_CONFIRMED ##################################
227 # called by delete_confirm, used to effectively confirm deletion of data in DB
228 } elsif ($op eq 'delete_confirmed') {
229 $template->param(delete_confirmed => 1);
230 my $dbh = C4::Context->dbh;
231 my $categorycode=uc($input->param('categorycode'));
232 my $sth=$dbh->prepare("delete from categories where categorycode=?");
233 $sth->execute($categorycode);
234 $sth->finish;
235 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
236 exit;
238 # END $OP eq DELETE_CONFIRMED
239 } else { # DEFAULT
240 $template->param(else => 1);
241 my @loop;
242 my ($count,$results)=StringSearch($searchfield,'web');
243 my $dbh = C4::Context->dbh;
244 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 = ?");
245 for (my $i=0; $i < $count; $i++){
246 $sth->execute( $results->[$i]{'categorycode'} );
247 my @selected_branches;
248 while ( my $branch = $sth->fetchrow_hashref ) {
249 push @selected_branches, $branch;
251 my $enrolmentperioddate = $results->[$i]{'enrolmentperioddate'};
252 if ($enrolmentperioddate && $enrolmentperioddate eq '0000-00-00') {
253 $enrolmentperioddate = undef;
255 $results->[$i]{'category_type'} //= '';
256 my %row = (
257 categorycode => $results->[$i]{'categorycode'},
258 description => $results->[$i]{'description'},
259 enrolmentperiod => $results->[$i]{'enrolmentperiod'},
260 enrolmentperioddate => $enrolmentperioddate,
261 upperagelimit => $results->[$i]{'upperagelimit'},
262 dateofbirthrequired => $results->[$i]{'dateofbirthrequired'},
263 enrolmentfee => sprintf("%.2f",$results->[$i]{'enrolmentfee'} || 0),
264 overduenoticerequired => $results->[$i]{'overduenoticerequired'},
265 issuelimit => $results->[$i]{'issuelimit'},
266 reservefee => sprintf("%.2f",$results->[$i]{'reservefee'} || 0),
267 hidelostitems => $results->[$i]{'hidelostitems'},
268 category_type => $results->[$i]{'category_type'},
269 "type_".$results->[$i]{'category_type'} => 1,
270 branches => \@selected_branches,
272 if (C4::Context->preference('EnhancedMessagingPreferences')) {
273 my $brief_prefs = _get_brief_messaging_prefs($results->[$i]{'categorycode'});
274 $row{messaging_prefs} = $brief_prefs if @$brief_prefs;
276 push @loop, \%row;
278 $template->param(loop => \@loop);
279 # check that I (institution) and C (child) exists. otherwise => warning to the user
280 $sth=$dbh->prepare("select category_type from categories where category_type='C'");
281 $sth->execute;
282 my ($categoryChild) = $sth->fetchrow;
283 $template->param(categoryChild => $categoryChild);
284 $sth=$dbh->prepare("select category_type from categories where category_type='I'");
285 $sth->execute;
286 my ($categoryInstitution) = $sth->fetchrow;
287 $template->param(categoryInstitution => $categoryInstitution);
288 $sth->finish;
291 } #---- END $OP eq DEFAULT
292 output_html_with_http_headers $input, $cookie, $template->output;
294 exit 0;
296 sub _get_brief_messaging_prefs {
297 my $categorycode = shift;
298 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
299 my $results = [];
300 PREF: foreach my $option ( @$messaging_options ) {
301 my $pref = C4::Members::Messaging::GetMessagingPreferences( { categorycode => $categorycode,
302 message_name => $option->{'message_name'} } );
303 next unless $pref->{'transports'};
304 my $brief_pref = {
305 message_attribute_id => $option->{'message_attribute_id'},
306 message_name => $option->{'message_name'},
307 $option->{'message_name'} => 1
309 foreach my $transport ( keys %{$pref->{'transports'}} ) {
310 push @{ $brief_pref->{'transports'} }, { transport => $transport };
312 push @$results, $brief_pref;
314 return $results;