3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
20 =head1 admin/itemtypes.pl
22 script to administer the categories table
23 written 20/02/2002 by paul.poulain@free.fr
24 This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
27 this script use an $op to know what to do.
28 if $op is empty or none of the above values,
29 - the default screen is build (with all records, or filtered datas).
30 - the user can clic on add, modify or delete record.
32 - if primkey exists, this is a modification,so we read the $primkey record
33 - builds the add/modify form
35 - the user has just send datas, so we create/modify the record
37 - we show the record having primkey=$primkey and ask for deletion validation form
39 - we delete the record having primkey=$primkey
46 use List::Util qw/min/;
54 my ( $searchstring, $type ) = @_;
55 my $dbh = C4::Context->dbh;
56 $searchstring =~ s/\'/\\\'/g;
57 my @data = split( ' ', $searchstring );
58 my $sth = $dbh->prepare(
59 "SELECT * FROM itemtypes WHERE (description LIKE ?) ORDER BY itemtype"
61 $sth->execute("$data[0]%");
62 return $sth->fetchall_arrayref({}); # return ref-to-array of ref-to-hashes
63 # like [ fetchrow_hashref(), fetchrow_hashref() ... ]
67 my $searchfield = $input->param('description');
68 my $script_name = "/cgi-bin/koha/admin/itemtypes.pl";
69 my $itemtype = $input->param('itemtype');
71 my $op = $input->param('op');
72 $searchfield =~ s/\,//g;
73 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
75 template_name => "admin/itemtypes.tmpl",
79 flagsrequired => { parameters => 1 },
84 $template->param(script_name => $script_name);
86 $template->param($op => 1); # we show only the TMPL_VAR names $op
88 $template->param(else => 1);
91 my $dbh = C4::Context->dbh;
93 ################## ADD_FORM ##################################
94 # called by default. Used to create form to add or modify a record
95 if ( $op eq 'add_form' ) {
96 #---- if primkey exists, it's a modify action, so read values to modify...
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 $src = "intranet"; # so that the getitemtypeimage functions know where they were called from -fbcit
106 my $imagedir_filesystem = getitemtypeimagedir($src);
107 my $imagedir_web = getitemtypeimagesrc($src);
108 opendir( DIR, $imagedir_filesystem )
109 or warn "cannot 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 => '' };
125 KohaImageSrc => $imagedir_web . '/' . $line,
126 checked => $line eq $data->{imageurl} ? 1 : 0,
134 my $remote_image = undef;
135 if ( defined $data->{imageurl} and $data->{imageurl} =~ /^http/i ) {
136 $remote_image = $data->{imageurl};
140 itemtype => $itemtype,
141 description => $data->{'description'},
142 renewalsallowed => $data->{'renewalsallowed'},
143 rentalcharge => sprintf( "%.2f", $data->{'rentalcharge'} ),
144 notforloan => $data->{'notforloan'},
145 imageurl => $data->{'imageurl'},
146 template => C4::Context->preference('template'),
147 summary => $data->{summary},
148 IMAGESLOOP => \@imagelist,
149 remote_image => $remote_image,
152 # END $OP eq ADD_FORM
153 ################## ADD_VALIDATE ##################################
154 # called by add_form, used to insert/modify data in DB
156 elsif ( $op eq 'add_validate' ) {
162 my $sth = $dbh->prepare($query);
163 $sth->execute($itemtype);
164 if ( $sth->fetchrow ) { # it's a modification
168 , renewalsallowed = ?
175 $sth = $dbh->prepare($query2);
177 $input->param('description'),
178 $input->param('renewalsallowed'),
179 $input->param('rentalcharge'),
180 ( $input->param('notforloan') ? 1 : 0 ),
182 $input->param('image') eq 'removeImage' ? '' : (
183 $input->param('image') eq 'remoteImage'
184 ? $input->param('remoteImage')
185 : $input->param('image') . ""
188 $input->param('summary'),
189 $input->param('itemtype')
192 else { # add a new itemtype & not modif an old
194 INSERT INTO itemtypes
195 (itemtype,description,renewalsallowed,rentalcharge, notforloan, imageurl,summary)
199 my $sth = $dbh->prepare($query);
200 my $image = $input->param('image');
202 $input->param('itemtype'),
203 $input->param('description'),
204 $input->param('renewalsallowed'),
205 $input->param('rentalcharge'),
206 $input->param('notforloan') ? 1 : 0,
207 $image eq 'removeImage' ? '' :
208 $image eq 'remoteImage' ? $input->param('remoteImage') :
210 $input->param('summary'),
214 print $input->redirect('itemtypes.pl');
217 # END $OP eq ADD_VALIDATE
218 ################## DELETE_CONFIRM ##################################
219 # called by default form, used to confirm deletion of data in DB
221 elsif ( $op eq 'delete_confirm' ) {
222 # Check both categoryitem and biblioitems, see Bug 199
224 for my $table ('biblioitems') {
227 "select count(*) as total from $table where itemtype=?");
228 $sth->execute($itemtype);
229 $total += $sth->fetchrow_hashref->{total};
234 "select itemtype,description,renewalsallowed,rentalcharge from itemtypes where itemtype=?"
236 $sth->execute($itemtype);
237 my $data = $sth->fetchrow_hashref;
239 itemtype => $itemtype,
240 description => $data->{description},
241 renewalsallowed => $data->{renewalsallowed},
242 rentalcharge => sprintf( "%.2f", $data->{rentalcharge} ),
243 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
251 elsif ( $op eq 'delete_confirmed' ) {
252 my $itemtype = uc( $input->param('itemtype') );
253 my $sth = $dbh->prepare("delete from itemtypes where itemtype=?");
254 $sth->execute($itemtype);
255 $sth = $dbh->prepare("delete from issuingrules where itemtype=?");
256 $sth->execute($itemtype);
257 print $input->redirect('itemtypes.pl');
259 # END $OP eq DELETE_CONFIRMED
260 ################## DEFAULT ##################################
263 my ($results) = StringSearch( $searchfield, 'web' );
264 my $page = $input->param('page') || 1;
265 my $first = ( $page - 1 ) * $pagesize;
267 # if we are on the last page, the number of the last word to display
268 # must not exceed the length of the results array
269 my $last = min( $first + $pagesize - 1, scalar @{$results} - 1, );
272 foreach my $itemtype ( @{$results}[ $first .. $last ] ) {
273 $itemtype->{toggle} = ($toggle++ % 2) ? 0 : 1 ;
274 $itemtype->{imageurl} = getitemtypeimagesrc('intranet') . "/$itemtype->{imageurl}";
275 $itemtype->{rentalcharge} = sprintf( '%.2f', $itemtype->{rentalcharge} );
276 push( @loop, $itemtype );
281 pagination_bar => pagination_bar(
282 $script_name, getnbpages( scalar @{$results}, $pagesize ),
286 } #---- END $OP eq DEFAULT
288 output_html_with_http_headers $input, $cookie, $template->output;