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;
51 my ($searchstring,$type)=@_;
52 my $dbh = C4::Context->dbh;
54 $searchstring=~ s/\'/\\\'/g;
55 my @data=split(' ',$searchstring);
56 push @data,q{} if $#data==-1;
58 my $sth=$dbh->prepare("Select * from categories where (description like ?) order by category_type,description,categorycode");
59 $sth->execute("$data[0]%");
61 while (my $data=$sth->fetchrow_hashref){
66 return (scalar(@results),\@results);
70 my $searchfield=$input->param('description');
71 my $script_name="/cgi-bin/koha/admin/categorie.pl";
72 my $categorycode=$input->param('categorycode');
73 my $op = $input->param('op') // '';
74 my $block_expired = $input->param("block_expired");
76 my ($template, $loggedinuser, $cookie)
77 = get_template_and_user({template_name => "admin/categorie.tmpl",
81 flagsrequired => {parameters => 'parameters_remaining_permissions'},
86 $template->param(script_name => $script_name,
87 categorycode => $categorycode,
88 searchfield => $searchfield);
91 ################## ADD_FORM ##################################
92 # called by default. Used to create form to add or modify a record
93 if ($op eq 'add_form') {
94 $template->param(add_form => 1);
96 #---- if primkey exists, it's a modify action, so read values to modify...
98 my @selected_branches;
100 my $dbh = C4::Context->dbh;
101 my $sth=$dbh->prepare("SELECT * FROM categories WHERE categorycode=?");
102 $sth->execute($categorycode);
103 $data=$sth->fetchrow_hashref;
105 $sth = $dbh->prepare("SELECT b.branchcode, b.branchname FROM categories_branches AS cb, branches AS b WHERE cb.branchcode = b.branchcode AND cb.categorycode = ?");
106 $sth->execute( $categorycode );
107 while ( my $branch = $sth->fetchrow_hashref ) {
108 push @selected_branches, $branch;
113 if ($data->{'enrolmentperioddate'} && $data->{'enrolmentperioddate'} eq '0000-00-00') {
114 $data->{'enrolmentperioddate'} = undef;
116 $data->{'category_type'} //= '';
118 my $branches = GetBranches;
120 foreach my $branch (sort keys %$branches) {
121 my $selected = ( grep {$$_{branchcode} eq $branch} @selected_branches ) ? 1 : 0;
122 push @branches_loop, {
123 branchcode => $$branches{$branch}{branchcode},
124 branchname => $$branches{$branch}{branchname},
125 selected => $selected,
130 description => $data->{'description'},
131 enrolmentperiod => $data->{'enrolmentperiod'},
132 enrolmentperioddate => $data->{'enrolmentperioddate'},
133 upperagelimit => $data->{'upperagelimit'},
134 dateofbirthrequired => $data->{'dateofbirthrequired'},
135 enrolmentfee => sprintf( "%.2f", $data->{'enrolmentfee'} || 0 ),
136 overduenoticerequired => $data->{'overduenoticerequired'},
137 issuelimit => $data->{'issuelimit'},
138 reservefee => sprintf( "%.2f", $data->{'reservefee'} || 0 ),
139 hidelostitems => $data->{'hidelostitems'},
140 category_type => $data->{'category_type'},
141 SMSSendDriver => C4::Context->preference("SMSSendDriver"),
142 TalkingTechItivaPhone =>
143 C4::Context->preference("TalkingTechItivaPhoneNotification"),
144 "type_" . $data->{'category_type'} => 1,
145 branches_loop => \@branches_loop,
146 BlockExpiredPatronOpacActions =>
147 $data->{'BlockExpiredPatronOpacActions'},
148 default_privacy => $data->{'default_privacy'},
151 if (C4::Context->preference('EnhancedMessagingPreferences')) {
152 C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode } , $template);
154 # END $OP eq ADD_FORM
155 ################## ADD_VALIDATE ##################################
156 # called by add_form, used to insert/modify data in DB
157 } elsif ($op eq 'add_validate') {
158 $template->param(add_validate => 1);
159 my $is_a_modif = $input->param("is_a_modif");
160 my $dbh = C4::Context->dbh;
161 if($input->param('enrolmentperioddate')){
162 $input->param('enrolmentperioddate' => C4::Dates::format_date_in_iso($input->param('enrolmentperioddate')) );
166 my $sth=$dbh->prepare("
170 enrolmentperioddate=?,
172 dateofbirthrequired=?,
176 overduenoticerequired=?,
178 BlockExpiredPatronOpacActions=?,
180 WHERE categorycode=?"
183 map { $input->param($_) } (
186 'enrolmentperioddate',
188 'dateofbirthrequired',
192 'overduenoticerequired',
199 my @branches = $input->param("branches");
201 $sth = $dbh->prepare("DELETE FROM categories_branches WHERE categorycode = ?");
202 $sth->execute( $input->param( "categorycode" ) );
203 $sth = $dbh->prepare(
204 "INSERT INTO categories_branches
205 ( categorycode, branchcode )
208 for my $branchcode ( @branches ) {
209 next if not $branchcode;
210 $sth->bind_param( 1, $input->param( "categorycode" ) );
211 $sth->bind_param( 2, $branchcode );
217 my $sth=$dbh->prepare("
218 INSERT INTO categories (
228 overduenoticerequired,
230 BlockExpiredPatronOpacActions,
233 VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
235 map { $input->param($_) } (
239 'enrolmentperioddate',
241 'dateofbirthrequired',
245 'overduenoticerequired',
254 if (C4::Context->preference('EnhancedMessagingPreferences')) {
255 C4::Form::MessagingPreferences::handle_form_action($input,
256 { categorycode => $input->param('categorycode') }, $template);
258 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
261 # END $OP eq ADD_VALIDATE
262 ################## DELETE_CONFIRM ##################################
263 # called by default form, used to confirm deletion of data in DB
264 } elsif ($op eq 'delete_confirm') {
265 my $schema = Koha::Database->new()->schema();
266 $template->param(delete_confirm => 1);
268 my $count = $schema->resultset('Borrower')->search( { categorycode => $categorycode } )->count();
269 my $category = $schema->resultset('Category')->find($categorycode);
270 $category->enrolmentperioddate( C4::Dates::format_date( $category->enrolmentperioddate() ) );
271 $template->param( category => $category, patrons_in_category => $count );
272 # END $OP eq DELETE_CONFIRM
273 ################## DELETE_CONFIRMED ##################################
274 # called by delete_confirm, used to effectively confirm deletion of data in DB
275 } elsif ($op eq 'delete_confirmed') {
276 $template->param(delete_confirmed => 1);
277 my $dbh = C4::Context->dbh;
278 my $categorycode=uc($input->param('categorycode'));
279 my $sth=$dbh->prepare("delete from categories where categorycode=?");
280 $sth->execute($categorycode);
282 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
285 # END $OP eq DELETE_CONFIRMED
287 $template->param(else => 1);
289 my ($count,$results)=StringSearch($searchfield,'web');
290 my $dbh = C4::Context->dbh;
291 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 = ?");
292 for (my $i=0; $i < $count; $i++){
293 $sth->execute( $results->[$i]{'categorycode'} );
294 my @selected_branches;
295 while ( my $branch = $sth->fetchrow_hashref ) {
296 push @selected_branches, $branch;
298 my $enrolmentperioddate = $results->[$i]{'enrolmentperioddate'};
299 if ($enrolmentperioddate && $enrolmentperioddate eq '0000-00-00') {
300 $enrolmentperioddate = undef;
302 $results->[$i]{'category_type'} //= '';
304 categorycode => $results->[$i]{'categorycode'},
305 description => $results->[$i]{'description'},
306 enrolmentperiod => $results->[$i]{'enrolmentperiod'},
307 enrolmentperioddate => $enrolmentperioddate,
308 upperagelimit => $results->[$i]{'upperagelimit'},
309 dateofbirthrequired => $results->[$i]{'dateofbirthrequired'},
310 enrolmentfee => sprintf("%.2f",$results->[$i]{'enrolmentfee'} || 0),
311 overduenoticerequired => $results->[$i]{'overduenoticerequired'},
312 issuelimit => $results->[$i]{'issuelimit'},
313 reservefee => sprintf("%.2f",$results->[$i]{'reservefee'} || 0),
314 hidelostitems => $results->[$i]{'hidelostitems'},
315 category_type => $results->[$i]{'category_type'},
316 default_privacy => $results->[$i]{'default_privacy'},
317 "type_".$results->[$i]{'category_type'} => 1,
318 branches => \@selected_branches,
320 if (C4::Context->preference('EnhancedMessagingPreferences')) {
321 my $brief_prefs = _get_brief_messaging_prefs($results->[$i]{'categorycode'});
322 $row{messaging_prefs} = $brief_prefs if @$brief_prefs;
326 $template->param(loop => \@loop);
327 # check that I (institution) and C (child) exists. otherwise => warning to the user
328 $sth=$dbh->prepare("select category_type from categories where category_type='C'");
330 my ($categoryChild) = $sth->fetchrow;
331 $template->param(categoryChild => $categoryChild);
332 $sth=$dbh->prepare("select category_type from categories where category_type='I'");
334 my ($categoryInstitution) = $sth->fetchrow;
335 $template->param(categoryInstitution => $categoryInstitution);
339 } #---- END $OP eq DEFAULT
340 output_html_with_http_headers $input, $cookie, $template->output;
344 sub _get_brief_messaging_prefs {
345 my $categorycode = shift;
346 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
348 PREF: foreach my $option ( @$messaging_options ) {
349 my $pref = C4::Members::Messaging::GetMessagingPreferences( { categorycode => $categorycode,
350 message_name => $option->{'message_name'} } );
351 next unless $pref->{'transports'};
353 message_attribute_id => $option->{'message_attribute_id'},
354 message_name => $option->{'message_name'},
355 $option->{'message_name'} => 1
357 foreach my $transport ( keys %{$pref->{'transports'}} ) {
358 push @{ $brief_pref->{'transports'} }, { transport => $transport };
360 push @$results, $brief_pref;