Bug 15326: (follow-up) Fix staff permissions and consider library limits
[koha.git] / rotating_collections / addItems.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17 #
18
19 use Modern::Perl;
20
21 use C4::Output qw( output_html_with_http_headers );
22 use C4::Auth qw( get_template_and_user );
23 use C4::Context;
24 use C4::RotatingCollections;
25
26 use Koha::Items;
27
28 use CGI qw ( -utf8 );
29
30 my $query = CGI->new;
31
32 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
33     {
34         template_name   => "rotating_collections/addItems.tt",
35         query           => $query,
36         type            => "intranet",
37         flagsrequired   => { tools => 'rotating_collections' },
38     }
39 );
40
41 if ( defined $query->param('action') and
42      $query->param('action') eq 'addItem' ) {
43     ## Add the given item to the collection
44     my $colId      = $query->param('colId');
45     my $barcode    = $query->param('barcode');
46     my $removeItem = $query->param('removeItem');
47     my $item       = Koha::Items->find({barcode => $barcode});
48     my $itemnumber = $item ? $item->itemnumber : undef;
49
50     my ( $success, $errorCode, $errorMessage );
51
52     $template->param( barcode => $barcode );
53
54     if ( !$removeItem ) {
55         ( $success, $errorCode, $errorMessage ) =
56           AddItemToCollection( $colId, $itemnumber );
57
58         $template->param(
59             previousActionAdd => 1,
60         );
61
62         if ($success) {
63             $template->param( addSuccess => 1 );
64         }
65         else {
66             $template->param( addFailure     => 1 );
67             $template->param( failureMessage => $errorMessage );
68         }
69     }
70     else {
71         ## Remove the given item from the collection
72         ( $success, $errorCode, $errorMessage ) =
73           RemoveItemFromCollection( $colId, $itemnumber );
74
75         $template->param(
76             previousActionRemove => 1,
77             removeChecked        => 1,
78         );
79
80         if ($success) {
81             $template->param( removeSuccess => 1 );
82         }
83         else {
84             $template->param( removeFailure  => 1 );
85             $template->param( failureMessage => $errorMessage );
86         }
87
88     }
89 }
90
91 my ( $colId, $colTitle, $colDescription, $colBranchcode ) =
92   GetCollection( scalar $query->param('colId') );
93 my $collectionItems = GetItemsInCollection($colId);
94 if ($collectionItems) {
95     $template->param( collectionItemsLoop => $collectionItems );
96 }
97
98 $template->param(
99     colId          => $colId,
100     colTitle       => $colTitle,
101     colDescription => $colDescription,
102     colBranchcode  => $colBranchcode,
103 );
104
105 output_html_with_http_headers $query, $cookie, $template->output;