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