Bug 25308: Escape querystring for Z39.50 popup from cataloging
[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::CourseReserves qw(ModCourseItem ModCourseReserve GetCourse);
29
30 use Koha::Items;
31
32 my $cgi = new CGI;
33
34 my $action    = $cgi->param('action')    || q{};
35 my $course_id = $cgi->param('course_id') || q{};
36 my $barcodes  = $cgi->param('barcodes')  || q{};
37
38 my $itype         = $cgi->param('itype');
39 my $ccode         = $cgi->param('ccode');
40 my $homebranch    = $cgi->param('homebranch');
41 my $holdingbranch = $cgi->param('holdingbranch');
42 my $location      = $cgi->param('location');
43 my $staff_note    = $cgi->param('staff_note');
44 my $public_note   = $cgi->param('public_note');
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         authnotrequired => 0,
58         flagsrequired   => { coursereserves => 'add_reserves' },
59     }
60 );
61
62 my $course = GetCourse($course_id);
63
64 if ( $course_id && $course ) {
65     $template->param( course => $course );
66
67     if ( !$action ) {
68         $template->param( action => 'display_form' );
69     }
70     elsif ( $action eq 'add' ) {
71         my @barcodes = uniq( split( /\s\n/, $barcodes ) );
72
73         my @items;
74         my @invalid_barcodes;
75         for my $b (@barcodes) {
76             my $item = Koha::Items->find( { barcode => $b } );
77
78             if ($item) {
79                 push( @items, $item );
80             }
81             else {
82                 push( @invalid_barcodes, $b );
83             }
84         }
85
86         foreach my $item (@items) {
87             my $ci_id = ModCourseItem(
88                 itemnumber            => $item->id,
89                 itype                 => $itype,
90                 ccode                 => $ccode,
91                 holdingbranch         => $holdingbranch,
92                 homebranch            => $homebranch,
93                 location              => $location,
94                 itype_enabled         => $itype_enabled,
95                 ccode_enabled         => $ccode_enabled,
96                 holdingbranch_enabled => $holdingbranch_enabled,
97                 homebranch_enabled    => $homebranch_enabled,
98                 location_enabled      => $location_enabled,
99             );
100
101             my $cr_id = ModCourseReserve(
102                 course_id   => $course_id,
103                 ci_id       => $ci_id,
104                 staff_note  => $staff_note,
105                 public_note => $public_note,
106             );
107         }
108
109         $template->param(
110             action           => 'display_results',
111             items_added      => \@items,
112             invalid_barcodes => \@invalid_barcodes,
113             course_id        => $course_id,
114         );
115     }
116 } else {
117     $template->param( action => 'invalid_course' );
118 }
119
120 output_html_with_http_headers $cgi, $cookie, $template->output;