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') // 'list';
75 my $block_expired = $input->param("block_expired");
78 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
80 template_name => "admin/categorie.tt",
84 flagsrequired => { parameters => 'parameters_remaining_permissions' },
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...
96 my @selected_branches;
98 my $dbh = C4::Context->dbh;
100 $dbh->prepare("SELECT * FROM categories WHERE categorycode=?");
101 $sth->execute($categorycode);
102 $data = $sth->fetchrow_hashref;
104 $sth = $dbh->prepare(
105 "SELECT b.branchcode, b.branchname
106 FROM categories_branches AS cb, branches AS b
107 WHERE cb.branchcode = b.branchcode AND cb.categorycode = ?
109 $sth->execute($categorycode);
110 while ( my $branch = $sth->fetchrow_hashref ) {
111 push @selected_branches, $branch;
116 if ( $data->{'enrolmentperioddate'}
117 && $data->{'enrolmentperioddate'} eq '0000-00-00' )
119 $data->{'enrolmentperioddate'} = undef;
122 $data->{'category_type'} //= '';
124 my $branches = GetBranches();
126 foreach my $branch ( sort keys %$branches ) {
128 ( grep { $$_{branchcode} eq $branch } @selected_branches ) ? 1 : 0;
131 branchcode => $$branches{$branch}{branchcode},
132 branchname => $$branches{$branch}{branchname},
133 selected => $selected,
138 branches_loop => \@branches_loop,
139 description => $data->{'description'},
140 enrolmentperiod => $data->{'enrolmentperiod'},
141 enrolmentperioddate => $data->{'enrolmentperioddate'},
142 upperagelimit => $data->{'upperagelimit'},
143 dateofbirthrequired => $data->{'dateofbirthrequired'},
144 enrolmentfee => sprintf( "%.2f", $data->{'enrolmentfee'} || 0 ),
145 overduenoticerequired => $data->{'overduenoticerequired'},
146 issuelimit => $data->{'issuelimit'},
147 reservefee => sprintf( "%.2f", $data->{'reservefee'} || 0 ),
148 hidelostitems => $data->{'hidelostitems'},
149 category_type => $data->{'category_type'},
150 default_privacy => $data->{'default_privacy'},
151 SMSSendDriver => C4::Context->preference("SMSSendDriver"),
152 "type_" . $data->{'category_type'} => 1,
153 BlockExpiredPatronOpacActions =>
154 $data->{'BlockExpiredPatronOpacActions'},
155 TalkingTechItivaPhone =>
156 C4::Context->preference("TalkingTechItivaPhoneNotification"),
159 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
160 C4::Form::MessagingPreferences::set_form_values(
161 { categorycode => $categorycode }, $template );
164 # END $OP eq ADD_FORM
165 ################## ADD_VALIDATE ##################################
166 # called by add_form, used to insert/modify data in DB
168 elsif ( $op eq 'add_validate' ) {
170 my $is_a_modif = $input->param("is_a_modif");
172 my $dbh = C4::Context->dbh;
174 if ( $input->param('enrolmentperioddate') ) {
176 'enrolmentperioddate' => C4::Dates::format_date_in_iso(
177 $input->param('enrolmentperioddate')
183 my $sth = $dbh->prepare( "
187 enrolmentperioddate=?,
189 dateofbirthrequired=?,
193 overduenoticerequired=?,
195 BlockExpiredPatronOpacActions=?,
197 WHERE categorycode=?"
200 map { $input->param($_) } (
201 'description', 'enrolmentperiod',
202 'enrolmentperioddate', 'upperagelimit',
203 'dateofbirthrequired', 'enrolmentfee',
204 'reservefee', 'hidelostitems',
205 'overduenoticerequired', 'category_type',
206 'block_expired', 'default_privacy',
210 my @branches = $input->param("branches");
212 $sth = $dbh->prepare(
213 "DELETE FROM categories_branches WHERE categorycode = ?"
215 $sth->execute( $input->param("categorycode") );
216 $sth = $dbh->prepare(
217 "INSERT INTO categories_branches ( categorycode, branchcode ) VALUES ( ?, ? )"
219 for my $branchcode (@branches) {
220 next if not $branchcode;
221 $sth->bind_param( 1, $input->param("categorycode") );
222 $sth->bind_param( 2, $branchcode );
229 my $sth = $dbh->prepare( "
230 INSERT INTO categories (
240 overduenoticerequired,
242 BlockExpiredPatronOpacActions,
245 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)" );
246 my $inserted = $sth->execute(
247 map { $input->param($_) } (
248 'categorycode', 'description',
249 'enrolmentperiod', 'enrolmentperioddate',
250 'upperagelimit', 'dateofbirthrequired',
251 'enrolmentfee', 'reservefee',
252 'hidelostitems', 'overduenoticerequired',
253 'category_type', 'block_expired',
258 push @messages, { type => 'message', code => 'success_on_insert' };
261 push @messages, { type => 'error', code => 'error_on_insert' };
265 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
266 C4::Form::MessagingPreferences::handle_form_action( $input,
267 { categorycode => $input->param('categorycode') }, $template );
272 # END $OP eq ADD_VALIDATE
273 ################## DELETE_CONFIRM ##################################
274 # called by default form, used to confirm deletion of data in DB
276 elsif ( $op eq 'delete_confirm' ) {
277 my $schema = Koha::Database->new()->schema();
278 $template->param( delete_confirm => 1 );
281 $schema->resultset('Borrower')
282 ->search( { categorycode => $categorycode } )->count();
284 my $category = $schema->resultset('Category')->find($categorycode);
286 $category->enrolmentperioddate(
287 C4::Dates::format_date( $category->enrolmentperioddate() ) );
289 $template->param( category => $category, patrons_in_category => $count );
291 # END $OP eq DELETE_CONFIRM
292 ################## DELETE_CONFIRMED ##################################
293 # called by delete_confirm, used to effectively confirm deletion of data in DB
295 elsif ( $op eq 'delete_confirmed' ) {
296 $template->param( delete_confirmed => 1 );
297 my $dbh = C4::Context->dbh;
299 my $categorycode = uc( $input->param('categorycode') );
301 my $sth = $dbh->prepare("delete from categories where categorycode=?");
303 my $deleted = $sth->execute($categorycode);
306 push @messages, { type => 'message', code => 'success_on_delete' };
308 push @messages, { type => 'error', code => 'error_on_delete' };
313 # END $OP eq DELETE_CONFIRMED
316 if ( $op eq 'list' ) {
317 $template->param( else => 1 );
319 my ( $count, $results ) = StringSearch( $searchfield, 'web' );
321 my $dbh = C4::Context->dbh;
322 my $sth = $dbh->prepare("
323 SELECT b.branchcode, b.branchname
324 FROM categories_branches AS cb, branches AS b
325 WHERE cb.branchcode = b.branchcode AND cb.categorycode = ?
328 for ( my $i = 0 ; $i < $count ; $i++ ) {
329 $sth->execute( $results->[$i]{'categorycode'} );
331 my @selected_branches;
332 while ( my $branch = $sth->fetchrow_hashref ) {
333 push @selected_branches, $branch;
336 my $enrolmentperioddate = $results->[$i]{'enrolmentperioddate'};
337 if ( $enrolmentperioddate && $enrolmentperioddate eq '0000-00-00' ) {
338 $enrolmentperioddate = undef;
341 $results->[$i]{'category_type'} //= '';
344 branches => \@selected_branches,
345 categorycode => $results->[$i]{'categorycode'},
346 description => $results->[$i]{'description'},
347 enrolmentperiod => $results->[$i]{'enrolmentperiod'},
348 enrolmentperioddate => $enrolmentperioddate,
349 upperagelimit => $results->[$i]{'upperagelimit'},
350 dateofbirthrequired => $results->[$i]{'dateofbirthrequired'},
351 overduenoticerequired => $results->[$i]{'overduenoticerequired'},
352 issuelimit => $results->[$i]{'issuelimit'},
353 hidelostitems => $results->[$i]{'hidelostitems'},
354 category_type => $results->[$i]{'category_type'},
355 default_privacy => $results->[$i]{'default_privacy'},
356 reservefee => sprintf( "%.2f", $results->[$i]{'reservefee'} || 0 ),
358 sprintf( "%.2f", $results->[$i]{'enrolmentfee'} || 0 ),
359 "type_" . $results->[$i]{'category_type'} => 1,
362 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
364 _get_brief_messaging_prefs( $results->[$i]{'categorycode'} );
365 $row{messaging_prefs} = $brief_prefs if @$brief_prefs;
370 $template->param( loop => \@loop );
372 # check that I (institution) and C (child) exists. otherwise => warning to the user
373 $sth = $dbh->prepare("select category_type from categories where category_type='C'");
375 my ($categoryChild) = $sth->fetchrow;
376 $template->param( categoryChild => $categoryChild );
378 $sth = $dbh->prepare("select category_type from categories where category_type='I'");
380 my ($categoryInstitution) = $sth->fetchrow;
381 $template->param( categoryInstitution => $categoryInstitution );
384 } #---- END $OP eq DEFAULT
387 script_name => $script_name,
388 categorycode => $categorycode,
389 searchfield => $searchfield,
390 messages => \@messages,
393 output_html_with_http_headers $input, $cookie, $template->output;
397 sub _get_brief_messaging_prefs {
398 my $categorycode = shift;
399 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
401 PREF: foreach my $option (@$messaging_options) {
402 my $pref = C4::Members::Messaging::GetMessagingPreferences(
404 categorycode => $categorycode,
405 message_name => $option->{'message_name'}
408 next unless $pref->{'transports'};
410 message_attribute_id => $option->{'message_attribute_id'},
411 message_name => $option->{'message_name'},
412 $option->{'message_name'} => 1
414 foreach my $transport ( keys %{ $pref->{'transports'} } ) {
415 push @{ $brief_pref->{'transports'} }, { transport => $transport };
417 push @$results, $brief_pref;