Bug 14285: Bengali locale needs to be re-defined
[koha.git] / admin / categorie.pl
blob27e95cb1f5897e68a26c1b36f5b96726963e698c
1 #!/usr/bin/perl
3 #script to administer the categories table
4 #written 20/02/2002 by paul.poulain@free.fr
6 # ALGO :
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.
11 # if $op=add_form
12 # - if primkey exists, this is a modification,so we read the $primkey record
13 # - builds the add/modify form
14 # if $op=add_validate
15 # - the user has just send datas, so we create/modify the record
16 # if $op=delete_form
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
26 # under the terms of the GNU General Public License as published by
27 # the Free Software Foundation; either version 3 of the License, or
28 # (at your option) any later version.
30 # Koha is distributed in the hope that it will be useful, but
31 # WITHOUT ANY WARRANTY; without even the implied warranty of
32 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 # GNU General Public License for more details.
35 # You should have received a copy of the GNU General Public License
36 # along with Koha; if not, see <http://www.gnu.org/licenses>.
38 use Modern::Perl;
40 use CGI qw ( -utf8 );
41 use C4::Context;
42 use C4::Auth;
43 use C4::Branch;
44 use C4::Output;
45 use C4::Dates;
46 use C4::Form::MessagingPreferences;
47 use Koha::Database;
49 sub StringSearch {
50 my ( $searchstring, $type ) = @_;
51 my $dbh = C4::Context->dbh;
52 $searchstring //= '';
53 $searchstring =~ s/\'/\\\'/g;
54 my @data = split( ' ', $searchstring );
55 push @data, q{} if $#data == -1;
56 my $count = @data;
57 my $sth = $dbh->prepare("Select * from categories where (description like ?) order by category_type,description,categorycode");
58 $sth->execute("$data[0]%");
59 my @results;
61 while ( my $data = $sth->fetchrow_hashref ) {
62 push( @results, $data );
65 # $sth->execute;
66 $sth->finish;
67 return ( scalar(@results), \@results );
70 my $input = new CGI;
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");
76 my @messages;
78 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
80 template_name => "admin/categorie.tt",
81 query => $input,
82 type => "intranet",
83 authnotrequired => 0,
84 flagsrequired => { parameters => 'parameters_remaining_permissions' },
85 debug => 1,
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...
95 my $data;
96 my @selected_branches;
97 if ($categorycode) {
98 my $dbh = C4::Context->dbh;
99 my $sth =
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;
113 $sth->finish;
116 if ( $data->{'enrolmentperioddate'}
117 && $data->{'enrolmentperioddate'} eq '0000-00-00' )
119 $data->{'enrolmentperioddate'} = undef;
122 $data->{'category_type'} //= '';
124 my $branches = GetBranches();
125 my @branches_loop;
126 foreach my $branch ( sort keys %$branches ) {
127 my $selected =
128 ( grep { $$_{branchcode} eq $branch } @selected_branches ) ? 1 : 0;
129 push @branches_loop,
131 branchcode => $$branches{$branch}{branchcode},
132 branchname => $$branches{$branch}{branchname},
133 selected => $selected,
137 $template->param(
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') ) {
175 $input->param(
176 'enrolmentperioddate' => C4::Dates::format_date_in_iso(
177 $input->param('enrolmentperioddate')
182 if ($is_a_modif) {
183 my $sth = $dbh->prepare( "
184 UPDATE categories
185 SET description=?,
186 enrolmentperiod=?,
187 enrolmentperioddate=?,
188 upperagelimit=?,
189 dateofbirthrequired=?,
190 enrolmentfee=?,
191 reservefee=?,
192 hidelostitems=?,
193 overduenoticerequired=?,
194 category_type=?,
195 BlockExpiredPatronOpacActions=?,
196 default_privacy=?
197 WHERE categorycode=?"
199 $sth->execute(
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',
207 'categorycode'
210 $sth->finish;
212 else {
213 my $sth = $dbh->prepare( "
214 INSERT INTO categories (
215 categorycode,
216 description,
217 enrolmentperiod,
218 enrolmentperioddate,
219 upperagelimit,
220 dateofbirthrequired,
221 enrolmentfee,
222 reservefee,
223 hidelostitems,
224 overduenoticerequired,
225 category_type,
226 BlockExpiredPatronOpacActions,
227 default_privacy
229 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)" );
230 my $inserted = $sth->execute(
231 map { $input->param($_) } (
232 'categorycode', 'description',
233 'enrolmentperiod', 'enrolmentperioddate',
234 'upperagelimit', 'dateofbirthrequired',
235 'enrolmentfee', 'reservefee',
236 'hidelostitems', 'overduenoticerequired',
237 'category_type', 'block_expired',
238 'default_privacy',
241 if ( $inserted ) {
242 push @messages, { type => 'message', code => 'success_on_insert' };
243 } else {
244 push @messages, { type => 'error', code => 'error_on_insert' };
248 my @branches = $input->param("branches");
249 if (@branches) {
250 my $sth = $dbh->prepare(
251 "DELETE FROM categories_branches WHERE categorycode = ?"
253 $sth->execute( $input->param("categorycode") );
254 $sth = $dbh->prepare(
255 "INSERT INTO categories_branches ( categorycode, branchcode ) VALUES ( ?, ? )"
257 for my $branchcode (@branches) {
258 next if not $branchcode;
259 $sth->bind_param( 1, $input->param("categorycode") );
260 $sth->bind_param( 2, $branchcode );
261 $sth->execute;
265 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
266 C4::Form::MessagingPreferences::handle_form_action( $input,
267 { categorycode => $input->param('categorycode') }, $template );
270 $searchfield = q||;
271 $op = 'list';
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 );
281 my $count =
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 my $deleted = $sth->execute($categorycode);
306 if ( $deleted ) {
307 push @messages, { type => 'message', code => 'success_on_delete' };
308 } else {
309 push @messages, { type => 'error', code => 'error_on_delete' };
312 $op = 'list';
314 # END $OP eq DELETE_CONFIRMED
317 if ( $op eq 'list' ) {
318 $template->param( else => 1 );
319 my @loop;
320 my ( $count, $results ) = StringSearch( $searchfield, 'web' );
322 my $dbh = C4::Context->dbh;
323 my $sth = $dbh->prepare("
324 SELECT b.branchcode, b.branchname
325 FROM categories_branches AS cb, branches AS b
326 WHERE cb.branchcode = b.branchcode AND cb.categorycode = ?
329 for ( my $i = 0 ; $i < $count ; $i++ ) {
330 $sth->execute( $results->[$i]{'categorycode'} );
332 my @selected_branches;
333 while ( my $branch = $sth->fetchrow_hashref ) {
334 push @selected_branches, $branch;
337 my $enrolmentperioddate = $results->[$i]{'enrolmentperioddate'};
338 if ( $enrolmentperioddate && $enrolmentperioddate eq '0000-00-00' ) {
339 $enrolmentperioddate = undef;
342 $results->[$i]{'category_type'} //= '';
344 my %row = (
345 branches => \@selected_branches,
346 categorycode => $results->[$i]{'categorycode'},
347 description => $results->[$i]{'description'},
348 enrolmentperiod => $results->[$i]{'enrolmentperiod'},
349 enrolmentperioddate => $enrolmentperioddate,
350 upperagelimit => $results->[$i]{'upperagelimit'},
351 dateofbirthrequired => $results->[$i]{'dateofbirthrequired'},
352 overduenoticerequired => $results->[$i]{'overduenoticerequired'},
353 issuelimit => $results->[$i]{'issuelimit'},
354 hidelostitems => $results->[$i]{'hidelostitems'},
355 category_type => $results->[$i]{'category_type'},
356 default_privacy => $results->[$i]{'default_privacy'},
357 reservefee => sprintf( "%.2f", $results->[$i]{'reservefee'} || 0 ),
358 enrolmentfee =>
359 sprintf( "%.2f", $results->[$i]{'enrolmentfee'} || 0 ),
360 "type_" . $results->[$i]{'category_type'} => 1,
363 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
364 my $brief_prefs =
365 _get_brief_messaging_prefs( $results->[$i]{'categorycode'} );
366 $row{messaging_prefs} = $brief_prefs if @$brief_prefs;
368 push @loop, \%row;
371 $template->param( loop => \@loop );
373 # check that I (institution) and C (child) exists. otherwise => warning to the user
374 $sth = $dbh->prepare("select category_type from categories where category_type='C'");
375 $sth->execute;
376 my ($categoryChild) = $sth->fetchrow;
377 $template->param( categoryChild => $categoryChild );
379 $sth = $dbh->prepare("select category_type from categories where category_type='I'");
380 $sth->execute;
381 my ($categoryInstitution) = $sth->fetchrow;
382 $template->param( categoryInstitution => $categoryInstitution );
383 $sth->finish;
385 } #---- END $OP eq DEFAULT
387 $template->param(
388 script_name => $script_name,
389 categorycode => $categorycode,
390 searchfield => $searchfield,
391 messages => \@messages,
394 output_html_with_http_headers $input, $cookie, $template->output;
396 exit 0;
398 sub _get_brief_messaging_prefs {
399 my $categorycode = shift;
400 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
401 my $results = [];
402 PREF: foreach my $option (@$messaging_options) {
403 my $pref = C4::Members::Messaging::GetMessagingPreferences(
405 categorycode => $categorycode,
406 message_name => $option->{'message_name'}
409 next unless $pref->{'transports'};
410 my $brief_pref = {
411 message_attribute_id => $option->{'message_attribute_id'},
412 message_name => $option->{'message_name'},
413 $option->{'message_name'} => 1
415 foreach my $transport ( keys %{ $pref->{'transports'} } ) {
416 push @{ $brief_pref->{'transports'} }, { transport => $transport };
418 push @$results, $brief_pref;
420 return $results;