Bug 36792: Limit POSIX imports
[koha.git] / course_reserves / batch_add_items.pl
1 #!/usr/bin/perl
2
3 #
4 # Copyright 2018 Bywater Solutions
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 use Modern::Perl;
22
23 use CGI qw( -utf8 );
24 use List::MoreUtils qw( uniq );
25
26 use C4::Auth qw( get_template_and_user );
27 use C4::Circulation qw( barcodedecode );
28 use C4::Output qw( output_html_with_http_headers );
29 use C4::CourseReserves qw( GetCourse ModCourse ModCourseItem ModCourseReserve );
30
31 use Koha::Items;
32
33 my $cgi = CGI->new;
34
35 my $action    = $cgi->param('action')    || q{};
36 my $course_id = $cgi->param('course_id') || q{};
37 my $barcodes  = $cgi->param('barcodes')  || q{};
38 my $biblionumbers = $cgi->param('biblionumbers') || q{};
39
40 my $itype         = $cgi->param('itype');
41 my $ccode         = $cgi->param('ccode');
42 my $homebranch    = $cgi->param('homebranch');
43 my $holdingbranch = $cgi->param('holdingbranch');
44 my $location      = $cgi->param('location');
45
46 my $itype_enabled         = scalar $cgi->param('itype_enabled') ? 1 : 0;
47 my $ccode_enabled         = scalar $cgi->param('ccode_enabled') ? 1 : 0;
48 my $homebranch_enabled    = scalar $cgi->param('homebranch_enabled') ? 1 : 0;
49 my $holdingbranch_enabled = scalar $cgi->param('holdingbranch_enabled') ? 1 : 0;
50 my $location_enabled      = scalar $cgi->param('location_enabled') ? 1 : 0;
51
52 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
53     {
54         template_name   => "course_reserves/batch_add_items.tt",
55         query           => $cgi,
56         type            => "intranet",
57         flagsrequired   => { coursereserves => 'add_reserves' },
58     }
59 );
60
61 my $course = GetCourse($course_id);
62
63 if ( $course_id && $course ) {
64     $template->param( course => $course );
65
66     if ( !$action ) {
67         $template->param( action => 'display_form' );
68     }
69     elsif ( $action eq 'add' ) {
70         my @barcodes = uniq( split( /\s\n/, $barcodes ) );
71         my @biblionumbers = uniq( split( /\s\n/, $biblionumbers ) );
72
73         if (@barcodes > 0) {
74             my @items;
75             my @invalid_barcodes;
76             for my $b (@barcodes) {
77                 $b = barcodedecode($b) if $b;
78                 my $item = Koha::Items->find( { barcode => $b } );
79
80                 if ($item) {
81                     push( @items, $item );
82                 }
83                 else {
84                     push( @invalid_barcodes, $b );
85                 }
86             }
87
88             foreach my $item (@items) {
89                 my $ci_id = ModCourseItem(
90                     itemnumber            => $item->id,
91                     biblionumber          => undef,
92                     itype                 => $itype,
93                     ccode                 => $ccode,
94                     holdingbranch         => $holdingbranch,
95                     homebranch            => $homebranch,
96                     location              => $location,
97                     itype_enabled         => $itype_enabled,
98                     ccode_enabled         => $ccode_enabled,
99                     holdingbranch_enabled => $holdingbranch_enabled,
100                     homebranch_enabled    => $homebranch_enabled,
101                     location_enabled      => $location_enabled,
102                 );
103
104                 my $staff_note  = $cgi->param('item_staff_note');
105                 my $public_note = $cgi->param('item_public_note');
106                 my $cr_id = ModCourseReserve(
107                     course_id   => $course_id,
108                     ci_id       => $ci_id,
109                     staff_note  => $staff_note,
110                     public_note => $public_note,
111                 );
112             }
113
114             $template->param(
115                 action           => 'display_results',
116                 items_added      => \@items,
117                 invalid_barcodes => \@invalid_barcodes,
118                 course_id        => $course_id,
119                 barcodes         => 1,
120             );
121
122         } elsif (@biblionumbers > 0) {
123             my @biblios;
124             my @invalid_biblionumbers;
125             for my $b (@biblionumbers) {
126                 my $biblio = Koha::Biblios->find( $b );
127
128                 if ($biblio) {
129                     push( @biblios, $biblio );
130                 }
131                 else {
132                     push( @invalid_biblionumbers, $b );
133                 }
134             }
135
136             foreach my $biblio (@biblios) {
137                 my $ci_id = ModCourseItem(
138                     itemnumber            => undef,
139                     biblionumber          => $biblio->id,
140                     itype                 => $itype,
141                     ccode                 => $ccode,
142                     holdingbranch         => $holdingbranch,
143                     homebranch            => $homebranch,
144                     location              => $location,
145                     itype_enabled         => $itype_enabled,
146                     ccode_enabled         => $ccode_enabled,
147                     holdingbranch_enabled => $holdingbranch_enabled,
148                     homebranch_enabled    => $homebranch_enabled,
149                     location_enabled      => $location_enabled,
150                 );
151
152                 my $staff_note  = $cgi->param('biblio_staff_note');
153                 my $public_note = $cgi->param('biblio_public_note');
154                 my $cr_id = ModCourseReserve(
155                     course_id   => $course_id,
156                     ci_id       => $ci_id,
157                     staff_note  => $staff_note,
158                     public_note => $public_note,
159                 );
160             }
161
162             $template->param(
163                 action           => 'display_results',
164                 biblios_added    => \@biblios,
165                 invalid_biblionumbers => \@invalid_biblionumbers,
166                 course_id        => $course_id,
167                 biblionumbers    => 1,
168             );
169         }
170     }
171 } else {
172     $template->param( action => 'invalid_course' );
173 }
174
175 output_html_with_http_headers $cgi, $cookie, $template->output;