2 # NOTE: 4-character tabs
4 #script to administer the categories table
5 #written 20/02/2002 by paul.poulain@free.fr
6 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
9 # this script use an $op to know what to do.
10 # if $op is empty or none of the above values,
11 # - the default screen is build (with all records, or filtered datas).
12 # - the user can clic on add, modify or delete record.
14 # - if primkey exists, this is a modification,so we read the $primkey record
15 # - builds the add/modify form
17 # - the user has just send datas, so we create/modify the record
19 # - we show the record having primkey=$primkey and ask for deletion validation form
20 # if $op=delete_confirm
21 # - we delete the record having primkey=$primkey
24 # Copyright 2000-2002 Katipo Communications
26 # This file is part of Koha.
28 # Koha is free software; you can redistribute it and/or modify it under the
29 # terms of the GNU General Public License as published by the Free Software
30 # Foundation; either version 2 of the License, or (at your option) any later
33 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
34 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
35 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
37 # You should have received a copy of the GNU General Public License along with
38 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
39 # Suite 330, Boston, MA 02111-1307 USA
44 use List::Util qw/min/;
51 use C4::Interface::CGI::Output;
54 my ($env,$searchstring,$type)=@_;
55 my $dbh = C4::Context->dbh;
56 $searchstring=~ s/\'/\\\'/g;
57 my @data=split(' ',$searchstring);
59 my $sth=$dbh->prepare("Select * from itemtypes where (description like ?) order by itemtype");
60 $sth->execute("$data[0]%");
62 while (my $data=$sth->fetchrow_hashref){
67 return (scalar(@results),\@results);
71 my $searchfield=$input->param('description');
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 => "admin/itemtypes.tmpl",
82 flagsrequired => {parameters => 1, management => 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 * from itemtypes where itemtype=?");
102 $sth->execute($itemtype);
103 $data=$sth->fetchrow_hashref;
106 # build list of images
107 my $imagedir_filesystem = getitemtypeimagedir();
108 my $imagedir_web = getitemtypeimagesrc();
109 opendir(DIR, $imagedir_filesystem)
110 or die "can't opendir ".$imagedir_filesystem.": ".$!;
112 while (my $line = readdir(DIR)) {
113 if ($line =~ /\.(gif|png)$/i) {
118 KohaImageSrc => $imagedir_web.'/'.$line,
119 checked => $line eq $data->{imageurl} ? 1 : 0,
126 my $remote_image = undef;
127 if (defined $data->{imageurl} and $data->{imageurl} =~ m/^http/) {
128 $remote_image = $data->{imageurl};
132 itemtype => $itemtype,
133 description => $data->{'description'},
134 renewalsallowed => $data->{'renewalsallowed'},
135 rentalcharge => sprintf("%.2f",$data->{'rentalcharge'}),
136 notforloan => $data->{'notforloan'},
137 imageurl => $data->{'imageurl'},
138 template => C4::Context->preference('template'),
139 IMAGESLOOP => \@imagelist,
140 remote_image => $remote_image,
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 my $dbh = C4::Context->dbh;
151 , renewalsallowed = ?
157 my $sth=$dbh->prepare($query);
159 $input->param('description'),
160 $input->param('renewalsallowed'),
161 $input->param('rentalcharge'),
162 $input->param('notforloan') ? 1 : 0,
163 $input->param('image') eq 'removeImage'
165 : $input->param('image') eq 'remoteImage'
166 ? $input->param('remoteImage')
167 : $input->param('image'),
168 $input->param('itemtype'),
172 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
174 # END $OP eq ADD_VALIDATE
175 ################## DELETE_CONFIRM ##################################
176 # called by default form, used to confirm deletion of data in DB
177 } elsif ($op eq 'delete_confirm') {
178 #start the page and read in includes
179 my $dbh = C4::Context->dbh;
181 # Check both categoryitem and biblioitems, see Bug 199
183 for my $table ('biblioitems') {
184 my $sth=$dbh->prepare("select count(*) as total from $table where itemtype=?");
185 $sth->execute($itemtype);
186 $total += $sth->fetchrow_hashref->{total};
190 my $sth=$dbh->prepare("select itemtype,description,renewalsallowed,rentalcharge from itemtypes where itemtype=?");
191 $sth->execute($itemtype);
192 my $data=$sth->fetchrow_hashref;
195 $template->param(itemtype => $itemtype,
196 description => $data->{description},
197 renewalsallowed => $data->{renewalsallowed},
198 rentalcharge => sprintf("%.2f",$data->{rentalcharge}),
199 imageurl => $data->{imageurl},
201 # END $OP eq DELETE_CONFIRM
202 ################## DELETE_CONFIRMED ##################################
203 # called by delete_confirm, used to effectively confirm deletion of data in DB
204 } elsif ($op eq 'delete_confirmed') {
205 #start the page and read in includes
206 my $dbh = C4::Context->dbh;
207 my $itemtype=uc($input->param('itemtype'));
208 my $sth=$dbh->prepare("delete from itemtypes where itemtype=?");
209 $sth->execute($itemtype);
210 $sth = $dbh->prepare("delete from issuingrules where itemtype=?");
211 $sth->execute($itemtype);
213 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
215 # END $OP eq DELETE_CONFIRMED
216 ################## DEFAULT ##################################
219 my ($count,$results)=StringSearch($env,$searchfield,'web');
221 my $page = $input->param('page') || 1;
222 my $first = ($page - 1) * $pagesize;
224 # if we are on the last page, the number of the last word to display
225 # must not exceed the length of the results array
227 $first + $pagesize - 1,
228 scalar @{$results} - 1,
233 foreach my $result (@{$results}[$first .. $last]) {
234 my $itemtype = $result;
235 $itemtype->{toggle} = ($toggle++%2 eq 0 ? 1 : 0);
236 $itemtype->{imageurl} =
237 getitemtypeimagesrcfromurl($itemtype->{imageurl});
238 $itemtype->{rentalcharge} = sprintf('%.2f', $itemtype->{rentalcharge});
240 push(@loop, $itemtype);
245 pagination_bar => pagination_bar(
247 getnbpages(scalar @{$results}, $pagesize),
252 } #---- END $OP eq DEFAULT
253 output_html_with_http_headers $input, $cookie, $template->output;