Bug 26118: Move translatable strings out of tags/review.tt and into tags-review.js
[koha.git] / admin / itemtypes.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2002 Paul Poulain
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21 =head1 admin/itemtypes.pl
22
23 =cut
24
25 use Modern::Perl;
26 use CGI qw ( -utf8 );
27
28 use File::Spec;
29
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;
37
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         flagsrequired   => { parameters => 'manage_itemtypes' },
49         debug           => 1,
50     }
51 );
52
53 my $dbh = C4::Context->dbh;
54
55 my $sip_media_type = $input->param('sip_media_type');
56 undef($sip_media_type) if defined($sip_media_type) and $sip_media_type =~ /^\s*$/;
57
58 if ( $op eq 'add_form' ) {
59     my $itemtype = Koha::ItemTypes->find($itemtype_code);
60
61     my $selected_branches = $itemtype ? $itemtype->get_library_limits : undef;
62     my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
63     my @branches_loop;
64     foreach my $branch ( @$branches ) {
65         my $selected = ($selected_branches && grep {$_->branchcode eq $branch->{branchcode}} @{ $selected_branches->as_list } ) ? 1 : 0;
66         push @branches_loop, {
67             branchcode => $branch->{branchcode},
68             branchname => $branch->{branchname},
69             selected   => $selected,
70         };
71     }
72
73     my $imagesets = C4::Koha::getImageSets( checked => ( $itemtype ? $itemtype->imageurl : undef ) );
74     my $searchcategory = GetAuthorisedValues("ITEMTYPECAT");
75     my $translated_languages = C4::Languages::getTranslatedLanguages( undef , C4::Context->preference('template') );
76     $template->param(
77         itemtype  => $itemtype,
78         imagesets => $imagesets,
79         searchcategory => $searchcategory,
80         can_be_translated => ( scalar(@$translated_languages) > 1 ? 1 : 0 ),
81         branches_loop    => \@branches_loop,
82     );
83 } elsif ( $op eq 'add_validate' ) {
84     my $is_a_modif   = $input->param('is_a_modif');
85     my $itemtype     = Koha::ItemTypes->find($itemtype_code);
86     my $description  = $input->param('description');
87     my $rentalcharge = $input->param('rentalcharge');
88     my $rentalcharge_daily = $input->param('rentalcharge_daily');
89     my $rentalcharge_hourly = $input->param('rentalcharge_hourly');
90     my $defaultreplacecost = $input->param('defaultreplacecost');
91     my $processfee = $input->param('processfee');
92     my $image = $input->param('image') || q||;
93     my @branches = grep { $_ ne q{} } $input->multi_param('branches');
94
95     my $notforloan = $input->param('notforloan') ? 1 : 0;
96     my $imageurl =
97       $image eq 'removeImage' ? ''
98       : (
99           $image eq 'remoteImage' ? $input->param('remoteImage')
100         : $image
101       );
102     my $summary        = $input->param('summary');
103     my $checkinmsg     = $input->param('checkinmsg');
104     my $checkinmsgtype = $input->param('checkinmsgtype');
105     my $hideinopac     = $input->param('hideinopac') // 0;
106     my $searchcategory = $input->param('searchcategory');
107     my $rentalcharge_daily_calendar  = $input->param('rentalcharge_daily_calendar') // 0;
108     my $rentalcharge_hourly_calendar = $input->param('rentalcharge_hourly_calendar') // 0;
109
110     if ( $itemtype and $is_a_modif ) {    # it's a modification
111         $itemtype->description($description);
112         $itemtype->rentalcharge($rentalcharge);
113         $itemtype->rentalcharge_daily($rentalcharge_daily);
114         $itemtype->rentalcharge_hourly($rentalcharge_hourly);
115         $itemtype->defaultreplacecost($defaultreplacecost);
116         $itemtype->processfee($processfee);
117         $itemtype->notforloan($notforloan);
118         $itemtype->imageurl($imageurl);
119         $itemtype->summary($summary);
120         $itemtype->checkinmsg($checkinmsg);
121         $itemtype->checkinmsgtype($checkinmsgtype);
122         $itemtype->sip_media_type($sip_media_type);
123         $itemtype->hideinopac($hideinopac);
124         $itemtype->searchcategory($searchcategory);
125         $itemtype->rentalcharge_daily_calendar($rentalcharge_daily_calendar);
126         $itemtype->rentalcharge_hourly_calendar($rentalcharge_hourly_calendar);
127
128         eval {
129           $itemtype->store;
130           $itemtype->replace_library_limits( \@branches );
131         };
132
133         if ($@) {
134             push @messages, { type => 'alert', code => 'error_on_update' };
135         } else {
136             push @messages, { type => 'message', code => 'success_on_update' };
137         }
138     } elsif ( not $itemtype and not $is_a_modif ) {
139         my $itemtype = Koha::ItemType->new(
140             {
141                 itemtype            => $itemtype_code,
142                 description         => $description,
143                 rentalcharge        => $rentalcharge,
144                 rentalcharge_daily  => $rentalcharge_daily,
145                 rentalcharge_hourly => $rentalcharge_hourly,
146                 defaultreplacecost  => $defaultreplacecost,
147                 processfee          => $processfee,
148                 notforloan          => $notforloan,
149                 imageurl            => $imageurl,
150                 summary             => $summary,
151                 checkinmsg          => $checkinmsg,
152                 checkinmsgtype      => $checkinmsgtype,
153                 sip_media_type      => $sip_media_type,
154                 hideinopac          => $hideinopac,
155                 searchcategory      => $searchcategory,
156                 rentalcharge_daily_calendar  => $rentalcharge_daily_calendar,
157                 rentalcharge_hourly_calendar => $rentalcharge_hourly_calendar,
158             }
159         );
160         eval {
161           $itemtype->store;
162           $itemtype->replace_library_limits( \@branches );
163         };
164
165         if ($@) {
166             push @messages, { type => 'alert', code => 'error_on_insert' };
167         } else {
168             push @messages, { type => 'message', code => 'success_on_insert' };
169         }
170     } else {
171         push @messages,
172           { type => 'alert',
173             code => 'already_exists',
174           };
175     }
176
177     $searchfield = '';
178     $op          = 'list';
179
180  } elsif ( $op eq 'delete_confirm' ) {
181     my $itemtype = Koha::ItemTypes->find($itemtype_code);
182     my $can_be_deleted = $itemtype->can_be_deleted();
183     if ($can_be_deleted == 0) {
184         push @messages, { type => 'alert', code => 'cannot_be_deleted'};
185         $op = 'list';
186     } else {
187         $template->param( itemtype => $itemtype, );
188     }
189
190 } elsif ( $op eq 'delete_confirmed' ) {
191
192     my $itemtype = Koha::ItemTypes->find($itemtype_code);
193     my $deleted = eval { $itemtype->delete };
194     if ( $@ or not $deleted ) {
195         push @messages, { type => 'alert', code => 'error_on_delete' };
196     } else {
197         push @messages, { type => 'message', code => 'success_on_delete' };
198     }
199
200     $op = 'list';
201 }
202
203 if ( $op eq 'list' ) {
204     my @itemtypes = Koha::ItemTypes->search->as_list;
205     $template->param(
206         itemtypes => \@itemtypes,
207         messages  => \@messages,
208     );
209 }
210
211 $template->param( op => $op );
212
213 output_html_with_http_headers $input, $cookie, $template->output;