Fixing record.abs for authorities and MARC21
[koha.git] / admin / categorie.pl
blob57e30d9be34c46a3302086b7f1acf6eae097afc4
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 with
36 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
37 # Suite 330, Boston, MA 02111-1307 USA
39 use strict;
40 use CGI;
41 use C4::Context;
42 use C4::Auth;
43 use C4::Output;
44 use C4::Form::MessagingPreferences;
46 sub StringSearch {
47 my ($searchstring,$type)=@_;
48 my $dbh = C4::Context->dbh;
49 $searchstring=~ s/\'/\\\'/g;
50 my @data=split(' ',$searchstring);
51 my $count=@data;
52 my $sth=$dbh->prepare("Select * from categories where (description like ?) order by category_type,description,categorycode");
53 $sth->execute("$data[0]%");
54 my @results;
55 while (my $data=$sth->fetchrow_hashref){
56 push(@results,$data);
58 # $sth->execute;
59 $sth->finish;
60 return (scalar(@results),\@results);
63 my $input = new CGI;
64 my $searchfield=$input->param('description');
65 my $script_name="/cgi-bin/koha/admin/categorie.pl";
66 my $categorycode=$input->param('categorycode');
67 my $op = $input->param('op');
69 my ($template, $loggedinuser, $cookie)
70 = get_template_and_user({template_name => "admin/categorie.tmpl",
71 query => $input,
72 type => "intranet",
73 authnotrequired => 0,
74 flagsrequired => {parameters => 1},
75 debug => 1,
76 });
79 $template->param(script_name => $script_name,
80 categorycode => $categorycode,
81 searchfield => $searchfield);
84 ################## ADD_FORM ##################################
85 # called by default. Used to create form to add or modify a record
86 if ($op eq 'add_form') {
87 $template->param(add_form => 1);
89 #---- if primkey exists, it's a modify action, so read values to modify...
90 my $data;
91 if ($categorycode) {
92 my $dbh = C4::Context->dbh;
93 my $sth=$dbh->prepare("select categorycode,description,enrolmentperiod,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,overduenoticerequired,category_type from categories where categorycode=?");
94 $sth->execute($categorycode);
95 $data=$sth->fetchrow_hashref;
96 $sth->finish;
99 $template->param(description => $data->{'description'},
100 enrolmentperiod => $data->{'enrolmentperiod'},
101 upperagelimit => $data->{'upperagelimit'},
102 dateofbirthrequired => $data->{'dateofbirthrequired'},
103 enrolmentfee => sprintf("%.2f",$data->{'enrolmentfee'}),
104 overduenoticerequired => $data->{'overduenoticerequired'},
105 issuelimit => $data->{'issuelimit'},
106 reservefee => sprintf("%.2f",$data->{'reservefee'}),
107 category_type => $data->{'category_type'},
108 "type_".$data->{'category_type'} => 1,
110 if (C4::Context->preference('EnhancedMessagingPreferences')) {
111 C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode } , $template);
113 # END $OP eq ADD_FORM
114 ################## ADD_VALIDATE ##################################
115 # called by add_form, used to insert/modify data in DB
116 } elsif ($op eq 'add_validate') {
117 $template->param(add_validate => 1);
118 my $is_a_modif = $input->param("is_a_modif");
119 my $dbh = C4::Context->dbh;
120 if ($is_a_modif) {
121 my $sth=$dbh->prepare("UPDATE categories SET description=?,enrolmentperiod=?,upperagelimit=?,dateofbirthrequired=?,enrolmentfee=?,reservefee=?,overduenoticerequired=?,category_type=? WHERE categorycode=?");
122 $sth->execute(map { $input->param($_) } ('description','enrolmentperiod','upperagelimit','dateofbirthrequired','enrolmentfee','reservefee','overduenoticerequired','category_type','categorycode'));
123 $sth->finish;
124 } else {
125 my $sth=$dbh->prepare("INSERT INTO categories (categorycode,description,enrolmentperiod,upperagelimit,dateofbirthrequired,enrolmentfee,reservefee,overduenoticerequired,category_type) values (?,?,?,?,?,?,?,?,?)");
126 $sth->execute(map { $input->param($_) } ('categorycode','description','enrolmentperiod','upperagelimit','dateofbirthrequired','enrolmentfee','reservefee','overduenoticerequired','category_type'));
127 $sth->finish;
129 if (C4::Context->preference('EnhancedMessagingPreferences')) {
130 C4::Form::MessagingPreferences::handle_form_action($input,
131 { categorycode => $input->param('categorycode') }, $template);
133 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
134 exit;
136 # END $OP eq ADD_VALIDATE
137 ################## DELETE_CONFIRM ##################################
138 # called by default form, used to confirm deletion of data in DB
139 } elsif ($op eq 'delete_confirm') {
140 $template->param(delete_confirm => 1);
142 my $dbh = C4::Context->dbh;
143 my $sth=$dbh->prepare("select count(*) as total from borrowers where categorycode=?");
144 $sth->execute($categorycode);
145 my $total = $sth->fetchrow_hashref;
146 $sth->finish;
147 $template->param(total => $total->{'total'});
149 my $sth2=$dbh->prepare("select categorycode,description,enrolmentperiod,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,overduenoticerequired,category_type from categories where categorycode=?");
150 $sth2->execute($categorycode);
151 my $data=$sth2->fetchrow_hashref;
152 $sth2->finish;
153 if ($total->{'total'} >0) {
154 $template->param(totalgtzero => 1);
157 $template->param(description => $data->{'description'},
158 enrolmentperiod => $data->{'enrolmentperiod'},
159 upperagelimit => $data->{'upperagelimit'},
160 dateofbirthrequired => $data->{'dateofbirthrequired'},
161 enrolmentfee => sprintf("%.2f",$data->{'enrolmentfee'}),
162 overduenoticerequired => $data->{'overduenoticerequired'},
163 issuelimit => $data->{'issuelimit'},
164 reservefee => sprintf("%.2f",$data->{'reservefee'}),
165 category_type => $data->{'category_type'},
167 # END $OP eq DELETE_CONFIRM
168 ################## DELETE_CONFIRMED ##################################
169 # called by delete_confirm, used to effectively confirm deletion of data in DB
170 } elsif ($op eq 'delete_confirmed') {
171 $template->param(delete_confirmed => 1);
172 my $dbh = C4::Context->dbh;
173 my $categorycode=uc($input->param('categorycode'));
174 my $sth=$dbh->prepare("delete from categories where categorycode=?");
175 $sth->execute($categorycode);
176 $sth->finish;
177 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
178 exit;
180 # END $OP eq DELETE_CONFIRMED
181 } else { # DEFAULT
182 $template->param(else => 1);
183 my @loop;
184 my ($count,$results)=StringSearch($searchfield,'web');
185 for (my $i=0; $i < $count; $i++){
186 my %row = (categorycode => $results->[$i]{'categorycode'},
187 description => $results->[$i]{'description'},
188 enrolmentperiod => $results->[$i]{'enrolmentperiod'},
189 upperagelimit => $results->[$i]{'upperagelimit'},
190 dateofbirthrequired => $results->[$i]{'dateofbirthrequired'},
191 enrolmentfee => sprintf("%.2f",$results->[$i]{'enrolmentfee'}),
192 overduenoticerequired => $results->[$i]{'overduenoticerequired'},
193 issuelimit => $results->[$i]{'issuelimit'},
194 reservefee => sprintf("%.2f",$results->[$i]{'reservefee'}),
195 category_type => $results->[$i]{'category_type'},
196 "type_".$results->[$i]{'category_type'} => 1);
197 if (C4::Context->preference('EnhancedMessagingPreferences')) {
198 my $brief_prefs = _get_brief_messaging_prefs($results->[$i]{'categorycode'});
199 $row{messaging_prefs} = $brief_prefs if @$brief_prefs;
201 push @loop, \%row;
203 $template->param(loop => \@loop);
204 # check that I (institution) and C (child) exists. otherwise => warning to the user
205 my $dbh = C4::Context->dbh;
206 my $sth=$dbh->prepare("select category_type from categories where category_type='C'");
207 $sth->execute;
208 my ($categoryChild) = $sth->fetchrow;
209 $template->param(categoryChild => $categoryChild);
210 $sth=$dbh->prepare("select category_type from categories where category_type='I'");
211 $sth->execute;
212 my ($categoryInstitution) = $sth->fetchrow;
213 $template->param(categoryInstitution => $categoryInstitution);
214 $sth->finish;
217 } #---- END $OP eq DEFAULT
218 output_html_with_http_headers $input, $cookie, $template->output;
220 exit 0;
222 sub _get_brief_messaging_prefs {
223 my $categorycode = shift;
224 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
225 my $results = [];
226 PREF: foreach my $option ( @$messaging_options ) {
227 my $pref = C4::Members::Messaging::GetMessagingPreferences( { categorycode => $categorycode,
228 message_name => $option->{'message_name'} } );
229 next unless @{$pref->{'transports'}};
230 my $brief_pref = { message_attribute_id => $option->{'message_attribute_id'},
231 message_name => $option->{'message_name'},
233 foreach my $transport ( @{$pref->{'transports'}} ) {
234 push @{ $brief_pref->{'transports'} }, { transport => $transport };
236 push @$results, $brief_pref;
238 return $results;