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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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
44 #use warnings; FIXME - Bug 2505
47 use List::Util qw/min/;
56 my ( $searchstring, $type ) = @_;
57 my $dbh = C4::Context->dbh;
58 $searchstring =~ s/\'/\\\'/g;
59 my @data = split( ' ', $searchstring );
60 my $sth = $dbh->prepare(
61 "SELECT * FROM itemtypes WHERE (description LIKE ?) ORDER BY itemtype"
63 $sth->execute("$data[0]%");
64 return $sth->fetchall_arrayref({}); # return ref-to-array of ref-to-hashes
65 # like [ fetchrow_hashref(), fetchrow_hashref() ... ]
69 my $searchfield = $input->param('description');
70 my $script_name = "/cgi-bin/koha/admin/itemtypes.pl";
71 my $itemtype = $input->param('itemtype');
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",
80 flagsrequired => { parameters => 'parameters_remaining_permissions' },
85 $template->param(script_name => $script_name);
87 $template->param($op => 1); # we show only the TMPL_VAR names $op
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...
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};
113 itemtype => $itemtype,
114 description => $data->{'description'},
115 rentalcharge => sprintf( "%.2f", $data->{'rentalcharge'} ),
116 notforloan => $data->{'notforloan'},
117 imageurl => $data->{'imageurl'},
118 template => C4::Context->preference('template'),
119 summary => $data->{summary},
120 checkinmsg => $data->{'checkinmsg'},
121 checkinmsgtype => $data->{'checkinmsgtype'},
122 imagesets => $imagesets,
123 remote_image => $remote_image,
124 sip_media_type => $data->{sip_media_type},
127 # END $OP eq ADD_FORM
128 ################## ADD_VALIDATE ##################################
129 # called by add_form, used to insert/modify data in DB
131 elsif ( $op eq 'add_validate' ) {
137 my $sth = $dbh->prepare($query);
138 $sth->execute($itemtype);
139 if ( $sth->fetchrow ) { # it's a modification
152 $sth = $dbh->prepare($query2);
154 $input->param('description'),
155 $input->param('rentalcharge'),
156 ( $input->param('notforloan') ? 1 : 0 ),
158 $input->param('image') eq 'removeImage' ? '' : (
159 $input->param('image') eq 'remoteImage'
160 ? $input->param('remoteImage')
161 : $input->param('image') . ""
164 $input->param('summary'),
165 $input->param('checkinmsg'),
166 $input->param('checkinmsgtype'),
167 $input->param('sip_media_type'),
168 $input->param('itemtype')
171 else { # add a new itemtype & not modif an old
173 INSERT INTO itemtypes
174 (itemtype,description,rentalcharge, notforloan, imageurl, summary, checkinmsg, checkinmsgtype, sip_media_type)
178 my $sth = $dbh->prepare($query);
179 my $image = $input->param('image');
181 $input->param('itemtype'),
182 $input->param('description'),
183 $input->param('rentalcharge'),
184 $input->param('notforloan') ? 1 : 0,
185 $image eq 'removeImage' ? '' :
186 $image eq 'remoteImage' ? $input->param('remoteImage') :
188 $input->param('summary'),
189 $input->param('checkinmsg'),
190 $input->param('checkinmsgtype'),
191 $input->param('sip_media_type'),
195 print $input->redirect('itemtypes.pl');
198 # END $OP eq ADD_VALIDATE
199 ################## DELETE_CONFIRM ##################################
200 # called by default form, used to confirm deletion of data in DB
202 elsif ( $op eq 'delete_confirm' ) {
203 # Check both items and biblioitems
204 my $sth = $dbh->prepare('
205 SELECT COUNT(*) AS total FROM (
206 SELECT itemtype AS t FROM biblioitems
208 SELECT itype AS t FROM items
212 $sth->execute($itemtype);
213 my $total = $sth->fetchrow_hashref->{'total'};
217 "select itemtype,description,rentalcharge from itemtypes where itemtype=?"
219 $sth->execute($itemtype);
220 my $data = $sth->fetchrow_hashref;
222 itemtype => $itemtype,
223 description => $data->{description},
224 rentalcharge => sprintf( "%.2f", $data->{rentalcharge} ),
225 imageurl => $data->{imageurl},
229 # END $OP eq DELETE_CONFIRM
230 ################## DELETE_CONFIRMED ##################################
231 # called by delete_confirm, used to effectively confirm deletion of data in DB
233 elsif ( $op eq 'delete_confirmed' ) {
234 my $itemtype = uc( $input->param('itemtype') );
235 my $sth = $dbh->prepare("delete from itemtypes where itemtype=?");
236 $sth->execute($itemtype);
237 $sth = $dbh->prepare("delete from issuingrules where itemtype=?");
238 $sth->execute($itemtype);
239 print $input->redirect('itemtypes.pl');
241 # END $OP eq DELETE_CONFIRMED
242 ################## DEFAULT ##################################
245 my ($results) = StringSearch( $searchfield, 'web' );
247 foreach my $itemtype ( @{$results} ) {
248 $itemtype->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtype->{imageurl} );
249 $itemtype->{rentalcharge} = sprintf( '%.2f', $itemtype->{rentalcharge} );
250 push( @loop, $itemtype );
256 } #---- END $OP eq DEFAULT
258 output_html_with_http_headers $input, $cookie, $template->output;