sync'ing with rel_2_0 (mostly prepare/execute pb, bug #662)
[koha.git] / admin / categorie.pl
1 #!/usr/bin/perl
2
3 #script to administer the categories table
4 #written 20/02/2002 by paul.poulain@free.fr
5
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
20
21
22 # Copyright 2000-2002 Katipo Communications
23 #
24 # This file is part of Koha.
25 #
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.
30 #
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.
34 #
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
38
39 use strict;
40 use CGI;
41 use C4::Context;
42 use C4::Output;
43 use C4::Search;
44 use HTML::Template;
45 use C4::Auth;
46 use C4::Interface::CGI::Output;
47
48 sub StringSearch  {
49         my ($env,$searchstring,$type)=@_;
50         my $dbh = C4::Context->dbh;
51         $searchstring=~ s/\'/\\\'/g;
52         my @data=split(' ',$searchstring);
53         my $count=@data;
54         my $query="Select * from categories where (description like \"$data[0]%\")";
55         my $sth=$dbh->prepare($query);
56         $sth->execute;
57         my @results;
58         my $cnt=0;
59         while (my $data=$sth->fetchrow_hashref){
60         push(@results,$data);
61         $cnt ++;
62         }
63         #  $sth->execute;
64         $sth->finish;
65         return ($cnt,\@results);
66 }
67
68 my $input = new CGI;
69 my $searchfield=$input->param('description');
70 my $script_name="/cgi-bin/koha/admin/categorie.pl";
71 my $categorycode=$input->param('categorycode');
72 my $op = $input->param('op');
73 $searchfield=~ s/\,//g;
74
75 my ($template, $loggedinuser, $cookie)
76     = get_template_and_user({template_name => "parameters/categorie.tmpl",
77                              query => $input,
78                              type => "intranet",
79                              authnotrequired => 0,
80                              flagsrequired => {parameters => 1},
81                              debug => 1,
82                              });
83
84
85 $template->param(script_name => $script_name,
86                  categorycode => $categorycode,
87                  searchfield => $searchfield);
88
89
90 ################## ADD_FORM ##################################
91 # called by default. Used to create form to add or  modify a record
92 if ($op eq 'add_form') {
93         $template->param(add_form => 1);
94         
95         #---- if primkey exists, it's a modify action, so read values to modify...
96         my $data;
97         if ($categorycode) {
98                 my $dbh = C4::Context->dbh;
99                 my $sth=$dbh->prepare("select categorycode,description,enrolmentperiod,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,overduenoticerequired from categories where categorycode='$categorycode'");
100                 $sth->execute;
101                 $data=$sth->fetchrow_hashref;
102                 $sth->finish;
103         }
104
105         $template->param(description             => $data->{'description'},
106                                 enrolmentperiod         => $data->{'enrolmentperiod'},
107                                 upperagelimit           => $data->{'upperagelimit'},
108                                 dateofbirthrequired     => $data->{'dateofbirthrequired'},
109                                 enrolmentfee            => $data->{'enrolmentfee'},
110                                 overduenoticerequired   => $data->{'overduenoticerequired'},
111                                 issuelimit              => $data->{'issuelimit'},
112                                 reservefee              => $data->{'reservefee'});
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 $dbh = C4::Context->dbh;
119         my $query = "replace categories (categorycode,description,enrolmentperiod,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,overduenoticerequired) values (";
120         $query.= $dbh->quote($input->param('categorycode')).",";
121         $query.= $dbh->quote($input->param('description')).",";
122         $query.= $dbh->quote($input->param('enrolmentperiod')).",";
123         $query.= $dbh->quote($input->param('upperagelimit')).",";
124         $query.= $dbh->quote($input->param('dateofbirthrequired')).",";
125         $query.= $dbh->quote($input->param('enrolmentfee')).",";
126         $query.= $dbh->quote($input->param('issuelimit')).",";
127         $query.= $dbh->quote($input->param('reservefee')).",";
128         $query.= $dbh->quote($input->param('overduenoticerequired')).")";
129         my $sth=$dbh->prepare($query);
130         $sth->execute;
131         $sth->finish;
132                                                                                                         # END $OP eq ADD_VALIDATE
133 ################## DELETE_CONFIRM ##################################
134 # called by default form, used to confirm deletion of data in DB
135 } elsif ($op eq 'delete_confirm') {
136         $template->param(delete_confirm => 1);
137
138         my $dbh = C4::Context->dbh;
139         my $sth=$dbh->prepare("select count(*) as total from categoryitem where categorycode='$categorycode'");
140         $sth->execute;
141         my $total = $sth->fetchrow_hashref;
142         $sth->finish;
143         $template->param(total => $total->{'total'});
144         
145         my $sth2=$dbh->prepare("select categorycode,description,enrolmentperiod,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,overduenoticerequired from categories where categorycode='$categorycode'");
146         $sth2->execute;
147         my $data=$sth2->fetchrow_hashref;
148         $sth2->finish;
149         if ($total->{'total'} >0) {
150                 $template->param(totalgtzero => 1);
151         }
152
153         $template->param(description             => $data->{'description'},
154                                 enrolmentperiod         => $data->{'enrolmentperiod'},
155                                 upperagelimit           => $data->{'upperagelimit'},
156                                 dateofbirthrequired     => $data->{'dateofbirthrequired'},
157                                 enrolmentfee            => $data->{'enrolmentfee'},
158                                 overduenoticerequired   => $data->{'overduenoticerequired'},
159                                 issuelimit              => $data->{'issuelimit'},
160                                 reservefee              => $data->{'reservefee'});
161
162
163                                                                                                         # END $OP eq DELETE_CONFIRM
164 ################## DELETE_CONFIRMED ##################################
165 # called by delete_confirm, used to effectively confirm deletion of data in DB
166 } elsif ($op eq 'delete_confirmed') {
167         $template->param(delete_confirmed => 1);
168         my $dbh = C4::Context->dbh;
169         my $categorycode=uc($input->param('categorycode'));
170         my $query = "delete from categories where categorycode='$categorycode'";
171         my $sth=$dbh->prepare($query);
172         $sth->execute;
173         $sth->finish;
174                                                                                                         # END $OP eq DELETE_CONFIRMED
175 } else { # DEFAULT
176         $template->param(else => 1);
177         my $env;
178         my @loop;
179         my ($count,$results)=StringSearch($env,$searchfield,'web');
180         my $toggle = 'white';
181         for (my $i=0; $i < $count; $i++){
182                 my %row = (categorycode => $results->[$i]{'categorycode'},
183                                 description => $results->[$i]{'description'},
184                                 enrolmentperiod => $results->[$i]{'enrolmentperiod'},
185                                 upperagelimit => $results->[$i]{'upperagelimit'},
186                                 dateofbirthrequired => $results->[$i]{'dateofbirthrequired'},
187                                 enrolmentfee => $results->[$i]{'enrolmentfee'},
188                                 overduenoticerequired => $results->[$i]{'overduenoticerequired'},
189                                 issuelimit => $results->[$i]{'issuelimit'},
190                                 reservefee => $results->[$i]{'reservefee'},
191                                 toggle => $toggle );    
192                 push @loop, \%row;
193                 if ( $toggle eq 'white' )
194                 {
195                         $toggle = '#ffffcc';
196                 }
197                 else
198                 {
199                         $toggle = 'white';
200                 }
201         }
202         $template->param(loop => \@loop);
203
204
205
206 } #---- END $OP eq DEFAULT
207
208
209
210 output_html_with_http_headers $input, $cookie, $template->output;
211