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.
45 use C4
::Form
::MessagingPreferences
;
48 my ($searchstring,$type)=@_;
49 my $dbh = C4
::Context
->dbh;
50 $searchstring=~ s/\'/\\\'/g;
51 my @data=split(' ',$searchstring);
53 my $sth=$dbh->prepare("Select * from categories where (description like ?) order by category_type,description,categorycode");
54 $sth->execute("$data[0]%");
56 while (my $data=$sth->fetchrow_hashref){
61 return (scalar(@results),\
@results);
65 my $searchfield=$input->param('description');
66 my $script_name="/cgi-bin/koha/admin/categorie.pl";
67 my $categorycode=$input->param('categorycode');
68 my $op = $input->param('op');
70 my ($template, $loggedinuser, $cookie)
71 = get_template_and_user
({template_name
=> "admin/categorie.tmpl",
75 flagsrequired
=> {parameters
=> 1},
80 $template->param(script_name
=> $script_name,
81 categorycode
=> $categorycode,
82 searchfield
=> $searchfield);
85 ################## ADD_FORM ##################################
86 # called by default. Used to create form to add or modify a record
87 if ($op eq 'add_form') {
88 $template->param(add_form
=> 1);
90 #---- if primkey exists, it's a modify action, so read values to modify...
93 my $dbh = C4
::Context
->dbh;
94 my $sth=$dbh->prepare("select categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,overduenoticerequired,category_type from categories where categorycode=?");
95 $sth->execute($categorycode);
96 $data=$sth->fetchrow_hashref;
100 $data->{'enrolmentperioddate'} = undef if ($data->{'enrolmentperioddate'} eq '0000-00-00');
102 $template->param(description
=> $data->{'description'},
103 enrolmentperiod
=> $data->{'enrolmentperiod'},
104 enrolmentperioddate
=> C4
::Dates
::format_date
($data->{'enrolmentperioddate'}),
105 upperagelimit
=> $data->{'upperagelimit'},
106 dateofbirthrequired
=> $data->{'dateofbirthrequired'},
107 enrolmentfee
=> sprintf("%.2f",$data->{'enrolmentfee'}),
108 overduenoticerequired
=> $data->{'overduenoticerequired'},
109 issuelimit
=> $data->{'issuelimit'},
110 reservefee
=> sprintf("%.2f",$data->{'reservefee'}),
111 category_type
=> $data->{'category_type'},
112 DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar(),
113 "type_".$data->{'category_type'} => 1,
115 if (C4
::Context
->preference('EnhancedMessagingPreferences')) {
116 C4
::Form
::MessagingPreferences
::set_form_values
({ categorycode
=> $categorycode } , $template);
118 # END $OP eq ADD_FORM
119 ################## ADD_VALIDATE ##################################
120 # called by add_form, used to insert/modify data in DB
121 } elsif ($op eq 'add_validate') {
122 $template->param(add_validate
=> 1);
123 my $is_a_modif = $input->param("is_a_modif");
124 my $dbh = C4
::Context
->dbh;
125 if($input->param('enrolmentperioddate')){
126 $input->param('enrolmentperioddate' => C4
::Dates
::format_date_in_iso
($input->param('enrolmentperioddate')) );
130 my $sth=$dbh->prepare("UPDATE categories SET description=?,enrolmentperiod=?, enrolmentperioddate=?,upperagelimit=?,dateofbirthrequired=?,enrolmentfee=?,reservefee=?,overduenoticerequired=?,category_type=? WHERE categorycode=?");
131 $sth->execute(map { $input->param($_) } ('description','enrolmentperiod','enrolmentperioddate','upperagelimit','dateofbirthrequired','enrolmentfee','reservefee','overduenoticerequired','category_type','categorycode'));
134 my $sth=$dbh->prepare("INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,enrolmentfee,reservefee,overduenoticerequired,category_type) values (?,?,?,?,?,?,?,?,?,?)");
135 $sth->execute(map { $input->param($_) } ('categorycode','description','enrolmentperiod','enrolmentperioddate','upperagelimit','dateofbirthrequired','enrolmentfee','reservefee','overduenoticerequired','category_type'));
138 if (C4
::Context
->preference('EnhancedMessagingPreferences')) {
139 C4
::Form
::MessagingPreferences
::handle_form_action
($input,
140 { categorycode
=> $input->param('categorycode') }, $template);
142 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
145 # END $OP eq ADD_VALIDATE
146 ################## DELETE_CONFIRM ##################################
147 # called by default form, used to confirm deletion of data in DB
148 } elsif ($op eq 'delete_confirm') {
149 $template->param(delete_confirm
=> 1);
151 my $dbh = C4
::Context
->dbh;
152 my $sth=$dbh->prepare("select count(*) as total from borrowers where categorycode=?");
153 $sth->execute($categorycode);
154 my $total = $sth->fetchrow_hashref;
156 $template->param(total
=> $total->{'total'});
158 my $sth2=$dbh->prepare("select categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,overduenoticerequired,category_type from categories where categorycode=?");
159 $sth2->execute($categorycode);
160 my $data=$sth2->fetchrow_hashref;
162 if ($total->{'total'} >0) {
163 $template->param(totalgtzero
=> 1);
166 $template->param( description
=> $data->{'description'},
167 enrolmentperiod
=> $data->{'enrolmentperiod'},
168 enrolmentperioddate
=> C4
::Dates
::format_date
($data->{'enrolmentperioddate'}),
169 upperagelimit
=> $data->{'upperagelimit'},
170 dateofbirthrequired
=> $data->{'dateofbirthrequired'},
171 enrolmentfee
=> sprintf("%.2f",$data->{'enrolmentfee'}),
172 overduenoticerequired
=> $data->{'overduenoticerequired'},
173 issuelimit
=> $data->{'issuelimit'},
174 reservefee
=> sprintf("%.2f",$data->{'reservefee'}),
175 category_type
=> $data->{'category_type'},
177 # END $OP eq DELETE_CONFIRM
178 ################## DELETE_CONFIRMED ##################################
179 # called by delete_confirm, used to effectively confirm deletion of data in DB
180 } elsif ($op eq 'delete_confirmed') {
181 $template->param(delete_confirmed
=> 1);
182 my $dbh = C4
::Context
->dbh;
183 my $categorycode=uc($input->param('categorycode'));
184 my $sth=$dbh->prepare("delete from categories where categorycode=?");
185 $sth->execute($categorycode);
187 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
190 # END $OP eq DELETE_CONFIRMED
192 $template->param(else => 1);
194 my ($count,$results)=StringSearch
($searchfield,'web');
195 for (my $i=0; $i < $count; $i++){
197 categorycode
=> $results->[$i]{'categorycode'},
198 description
=> $results->[$i]{'description'},
199 enrolmentperiod
=> $results->[$i]{'enrolmentperiod'},
200 enrolmentperioddate
=> C4
::Dates
::format_date
($results->[$i]{'enrolmentperioddate'}),
201 upperagelimit
=> $results->[$i]{'upperagelimit'},
202 dateofbirthrequired
=> $results->[$i]{'dateofbirthrequired'},
203 enrolmentfee
=> sprintf("%.2f",$results->[$i]{'enrolmentfee'}),
204 overduenoticerequired
=> $results->[$i]{'overduenoticerequired'},
205 issuelimit
=> $results->[$i]{'issuelimit'},
206 reservefee
=> sprintf("%.2f",$results->[$i]{'reservefee'}),
207 category_type
=> $results->[$i]{'category_type'},
208 "type_".$results->[$i]{'category_type'} => 1);
209 if (C4
::Context
->preference('EnhancedMessagingPreferences')) {
210 my $brief_prefs = _get_brief_messaging_prefs
($results->[$i]{'categorycode'});
211 $row{messaging_prefs
} = $brief_prefs if @
$brief_prefs;
215 $template->param(loop => \
@loop);
216 # check that I (institution) and C (child) exists. otherwise => warning to the user
217 my $dbh = C4
::Context
->dbh;
218 my $sth=$dbh->prepare("select category_type from categories where category_type='C'");
220 my ($categoryChild) = $sth->fetchrow;
221 $template->param(categoryChild
=> $categoryChild);
222 $sth=$dbh->prepare("select category_type from categories where category_type='I'");
224 my ($categoryInstitution) = $sth->fetchrow;
225 $template->param(categoryInstitution
=> $categoryInstitution);
229 } #---- END $OP eq DEFAULT
230 output_html_with_http_headers
$input, $cookie, $template->output;
234 sub _get_brief_messaging_prefs
{
235 my $categorycode = shift;
236 my $messaging_options = C4
::Members
::Messaging
::GetMessagingOptions
();
238 PREF
: foreach my $option ( @
$messaging_options ) {
239 my $pref = C4
::Members
::Messaging
::GetMessagingPreferences
( { categorycode
=> $categorycode,
240 message_name
=> $option->{'message_name'} } );
241 next unless @
{$pref->{'transports'}};
242 my $brief_pref = { message_attribute_id
=> $option->{'message_attribute_id'},
243 message_name
=> $option->{'message_name'},
245 foreach my $transport ( @
{$pref->{'transports'}} ) {
246 push @
{ $brief_pref->{'transports'} }, { transport
=> $transport };
248 push @
$results, $brief_pref;