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 );
61 "Select * from itemtypes where (description like ?) order by itemtype");
62 $sth->execute("$data[0]%");
65 while ( my $data = $sth->fetchrow_hashref ) {
66 push( @results, $data );
71 return ( scalar(@results), \@results );
75 my $searchfield = $input->param('description');
76 my $script_name = "/cgi-bin/koha/admin/itemtypes.pl";
77 my $itemtype = $input->param('itemtype');
79 my $op = $input->param('op');
80 $searchfield =~ s/\,//g;
81 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
83 template_name => "admin/itemtypes.tmpl",
87 flagsrequired => { parameters => 1 },
94 script_name => $script_name,
96 ); # we show only the TMPL_VAR names $op
100 script_name => $script_name,
102 ); # we show only the TMPL_VAR names $op
104 ################## ADD_FORM ##################################
105 # called by default. Used to create form to add or modify a record
106 if ( $op eq 'add_form' ) {
108 #start the page and read in includes
109 #---- if primkey exists, it's a modify action, so read values to modify...
112 my $dbh = C4::Context->dbh;
113 my $sth = $dbh->prepare("select * from itemtypes where itemtype=?");
114 $sth->execute($itemtype);
115 $data = $sth->fetchrow_hashref;
119 # build list of images
120 my $imagedir_filesystem = getitemtypeimagedir();
121 my $imagedir_web = getitemtypeimagesrc();
122 opendir( DIR, $imagedir_filesystem )
123 or warn "can't opendir " . $imagedir_filesystem . ": " . $!;
126 my $image_per_line = 12;
127 while ( my $line = readdir(DIR) ) {
129 if ( $line =~ /\.(gif|png)$/i ) {
130 if ( $i == $image_per_line ) {
132 push @imagelist, { KohaImage => '', KohaImageSrc => '' };
139 KohaImageSrc => $imagedir_web . '/' . $line,
140 checked => $line eq $data->{imageurl} ? 1 : 0,
148 my $remote_image = undef;
149 if ( defined $data->{imageurl} and $data->{imageurl} =~ m/^http/ ) {
150 $remote_image = $data->{imageurl};
154 itemtype => $itemtype,
155 description => $data->{'description'},
156 renewalsallowed => $data->{'renewalsallowed'},
157 rentalcharge => sprintf( "%.2f", $data->{'rentalcharge'} ),
158 notforloan => $data->{'notforloan'},
159 imageurl => $data->{'imageurl'},
160 template => C4::Context->preference('template'),
161 summary => $data->{summary},
162 IMAGESLOOP => \@imagelist,
163 remote_image => $remote_image,
166 # END $OP eq ADD_FORM
167 ################## ADD_VALIDATE ##################################
168 # called by add_form, used to insert/modify data in DB
170 elsif ( $op eq 'add_validate' ) {
171 my $dbh = C4::Context->dbh;
179 my $sth = $dbh->prepare($query);
180 $sth->execute($itemtype);
181 if ( $sth->fetchrow ) {
185 if ($modif) { # it 's a modification
189 , renewalsallowed = ?
196 my $sth = $dbh->prepare($query);
198 $input->param('description'),
199 $input->param('renewalsallowed'),
200 $input->param('rentalcharge'),
201 ( $input->param('notforloan') ? 1 : 0 ),
203 $input->param('image') eq 'removeImage' ? ''
205 $input->param('image') eq 'remoteImage'
206 ? $input->param('remoteImage')
207 : $input->param('image') . ""
210 $input->param('summary'),
211 $input->param('itemtype')
214 else { # add a new itemtype & not modif an old
216 INSERT INTO itemtypes
217 (itemtype,description,renewalsallowed,rentalcharge, notforloan, imageurl,summary)
221 my $sth = $dbh->prepare($query);
223 $input->param('itemtype'),
224 $input->param('description'),
225 $input->param('renewalsallowed'),
226 $input->param('rentalcharge'),
227 $input->param('notforloan') ? 1 : 0,
228 $input->param('image') eq 'removeImage' ? ''
229 : $input->param('image') eq 'remoteImage'
230 ? $input->param('remoteImage')
231 : $input->param('image'),
232 $input->param('summary'),
236 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
239 # END $OP eq ADD_VALIDATE
240 ################## DELETE_CONFIRM ##################################
241 # called by default form, used to confirm deletion of data in DB
243 elsif ( $op eq 'delete_confirm' ) {
245 #start the page and read in includes
246 my $dbh = C4::Context->dbh;
248 # Check both categoryitem and biblioitems, see Bug 199
250 for my $table ('biblioitems') {
253 "select count(*) as total from $table where itemtype=?");
254 $sth->execute($itemtype);
255 $total += $sth->fetchrow_hashref->{total};
261 "select itemtype,description,renewalsallowed,rentalcharge from itemtypes where itemtype=?"
263 $sth->execute($itemtype);
264 my $data = $sth->fetchrow_hashref;
268 itemtype => $itemtype,
269 description => $data->{description},
270 renewalsallowed => $data->{renewalsallowed},
271 rentalcharge => sprintf( "%.2f", $data->{rentalcharge} ),
272 imageurl => $data->{imageurl},
276 # END $OP eq DELETE_CONFIRM
277 ################## DELETE_CONFIRMED ##################################
278 # called by delete_confirm, used to effectively confirm deletion of data in DB
280 elsif ( $op eq 'delete_confirmed' ) {
282 #start the page and read in includes
283 my $dbh = C4::Context->dbh;
284 my $itemtype = uc( $input->param('itemtype') );
285 my $sth = $dbh->prepare("delete from itemtypes where itemtype=?");
286 $sth->execute($itemtype);
287 $sth = $dbh->prepare("delete from issuingrules where itemtype=?");
288 $sth->execute($itemtype);
290 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
293 # END $OP eq DELETE_CONFIRMED
294 ################## DEFAULT ##################################
297 my ( $count, $results ) = StringSearch( $searchfield, 'web' );
299 my $page = $input->param('page') || 1;
300 my $first = ( $page - 1 ) * $pagesize;
302 # if we are on the last page, the number of the last word to display
303 # must not exceed the length of the results array
304 my $last = min( $first + $pagesize - 1, scalar @{$results} - 1, );
308 foreach my $result ( @{$results}[ $first .. $last ] ) {
309 my $itemtype = $result;
310 $itemtype->{toggle} = ( $toggle++ % 2 eq 0 ? 1 : 0 );
311 $itemtype->{imageurl} =
312 getitemtypeimagesrcfromurl( $itemtype->{imageurl} );
313 $itemtype->{rentalcharge} =
314 sprintf( '%.2f', $itemtype->{rentalcharge} );
316 push( @loop, $itemtype );
321 pagination_bar => pagination_bar(
322 $script_name, getnbpages( scalar @{$results}, $pagesize ),
326 } #---- END $OP eq DEFAULT
328 output_html_with_http_headers $input, $cookie, $template->output;