Bug 18501: (follow-up) Test undefined userenv behaviour
[koha.git] / admin / itemtypes.pl
blobae98300cfc2be7621b9be416b481d97302099bec
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2002 Paul Poulain
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 =head1 admin/itemtypes.pl
23 =cut
25 use Modern::Perl;
26 use CGI qw ( -utf8 );
28 use File::Spec;
30 use C4::Koha;
31 use C4::Context;
32 use C4::Auth;
33 use C4::Output;
34 use Koha::ItemTypes;
35 use Koha::ItemType;
36 use Koha::Localizations;
38 my $input = new CGI;
39 my $searchfield = $input->param('description');
40 my $itemtype_code = $input->param('itemtype');
41 my $op = $input->param('op') // 'list';
42 my @messages;
43 $searchfield =~ s/\,//g if $searchfield;
44 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
45 { template_name => "admin/itemtypes.tt",
46 query => $input,
47 type => "intranet",
48 authnotrequired => 0,
49 flagsrequired => { parameters => 'manage_itemtypes' },
50 debug => 1,
54 my $dbh = C4::Context->dbh;
56 my $sip_media_type = $input->param('sip_media_type');
57 undef($sip_media_type) if defined($sip_media_type) and $sip_media_type =~ /^\s*$/;
59 if ( $op eq 'add_form' ) {
60 my $itemtype = Koha::ItemTypes->find($itemtype_code);
62 my $selected_branches = $itemtype ? $itemtype->get_library_limits : undef;
63 my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
64 my @branches_loop;
65 foreach my $branch ( @$branches ) {
66 my $selected = ($selected_branches && grep {$_->branchcode eq $branch->{branchcode}} @{ $selected_branches->as_list } ) ? 1 : 0;
67 push @branches_loop, {
68 branchcode => $branch->{branchcode},
69 branchname => $branch->{branchname},
70 selected => $selected,
74 my $parent_type = $itemtype ? $itemtype->parent_type : undef;
75 my $parent_types = Koha::ItemTypes->search({parent_type=>undef,itemtype => {'!='=>$itemtype_code}});
76 my $imagesets = C4::Koha::getImageSets( checked => ( $itemtype ? $itemtype->imageurl : undef ) );
77 my $searchcategory = GetAuthorisedValues("ITEMTYPECAT");
78 my $translated_languages = C4::Languages::getTranslatedLanguages( undef , C4::Context->preference('template') );
79 $template->param(
80 itemtype => $itemtype,
81 parent_type => $parent_type,
82 parent_types => $parent_types,
83 is_a_parent => $itemtype ? Koha::ItemTypes->search({parent_type=>$itemtype_code})->count : 0,
84 imagesets => $imagesets,
85 searchcategory => $searchcategory,
86 can_be_translated => ( scalar(@$translated_languages) > 1 ? 1 : 0 ),
87 branches_loop => \@branches_loop,
89 } elsif ( $op eq 'add_validate' ) {
90 my $is_a_modif = $input->param('is_a_modif');
91 my $itemtype = Koha::ItemTypes->find($itemtype_code);
92 my $parent_type = $input->param('parent_type') || undef;
93 my $description = $input->param('description');
94 my $rentalcharge = $input->param('rentalcharge');
95 my $rentalcharge_daily = $input->param('rentalcharge_daily');
96 my $rentalcharge_hourly = $input->param('rentalcharge_hourly');
97 my $defaultreplacecost = $input->param('defaultreplacecost');
98 my $processfee = $input->param('processfee');
99 my $image = $input->param('image') || q||;
100 my @branches = grep { $_ ne q{} } $input->multi_param('branches');
102 my $notforloan = $input->param('notforloan') ? 1 : 0;
103 my $imageurl =
104 $image eq 'removeImage' ? ''
106 $image eq 'remoteImage' ? $input->param('remoteImage')
107 : $image
109 my $summary = $input->param('summary');
110 my $checkinmsg = $input->param('checkinmsg');
111 my $checkinmsgtype = $input->param('checkinmsgtype');
112 my $hideinopac = $input->param('hideinopac') // 0;
113 my $searchcategory = $input->param('searchcategory');
114 my $rentalcharge_daily_calendar = $input->param('rentalcharge_daily_calendar') // 0;
115 my $rentalcharge_hourly_calendar = $input->param('rentalcharge_hourly_calendar') // 0;
117 if ( $itemtype and $is_a_modif ) { # it's a modification
118 $itemtype->description($description);
119 $itemtype->parent_type($parent_type);
120 $itemtype->rentalcharge($rentalcharge);
121 $itemtype->rentalcharge_daily($rentalcharge_daily);
122 $itemtype->rentalcharge_hourly($rentalcharge_hourly);
123 $itemtype->defaultreplacecost($defaultreplacecost);
124 $itemtype->processfee($processfee);
125 $itemtype->notforloan($notforloan);
126 $itemtype->imageurl($imageurl);
127 $itemtype->summary($summary);
128 $itemtype->checkinmsg($checkinmsg);
129 $itemtype->checkinmsgtype($checkinmsgtype);
130 $itemtype->sip_media_type($sip_media_type);
131 $itemtype->hideinopac($hideinopac);
132 $itemtype->searchcategory($searchcategory);
133 $itemtype->rentalcharge_daily_calendar($rentalcharge_daily_calendar);
134 $itemtype->rentalcharge_hourly_calendar($rentalcharge_hourly_calendar);
136 eval {
137 $itemtype->store;
138 $itemtype->replace_library_limits( \@branches );
141 if ($@) {
142 push @messages, { type => 'alert', code => 'error_on_update' };
143 } else {
144 push @messages, { type => 'message', code => 'success_on_update' };
146 } elsif ( not $itemtype and not $is_a_modif ) {
147 my $itemtype = Koha::ItemType->new(
149 itemtype => $itemtype_code,
150 description => $description,
151 parent_type => $parent_type,
152 rentalcharge => $rentalcharge,
153 rentalcharge_daily => $rentalcharge_daily,
154 rentalcharge_hourly => $rentalcharge_hourly,
155 defaultreplacecost => $defaultreplacecost,
156 processfee => $processfee,
157 notforloan => $notforloan,
158 imageurl => $imageurl,
159 summary => $summary,
160 checkinmsg => $checkinmsg,
161 checkinmsgtype => $checkinmsgtype,
162 sip_media_type => $sip_media_type,
163 hideinopac => $hideinopac,
164 searchcategory => $searchcategory,
165 rentalcharge_daily_calendar => $rentalcharge_daily_calendar,
166 rentalcharge_hourly_calendar => $rentalcharge_hourly_calendar,
169 eval {
170 $itemtype->store;
171 $itemtype->replace_library_limits( \@branches );
174 if ($@) {
175 push @messages, { type => 'alert', code => 'error_on_insert' };
176 } else {
177 push @messages, { type => 'message', code => 'success_on_insert' };
179 } else {
180 push @messages,
181 { type => 'alert',
182 code => 'already_exists',
186 $searchfield = '';
187 $op = 'list';
189 } elsif ( $op eq 'delete_confirm' ) {
190 my $itemtype = Koha::ItemTypes->find($itemtype_code);
191 my $can_be_deleted = $itemtype->can_be_deleted();
192 if ($can_be_deleted == 0) {
193 push @messages, { type => 'alert', code => 'cannot_be_deleted'};
194 $op = 'list';
195 } else {
196 $template->param( itemtype => $itemtype, );
199 } elsif ( $op eq 'delete_confirmed' ) {
201 my $itemtype = Koha::ItemTypes->find($itemtype_code);
202 my $deleted = eval { $itemtype->delete };
203 if ( $@ or not $deleted ) {
204 push @messages, { type => 'alert', code => 'error_on_delete' };
205 } else {
206 push @messages, { type => 'message', code => 'success_on_delete' };
209 $op = 'list';
212 if ( $op eq 'list' ) {
213 my @itemtypes = Koha::ItemTypes->search->as_list;
214 $template->param(
215 itemtypes => \@itemtypes,
216 messages => \@messages,
220 $template->param( op => $op );
222 output_html_with_http_headers $input, $cookie, $template->output;