Bug 11188 - Make gather_print_notices.pl die on failed open()
[koha.git] / admin / categorie.pl
blob952ef7a1fdac8b3eeef5d07d183cc0a8f6298559
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
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
29 # version.
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.
39 use Modern::Perl;
41 use CGI;
42 use C4::Context;
43 use C4::Auth;
44 use C4::Branch;
45 use C4::Output;
46 use C4::Dates;
47 use C4::Form::MessagingPreferences;
49 sub StringSearch {
50 my ($searchstring,$type)=@_;
51 my $dbh = C4::Context->dbh;
52 $searchstring=~ s/\'/\\\'/g;
53 my @data=split(' ',$searchstring);
54 my $count=@data;
55 my $sth=$dbh->prepare("Select * from categories where (description like ?) order by category_type,description,categorycode");
56 $sth->execute("$data[0]%");
57 my @results;
58 while (my $data=$sth->fetchrow_hashref){
59 push(@results,$data);
61 # $sth->execute;
62 $sth->finish;
63 return (scalar(@results),\@results);
66 my $input = new CGI;
67 my $searchfield=$input->param('description');
68 my $script_name="/cgi-bin/koha/admin/categorie.pl";
69 my $categorycode=$input->param('categorycode');
70 my $op = $input->param('op');
72 my ($template, $loggedinuser, $cookie)
73 = get_template_and_user({template_name => "admin/categorie.tmpl",
74 query => $input,
75 type => "intranet",
76 authnotrequired => 0,
77 flagsrequired => {parameters => 'parameters_remaining_permissions'},
78 debug => 1,
79 });
82 $template->param(script_name => $script_name,
83 categorycode => $categorycode,
84 searchfield => $searchfield);
87 ################## ADD_FORM ##################################
88 # called by default. Used to create form to add or modify a record
89 if ($op eq 'add_form') {
90 $template->param(add_form => 1);
92 #---- if primkey exists, it's a modify action, so read values to modify...
93 my $data;
94 my @selected_branches;
95 if ($categorycode) {
96 my $dbh = C4::Context->dbh;
97 my $sth=$dbh->prepare("select categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,hidelostitems,overduenoticerequired,category_type from categories where categorycode=?");
98 $sth->execute($categorycode);
99 $data=$sth->fetchrow_hashref;
101 $sth = $dbh->prepare("SELECT b.branchcode, b.branchname FROM categories_branches AS cb, branches AS b WHERE cb.branchcode = b.branchcode AND cb.categorycode = ?");
102 $sth->execute( $categorycode );
103 while ( my $branch = $sth->fetchrow_hashref ) {
104 push @selected_branches, $branch;
106 $sth->finish;
109 $data->{'enrolmentperioddate'} = undef if ($data->{'enrolmentperioddate'} eq '0000-00-00');
111 my $branches = GetBranches;
112 my @branches_loop;
113 foreach my $branch (sort keys %$branches) {
114 my $selected = ( grep {$$_{branchcode} eq $branch} @selected_branches ) ? 1 : 0;
115 push @branches_loop, {
116 branchcode => $$branches{$branch}{branchcode},
117 branchname => $$branches{$branch}{branchname},
118 selected => $selected,
122 $template->param(description => $data->{'description'},
123 enrolmentperiod => $data->{'enrolmentperiod'},
124 enrolmentperioddate => C4::Dates::format_date($data->{'enrolmentperioddate'}),
125 upperagelimit => $data->{'upperagelimit'},
126 dateofbirthrequired => $data->{'dateofbirthrequired'},
127 enrolmentfee => sprintf("%.2f",$data->{'enrolmentfee'}),
128 overduenoticerequired => $data->{'overduenoticerequired'},
129 issuelimit => $data->{'issuelimit'},
130 reservefee => sprintf("%.2f",$data->{'reservefee'}),
131 hidelostitems => $data->{'hidelostitems'},
132 category_type => $data->{'category_type'},
133 SMSSendDriver => C4::Context->preference("SMSSendDriver"),
134 TalkingTechItivaPhone => C4::Context->preference("TalkingTechItivaPhoneNotification"),
135 "type_".$data->{'category_type'} => 1,
136 branches_loop => \@branches_loop,
138 if (C4::Context->preference('EnhancedMessagingPreferences')) {
139 C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode } , $template);
141 # END $OP eq ADD_FORM
142 ################## ADD_VALIDATE ##################################
143 # called by add_form, used to insert/modify data in DB
144 } elsif ($op eq 'add_validate') {
145 $template->param(add_validate => 1);
146 my $is_a_modif = $input->param("is_a_modif");
147 my $dbh = C4::Context->dbh;
148 if($input->param('enrolmentperioddate')){
149 $input->param('enrolmentperioddate' => C4::Dates::format_date_in_iso($input->param('enrolmentperioddate')) );
152 if ($is_a_modif) {
153 my $sth=$dbh->prepare("UPDATE categories SET description=?,enrolmentperiod=?, enrolmentperioddate=?,upperagelimit=?,dateofbirthrequired=?,enrolmentfee=?,reservefee=?,hidelostitems=?,overduenoticerequired=?,category_type=? WHERE categorycode=?");
154 $sth->execute(map { $input->param($_) } ('description','enrolmentperiod','enrolmentperioddate','upperagelimit','dateofbirthrequired','enrolmentfee','reservefee','hidelostitems','overduenoticerequired','category_type','categorycode'));
155 my @branches = $input->param("branches");
156 if ( @branches ) {
157 $sth = $dbh->prepare("DELETE FROM categories_branches WHERE categorycode = ?");
158 $sth->execute( $input->param( "categorycode" ) );
159 $sth = $dbh->prepare(
160 "INSERT INTO categories_branches
161 ( categorycode, branchcode )
162 VALUES ( ?, ? )"
164 for my $branchcode ( @branches ) {
165 next if not $branchcode;
166 $sth->bind_param( 1, $input->param( "categorycode" ) );
167 $sth->bind_param( 2, $branchcode );
168 $sth->execute;
171 $sth->finish;
172 } else {
173 my $sth=$dbh->prepare("INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,enrolmentfee,reservefee,hidelostitems,overduenoticerequired,category_type) values (?,?,?,?,?,?,?,?,?,?,?)");
174 $sth->execute(map { $input->param($_) } ('categorycode','description','enrolmentperiod','enrolmentperioddate','upperagelimit','dateofbirthrequired','enrolmentfee','reservefee','hidelostitems','overduenoticerequired','category_type'));
175 $sth->finish;
177 if (C4::Context->preference('EnhancedMessagingPreferences')) {
178 C4::Form::MessagingPreferences::handle_form_action($input,
179 { categorycode => $input->param('categorycode') }, $template);
181 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
182 exit;
184 # END $OP eq ADD_VALIDATE
185 ################## DELETE_CONFIRM ##################################
186 # called by default form, used to confirm deletion of data in DB
187 } elsif ($op eq 'delete_confirm') {
188 $template->param(delete_confirm => 1);
190 my $dbh = C4::Context->dbh;
191 my $sth=$dbh->prepare("select count(*) as total from borrowers where categorycode=?");
192 $sth->execute($categorycode);
193 my $total = $sth->fetchrow_hashref;
194 $sth->finish;
195 $template->param(total => $total->{'total'});
197 my $sth2=$dbh->prepare("select categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,hidelostitems,overduenoticerequired,category_type from categories where categorycode=?");
198 $sth2->execute($categorycode);
199 my $data=$sth2->fetchrow_hashref;
200 $sth2->finish;
201 if ($total->{'total'} >0) {
202 $template->param(totalgtzero => 1);
205 $template->param( description => $data->{'description'},
206 enrolmentperiod => $data->{'enrolmentperiod'},
207 enrolmentperioddate => C4::Dates::format_date($data->{'enrolmentperioddate'}),
208 upperagelimit => $data->{'upperagelimit'},
209 dateofbirthrequired => $data->{'dateofbirthrequired'},
210 enrolmentfee => sprintf("%.2f",$data->{'enrolmentfee'}),
211 overduenoticerequired => $data->{'overduenoticerequired'},
212 issuelimit => $data->{'issuelimit'},
213 reservefee => sprintf("%.2f",$data->{'reservefee'}),
214 hidelostitems => $data->{'hidelostitems'},
215 category_type => $data->{'category_type'},
217 # END $OP eq DELETE_CONFIRM
218 ################## DELETE_CONFIRMED ##################################
219 # called by delete_confirm, used to effectively confirm deletion of data in DB
220 } elsif ($op eq 'delete_confirmed') {
221 $template->param(delete_confirmed => 1);
222 my $dbh = C4::Context->dbh;
223 my $categorycode=uc($input->param('categorycode'));
224 my $sth=$dbh->prepare("delete from categories where categorycode=?");
225 $sth->execute($categorycode);
226 $sth->finish;
227 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
228 exit;
230 # END $OP eq DELETE_CONFIRMED
231 } else { # DEFAULT
232 $template->param(else => 1);
233 my @loop;
234 my ($count,$results)=StringSearch($searchfield,'web');
235 my $dbh = C4::Context->dbh;
236 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 = ?");
237 for (my $i=0; $i < $count; $i++){
238 $sth->execute( $results->[$i]{'categorycode'} );
239 my @selected_branches;
240 while ( my $branch = $sth->fetchrow_hashref ) {
241 push @selected_branches, $branch;
243 my %row = (
244 categorycode => $results->[$i]{'categorycode'},
245 description => $results->[$i]{'description'},
246 enrolmentperiod => $results->[$i]{'enrolmentperiod'},
247 enrolmentperioddate => C4::Dates::format_date($results->[$i]{'enrolmentperioddate'}),
248 upperagelimit => $results->[$i]{'upperagelimit'},
249 dateofbirthrequired => $results->[$i]{'dateofbirthrequired'},
250 enrolmentfee => sprintf("%.2f",$results->[$i]{'enrolmentfee'}),
251 overduenoticerequired => $results->[$i]{'overduenoticerequired'},
252 issuelimit => $results->[$i]{'issuelimit'},
253 reservefee => sprintf("%.2f",$results->[$i]{'reservefee'}),
254 hidelostitems => $results->[$i]{'hidelostitems'},
255 category_type => $results->[$i]{'category_type'},
256 "type_".$results->[$i]{'category_type'} => 1,
257 branches => \@selected_branches,
259 if (C4::Context->preference('EnhancedMessagingPreferences')) {
260 my $brief_prefs = _get_brief_messaging_prefs($results->[$i]{'categorycode'});
261 $row{messaging_prefs} = $brief_prefs if @$brief_prefs;
263 push @loop, \%row;
265 $template->param(loop => \@loop);
266 # check that I (institution) and C (child) exists. otherwise => warning to the user
267 $sth=$dbh->prepare("select category_type from categories where category_type='C'");
268 $sth->execute;
269 my ($categoryChild) = $sth->fetchrow;
270 $template->param(categoryChild => $categoryChild);
271 $sth=$dbh->prepare("select category_type from categories where category_type='I'");
272 $sth->execute;
273 my ($categoryInstitution) = $sth->fetchrow;
274 $template->param(categoryInstitution => $categoryInstitution);
275 $sth->finish;
278 } #---- END $OP eq DEFAULT
279 output_html_with_http_headers $input, $cookie, $template->output;
281 exit 0;
283 sub _get_brief_messaging_prefs {
284 my $categorycode = shift;
285 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
286 my $results = [];
287 PREF: foreach my $option ( @$messaging_options ) {
288 my $pref = C4::Members::Messaging::GetMessagingPreferences( { categorycode => $categorycode,
289 message_name => $option->{'message_name'} } );
290 next unless $pref->{'transports'};
291 my $brief_pref = {
292 message_attribute_id => $option->{'message_attribute_id'},
293 message_name => $option->{'message_name'},
294 $option->{'message_name'} => 1
296 foreach my $transport ( keys %{$pref->{'transports'}} ) {
297 push @{ $brief_pref->{'transports'} }, { transport => $transport };
299 push @$results, $brief_pref;
301 return $results;