3 #script to administer the categories table
4 #written 20/02/2002 by paul.poulain@free.fr
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.
12 # - if primkey exists, this is a modification,so we read the $primkey record
13 # - builds the add/modify form
15 # - the user has just send datas, so we create/modify the record
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
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.
47 use C4
::Form
::MessagingPreferences
;
50 my ($searchstring,$type)=@_;
51 my $dbh = C4
::Context
->dbh;
53 $searchstring=~ s/\'/\\\'/g;
54 my @data=split(' ',$searchstring);
55 push @data,q{} if $#data==-1;
57 my $sth=$dbh->prepare("Select * from categories where (description like ?) order by category_type,description,categorycode");
58 $sth->execute("$data[0]%");
60 while (my $data=$sth->fetchrow_hashref){
65 return (scalar(@results),\
@results);
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");
75 my ($template, $loggedinuser, $cookie)
76 = get_template_and_user
({template_name
=> "admin/categorie.tmpl",
80 flagsrequired
=> {parameters
=> 'parameters_remaining_permissions'},
85 $template->param(script_name
=> $script_name,
86 categorycode
=> $categorycode,
87 searchfield
=> $searchfield);
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);
95 #---- if primkey exists, it's a modify action, so read values to modify...
97 my @selected_branches;
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;
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;
112 if ($data->{'enrolmentperioddate'} && $data->{'enrolmentperioddate'} eq '0000-00-00') {
113 $data->{'enrolmentperioddate'} = undef;
115 $data->{'category_type'} //= '';
117 my $branches = GetBranches
;
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,
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'},
145 if (C4
::Context
->preference('EnhancedMessagingPreferences')) {
146 C4
::Form
::MessagingPreferences
::set_form_values
({ categorycode
=> $categorycode } , $template);
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')) );
160 my $sth=$dbh->prepare("
164 enrolmentperioddate=?,
166 dateofbirthrequired=?,
170 overduenoticerequired=?,
172 BlockExpiredPatronOpacActions=?
173 WHERE categorycode=?"
176 map { $input->param($_) } (
179 'enrolmentperioddate',
181 'dateofbirthrequired',
185 'overduenoticerequired',
191 my @branches = $input->param("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 )
200 for my $branchcode ( @branches ) {
201 next if not $branchcode;
202 $sth->bind_param( 1, $input->param( "categorycode" ) );
203 $sth->bind_param( 2, $branchcode );
209 my $sth=$dbh->prepare("
210 INSERT INTO categories (
220 overduenoticerequired,
222 BlockExpiredPatronOpacActions
224 VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
226 map { $input->param($_) } (
230 'enrolmentperioddate',
232 'dateofbirthrequired',
236 'overduenoticerequired',
244 if (C4
::Context
->preference('EnhancedMessagingPreferences')) {
245 C4
::Form
::MessagingPreferences
::handle_form_action
($input,
246 { categorycode
=> $input->param('categorycode') }, $template);
248 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
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);
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;
262 $template->param(total
=> $total->{'total'});
264 my $sth2=$dbh->prepare("SELECT * FROM categories WHERE categorycode=?");
265 $sth2->execute($categorycode);
266 my $data=$sth2->fetchrow_hashref;
268 if ($total->{'total'} >0) {
269 $template->param(totalgtzero
=> 1);
272 if ($data->{'enrolmentperioddate'} && $data->{'enrolmentperioddate'} eq '0000-00-00') {
273 $data->{'enrolmentperioddate'} = undef;
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'},
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);
298 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
301 # END $OP eq DELETE_CONFIRMED
303 $template->param(else => 1);
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;
314 my $enrolmentperioddate = $results->[$i]{'enrolmentperioddate'};
315 if ($enrolmentperioddate && $enrolmentperioddate eq '0000-00-00') {
316 $enrolmentperioddate = undef;
318 $results->[$i]{'category_type'} //= '';
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,
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;
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'");
345 my ($categoryChild) = $sth->fetchrow;
346 $template->param(categoryChild
=> $categoryChild);
347 $sth=$dbh->prepare("select category_type from categories where category_type='I'");
349 my ($categoryInstitution) = $sth->fetchrow;
350 $template->param(categoryInstitution
=> $categoryInstitution);
354 } #---- END $OP eq DEFAULT
355 output_html_with_http_headers
$input, $cookie, $template->output;
359 sub _get_brief_messaging_prefs
{
360 my $categorycode = shift;
361 my $messaging_options = C4
::Members
::Messaging
::GetMessagingOptions
();
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'};
368 message_attribute_id
=> $option->{'message_attribute_id'},
369 message_name
=> $option->{'message_name'},
370 $option->{'message_name'} => 1
372 foreach my $transport ( keys %{$pref->{'transports'}} ) {
373 push @
{ $brief_pref->{'transports'} }, { transport
=> $transport };
375 push @
$results, $brief_pref;