Bug 13799: Change 'required' value type from string to boolean
[koha.git] / admin / itemtypes.pl
blob9e2fdfeebb37b784c3bb768b0f9e90d9d1817be2
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 itemtypes.hideinopac,
103 itemtypes.searchcategory,
104 COALESCE( localization.translation, itemtypes.description ) AS translated_description
105 FROM itemtypes
106 LEFT JOIN localization ON itemtypes.itemtype = localization.code
107 AND localization.entity='itemtypes'
108 AND localization.lang = ?
109 WHERE itemtype = ?
111 my $language = C4::Languages::getlanguage();
112 $sth->execute($language, $itemtype);
113 $data = $sth->fetchrow_hashref;
116 my $imagesets = C4::Koha::getImageSets( checked => $data->{'imageurl'} );
118 my $remote_image = undef;
119 if ( defined $data->{imageurl} and $data->{imageurl} =~ /^http/i ) {
120 $remote_image = $data->{imageurl};
123 my $searchcategory = GetAuthorisedValues("ITEMTYPECAT", $data->{'searchcategory'});
125 $template->param(
126 itemtype => $itemtype,
127 description => $data->{'description'},
128 rentalcharge => sprintf( "%.2f", $data->{'rentalcharge'} ),
129 notforloan => $data->{'notforloan'},
130 imageurl => $data->{'imageurl'},
131 template => C4::Context->preference('template'),
132 summary => $data->{summary},
133 checkinmsg => $data->{'checkinmsg'},
134 checkinmsgtype => $data->{'checkinmsgtype'},
135 imagesets => $imagesets,
136 remote_image => $remote_image,
137 sip_media_type => $data->{sip_media_type},
138 hideinopac => $data->{'hideinopac'},
139 searchcategory => $searchcategory,
142 # END $OP eq ADD_FORM
143 ################## ADD_VALIDATE ##################################
144 # called by add_form, used to insert/modify data in DB
146 elsif ( $op eq 'add_validate' ) {
147 my $is_a_modif = $input->param('is_a_modif');
148 my ( $already_exists ) = $dbh->selectrow_array(q|
149 SELECT itemtype
150 FROM itemtypes
151 WHERE itemtype = ?
152 |, undef, $itemtype );
153 if ( $already_exists and $is_a_modif ) { # it's a modification
154 my $query2 = '
155 UPDATE itemtypes
156 SET description = ?
157 , rentalcharge = ?
158 , notforloan = ?
159 , imageurl = ?
160 , summary = ?
161 , checkinmsg = ?
162 , checkinmsgtype = ?
163 , sip_media_type = ?
164 , hideinopac = ?
165 , searchcategory = ?
166 WHERE itemtype = ?
168 my $sth = $dbh->prepare($query2);
169 $sth->execute(
170 $input->param('description'),
171 $input->param('rentalcharge'),
172 ( $input->param('notforloan') ? 1 : 0 ),
174 $input->param('image') eq 'removeImage' ? '' : (
175 $input->param('image') eq 'remoteImage'
176 ? $input->param('remoteImage')
177 : $input->param('image') . ""
180 $input->param('summary'),
181 $input->param('checkinmsg'),
182 $input->param('checkinmsgtype'),
183 $sip_media_type,
184 $input->param('hideinopac') ? 1 : 0,
185 $input->param('searchcategory'),
186 $input->param('itemtype')
189 elsif ( not $already_exists and not $is_a_modif ) {
190 my $query = "
191 INSERT INTO itemtypes
192 (itemtype,description,rentalcharge, notforloan, imageurl, summary, checkinmsg, checkinmsgtype, sip_media_type, hideinopac, searchcategory)
193 VALUES
194 (?,?,?,?,?,?,?,?,?,?,?);
196 my $sth = $dbh->prepare($query);
197 my $image = $input->param('image');
198 $sth->execute(
199 $input->param('itemtype'),
200 $input->param('description'),
201 $input->param('rentalcharge'),
202 $input->param('notforloan') ? 1 : 0,
203 $image eq 'removeImage' ? '' :
204 $image eq 'remoteImage' ? $input->param('remoteImage') :
205 $image,
206 $input->param('summary'),
207 $input->param('checkinmsg'),
208 $input->param('checkinmsgtype'),
209 $sip_media_type,
210 $input->param('hideinopac') ? 1 : 0,
211 $input->param('searchcategory'),
214 else {
215 push @messages, {
216 type => 'error',
217 code => 'already_exists',
221 $searchfield = '';
222 $op = 'list';
223 # END $OP eq ADD_VALIDATE
224 ################## DELETE_CONFIRM ##################################
225 # called by default form, used to confirm deletion of data in DB
227 elsif ( $op eq 'delete_confirm' ) {
228 # Check both items and biblioitems
229 my $sth = $dbh->prepare('
230 SELECT COUNT(*) AS total FROM (
231 SELECT itemtype AS t FROM biblioitems
232 UNION ALL
233 SELECT itype AS t FROM items
234 ) AS tmp
235 WHERE tmp.t=?
237 $sth->execute($itemtype);
238 my $total = $sth->fetchrow_hashref->{'total'};
240 my $sth =
241 $dbh->prepare(
242 "select itemtype,description,rentalcharge from itemtypes where itemtype=?"
244 $sth->execute($itemtype);
245 my $data = $sth->fetchrow_hashref;
246 $template->param(
247 itemtype => $itemtype,
248 description => $data->{description},
249 rentalcharge => sprintf( "%.2f", $data->{rentalcharge} ),
250 imageurl => $data->{imageurl},
251 total => $total
254 # END $OP eq DELETE_CONFIRM
255 ################## DELETE_CONFIRMED ##################################
256 # called by delete_confirm, used to effectively confirm deletion of data in DB
258 elsif ( $op eq 'delete_confirmed' ) {
259 my $itemtype = uc( $input->param('itemtype') );
260 my $sth = $dbh->prepare("delete from itemtypes where itemtype=?");
261 $sth->execute($itemtype);
262 $sth = $dbh->prepare("delete from issuingrules where itemtype=?");
263 $sth->execute($itemtype);
264 print $input->redirect('itemtypes.pl');
265 exit;
266 # END $OP eq DELETE_CONFIRMED
267 ################## DEFAULT ##################################
270 if ( $op eq 'list' ) {
271 my $results = C4::Koha::GetItemTypes( style => 'array' );
272 my @loop;
273 foreach my $itemtype ( @{$results} ) {
274 $itemtype->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtype->{imageurl} );
275 $itemtype->{rentalcharge} = sprintf( '%.2f', $itemtype->{rentalcharge} );
277 my @translated_descriptions = Koha::Localizations->search(
278 { entity => 'itemtypes',
279 code => $itemtype->{itemtype},
282 $itemtype->{translated_descriptions} = [ map {
284 lang => $_->lang,
285 translation => $_->translation,
287 } @translated_descriptions ];
289 push( @loop, $itemtype );
292 $template->param(
293 loop => \@loop,
294 else => 1,
295 messages => \@messages,
299 output_html_with_http_headers $input, $cookie, $template->output;