Bug 14977: Remove C4::Dates from files t/db_dependent/*.t
[koha.git] / admin / itemtypes.pl
blob2af82a3ec5550e1d2d653b6429d7bf237ac5fd06
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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 warnings; FIXME - Bug 2505
45 use CGI qw ( -utf8 );
47 use List::Util qw/min/;
48 use File::Spec;
50 use C4::Koha;
51 use C4::Context;
52 use C4::Auth;
53 use C4::Output;
55 use Koha::Localizations;
57 my $input = new CGI;
58 my $searchfield = $input->param('description');
59 my $script_name = "/cgi-bin/koha/admin/itemtypes.pl";
60 my $itemtype = $input->param('itemtype');
61 my $op = $input->param('op') // 'list';
62 my @messages;
63 $searchfield =~ s/\,//g;
64 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
66 template_name => "admin/itemtypes.tt",
67 query => $input,
68 type => "intranet",
69 authnotrequired => 0,
70 flagsrequired => { parameters => 'parameters_remaining_permissions' },
71 debug => 1,
75 $template->param(script_name => $script_name);
76 if ($op) {
77 $template->param($op => 1); # we show only the TMPL_VAR names $op
80 my $dbh = C4::Context->dbh;
82 my $sip_media_type = $input->param('sip_media_type');
83 undef($sip_media_type) if defined($sip_media_type) and $sip_media_type =~ /^\s*$/;
85 ################## ADD_FORM ##################################
86 # called by default. Used to create form to add or modify a record
87 if ( $op eq 'add_form' ) {
88 #---- if primkey exists, it's a modify action, so read values to modify...
89 my $data;
90 if ($itemtype) {
91 my $sth = $dbh->prepare(q|
92 SELECT
93 itemtypes.itemtype,
94 itemtypes.description,
95 itemtypes.rentalcharge,
96 itemtypes.notforloan,
97 itemtypes.imageurl,
98 itemtypes.summary,
99 itemtypes.checkinmsg,
100 itemtypes.checkinmsgtype,
101 itemtypes.sip_media_type,
102 COALESCE( localization.translation, itemtypes.description ) AS translated_description
103 FROM itemtypes
104 LEFT JOIN localization ON itemtypes.itemtype = localization.code
105 AND localization.entity='itemtypes'
106 AND localization.lang = ?
107 WHERE itemtype = ?
109 my $language = C4::Languages::getlanguage();
110 $sth->execute($language, $itemtype);
111 $data = $sth->fetchrow_hashref;
114 my $imagesets = C4::Koha::getImageSets( checked => $data->{'imageurl'} );
116 my $remote_image = undef;
117 if ( defined $data->{imageurl} and $data->{imageurl} =~ /^http/i ) {
118 $remote_image = $data->{imageurl};
121 my $searchcategory = GetAuthorisedValues("ITEMTYPECAT", $data->{'searchcategory'});
123 $template->param(
124 itemtype => $itemtype,
125 description => $data->{'description'},
126 rentalcharge => sprintf( "%.2f", $data->{'rentalcharge'} ),
127 notforloan => $data->{'notforloan'},
128 imageurl => $data->{'imageurl'},
129 template => C4::Context->preference('template'),
130 summary => $data->{summary},
131 checkinmsg => $data->{'checkinmsg'},
132 checkinmsgtype => $data->{'checkinmsgtype'},
133 imagesets => $imagesets,
134 remote_image => $remote_image,
135 sip_media_type => $data->{sip_media_type},
136 hideinopac => $data->{'hideinopac'},
137 searchcategory => $searchcategory,
140 # END $OP eq ADD_FORM
141 ################## ADD_VALIDATE ##################################
142 # called by add_form, used to insert/modify data in DB
144 elsif ( $op eq 'add_validate' ) {
145 my $is_a_modif = $input->param('is_a_modif');
146 my ( $already_exists ) = $dbh->selectrow_array(q|
147 SELECT itemtype
148 FROM itemtypes
149 WHERE itemtype = ?
150 |, undef, $itemtype );
151 if ( $already_exists and $is_a_modif ) { # it's a modification
152 my $query2 = '
153 UPDATE itemtypes
154 SET description = ?
155 , rentalcharge = ?
156 , notforloan = ?
157 , imageurl = ?
158 , summary = ?
159 , checkinmsg = ?
160 , checkinmsgtype = ?
161 , sip_media_type = ?
162 , hideinopac = ?
163 , searchcategory = ?
164 WHERE itemtype = ?
166 my $sth = $dbh->prepare($query2);
167 $sth->execute(
168 $input->param('description'),
169 $input->param('rentalcharge'),
170 ( $input->param('notforloan') ? 1 : 0 ),
172 $input->param('image') eq 'removeImage' ? '' : (
173 $input->param('image') eq 'remoteImage'
174 ? $input->param('remoteImage')
175 : $input->param('image') . ""
178 $input->param('summary'),
179 $input->param('checkinmsg'),
180 $input->param('checkinmsgtype'),
181 $sip_media_type,
182 $input->param('hideinopac') ? 1 : 0,
183 $input->param('searchcategory'),
184 $input->param('itemtype')
187 elsif ( not $already_exists and not $is_a_modif ) {
188 my $query = "
189 INSERT INTO itemtypes
190 (itemtype,description,rentalcharge, notforloan, imageurl, summary, checkinmsg, checkinmsgtype, sip_media_type, hideinopac, searchcategory)
191 VALUES
192 (?,?,?,?,?,?,?,?,?,?,?);
194 my $sth = $dbh->prepare($query);
195 my $image = $input->param('image');
196 $sth->execute(
197 $input->param('itemtype'),
198 $input->param('description'),
199 $input->param('rentalcharge'),
200 $input->param('notforloan') ? 1 : 0,
201 $image eq 'removeImage' ? '' :
202 $image eq 'remoteImage' ? $input->param('remoteImage') :
203 $image,
204 $input->param('summary'),
205 $input->param('checkinmsg'),
206 $input->param('checkinmsgtype'),
207 $sip_media_type,
208 $input->param('hideinopac') ? 1 : 0,
209 $input->param('searchcategory'),
212 else {
213 push @messages, {
214 type => 'error',
215 code => 'already_exists',
219 $searchfield = '';
220 $op = 'list';
221 # END $OP eq ADD_VALIDATE
222 ################## DELETE_CONFIRM ##################################
223 # called by default form, used to confirm deletion of data in DB
225 elsif ( $op eq 'delete_confirm' ) {
226 # Check both items and biblioitems
227 my $sth = $dbh->prepare('
228 SELECT COUNT(*) AS total FROM (
229 SELECT itemtype AS t FROM biblioitems
230 UNION ALL
231 SELECT itype AS t FROM items
232 ) AS tmp
233 WHERE tmp.t=?
235 $sth->execute($itemtype);
236 my $total = $sth->fetchrow_hashref->{'total'};
238 my $sth =
239 $dbh->prepare(
240 "select itemtype,description,rentalcharge from itemtypes where itemtype=?"
242 $sth->execute($itemtype);
243 my $data = $sth->fetchrow_hashref;
244 $template->param(
245 itemtype => $itemtype,
246 description => $data->{description},
247 rentalcharge => sprintf( "%.2f", $data->{rentalcharge} ),
248 imageurl => $data->{imageurl},
249 total => $total
252 # END $OP eq DELETE_CONFIRM
253 ################## DELETE_CONFIRMED ##################################
254 # called by delete_confirm, used to effectively confirm deletion of data in DB
256 elsif ( $op eq 'delete_confirmed' ) {
257 my $itemtype = uc( $input->param('itemtype') );
258 my $sth = $dbh->prepare("delete from itemtypes where itemtype=?");
259 $sth->execute($itemtype);
260 $sth = $dbh->prepare("delete from issuingrules where itemtype=?");
261 $sth->execute($itemtype);
262 print $input->redirect('itemtypes.pl');
263 exit;
264 # END $OP eq DELETE_CONFIRMED
265 ################## DEFAULT ##################################
268 if ( $op eq 'list' ) {
269 my $results = C4::Koha::GetItemTypes( style => 'array' );
270 my @loop;
271 foreach my $itemtype ( @{$results} ) {
272 $itemtype->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtype->{imageurl} );
273 $itemtype->{rentalcharge} = sprintf( '%.2f', $itemtype->{rentalcharge} );
275 my @translated_descriptions = Koha::Localizations->search(
276 { entity => 'itemtypes',
277 code => $itemtype->{itemtype},
280 $itemtype->{translated_descriptions} = [ map {
282 lang => $_->lang,
283 translation => $_->translation,
285 } @translated_descriptions ];
287 push( @loop, $itemtype );
290 $template->param(
291 loop => \@loop,
292 else => 1,
293 messages => \@messages,
297 output_html_with_http_headers $input, $cookie, $template->output;