Bug 30477: Add new UNIMARC installer translation files
[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
29 use C4::Koha qw( getImageSets GetAuthorisedValues );
30 use C4::Context;
31 use C4::Auth qw( get_template_and_user );
32 use C4::Output qw( output_html_with_http_headers );
33 use Koha::ItemTypes;
34 use Koha::ItemType;
35 use Koha::Localizations;
36
37 my $input         = CGI->new;
38 my $searchfield   = $input->param('description');
39 my $itemtype_code = $input->param('itemtype');
40 my $op            = $input->param('op') // 'list';
41 my @messages;
42 $searchfield =~ s/\,//g if $searchfield;
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44     {   template_name   => "admin/itemtypes.tt",
45         query           => $input,
46         type            => "intranet",
47         flagsrequired   => { parameters => 'manage_itemtypes' },
48     }
49 );
50
51 my $dbh = C4::Context->dbh;
52
53 my $sip_media_type = $input->param('sip_media_type');
54 undef($sip_media_type) if defined($sip_media_type) and $sip_media_type =~ /^\s*$/;
55
56 if ( $op eq 'add_form' ) {
57     my $itemtype = Koha::ItemTypes->find($itemtype_code);
58
59     my $parent_type = $itemtype ? $itemtype->parent_type : undef;
60     my $parent_types = Koha::ItemTypes->search({parent_type=>undef,itemtype => {'!='=>$itemtype_code}});
61     my $imagesets = C4::Koha::getImageSets( checked => ( $itemtype ? $itemtype->imageurl : undef ) );
62     my $searchcategory = GetAuthorisedValues("ITEMTYPECAT");
63     my $translated_languages = C4::Languages::getTranslatedLanguages( "both", C4::Context->preference('template') );
64     $template->param(
65         itemtype  => $itemtype,
66         parent_type => $parent_type,
67         parent_types => $parent_types,
68         is_a_parent => $itemtype ? Koha::ItemTypes->search({parent_type=>$itemtype_code})->count : 0,
69         imagesets => $imagesets,
70         searchcategory => $searchcategory,
71         can_be_translated => ( scalar(@$translated_languages) > 1 ? 1 : 0 ),
72     );
73 } elsif ( $op eq 'add_validate' ) {
74     my $is_a_modif   = $input->param('is_a_modif');
75     my $itemtype     = Koha::ItemTypes->find($itemtype_code);
76     my $parent_type  = $input->param('parent_type') || undef;
77     my $description  = $input->param('description');
78     my $rentalcharge = $input->param('rentalcharge');
79     my $rentalcharge_daily = $input->param('rentalcharge_daily');
80     my $rentalcharge_hourly = $input->param('rentalcharge_hourly');
81     my $defaultreplacecost = $input->param('defaultreplacecost');
82     my $processfee = $input->param('processfee');
83     my $image = $input->param('image') || q||;
84     my @branches = grep { $_ ne q{} } $input->multi_param('branches');
85
86     my $notforloan = $input->param('notforloan') ? 1 : 0;
87     my $imageurl =
88       $image eq 'removeImage' ? ''
89       : (
90           $image eq 'remoteImage' ? $input->param('remoteImage')
91         : $image
92       );
93     my $summary        = $input->param('summary');
94     my $checkinmsg     = $input->param('checkinmsg');
95     my $checkinmsgtype = $input->param('checkinmsgtype');
96     my $hideinopac     = $input->param('hideinopac') // 0;
97     my $searchcategory = $input->param('searchcategory');
98     my $rentalcharge_daily_calendar  = $input->param('rentalcharge_daily_calendar') // 0;
99     my $rentalcharge_hourly_calendar = $input->param('rentalcharge_hourly_calendar') // 0;
100     my $automatic_checkin = $input->param('automatic_checkin') // 0;
101
102     if ( $itemtype and $is_a_modif ) {    # it's a modification
103         $itemtype->description($description);
104         $itemtype->parent_type($parent_type);
105         $itemtype->rentalcharge($rentalcharge);
106         $itemtype->rentalcharge_daily($rentalcharge_daily);
107         $itemtype->rentalcharge_hourly($rentalcharge_hourly);
108         $itemtype->defaultreplacecost($defaultreplacecost);
109         $itemtype->processfee($processfee);
110         $itemtype->notforloan($notforloan);
111         $itemtype->imageurl($imageurl);
112         $itemtype->summary($summary);
113         $itemtype->checkinmsg($checkinmsg);
114         $itemtype->checkinmsgtype($checkinmsgtype);
115         $itemtype->sip_media_type($sip_media_type);
116         $itemtype->hideinopac($hideinopac);
117         $itemtype->searchcategory($searchcategory);
118         $itemtype->rentalcharge_daily_calendar($rentalcharge_daily_calendar);
119         $itemtype->rentalcharge_hourly_calendar($rentalcharge_hourly_calendar);
120         $itemtype->automatic_checkin($automatic_checkin);
121
122         eval {
123           $itemtype->store;
124           $itemtype->replace_library_limits( \@branches );
125         };
126
127         if ($@) {
128             push @messages, { type => 'alert', code => 'error_on_update' };
129         } else {
130             push @messages, { type => 'message', code => 'success_on_update' };
131         }
132     } elsif ( not $itemtype and not $is_a_modif ) {
133         my $itemtype = Koha::ItemType->new(
134             {
135                 itemtype            => $itemtype_code,
136                 description         => $description,
137                 parent_type         => $parent_type,
138                 rentalcharge        => $rentalcharge,
139                 rentalcharge_daily  => $rentalcharge_daily,
140                 rentalcharge_hourly => $rentalcharge_hourly,
141                 defaultreplacecost  => $defaultreplacecost,
142                 processfee          => $processfee,
143                 notforloan          => $notforloan,
144                 imageurl            => $imageurl,
145                 summary             => $summary,
146                 checkinmsg          => $checkinmsg,
147                 checkinmsgtype      => $checkinmsgtype,
148                 sip_media_type      => $sip_media_type,
149                 hideinopac          => $hideinopac,
150                 searchcategory      => $searchcategory,
151                 rentalcharge_daily_calendar  => $rentalcharge_daily_calendar,
152                 rentalcharge_hourly_calendar => $rentalcharge_hourly_calendar,
153                 automatic_checkin   => $automatic_checkin,
154             }
155         );
156         eval {
157           $itemtype->store;
158           $itemtype->replace_library_limits( \@branches );
159         };
160
161         if ($@) {
162             push @messages, { type => 'alert', code => 'error_on_insert' };
163         } else {
164             push @messages, { type => 'message', code => 'success_on_insert' };
165         }
166     } else {
167         push @messages,
168           { type => 'alert',
169             code => 'already_exists',
170           };
171     }
172
173     $searchfield = '';
174     $op          = 'list';
175
176  } elsif ( $op eq 'delete_confirm' ) {
177     my $itemtype = Koha::ItemTypes->find($itemtype_code);
178     my $can_be_deleted = $itemtype->can_be_deleted();
179     if ($can_be_deleted == 0) {
180         push @messages, { type => 'alert', code => 'cannot_be_deleted'};
181         $op = 'list';
182     } else {
183         $template->param( itemtype => $itemtype, );
184     }
185
186 } elsif ( $op eq 'delete_confirmed' ) {
187
188     my $itemtype = Koha::ItemTypes->find($itemtype_code);
189     my $deleted = eval { $itemtype->delete };
190     if ( $@ or not $deleted ) {
191         push @messages, { type => 'alert', code => 'error_on_delete' };
192     } else {
193         push @messages, { type => 'message', code => 'success_on_delete' };
194     }
195
196     $op = 'list';
197 }
198
199 if ( $op eq 'list' ) {
200     $template->param(
201         itemtypes => Koha::ItemTypes->search,
202         messages  => \@messages,
203     );
204 }
205
206 $template->param( op => $op );
207
208 output_html_with_http_headers $input, $cookie, $template->output;