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 );
271 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
274 # END $OP eq ADD_VALIDATE
275 ################## DELETE_CONFIRM ##################################
276 # called by default form, used to confirm deletion of data in DB
278 elsif ( $op eq 'delete_confirm' ) {
279 my $schema = Koha
::Database
->new()->schema();
280 $template->param( delete_confirm
=> 1 );
283 $schema->resultset('Borrower')
284 ->search( { categorycode
=> $categorycode } )->count();
286 my $category = $schema->resultset('Category')->find($categorycode);
288 $category->enrolmentperioddate(
289 C4
::Dates
::format_date
( $category->enrolmentperioddate() ) );
291 $template->param( category
=> $category, patrons_in_category
=> $count );
293 # END $OP eq DELETE_CONFIRM
294 ################## DELETE_CONFIRMED ##################################
295 # called by delete_confirm, used to effectively confirm deletion of data in DB
297 elsif ( $op eq 'delete_confirmed' ) {
298 $template->param( delete_confirmed
=> 1 );
299 my $dbh = C4
::Context
->dbh;
301 my $categorycode = uc( $input->param('categorycode') );
303 my $sth = $dbh->prepare("delete from categories where categorycode=?");
305 $sth->execute($categorycode);
308 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
311 # END $OP eq DELETE_CONFIRMED
314 $template->param( else => 1 );
316 my ( $count, $results ) = StringSearch
( $searchfield, 'web' );
318 my $dbh = C4
::Context
->dbh;
319 my $sth = $dbh->prepare("
320 SELECT b.branchcode, b.branchname
321 FROM categories_branches AS cb, branches AS b
322 WHERE cb.branchcode = b.branchcode AND cb.categorycode = ?
325 for ( my $i = 0 ; $i < $count ; $i++ ) {
326 $sth->execute( $results->[$i]{'categorycode'} );
328 my @selected_branches;
329 while ( my $branch = $sth->fetchrow_hashref ) {
330 push @selected_branches, $branch;
333 my $enrolmentperioddate = $results->[$i]{'enrolmentperioddate'};
334 if ( $enrolmentperioddate && $enrolmentperioddate eq '0000-00-00' ) {
335 $enrolmentperioddate = undef;
338 $results->[$i]{'category_type'} //= '';
341 branches
=> \
@selected_branches,
342 categorycode
=> $results->[$i]{'categorycode'},
343 description
=> $results->[$i]{'description'},
344 enrolmentperiod
=> $results->[$i]{'enrolmentperiod'},
345 enrolmentperioddate
=> $enrolmentperioddate,
346 upperagelimit
=> $results->[$i]{'upperagelimit'},
347 dateofbirthrequired
=> $results->[$i]{'dateofbirthrequired'},
348 overduenoticerequired
=> $results->[$i]{'overduenoticerequired'},
349 issuelimit
=> $results->[$i]{'issuelimit'},
350 hidelostitems
=> $results->[$i]{'hidelostitems'},
351 category_type
=> $results->[$i]{'category_type'},
352 default_privacy
=> $results->[$i]{'default_privacy'},
353 reservefee
=> sprintf( "%.2f", $results->[$i]{'reservefee'} || 0 ),
355 sprintf( "%.2f", $results->[$i]{'enrolmentfee'} || 0 ),
356 "type_" . $results->[$i]{'category_type'} => 1,
359 if ( C4
::Context
->preference('EnhancedMessagingPreferences') ) {
361 _get_brief_messaging_prefs
( $results->[$i]{'categorycode'} );
362 $row{messaging_prefs
} = $brief_prefs if @
$brief_prefs;
367 $template->param( loop => \
@loop );
369 # check that I (institution) and C (child) exists. otherwise => warning to the user
370 $sth = $dbh->prepare("select category_type from categories where category_type='C'");
372 my ($categoryChild) = $sth->fetchrow;
373 $template->param( categoryChild
=> $categoryChild );
375 $sth = $dbh->prepare("select category_type from categories where category_type='I'");
377 my ($categoryInstitution) = $sth->fetchrow;
378 $template->param( categoryInstitution
=> $categoryInstitution );
381 } #---- END $OP eq DEFAULT
382 output_html_with_http_headers
$input, $cookie, $template->output;
386 sub _get_brief_messaging_prefs
{
387 my $categorycode = shift;
388 my $messaging_options = C4
::Members
::Messaging
::GetMessagingOptions
();
390 PREF
: foreach my $option (@
$messaging_options) {
391 my $pref = C4
::Members
::Messaging
::GetMessagingPreferences
(
393 categorycode
=> $categorycode,
394 message_name
=> $option->{'message_name'}
397 next unless $pref->{'transports'};
399 message_attribute_id
=> $option->{'message_attribute_id'},
400 message_name
=> $option->{'message_name'},
401 $option->{'message_name'} => 1
403 foreach my $transport ( keys %{ $pref->{'transports'} } ) {
404 push @
{ $brief_pref->{'transports'} }, { transport
=> $transport };
406 push @
$results, $brief_pref;