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