Bug 15682 - Only allow merging of 2 or more records form lsits (for consistency)
[koha.git] / admin / itemtypes.pl
blob67b4c48c54054715649ae7ed50b22bae1fa24d3c
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'});
124 my $translated_languages = C4::Languages::getTranslatedLanguages( undef , C4::Context->preference('template') );
126 $template->param(
127 itemtype => $itemtype,
128 description => $data->{'description'},
129 rentalcharge => sprintf( "%.2f", $data->{'rentalcharge'} ),
130 notforloan => $data->{'notforloan'},
131 imageurl => $data->{'imageurl'},
132 template => C4::Context->preference('template'),
133 summary => $data->{summary},
134 checkinmsg => $data->{'checkinmsg'},
135 checkinmsgtype => $data->{'checkinmsgtype'},
136 imagesets => $imagesets,
137 remote_image => $remote_image,
138 sip_media_type => $data->{sip_media_type},
139 hideinopac => $data->{'hideinopac'},
140 searchcategory => $searchcategory,
141 can_be_translated => ( scalar(@$translated_languages) > 1 ? 1 : 0 ),
144 # END $OP eq ADD_FORM
145 ################## ADD_VALIDATE ##################################
146 # called by add_form, used to insert/modify data in DB
148 elsif ( $op eq 'add_validate' ) {
149 my $is_a_modif = $input->param('is_a_modif');
150 my ( $already_exists ) = $dbh->selectrow_array(q|
151 SELECT itemtype
152 FROM itemtypes
153 WHERE itemtype = ?
154 |, undef, $itemtype );
155 if ( $already_exists and $is_a_modif ) { # it's a modification
156 my $query2 = '
157 UPDATE itemtypes
158 SET description = ?
159 , rentalcharge = ?
160 , notforloan = ?
161 , imageurl = ?
162 , summary = ?
163 , checkinmsg = ?
164 , checkinmsgtype = ?
165 , sip_media_type = ?
166 , hideinopac = ?
167 , searchcategory = ?
168 WHERE itemtype = ?
170 my $sth = $dbh->prepare($query2);
171 $sth->execute(
172 $input->param('description'),
173 $input->param('rentalcharge'),
174 ( $input->param('notforloan') ? 1 : 0 ),
176 $input->param('image') eq 'removeImage' ? '' : (
177 $input->param('image') eq 'remoteImage'
178 ? $input->param('remoteImage')
179 : $input->param('image') . ""
182 $input->param('summary'),
183 $input->param('checkinmsg'),
184 $input->param('checkinmsgtype'),
185 $sip_media_type,
186 $input->param('hideinopac') ? 1 : 0,
187 $input->param('searchcategory'),
188 $input->param('itemtype')
191 elsif ( not $already_exists and not $is_a_modif ) {
192 my $query = "
193 INSERT INTO itemtypes
194 (itemtype,description,rentalcharge, notforloan, imageurl, summary, checkinmsg, checkinmsgtype, sip_media_type, hideinopac, searchcategory)
195 VALUES
196 (?,?,?,?,?,?,?,?,?,?,?);
198 my $sth = $dbh->prepare($query);
199 my $image = $input->param('image');
200 $sth->execute(
201 $input->param('itemtype'),
202 $input->param('description'),
203 $input->param('rentalcharge'),
204 $input->param('notforloan') ? 1 : 0,
205 $image eq 'removeImage' ? '' :
206 $image eq 'remoteImage' ? $input->param('remoteImage') :
207 $image,
208 $input->param('summary'),
209 $input->param('checkinmsg'),
210 $input->param('checkinmsgtype'),
211 $sip_media_type,
212 $input->param('hideinopac') ? 1 : 0,
213 $input->param('searchcategory'),
216 else {
217 push @messages, {
218 type => 'error',
219 code => 'already_exists',
223 $searchfield = '';
224 $op = 'list';
225 # END $OP eq ADD_VALIDATE
226 ################## DELETE_CONFIRM ##################################
227 # called by default form, used to confirm deletion of data in DB
229 elsif ( $op eq 'delete_confirm' ) {
230 # Check both items and biblioitems
231 my $sth = $dbh->prepare('
232 SELECT COUNT(*) AS total FROM (
233 SELECT itemtype AS t FROM biblioitems
234 UNION ALL
235 SELECT itype AS t FROM items
236 ) AS tmp
237 WHERE tmp.t=?
239 $sth->execute($itemtype);
240 my $total = $sth->fetchrow_hashref->{'total'};
242 my $sth =
243 $dbh->prepare(
244 "select itemtype,description,rentalcharge from itemtypes where itemtype=?"
246 $sth->execute($itemtype);
247 my $data = $sth->fetchrow_hashref;
248 $template->param(
249 itemtype => $itemtype,
250 description => $data->{description},
251 rentalcharge => sprintf( "%.2f", $data->{rentalcharge} ),
252 imageurl => $data->{imageurl},
253 total => $total
256 # END $OP eq DELETE_CONFIRM
257 ################## DELETE_CONFIRMED ##################################
258 # called by delete_confirm, used to effectively confirm deletion of data in DB
260 elsif ( $op eq 'delete_confirmed' ) {
261 my $itemtype = uc( $input->param('itemtype') );
262 my $sth = $dbh->prepare("delete from itemtypes where itemtype=?");
263 $sth->execute($itemtype);
264 $sth = $dbh->prepare("delete from issuingrules where itemtype=?");
265 $sth->execute($itemtype);
266 print $input->redirect('itemtypes.pl');
267 exit;
268 # END $OP eq DELETE_CONFIRMED
269 ################## DEFAULT ##################################
272 if ( $op eq 'list' ) {
273 my $results = C4::Koha::GetItemTypes( style => 'array' );
274 my @loop;
275 foreach my $itemtype ( @{$results} ) {
276 $itemtype->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtype->{imageurl} );
277 $itemtype->{rentalcharge} = sprintf( '%.2f', $itemtype->{rentalcharge} );
279 my @translated_descriptions = Koha::Localizations->search(
280 { entity => 'itemtypes',
281 code => $itemtype->{itemtype},
284 $itemtype->{translated_descriptions} = [ map {
286 lang => $_->lang,
287 translation => $_->translation,
289 } @translated_descriptions ];
291 push( @loop, $itemtype );
294 $template->param(
295 loop => \@loop,
296 else => 1,
297 messages => \@messages,
301 output_html_with_http_headers $input, $cookie, $template->output;