Bug 20467: (QA follow-up) Filter barcodes, remove selector code, use template plugins
[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;
27 use C4::Output;
28 use C4::Koha;
29 use C4::CourseReserves qw(ModCourseItem ModCourseReserve GetCourse);
30
31 use Koha::Items;
32 use Koha::ItemTypes;
33
34 my $cgi = new CGI;
35
36 my $action    = $cgi->param('action')    || q{};
37 my $course_id = $cgi->param('course_id') || q{};
38 my $barcodes  = $cgi->param('barcodes')  || q{};
39
40 my $itype         = $cgi->param('itype');
41 my $ccode         = $cgi->param('ccode');
42 my $holdingbranch = $cgi->param('holdingbranch');
43 my $location      = $cgi->param('location');
44 my $staff_note    = $cgi->param('staff_note');
45 my $public_note   = $cgi->param('public_note');
46
47 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
48     {
49         template_name   => "course_reserves/batch_add_items.tt",
50         query           => $cgi,
51         type            => "intranet",
52         authnotrequired => 0,
53         flagsrequired   => { coursereserves => 'add_reserves' },
54     }
55 );
56
57 $template->param( course => GetCourse($course_id) );
58
59 if ( !$action ) {
60     $template->param( action => 'display_form' );
61 }
62 elsif ( $action eq 'add' ) {
63     my @barcodes = uniq( split( /\s\n/, $barcodes ) );
64
65     my @items;
66     my @invalid_barcodes;
67     for my $b (@barcodes) {
68         my $item = Koha::Items->find( { barcode => $b } );
69
70         if ($item) {
71             push( @items, $item );
72         }
73         else {
74             push( @invalid_barcodes, $b );
75         }
76     }
77
78     foreach my $item (@items) {
79         my $ci_id = ModCourseItem(
80             itemnumber    => $item->id,
81             itype         => $itype,
82             ccode         => $ccode,
83             holdingbranch => $holdingbranch,
84             location      => $location,
85         );
86
87         my $cr_id = ModCourseReserve(
88             course_id   => $course_id,
89             ci_id       => $ci_id,
90             staff_note  => $staff_note,
91             public_note => $public_note,
92         );
93     }
94
95     $template->param(
96         action           => 'display_results',
97         items_added      => \@items,
98         invalid_barcodes => \@invalid_barcodes,
99         course_id        => $course_id,
100     );
101 }
102
103 output_html_with_http_headers $cgi, $cookie, $template->output;