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/;
50 use C4::Interface::CGI::Output;
53 my ($env,$searchstring,$type)=@_;
54 my $dbh = C4::Context->dbh;
55 $searchstring=~ s/\'/\\\'/g;
56 my @data=split(' ',$searchstring);
58 my $sth=$dbh->prepare("Select * from itemtypes where (description like ?) order by itemtype");
59 $sth->execute("$data[0]%");
61 while (my $data=$sth->fetchrow_hashref){
66 return (scalar(@results),\@results);
70 my $searchfield=$input->param('description');
71 my $script_name="/cgi-bin/koha/admin/itemtypes.pl";
72 my $itemtype=$input->param('itemtype');
74 my $op = $input->param('op');
75 $searchfield=~ s/\,//g;
76 my ($template, $borrowernumber, $cookie)
77 = get_template_and_user({template_name => "admin/itemtypes.tmpl",
81 flagsrequired => {parameters => 1},
86 $template->param(script_name => $script_name,
87 $op => 1); # we show only the TMPL_VAR names $op
89 $template->param(script_name => $script_name,
90 else => 1); # we show only the TMPL_VAR names $op
92 ################## ADD_FORM ##################################
93 # called by default. Used to create form to add or modify a record
94 if ($op eq 'add_form') {
95 #start the page and read in includes
96 #---- if primkey exists, it's a modify action, so read values to modify...
99 my $dbh = C4::Context->dbh;
100 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
101 $sth->execute($itemtype);
102 $data=$sth->fetchrow_hashref;
105 # build list of images
106 my $imagedir_filesystem = getitemtypeimagedir();
107 my $imagedir_web = getitemtypeimagesrc();
108 opendir(DIR, $imagedir_filesystem)
109 or warn "can't opendir ".$imagedir_filesystem.": ".$!;
112 my $image_per_line=12;
113 while (my $line = readdir(DIR)) {
115 if ($line =~ /\.(gif|png)$/i) {
116 if($i==$image_per_line){
118 push @imagelist,{KohaImage => '',KohaImageSrc => ''};
124 KohaImageSrc => $imagedir_web.'/'.$line,
125 checked => $line eq $data->{imageurl} ? 1 : 0,
133 my $remote_image = undef;
134 if (defined $data->{imageurl} and $data->{imageurl} =~ m/^http/) {
135 $remote_image = $data->{imageurl};
139 itemtype => $itemtype,
140 description => $data->{'description'},
141 renewalsallowed => $data->{'renewalsallowed'},
142 rentalcharge => sprintf("%.2f",$data->{'rentalcharge'}),
143 notforloan => $data->{'notforloan'},
144 imageurl => $data->{'imageurl'},
145 template => C4::Context->preference('template'),
146 summary => $data->{summary},
147 IMAGESLOOP => \@imagelist,
148 remote_image => $remote_image,
150 # END $OP eq ADD_FORM
151 ################## ADD_VALIDATE ##################################
152 # called by add_form, used to insert/modify data in DB
153 } elsif ($op eq 'add_validate') {
154 my $dbh = C4::Context->dbh;
162 my $sth = $dbh->prepare($query);
163 $sth->execute($itemtype);
168 if($modif){ # it 's a modification
172 , renewalsallowed = ?
179 my $sth=$dbh->prepare($query);
181 $input->param('description'),
182 $input->param('renewalsallowed'),
183 $input->param('rentalcharge'),
184 ($input->param('notforloan') ? 1 : 0),
185 ($input->param('image') eq 'removeImage'
187 :($input->param('image') eq 'remoteImage'
188 ? $input->param('remoteImage')
189 :$input->param('image')."")
191 $input->param('summary'),
192 $input->param('itemtype')
195 else { # add a new itemtype & not modif an old
197 INSERT INTO itemtypes
198 (itemtype,description,renewalsallowed,rentalcharge, notforloan, imageurl,summary)
202 my $sth=$dbh->prepare($query);
204 $input->param('itemtype'),
205 $input->param('description'),
206 $input->param('renewalsallowed'),
207 $input->param('rentalcharge'),
208 $input->param('notforloan') ? 1 : 0,
209 $input->param('image') eq 'removeImage'
211 : $input->param('image') eq 'remoteImage'
212 ? $input->param('remoteImage')
213 : $input->param('image'),
214 $input->param('summary'),
218 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
220 # END $OP eq ADD_VALIDATE
221 ################## DELETE_CONFIRM ##################################
222 # called by default form, used to confirm deletion of data in DB
223 } elsif ($op eq 'delete_confirm') {
224 #start the page and read in includes
225 my $dbh = C4::Context->dbh;
227 # Check both categoryitem and biblioitems, see Bug 199
229 for my $table ('biblioitems') {
230 my $sth=$dbh->prepare("select count(*) as total from $table where itemtype=?");
231 $sth->execute($itemtype);
232 $total += $sth->fetchrow_hashref->{total};
236 my $sth=$dbh->prepare("select itemtype,description,renewalsallowed,rentalcharge from itemtypes where itemtype=?");
237 $sth->execute($itemtype);
238 my $data=$sth->fetchrow_hashref;
241 $template->param(itemtype => $itemtype,
242 description => $data->{description},
243 renewalsallowed => $data->{renewalsallowed},
244 rentalcharge => sprintf("%.2f",$data->{rentalcharge}),
245 imageurl => $data->{imageurl},
247 # END $OP eq DELETE_CONFIRM
248 ################## DELETE_CONFIRMED ##################################
249 # called by delete_confirm, used to effectively confirm deletion of data in DB
250 } elsif ($op eq 'delete_confirmed') {
251 #start the page and read in includes
252 my $dbh = C4::Context->dbh;
253 my $itemtype=uc($input->param('itemtype'));
254 my $sth=$dbh->prepare("delete from itemtypes where itemtype=?");
255 $sth->execute($itemtype);
256 $sth = $dbh->prepare("delete from issuingrules where itemtype=?");
257 $sth->execute($itemtype);
259 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
261 # END $OP eq DELETE_CONFIRMED
262 ################## DEFAULT ##################################
265 my ($count,$results)=StringSearch($env,$searchfield,'web');
267 my $page = $input->param('page') || 1;
268 my $first = ($page - 1) * $pagesize;
270 # if we are on the last page, the number of the last word to display
271 # must not exceed the length of the results array
273 $first + $pagesize - 1,
274 scalar @{$results} - 1,
279 foreach my $result (@{$results}[$first .. $last]) {
280 my $itemtype = $result;
281 $itemtype->{toggle} = ($toggle++%2 eq 0 ? 1 : 0);
282 $itemtype->{imageurl} =
283 getitemtypeimagesrcfromurl($itemtype->{imageurl});
284 $itemtype->{rentalcharge} = sprintf('%.2f', $itemtype->{rentalcharge});
286 push(@loop, $itemtype);
291 pagination_bar => pagination_bar(
293 getnbpages(scalar @{$results}, $pagesize),
298 } #---- END $OP eq DEFAULT
299 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
300 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
301 IntranetNav => C4::Context->preference("IntranetNav"),
303 output_html_with_http_headers $input, $cookie, $template->output;