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/;
52 my ($searchstring,$type)=@_;
53 my $dbh = C4::Context->dbh;
54 $searchstring=~ s/\'/\\\'/g;
55 my @data=split(' ',$searchstring);
57 my $sth=$dbh->prepare("Select * from itemtypes where (description like ?) order by itemtype");
58 $sth->execute("$data[0]%");
60 while (my $data=$sth->fetchrow_hashref){
65 return (scalar(@results),\@results);
69 my $searchfield=$input->param('description');
70 my $script_name="/cgi-bin/koha/admin/itemtypes.pl";
71 my $itemtype=$input->param('itemtype');
73 my $op = $input->param('op');
74 $searchfield=~ s/\,//g;
75 my ($template, $borrowernumber, $cookie)
76 = get_template_and_user({template_name => "admin/itemtypes.tmpl",
80 flagsrequired => {parameters => 1},
85 $template->param(script_name => $script_name,
86 $op => 1); # we show only the TMPL_VAR names $op
88 $template->param(script_name => $script_name,
89 else => 1); # we show only the TMPL_VAR names $op
91 ################## ADD_FORM ##################################
92 # called by default. Used to create form to add or modify a record
93 if ($op eq 'add_form') {
94 #start the page and read in includes
95 #---- if primkey exists, it's a modify action, so read values to modify...
98 my $dbh = C4::Context->dbh;
99 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
100 $sth->execute($itemtype);
101 $data=$sth->fetchrow_hashref;
104 # build list of images
105 my $imagedir_filesystem = getitemtypeimagedir();
106 my $imagedir_web = getitemtypeimagesrc();
107 opendir(DIR, $imagedir_filesystem)
108 or warn "can't opendir ".$imagedir_filesystem.": ".$!;
111 my $image_per_line=12;
112 while (my $line = readdir(DIR)) {
114 if ($line =~ /\.(gif|png)$/i) {
115 if($i==$image_per_line){
117 push @imagelist,{KohaImage => '',KohaImageSrc => ''};
123 KohaImageSrc => $imagedir_web.'/'.$line,
124 checked => $line eq $data->{imageurl} ? 1 : 0,
132 my $remote_image = undef;
133 if (defined $data->{imageurl} and $data->{imageurl} =~ m/^http/) {
134 $remote_image = $data->{imageurl};
138 itemtype => $itemtype,
139 description => $data->{'description'},
140 renewalsallowed => $data->{'renewalsallowed'},
141 rentalcharge => sprintf("%.2f",$data->{'rentalcharge'}),
142 notforloan => $data->{'notforloan'},
143 imageurl => $data->{'imageurl'},
144 template => C4::Context->preference('template'),
145 summary => $data->{summary},
146 IMAGESLOOP => \@imagelist,
147 remote_image => $remote_image,
149 # END $OP eq ADD_FORM
150 ################## ADD_VALIDATE ##################################
151 # called by add_form, used to insert/modify data in DB
152 } elsif ($op eq 'add_validate') {
153 my $dbh = C4::Context->dbh;
161 my $sth = $dbh->prepare($query);
162 $sth->execute($itemtype);
167 if($modif){ # it 's a modification
171 , renewalsallowed = ?
178 my $sth=$dbh->prepare($query);
180 $input->param('description'),
181 $input->param('renewalsallowed'),
182 $input->param('rentalcharge'),
183 ($input->param('notforloan') ? 1 : 0),
184 ($input->param('image') eq 'removeImage'
186 :($input->param('image') eq 'remoteImage'
187 ? $input->param('remoteImage')
188 :$input->param('image')."")
190 $input->param('summary'),
191 $input->param('itemtype')
194 else { # add a new itemtype & not modif an old
196 INSERT INTO itemtypes
197 (itemtype,description,renewalsallowed,rentalcharge, notforloan, imageurl,summary)
201 my $sth=$dbh->prepare($query);
203 $input->param('itemtype'),
204 $input->param('description'),
205 $input->param('renewalsallowed'),
206 $input->param('rentalcharge'),
207 $input->param('notforloan') ? 1 : 0,
208 $input->param('image') eq 'removeImage' ? '' : $input->param('image') eq 'remoteImage' ? $input->param('remoteImage') : $input->param('image'),
209 $input->param('summary'),
213 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
215 # END $OP eq ADD_VALIDATE
216 ################## DELETE_CONFIRM ##################################
217 # called by default form, used to confirm deletion of data in DB
218 } elsif ($op eq 'delete_confirm') {
219 #start the page and read in includes
220 my $dbh = C4::Context->dbh;
222 # Check both categoryitem and biblioitems, see Bug 199
224 for my $table ('biblioitems') {
225 my $sth=$dbh->prepare("select count(*) as total from $table where itemtype=?");
226 $sth->execute($itemtype);
227 $total += $sth->fetchrow_hashref->{total};
231 my $sth=$dbh->prepare("select itemtype,description,renewalsallowed,rentalcharge from itemtypes where itemtype=?");
232 $sth->execute($itemtype);
233 my $data=$sth->fetchrow_hashref;
236 $template->param(itemtype => $itemtype,
237 description => $data->{description},
238 renewalsallowed => $data->{renewalsallowed},
239 rentalcharge => sprintf("%.2f",$data->{rentalcharge}),
240 imageurl => $data->{imageurl},
242 # END $OP eq DELETE_CONFIRM
243 ################## DELETE_CONFIRMED ##################################
244 # called by delete_confirm, used to effectively confirm deletion of data in DB
245 } elsif ($op eq 'delete_confirmed') {
246 #start the page and read in includes
247 my $dbh = C4::Context->dbh;
248 my $itemtype=uc($input->param('itemtype'));
249 my $sth=$dbh->prepare("delete from itemtypes where itemtype=?");
250 $sth->execute($itemtype);
251 $sth = $dbh->prepare("delete from issuingrules where itemtype=?");
252 $sth->execute($itemtype);
254 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
256 # END $OP eq DELETE_CONFIRMED
257 ################## DEFAULT ##################################
259 my ($count,$results)=StringSearch($searchfield,'web');
261 my $page = $input->param('page') || 1;
262 my $first = ($page - 1) * $pagesize;
264 # if we are on the last page, the number of the last word to display
265 # must not exceed the length of the results array
267 $first + $pagesize - 1,
268 scalar @{$results} - 1,
273 foreach my $result (@{$results}[$first .. $last]) {
274 my $itemtype = $result;
275 $itemtype->{toggle} = ($toggle++%2 eq 0 ? 1 : 0);
276 $itemtype->{imageurl} =
277 getitemtypeimagesrcfromurl($itemtype->{imageurl});
278 $itemtype->{rentalcharge} = sprintf('%.2f', $itemtype->{rentalcharge});
280 push(@loop, $itemtype);
285 pagination_bar => pagination_bar(
287 getnbpages(scalar @{$results}, $pagesize),
292 } #---- END $OP eq DEFAULT
293 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
294 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
295 IntranetNav => C4::Context->preference("IntranetNav"),
297 output_html_with_http_headers $input, $cookie, $template->output;