MT 2206 addorder crashing
[koha.git] / admin / itemtypes.pl
blob9f0395cdbc0f32fdae9e1af4bb0c0876d7b8d61a
1 #!/usr/bin/perl
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
10 # version.
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)
26 ALGO :
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.
31 if $op=add_form
32 - if primkey exists, this is a modification,so we read the $primkey record
33 - builds the add/modify form
34 if $op=add_validate
35 - the user has just send datas, so we create/modify the record
36 if $op=delete_form
37 - we show the record having primkey=$primkey and ask for deletion validation form
38 if $op=delete_confirm
39 - we delete the record having primkey=$primkey
41 =cut
43 use strict;
44 use CGI;
46 use List::Util qw/min/;
47 use File::Spec;
49 use C4::Koha;
50 use C4::Context;
51 use C4::Auth;
52 use C4::Output;
54 sub StringSearch {
55 my ( $searchstring, $type ) = @_;
56 my $dbh = C4::Context->dbh;
57 $searchstring =~ s/\'/\\\'/g;
58 my @data = split( ' ', $searchstring );
59 my $sth = $dbh->prepare(
60 "SELECT * FROM itemtypes WHERE (description LIKE ?) ORDER BY itemtype"
62 $sth->execute("$data[0]%");
63 return $sth->fetchall_arrayref({}); # return ref-to-array of ref-to-hashes
64 # like [ fetchrow_hashref(), fetchrow_hashref() ... ]
67 my $input = new CGI;
68 my $searchfield = $input->param('description');
69 my $script_name = "/cgi-bin/koha/admin/itemtypes.pl";
70 my $itemtype = $input->param('itemtype');
71 my $pagesize = 10;
72 my $op = $input->param('op');
73 $searchfield =~ s/\,//g;
74 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
76 template_name => "admin/itemtypes.tmpl",
77 query => $input,
78 type => "intranet",
79 authnotrequired => 0,
80 flagsrequired => { parameters => 1 },
81 debug => 1,
85 $template->param(script_name => $script_name);
86 if ($op) {
87 $template->param($op => 1); # we show only the TMPL_VAR names $op
88 } else {
89 $template->param(else => 1);
92 my $dbh = C4::Context->dbh;
94 ################## ADD_FORM ##################################
95 # called by default. Used to create form to add or modify a record
96 if ( $op eq 'add_form' ) {
97 #---- if primkey exists, it's a modify action, so read values to modify...
98 my $data;
99 if ($itemtype) {
100 my $sth = $dbh->prepare("select * from itemtypes where itemtype=?");
101 $sth->execute($itemtype);
102 $data = $sth->fetchrow_hashref;
105 my $imagesets = C4::Koha::getImageSets( checked => $data->{'imageurl'} );
107 my $remote_image = undef;
108 if ( defined $data->{imageurl} and $data->{imageurl} =~ /^http/i ) {
109 $remote_image = $data->{imageurl};
112 $template->param(
113 itemtype => $itemtype,
114 description => $data->{'description'},
115 renewalsallowed => $data->{'renewalsallowed'},
116 rentalcharge => sprintf( "%.2f", $data->{'rentalcharge'} ),
117 notforloan => $data->{'notforloan'},
118 imageurl => $data->{'imageurl'},
119 template => C4::Context->preference('template'),
120 summary => $data->{summary},
121 imagesets => $imagesets,
122 remote_image => $remote_image,
125 # END $OP eq ADD_FORM
126 ################## ADD_VALIDATE ##################################
127 # called by add_form, used to insert/modify data in DB
129 elsif ( $op eq 'add_validate' ) {
130 my $query = "
131 SELECT itemtype
132 FROM itemtypes
133 WHERE itemtype = ?
135 my $sth = $dbh->prepare($query);
136 $sth->execute($itemtype);
137 if ( $sth->fetchrow ) { # it's a modification
138 my $query2 = '
139 UPDATE itemtypes
140 SET description = ?
141 , renewalsallowed = ?
142 , rentalcharge = ?
143 , notforloan = ?
144 , imageurl = ?
145 , summary = ?
146 WHERE itemtype = ?
148 $sth = $dbh->prepare($query2);
149 $sth->execute(
150 $input->param('description'),
151 $input->param('renewalsallowed'),
152 $input->param('rentalcharge'),
153 ( $input->param('notforloan') ? 1 : 0 ),
155 $input->param('image') eq 'removeImage' ? '' : (
156 $input->param('image') eq 'remoteImage'
157 ? $input->param('remoteImage')
158 : $input->param('image') . ""
161 $input->param('summary'),
162 $input->param('itemtype')
165 else { # add a new itemtype & not modif an old
166 my $query = "
167 INSERT INTO itemtypes
168 (itemtype,description,renewalsallowed,rentalcharge, notforloan, imageurl,summary)
169 VALUES
170 (?,?,?,?,?,?,?);
172 my $sth = $dbh->prepare($query);
173 my $image = $input->param('image');
174 $sth->execute(
175 $input->param('itemtype'),
176 $input->param('description'),
177 $input->param('renewalsallowed'),
178 $input->param('rentalcharge'),
179 $input->param('notforloan') ? 1 : 0,
180 $image eq 'removeImage' ? '' :
181 $image eq 'remoteImage' ? $input->param('remoteImage') :
182 $image,
183 $input->param('summary'),
187 print $input->redirect('itemtypes.pl');
188 exit;
190 # END $OP eq ADD_VALIDATE
191 ################## DELETE_CONFIRM ##################################
192 # called by default form, used to confirm deletion of data in DB
194 elsif ( $op eq 'delete_confirm' ) {
195 # Check both categoryitem and biblioitems, see Bug 199
196 my $total = 0;
197 for my $table ('biblioitems') {
198 my $sth =
199 $dbh->prepare(
200 "select count(*) as total from $table where itemtype=?");
201 $sth->execute($itemtype);
202 $total += $sth->fetchrow_hashref->{total};
205 my $sth =
206 $dbh->prepare(
207 "select itemtype,description,renewalsallowed,rentalcharge from itemtypes where itemtype=?"
209 $sth->execute($itemtype);
210 my $data = $sth->fetchrow_hashref;
211 $template->param(
212 itemtype => $itemtype,
213 description => $data->{description},
214 renewalsallowed => $data->{renewalsallowed},
215 rentalcharge => sprintf( "%.2f", $data->{rentalcharge} ),
216 imageurl => $data->{imageurl},
217 total => $total
220 # END $OP eq DELETE_CONFIRM
221 ################## DELETE_CONFIRMED ##################################
222 # called by delete_confirm, used to effectively confirm deletion of data in DB
224 elsif ( $op eq 'delete_confirmed' ) {
225 my $itemtype = uc( $input->param('itemtype') );
226 my $sth = $dbh->prepare("delete from itemtypes where itemtype=?");
227 $sth->execute($itemtype);
228 $sth = $dbh->prepare("delete from issuingrules where itemtype=?");
229 $sth->execute($itemtype);
230 print $input->redirect('itemtypes.pl');
231 exit;
232 # END $OP eq DELETE_CONFIRMED
233 ################## DEFAULT ##################################
235 else { # DEFAULT
236 my ($results) = StringSearch( $searchfield, 'web' );
237 my $page = $input->param('page') || 1;
238 my $first = ( $page - 1 ) * $pagesize;
240 # if we are on the last page, the number of the last word to display
241 # must not exceed the length of the results array
242 my $last = min( $first + $pagesize - 1, scalar @{$results} - 1, );
243 my @loop;
244 foreach my $itemtype ( @{$results}[ $first .. $last ] ) {
245 $itemtype->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtype->{imageurl} );
246 $itemtype->{rentalcharge} = sprintf( '%.2f', $itemtype->{rentalcharge} );
247 push( @loop, $itemtype );
250 $template->param(
251 loop => \@loop,
252 pagination_bar => pagination_bar(
253 $script_name, getnbpages( scalar @{$results}, $pagesize ),
254 $page, 'page'
257 } #---- END $OP eq DEFAULT
259 output_html_with_http_headers $input, $cookie, $template->output;