Bug 21190: Introduce preferences AuthFailureLog and AuthSuccessLog
[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         authnotrequired => 0,
49         flagsrequired   => { parameters => 'manage_itemtypes' },
50         debug           => 1,
51     }
52 );
53
54 my $dbh = C4::Context->dbh;
55
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*$/;
58
59 if ( $op eq 'add_form' ) {
60     my $itemtype = Koha::ItemTypes->find($itemtype_code);
61
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,
71         };
72     }
73
74     my $imagesets = C4::Koha::getImageSets( checked => ( $itemtype ? $itemtype->imageurl : undef ) );
75     my $searchcategory = GetAuthorisedValues("ITEMTYPECAT");
76     my $translated_languages = C4::Languages::getTranslatedLanguages( undef , C4::Context->preference('template') );
77     $template->param(
78         itemtype  => $itemtype,
79         imagesets => $imagesets,
80         searchcategory => $searchcategory,
81         can_be_translated => ( scalar(@$translated_languages) > 1 ? 1 : 0 ),
82         branches_loop    => \@branches_loop,
83     );
84 } elsif ( $op eq 'add_validate' ) {
85     my $is_a_modif   = $input->param('is_a_modif');
86     my $itemtype     = Koha::ItemTypes->find($itemtype_code);
87     my $description  = $input->param('description');
88     my $rentalcharge = $input->param('rentalcharge');
89     my $rentalcharge_daily = $input->param('rentalcharge_daily');
90     my $rentalcharge_hourly = $input->param('rentalcharge_hourly');
91     my $defaultreplacecost = $input->param('defaultreplacecost');
92     my $processfee = $input->param('processfee');
93     my $image = $input->param('image') || q||;
94     my @branches = grep { $_ ne q{} } $input->multi_param('branches');
95
96     my $notforloan = $input->param('notforloan') ? 1 : 0;
97     my $imageurl =
98       $image eq 'removeImage' ? ''
99       : (
100           $image eq 'remoteImage' ? $input->param('remoteImage')
101         : $image
102       );
103     my $summary        = $input->param('summary');
104     my $checkinmsg     = $input->param('checkinmsg');
105     my $checkinmsgtype = $input->param('checkinmsgtype');
106     my $hideinopac     = $input->param('hideinopac') // 0;
107     my $searchcategory = $input->param('searchcategory');
108     my $rentalcharge_daily_calendar  = $input->param('rentalcharge_daily_calendar') // 0;
109     my $rentalcharge_hourly_calendar = $input->param('rentalcharge_hourly_calendar') // 0;
110
111     if ( $itemtype and $is_a_modif ) {    # it's a modification
112         $itemtype->description($description);
113         $itemtype->rentalcharge($rentalcharge);
114         $itemtype->rentalcharge_daily($rentalcharge_daily);
115         $itemtype->rentalcharge_hourly($rentalcharge_hourly);
116         $itemtype->defaultreplacecost($defaultreplacecost);
117         $itemtype->processfee($processfee);
118         $itemtype->notforloan($notforloan);
119         $itemtype->imageurl($imageurl);
120         $itemtype->summary($summary);
121         $itemtype->checkinmsg($checkinmsg);
122         $itemtype->checkinmsgtype($checkinmsgtype);
123         $itemtype->sip_media_type($sip_media_type);
124         $itemtype->hideinopac($hideinopac);
125         $itemtype->searchcategory($searchcategory);
126         $itemtype->rentalcharge_daily_calendar($rentalcharge_daily_calendar);
127         $itemtype->rentalcharge_hourly_calendar($rentalcharge_hourly_calendar);
128
129         eval {
130           $itemtype->store;
131           $itemtype->replace_library_limits( \@branches );
132         };
133
134         if ($@) {
135             push @messages, { type => 'alert', code => 'error_on_update' };
136         } else {
137             push @messages, { type => 'message', code => 'success_on_update' };
138         }
139     } elsif ( not $itemtype and not $is_a_modif ) {
140         my $itemtype = Koha::ItemType->new(
141             {
142                 itemtype            => $itemtype_code,
143                 description         => $description,
144                 rentalcharge        => $rentalcharge,
145                 rentalcharge_daily  => $rentalcharge_daily,
146                 rentalcharge_hourly => $rentalcharge_hourly,
147                 defaultreplacecost  => $defaultreplacecost,
148                 processfee          => $processfee,
149                 notforloan          => $notforloan,
150                 imageurl            => $imageurl,
151                 summary             => $summary,
152                 checkinmsg          => $checkinmsg,
153                 checkinmsgtype      => $checkinmsgtype,
154                 sip_media_type      => $sip_media_type,
155                 hideinopac          => $hideinopac,
156                 searchcategory      => $searchcategory,
157                 rentalcharge_daily_calendar  => $rentalcharge_daily_calendar,
158                 rentalcharge_hourly_calendar => $rentalcharge_hourly_calendar,
159             }
160         );
161         eval {
162           $itemtype->store;
163           $itemtype->replace_library_limits( \@branches );
164         };
165
166         if ($@) {
167             push @messages, { type => 'alert', code => 'error_on_insert' };
168         } else {
169             push @messages, { type => 'message', code => 'success_on_insert' };
170         }
171     } else {
172         push @messages,
173           { type => 'alert',
174             code => 'already_exists',
175           };
176     }
177
178     $searchfield = '';
179     $op          = 'list';
180
181  } elsif ( $op eq 'delete_confirm' ) {
182     my $itemtype = Koha::ItemTypes->find($itemtype_code);
183     my $can_be_deleted = $itemtype->can_be_deleted();
184     if ($can_be_deleted == 0) {
185         push @messages, { type => 'alert', code => 'cannot_be_deleted'};
186         $op = 'list';
187     } else {
188         $template->param( itemtype => $itemtype, );
189     }
190
191 } elsif ( $op eq 'delete_confirmed' ) {
192
193     my $itemtype = Koha::ItemTypes->find($itemtype_code);
194     my $deleted = eval { $itemtype->delete };
195     if ( $@ or not $deleted ) {
196         push @messages, { type => 'alert', code => 'error_on_delete' };
197     } else {
198         push @messages, { type => 'message', code => 'success_on_delete' };
199     }
200
201     $op = 'list';
202 }
203
204 if ( $op eq 'list' ) {
205     my @itemtypes = Koha::ItemTypes->search->as_list;
206     $template->param(
207         itemtypes => \@itemtypes,
208         messages  => \@messages,
209     );
210 }
211
212 $template->param( op => $op );
213
214 output_html_with_http_headers $input, $cookie, $template->output;