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
21 # Copyright 2000-2002 Katipo Communications
23 # This file is part of Koha.
25 # Koha is free software; you can redistribute it and/or modify it under the
26 # terms of the GNU General Public License as published by the Free Software
27 # Foundation; either version 2 of the License, or (at your option) any later
30 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
31 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
32 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
34 # You should have received a copy of the GNU General Public License along
35 # with Koha; if not, write to the Free Software Foundation, Inc.,
36 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46 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]%");
61 while ( my $data = $sth->fetchrow_hashref ) {
62 push( @results, $data );
67 return ( scalar(@results), \@results );
71 my $searchfield = $input->param('description');
72 my $script_name = "/cgi-bin/koha/admin/categorie.pl";
73 my $categorycode = $input->param('categorycode');
74 my $op = $input->param('op') // '';
75 my $block_expired = $input->param("block_expired");
77 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
79 template_name => "admin/categorie.tt",
83 flagsrequired => { parameters => 'parameters_remaining_permissions' },
89 script_name => $script_name,
90 categorycode => $categorycode,
91 searchfield => $searchfield
94 ################## ADD_FORM ##################################
95 # called by default. Used to create form to add or modify a record
96 if ( $op eq 'add_form' ) {
97 $template->param( add_form => 1 );
99 #---- if primkey exists, it's a modify action, so read values to modify...
101 my @selected_branches;
103 my $dbh = C4::Context->dbh;
105 $dbh->prepare("SELECT * FROM categories WHERE categorycode=?");
106 $sth->execute($categorycode);
107 $data = $sth->fetchrow_hashref;
109 $sth = $dbh->prepare(
110 "SELECT b.branchcode, b.branchname
111 FROM categories_branches AS cb, branches AS b
112 WHERE cb.branchcode = b.branchcode AND cb.categorycode = ?
114 $sth->execute($categorycode);
115 while ( my $branch = $sth->fetchrow_hashref ) {
116 push @selected_branches, $branch;
121 if ( $data->{'enrolmentperioddate'}
122 && $data->{'enrolmentperioddate'} eq '0000-00-00' )
124 $data->{'enrolmentperioddate'} = undef;
127 $data->{'category_type'} //= '';
129 my $branches = GetBranches();
131 foreach my $branch ( sort keys %$branches ) {
133 ( grep { $$_{branchcode} eq $branch } @selected_branches ) ? 1 : 0;
136 branchcode => $$branches{$branch}{branchcode},
137 branchname => $$branches{$branch}{branchname},
138 selected => $selected,
143 branches_loop => \@branches_loop,
144 description => $data->{'description'},
145 enrolmentperiod => $data->{'enrolmentperiod'},
146 enrolmentperioddate => $data->{'enrolmentperioddate'},
147 upperagelimit => $data->{'upperagelimit'},
148 dateofbirthrequired => $data->{'dateofbirthrequired'},
149 enrolmentfee => sprintf( "%.2f", $data->{'enrolmentfee'} || 0 ),
150 overduenoticerequired => $data->{'overduenoticerequired'},
151 issuelimit => $data->{'issuelimit'},
152 reservefee => sprintf( "%.2f", $data->{'reservefee'} || 0 ),
153 hidelostitems => $data->{'hidelostitems'},
154 category_type => $data->{'category_type'},
155 default_privacy => $data->{'default_privacy'},
156 SMSSendDriver => C4::Context->preference("SMSSendDriver"),
157 "type_" . $data->{'category_type'} => 1,
158 BlockExpiredPatronOpacActions =>
159 $data->{'BlockExpiredPatronOpacActions'},
160 TalkingTechItivaPhone =>
161 C4::Context->preference("TalkingTechItivaPhoneNotification"),
164 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
165 C4::Form::MessagingPreferences::set_form_values(
166 { categorycode => $categorycode }, $template );
169 # END $OP eq ADD_FORM
170 ################## ADD_VALIDATE ##################################
171 # called by add_form, used to insert/modify data in DB
173 elsif ( $op eq 'add_validate' ) {
174 $template->param( add_validate => 1 );
176 my $is_a_modif = $input->param("is_a_modif");
178 my $dbh = C4::Context->dbh;
180 if ( $input->param('enrolmentperioddate') ) {
182 'enrolmentperioddate' => C4::Dates::format_date_in_iso(
183 $input->param('enrolmentperioddate')
189 my $sth = $dbh->prepare( "
193 enrolmentperioddate=?,
195 dateofbirthrequired=?,
199 overduenoticerequired=?,
201 BlockExpiredPatronOpacActions=?,
203 WHERE categorycode=?"
206 map { $input->param($_) } (
207 'description', 'enrolmentperiod',
208 'enrolmentperioddate', 'upperagelimit',
209 'dateofbirthrequired', 'enrolmentfee',
210 'reservefee', 'hidelostitems',
211 'overduenoticerequired', 'category_type',
212 'block_expired', 'default_privacy',
216 my @branches = $input->param("branches");
218 $sth = $dbh->prepare(
219 "DELETE FROM categories_branches WHERE categorycode = ?"
221 $sth->execute( $input->param("categorycode") );
222 $sth = $dbh->prepare(
223 "INSERT INTO categories_branches ( categorycode, branchcode ) VALUES ( ?, ? )"
225 for my $branchcode (@branches) {
226 next if not $branchcode;
227 $sth->bind_param( 1, $input->param("categorycode") );
228 $sth->bind_param( 2, $branchcode );
235 my $sth = $dbh->prepare( "
236 INSERT INTO categories (
246 overduenoticerequired,
248 BlockExpiredPatronOpacActions,
251 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)" );
253 map { $input->param($_) } (
254 'categorycode', 'description',
255 'enrolmentperiod', 'enrolmentperioddate',
256 'upperagelimit', 'dateofbirthrequired',
257 'enrolmentfee', 'reservefee',
258 'hidelostitems', 'overduenoticerequired',
259 'category_type', 'block_expired',
266 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
267 C4::Form::MessagingPreferences::handle_form_action( $input,
268 { categorycode => $input->param('categorycode') }, $template );
270 print $input->redirect("/cgi-bin/koha/admin/categorie.pl");
273 # END $OP eq ADD_VALIDATE
274 ################## DELETE_CONFIRM ##################################
275 # called by default form, used to confirm deletion of data in DB
277 elsif ( $op eq 'delete_confirm' ) {
278 my $schema = Koha::Database->new()->schema();
279 $template->param( delete_confirm => 1 );
282 $schema->resultset('Borrower')
283 ->search( { categorycode => $categorycode } )->count();
285 my $category = $schema->resultset('Category')->find($categorycode);
287 $category->enrolmentperioddate(
288 C4::Dates::format_date( $category->enrolmentperioddate() ) );
290 $template->param( category => $category, patrons_in_category => $count );
292 # END $OP eq DELETE_CONFIRM
293 ################## DELETE_CONFIRMED ##################################
294 # called by delete_confirm, used to effectively confirm deletion of data in DB
296 elsif ( $op eq 'delete_confirmed' ) {
297 $template->param( delete_confirmed => 1 );
298 my $dbh = C4::Context->dbh;
300 my $categorycode = uc( $input->param('categorycode') );
302 my $sth = $dbh->prepare("delete from categories where categorycode=?");
304 $sth->execute($categorycode);
307 print $input->redirect("/cgi-bin/koha/admin/categorie.pl");
310 # END $OP eq DELETE_CONFIRMED
313 $template->param( else => 1 );
315 my ( $count, $results ) = StringSearch( $searchfield, 'web' );
317 my $dbh = C4::Context->dbh;
318 my $sth = $dbh->prepare("
319 SELECT b.branchcode, b.branchname
320 FROM categories_branches AS cb, branches AS b
321 WHERE cb.branchcode = b.branchcode AND cb.categorycode = ?
324 for ( my $i = 0 ; $i < $count ; $i++ ) {
325 $sth->execute( $results->[$i]{'categorycode'} );
327 my @selected_branches;
328 while ( my $branch = $sth->fetchrow_hashref ) {
329 push @selected_branches, $branch;
332 my $enrolmentperioddate = $results->[$i]{'enrolmentperioddate'};
333 if ( $enrolmentperioddate && $enrolmentperioddate eq '0000-00-00' ) {
334 $enrolmentperioddate = undef;
337 $results->[$i]{'category_type'} //= '';
340 branches => \@selected_branches,
341 categorycode => $results->[$i]{'categorycode'},
342 description => $results->[$i]{'description'},
343 enrolmentperiod => $results->[$i]{'enrolmentperiod'},
344 enrolmentperioddate => $enrolmentperioddate,
345 upperagelimit => $results->[$i]{'upperagelimit'},
346 dateofbirthrequired => $results->[$i]{'dateofbirthrequired'},
347 overduenoticerequired => $results->[$i]{'overduenoticerequired'},
348 issuelimit => $results->[$i]{'issuelimit'},
349 hidelostitems => $results->[$i]{'hidelostitems'},
350 category_type => $results->[$i]{'category_type'},
351 default_privacy => $results->[$i]{'default_privacy'},
352 reservefee => sprintf( "%.2f", $results->[$i]{'reservefee'} || 0 ),
354 sprintf( "%.2f", $results->[$i]{'enrolmentfee'} || 0 ),
355 "type_" . $results->[$i]{'category_type'} => 1,
358 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
360 _get_brief_messaging_prefs( $results->[$i]{'categorycode'} );
361 $row{messaging_prefs} = $brief_prefs if @$brief_prefs;
366 $template->param( loop => \@loop );
368 # check that I (institution) and C (child) exists. otherwise => warning to the user
369 $sth = $dbh->prepare("select category_type from categories where category_type='C'");
371 my ($categoryChild) = $sth->fetchrow;
372 $template->param( categoryChild => $categoryChild );
374 $sth = $dbh->prepare("select category_type from categories where category_type='I'");
376 my ($categoryInstitution) = $sth->fetchrow;
377 $template->param( categoryInstitution => $categoryInstitution );
380 } #---- END $OP eq DEFAULT
381 output_html_with_http_headers $input, $cookie, $template->output;
385 sub _get_brief_messaging_prefs {
386 my $categorycode = shift;
387 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
389 PREF: foreach my $option (@$messaging_options) {
390 my $pref = C4::Members::Messaging::GetMessagingPreferences(
392 categorycode => $categorycode,
393 message_name => $option->{'message_name'}
396 next unless $pref->{'transports'};
398 message_attribute_id => $option->{'message_attribute_id'},
399 message_name => $option->{'message_name'},
400 $option->{'message_name'} => 1
402 foreach my $transport ( keys %{ $pref->{'transports'} } ) {
403 push @{ $brief_pref->{'transports'} }, { transport => $transport };
405 push @$results, $brief_pref;