Bug 22990: Add CSRF protection to boraccount, pay and suggestion
[koha.git] / course_reserves / add_items.pl
1 #!/usr/bin/perl
2
3 #
4 # Copyright 2012 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
25 use C4::Auth qw( get_template_and_user );
26 use C4::Circulation qw( barcodedecode );
27 use C4::Output qw( output_html_with_http_headers );
28 use C4::Koha qw( GetAuthorisedValues );
29 use Koha::Items;
30
31 use C4::CourseReserves qw( GetCourse GetCourseReserve ModCourse ModCourseItem ModCourseReserve );
32
33 use Koha::Items;
34 use Koha::ItemTypes;
35
36 my $cgi = CGI->new;
37
38 my $action       = $cgi->param('action')       || '';
39 my $course_id    = $cgi->param('course_id')    || '';
40 my $barcode      = $cgi->param('barcode')      || '';
41 my $return       = $cgi->param('return')       || '';
42 my $itemnumber   = $cgi->param('itemnumber')   || '';
43 my $is_edit      = $cgi->param('is_edit')      || '';
44 my $biblionumber = $cgi->param('biblionumber') || '';
45
46 $barcode =~ s/^\s*|\s*$//g; #remove leading/trailing whitespace
47 $barcode = barcodedecode($barcode) if $barcode;
48 $biblionumber =~ s/^\s*|\s*$//g; #remove leading/trailing whitespace
49
50 my ( $item, $biblio );
51
52 if ( $barcode || $itemnumber ) {
53     # adding an item to course items
54     $item = $itemnumber ? Koha::Items->find( $itemnumber ) : Koha::Items->find({ barcode => $barcode });
55     if ( $item ) {
56         $itemnumber = $item->id;
57         $biblio = $item->biblio;
58     }
59 } else {
60     # adding a biblio to course items
61     $biblio = Koha::Biblios->find( $biblionumber );
62 }
63
64 my $step = ( $action eq 'lookup' && ( $item or $biblio ) ) ? '2' : '1';
65
66 my $tmpl = ($course_id) ? "add_items-step$step.tt" : "invalid-course.tt";
67 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
68     {   template_name   => "course_reserves/$tmpl",
69         query           => $cgi,
70         type            => "intranet",
71         flagsrequired   => { coursereserves => 'add_reserves' },
72     }
73 );
74
75 if ( !$item && !$biblio && $action eq 'lookup' ){
76     $template->param( ERROR_ITEM_NOT_FOUND => 1 );
77     $template->param( UNKNOWN_BARCODE => $barcode ) if $barcode;
78     $template->param( UNKNOWN_BIBLIONUMBER => $biblionumber ) if $biblionumber;
79 }
80
81 $template->param( course => GetCourse($course_id) );
82
83 if ( $action eq 'lookup' and $item ) {
84     my $course_item = Koha::Course::Items->find({ itemnumber => $item->id });
85     my $course_reserve =
86       ($course_item)
87       ? GetCourseReserve(
88         course_id => $course_id,
89         ci_id     => $course_item->ci_id,
90       )
91       : undef;
92
93     my $itemtypes = Koha::ItemTypes->search;
94     $template->param(
95         item           => $item,
96         biblio         => $biblio,
97         course_item    => $course_item,
98         course_reserve => $course_reserve,
99         is_edit        => $is_edit,
100
101         ccodes    => GetAuthorisedValues('CCODE'),
102         locations => GetAuthorisedValues('LOC'),
103         itypes    => $itemtypes, # FIXME We certainly want to display the translated_description in the template
104         return    => $return,
105     );
106
107 } elsif ( $action eq 'lookup' and $biblio ) {
108     my $course_item = Koha::Course::Items->find({ biblionumber => $biblio->biblionumber });
109     my $course_reserve =
110       ($course_item)
111       ? GetCourseReserve(
112         course_id => $course_id,
113         ci_id     => $course_item->ci_id,
114       )
115       : undef;
116
117     my $itemtypes = Koha::ItemTypes->search;
118     $template->param(
119         biblio         => $biblio,
120         course_item    => $course_item,
121         course_reserve => $course_reserve,
122         is_edit        => $is_edit,
123
124         return    => $return,
125     );
126
127 } elsif ( $action eq 'add' ) {
128     my $itype         = scalar $cgi->param('itype');
129     my $ccode         = scalar $cgi->param('ccode');
130     my $homebranch    = $cgi->param('homebranch');
131     my $holdingbranch = scalar $cgi->param('holdingbranch');
132     my $location      = scalar $cgi->param('location');
133
134     my $itype_enabled         = scalar $cgi->param('itype_enabled') ? 1 : 0;
135     my $ccode_enabled         = scalar $cgi->param('ccode_enabled') ? 1 : 0;
136     my $homebranch_enabled    = $cgi->param('homebranch_enabled') ? 1 : 0;
137     my $holdingbranch_enabled = scalar $cgi->param('holdingbranch_enabled') ? 1 : 0;
138     my $location_enabled      = scalar $cgi->param('location_enabled') ? 1 : 0;
139
140     my $ci_id = ModCourseItem(
141             itemnumber    => $itemnumber,
142             biblionumber  => $biblionumber,
143             itype         => $itype,
144             ccode         => $ccode,
145             homebranch    => $homebranch,
146             holdingbranch => $holdingbranch,
147             location      => $location,
148             itype_enabled         => $itype_enabled,
149             ccode_enabled         => $ccode_enabled,
150             homebranch_enabled    => $homebranch_enabled,
151             holdingbranch_enabled => $holdingbranch_enabled,
152             location_enabled      => $location_enabled,
153     );
154
155     my $cr_id = ModCourseReserve(
156         course_id   => $course_id,
157         ci_id       => $ci_id,
158         staff_note  => scalar $cgi->param('staff_note'),
159         public_note => scalar $cgi->param('public_note'),
160     );
161
162     if ( $return ) {
163         print $cgi->redirect("/cgi-bin/koha/course_reserves/course-details.pl?course_id=$return");
164         exit;
165     }
166 }
167
168 output_html_with_http_headers $cgi, $cookie, $template->output;