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 $imagedir_filesystem = getitemtypeimagedir();
106 my $imagedir_web = getitemtypeimagesrc();
107 opendir( DIR, $imagedir_filesystem )
108 or warn "cannot 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 => '' };
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} =~ /^http/i ) {
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,
151 # END $OP eq ADD_FORM
152 ################## ADD_VALIDATE ##################################
153 # called by add_form, used to insert/modify data in DB
155 elsif ( $op eq 'add_validate' ) {
161 my $sth = $dbh->prepare($query);
162 $sth->execute($itemtype);
163 if ( $sth->fetchrow ) { # it's a modification
167 , renewalsallowed = ?
174 $sth = $dbh->prepare($query2);
176 $input->param('description'),
177 $input->param('renewalsallowed'),
178 $input->param('rentalcharge'),
179 ( $input->param('notforloan') ? 1 : 0 ),
181 $input->param('image') eq 'removeImage' ? '' : (
182 $input->param('image') eq 'remoteImage'
183 ? $input->param('remoteImage')
184 : $input->param('image') . ""
187 $input->param('summary'),
188 $input->param('itemtype')
191 else { # add a new itemtype & not modif an old
193 INSERT INTO itemtypes
194 (itemtype,description,renewalsallowed,rentalcharge, notforloan, imageurl,summary)
198 my $sth = $dbh->prepare($query);
199 my $image = $input->param('image');
201 $input->param('itemtype'),
202 $input->param('description'),
203 $input->param('renewalsallowed'),
204 $input->param('rentalcharge'),
205 $input->param('notforloan') ? 1 : 0,
206 $image eq 'removeImage' ? '' :
207 $image eq 'remoteImage' ? $input->param('remoteImage') :
209 $input->param('summary'),
213 print $input->redirect('itemtypes.pl');
216 # END $OP eq ADD_VALIDATE
217 ################## DELETE_CONFIRM ##################################
218 # called by default form, used to confirm deletion of data in DB
220 elsif ( $op eq 'delete_confirm' ) {
221 # Check both categoryitem and biblioitems, see Bug 199
223 for my $table ('biblioitems') {
226 "select count(*) as total from $table where itemtype=?");
227 $sth->execute($itemtype);
228 $total += $sth->fetchrow_hashref->{total};
233 "select itemtype,description,renewalsallowed,rentalcharge from itemtypes where itemtype=?"
235 $sth->execute($itemtype);
236 my $data = $sth->fetchrow_hashref;
238 itemtype => $itemtype,
239 description => $data->{description},
240 renewalsallowed => $data->{renewalsallowed},
241 rentalcharge => sprintf( "%.2f", $data->{rentalcharge} ),
242 imageurl => $data->{imageurl},
246 # END $OP eq DELETE_CONFIRM
247 ################## DELETE_CONFIRMED ##################################
248 # called by delete_confirm, used to effectively confirm deletion of data in DB
250 elsif ( $op eq 'delete_confirmed' ) {
251 my $itemtype = uc( $input->param('itemtype') );
252 my $sth = $dbh->prepare("delete from itemtypes where itemtype=?");
253 $sth->execute($itemtype);
254 $sth = $dbh->prepare("delete from issuingrules where itemtype=?");
255 $sth->execute($itemtype);
256 print $input->redirect('itemtypes.pl');
258 # END $OP eq DELETE_CONFIRMED
259 ################## DEFAULT ##################################
262 my ($results) = StringSearch( $searchfield, 'web' );
263 my $page = $input->param('page') || 1;
264 my $first = ( $page - 1 ) * $pagesize;
266 # if we are on the last page, the number of the last word to display
267 # must not exceed the length of the results array
268 my $last = min( $first + $pagesize - 1, scalar @{$results} - 1, );
271 foreach my $itemtype ( @{$results}[ $first .. $last ] ) {
272 $itemtype->{toggle} = ($toggle++ % 2) ? 0 : 1 ;
273 $itemtype->{imageurl} = getitemtypeimagesrcfromurl( $itemtype->{imageurl} );
274 $itemtype->{rentalcharge} = sprintf( '%.2f', $itemtype->{rentalcharge} );
275 push( @loop, $itemtype );
280 pagination_bar => pagination_bar(
281 $script_name, getnbpages( scalar @{$results}, $pagesize ),
285 } #---- END $OP eq DEFAULT
287 output_html_with_http_headers $input, $cookie, $template->output;