f77c4ed81f5abeac9ef8f9b555d2a7b19696b832
[srvgit] / 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;
22 use C4::Auth;
23 use C4::Context;
24 use C4::RotatingCollections;
25 use C4::Items;
26
27 use Koha::Items;
28
29 use CGI qw ( -utf8 );
30
31 my $query = CGI->new;
32
33 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34     {
35         template_name   => "rotating_collections/addItems.tt",
36         query           => $query,
37         type            => "intranet",
38         flagsrequired   => { tools => 'rotating_collections' },
39     }
40 );
41
42 if ( defined $query->param('action') and
43      $query->param('action') eq 'addItem' ) {
44     ## Add the given item to the collection
45     my $colId      = $query->param('colId');
46     my $barcode    = $query->param('barcode');
47     my $removeItem = $query->param('removeItem');
48     my $item       = Koha::Items->find({barcode => $barcode});
49     my $itemnumber = $item ? $item->itemnumber : undef;
50
51     my ( $success, $errorCode, $errorMessage );
52
53     $template->param( barcode => $barcode );
54
55     if ( !$removeItem ) {
56         ( $success, $errorCode, $errorMessage ) =
57           AddItemToCollection( $colId, $itemnumber );
58
59         $template->param(
60             previousActionAdd => 1,
61         );
62
63         if ($success) {
64             $template->param( addSuccess => 1 );
65         }
66         else {
67             $template->param( addFailure     => 1 );
68             $template->param( failureMessage => $errorMessage );
69         }
70     }
71     else {
72         ## Remove the given item from the collection
73         ( $success, $errorCode, $errorMessage ) =
74           RemoveItemFromCollection( $colId, $itemnumber );
75
76         $template->param(
77             previousActionRemove => 1,
78             removeChecked        => 1,
79         );
80
81         if ($success) {
82             $template->param( removeSuccess => 1 );
83         }
84         else {
85             $template->param( removeFailure  => 1 );
86             $template->param( failureMessage => $errorMessage );
87         }
88
89     }
90 }
91
92 my ( $colId, $colTitle, $colDescription, $colBranchcode ) =
93   GetCollection( scalar $query->param('colId') );
94 my $collectionItems = GetItemsInCollection($colId);
95 if ($collectionItems) {
96     $template->param( collectionItemsLoop => $collectionItems );
97 }
98
99 $template->param(
100     colId          => $colId,
101     colTitle       => $colTitle,
102     colDescription => $colDescription,
103     colBranchcode  => $colBranchcode,
104 );
105
106 output_html_with_http_headers $query, $cookie, $template->output;