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
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
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.
47 use C4::Form::MessagingPreferences;
50 my ($searchstring,$type)=@_;
51 my $dbh = C4::Context->dbh;
52 $searchstring=~ s/\'/\\\'/g;
53 my @data=split(' ',$searchstring);
55 my $sth=$dbh->prepare("Select * from categories where (description like ?) order by category_type,description,categorycode");
56 $sth->execute("$data[0]%");
58 while (my $data=$sth->fetchrow_hashref){
63 return (scalar(@results),\@results);
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",
77 flagsrequired => {parameters => 'parameters_remaining_permissions'},
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...
94 my @selected_branches;
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;
109 $data->{'enrolmentperioddate'} = undef if ($data->{'enrolmentperioddate'} eq '0000-00-00');
111 my $branches = GetBranches;
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 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
134 SMSSendDriver => C4::Context->preference("SMSSendDriver"),
135 TalkingTechItivaPhone => C4::Context->preference("TalkingTechItivaPhoneNotification"),
136 "type_".$data->{'category_type'} => 1,
137 branches_loop => \@branches_loop,
139 if (C4::Context->preference('EnhancedMessagingPreferences')) {
140 C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode } , $template);
142 # END $OP eq ADD_FORM
143 ################## ADD_VALIDATE ##################################
144 # called by add_form, used to insert/modify data in DB
145 } elsif ($op eq 'add_validate') {
146 $template->param(add_validate => 1);
147 my $is_a_modif = $input->param("is_a_modif");
148 my $dbh = C4::Context->dbh;
149 if($input->param('enrolmentperioddate')){
150 $input->param('enrolmentperioddate' => C4::Dates::format_date_in_iso($input->param('enrolmentperioddate')) );
154 my $sth=$dbh->prepare("UPDATE categories SET description=?,enrolmentperiod=?, enrolmentperioddate=?,upperagelimit=?,dateofbirthrequired=?,enrolmentfee=?,reservefee=?,hidelostitems=?,overduenoticerequired=?,category_type=? WHERE categorycode=?");
155 $sth->execute(map { $input->param($_) } ('description','enrolmentperiod','enrolmentperioddate','upperagelimit','dateofbirthrequired','enrolmentfee','reservefee','hidelostitems','overduenoticerequired','category_type','categorycode'));
156 my @branches = $input->param("branches");
158 $sth = $dbh->prepare("DELETE FROM categories_branches WHERE categorycode = ?");
159 $sth->execute( $input->param( "categorycode" ) );
160 $sth = $dbh->prepare(
161 "INSERT INTO categories_branches
162 ( categorycode, branchcode )
165 for my $branchcode ( @branches ) {
166 next if not $branchcode;
167 $sth->bind_param( 1, $input->param( "categorycode" ) );
168 $sth->bind_param( 2, $branchcode );
174 my $sth=$dbh->prepare("INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,enrolmentfee,reservefee,hidelostitems,overduenoticerequired,category_type) values (?,?,?,?,?,?,?,?,?,?,?)");
175 $sth->execute(map { $input->param($_) } ('categorycode','description','enrolmentperiod','enrolmentperioddate','upperagelimit','dateofbirthrequired','enrolmentfee','reservefee','hidelostitems','overduenoticerequired','category_type'));
178 if (C4::Context->preference('EnhancedMessagingPreferences')) {
179 C4::Form::MessagingPreferences::handle_form_action($input,
180 { categorycode => $input->param('categorycode') }, $template);
182 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
185 # END $OP eq ADD_VALIDATE
186 ################## DELETE_CONFIRM ##################################
187 # called by default form, used to confirm deletion of data in DB
188 } elsif ($op eq 'delete_confirm') {
189 $template->param(delete_confirm => 1);
191 my $dbh = C4::Context->dbh;
192 my $sth=$dbh->prepare("select count(*) as total from borrowers where categorycode=?");
193 $sth->execute($categorycode);
194 my $total = $sth->fetchrow_hashref;
196 $template->param(total => $total->{'total'});
198 my $sth2=$dbh->prepare("select categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,hidelostitems,overduenoticerequired,category_type from categories where categorycode=?");
199 $sth2->execute($categorycode);
200 my $data=$sth2->fetchrow_hashref;
202 if ($total->{'total'} >0) {
203 $template->param(totalgtzero => 1);
206 $template->param( description => $data->{'description'},
207 enrolmentperiod => $data->{'enrolmentperiod'},
208 enrolmentperioddate => C4::Dates::format_date($data->{'enrolmentperioddate'}),
209 upperagelimit => $data->{'upperagelimit'},
210 dateofbirthrequired => $data->{'dateofbirthrequired'},
211 enrolmentfee => sprintf("%.2f",$data->{'enrolmentfee'}),
212 overduenoticerequired => $data->{'overduenoticerequired'},
213 issuelimit => $data->{'issuelimit'},
214 reservefee => sprintf("%.2f",$data->{'reservefee'}),
215 hidelostitems => $data->{'hidelostitems'},
216 category_type => $data->{'category_type'},
218 # END $OP eq DELETE_CONFIRM
219 ################## DELETE_CONFIRMED ##################################
220 # called by delete_confirm, used to effectively confirm deletion of data in DB
221 } elsif ($op eq 'delete_confirmed') {
222 $template->param(delete_confirmed => 1);
223 my $dbh = C4::Context->dbh;
224 my $categorycode=uc($input->param('categorycode'));
225 my $sth=$dbh->prepare("delete from categories where categorycode=?");
226 $sth->execute($categorycode);
228 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
231 # END $OP eq DELETE_CONFIRMED
233 $template->param(else => 1);
235 my ($count,$results)=StringSearch($searchfield,'web');
236 my $dbh = C4::Context->dbh;
237 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 = ?");
238 for (my $i=0; $i < $count; $i++){
239 $sth->execute( $results->[$i]{'categorycode'} );
240 my @selected_branches;
241 while ( my $branch = $sth->fetchrow_hashref ) {
242 push @selected_branches, $branch;
245 categorycode => $results->[$i]{'categorycode'},
246 description => $results->[$i]{'description'},
247 enrolmentperiod => $results->[$i]{'enrolmentperiod'},
248 enrolmentperioddate => C4::Dates::format_date($results->[$i]{'enrolmentperioddate'}),
249 upperagelimit => $results->[$i]{'upperagelimit'},
250 dateofbirthrequired => $results->[$i]{'dateofbirthrequired'},
251 enrolmentfee => sprintf("%.2f",$results->[$i]{'enrolmentfee'}),
252 overduenoticerequired => $results->[$i]{'overduenoticerequired'},
253 issuelimit => $results->[$i]{'issuelimit'},
254 reservefee => sprintf("%.2f",$results->[$i]{'reservefee'}),
255 hidelostitems => $results->[$i]{'hidelostitems'},
256 category_type => $results->[$i]{'category_type'},
257 "type_".$results->[$i]{'category_type'} => 1,
258 branches => \@selected_branches,
260 if (C4::Context->preference('EnhancedMessagingPreferences')) {
261 my $brief_prefs = _get_brief_messaging_prefs($results->[$i]{'categorycode'});
262 $row{messaging_prefs} = $brief_prefs if @$brief_prefs;
266 $template->param(loop => \@loop);
267 # check that I (institution) and C (child) exists. otherwise => warning to the user
268 $sth=$dbh->prepare("select category_type from categories where category_type='C'");
270 my ($categoryChild) = $sth->fetchrow;
271 $template->param(categoryChild => $categoryChild);
272 $sth=$dbh->prepare("select category_type from categories where category_type='I'");
274 my ($categoryInstitution) = $sth->fetchrow;
275 $template->param(categoryInstitution => $categoryInstitution);
279 } #---- END $OP eq DEFAULT
280 output_html_with_http_headers $input, $cookie, $template->output;
284 sub _get_brief_messaging_prefs {
285 my $categorycode = shift;
286 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
288 PREF: foreach my $option ( @$messaging_options ) {
289 my $pref = C4::Members::Messaging::GetMessagingPreferences( { categorycode => $categorycode,
290 message_name => $option->{'message_name'} } );
291 next unless $pref->{'transports'};
293 message_attribute_id => $option->{'message_attribute_id'},
294 message_name => $option->{'message_name'},
295 $option->{'message_name'} => 1
297 foreach my $transport ( keys %{$pref->{'transports'}} ) {
298 push @{ $brief_pref->{'transports'} }, { transport => $transport };
300 push @$results, $brief_pref;