3 #script to administer the categories table
4 #written 20/02/2002 by paul.poulain@free.fr
5 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
8 # this script use an $op to know what to do.
9 # if $op is empty or none of the above values,
10 # - the default screen is build (with all records, or filtered datas).
11 # - the user can clic on add, modify or delete record.
13 # - if primkey exists, this is a modification,so we read the $primkey record
14 # - builds the add/modify form
16 # - the user has just send datas, so we create/modify the record
18 # - we show the record having primkey=$primkey and ask for deletion validation form
19 # if $op=delete_confirm
20 # - we delete the record having primkey=$primkey
23 # Copyright 2000-2002 Katipo Communications
25 # This file is part of Koha.
27 # Koha is free software; you can redistribute it and/or modify it under the
28 # terms of the GNU General Public License as published by the Free Software
29 # Foundation; either version 2 of the License, or (at your option) any later
32 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
33 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
34 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
36 # You should have received a copy of the GNU General Public License along with
37 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
38 # Suite 330, Boston, MA 02111-1307 USA
46 use C4::Interface::CGI::Output;
50 my ($env,$searchstring,$type)=@_;
51 my $dbh = C4::Context->dbh;
52 $searchstring=~ s/\'/\\\'/g;
53 my @data=split(' ',$searchstring);
55 my $query="Select * from itemtypes where (description like \"$data[0]%\") order by itemtype";
56 my $sth=$dbh->prepare($query);
60 while (my $data=$sth->fetchrow_hashref){
66 return ($cnt,\@results);
70 my $searchfield=$input->param('description');
71 my $offset=$input->param('offset');
72 my $script_name="/cgi-bin/koha/admin/itemtypes.pl";
73 my $itemtype=$input->param('itemtype');
75 my $op = $input->param('op');
76 $searchfield=~ s/\,//g;
77 my ($template, $borrowernumber, $cookie)
78 = get_template_and_user({template_name => "parameters/itemtypes.tmpl",
82 flagsrequired => {parameters => 1},
87 $template->param(script_name => $script_name,
88 $op => 1); # we show only the TMPL_VAR names $op
90 $template->param(script_name => $script_name,
91 else => 1); # we show only the TMPL_VAR names $op
93 ################## ADD_FORM ##################################
94 # called by default. Used to create form to add or modify a record
95 if ($op eq 'add_form') {
96 #start the page and read in includes
97 #---- if primkey exists, it's a modify action, so read values to modify...
100 my $dbh = C4::Context->dbh;
101 my $sth=$dbh->prepare("select itemtype,description,loanlength,renewalsallowed,rentalcharge from itemtypes where itemtype='$itemtype'");
103 $data=$sth->fetchrow_hashref;
106 $template->param(itemtype => $itemtype,
107 description => $data->{'description'},
108 loanlength => $data->{'loanlength'},
109 renewalsallowed => $data->{'renewalsallowed'},
110 rentalcharge => $data->{'rentalcharge'});
112 # END $OP eq ADD_FORM
113 ################## ADD_VALIDATE ##################################
114 # called by add_form, used to insert/modify data in DB
115 } elsif ($op eq 'add_validate') {
116 my $dbh = C4::Context->dbh;
117 my $query = "replace itemtypes (itemtype,description,loanlength,renewalsallowed,rentalcharge) values (";
118 $query.= $dbh->quote($input->param('itemtype')).",";
119 $query.= $dbh->quote($input->param('description')).",";
120 $query.= $dbh->quote($input->param('loanlength')).",";
121 if ($input->param('renewalsallowed') ne 1) {
126 $query.= $dbh->quote($input->param('rentalcharge')).")";
127 my $sth=$dbh->prepare($query);
130 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
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 #start the page and read in includes
137 my $dbh = C4::Context->dbh;
138 my $sth=$dbh->prepare("select count(*) as total from categoryitem where itemtype='$itemtype'");
140 my $total = $sth->fetchrow_hashref;
142 # FIXME - There's already a $sth in this scope.
143 my $sth=$dbh->prepare("select itemtype,description,loanlength,renewalsallowed,rentalcharge from itemtypes where itemtype='$itemtype'");
145 my $data=$sth->fetchrow_hashref;
147 $template->param(itemtype => $itemtype,
148 description => $data->{'description'},
149 loanlength => $data->{'loanlength'},
150 renewalsallowed => $data->{'renewalsallowed'},
151 rentalcharge => $data->{'rentalcharge'},
152 total => $total->{'total'});
153 # END $OP eq DELETE_CONFIRM
154 ################## DELETE_CONFIRMED ##################################
155 # called by delete_confirm, used to effectively confirm deletion of data in DB
156 } elsif ($op eq 'delete_confirmed') {
157 #start the page and read in includes
158 my $dbh = C4::Context->dbh;
159 my $itemtype=uc($input->param('itemtype'));
160 my $query = "delete from itemtypes where itemtype='$itemtype'";
161 my $sth=$dbh->prepare($query);
164 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
166 # END $OP eq DELETE_CONFIRMED
167 ################## DEFAULT ##################################
170 my ($count,$results)=StringSearch($env,$searchfield,'web');
173 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
175 if ($toggle eq 'white'){
176 $row_data{toggle}="#ffffcc";
178 $row_data{toggle}="white";
180 $row_data{itemtype} = $results->[$i]{'itemtype'};
181 $row_data{description} = $results->[$i]{'description'};
182 $row_data{loanlength} = $results->[$i]{'loanlength'};
183 $row_data{renewalsallowed} = $results->[$i]{'renewalsallowed'};
184 $row_data{rentalcharge} = $results->[$i]{'rentalcharge'};
185 push(@loop_data, \%row_data);
187 $template->param(loop => \@loop_data);
188 } #---- END $OP eq DEFAULT
189 output_html_with_http_headers $input, $cookie, $template->output;