970ae189339c3194377a9e0a9edca5212107fa8e
[koha-equinox.git] / svc / members / add_to_list
1 #!/usr/bin/perl
2
3 # Copyright 2013 BibLibre
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21 use CGI;
22
23 use C4::Auth qw( check_cookie_auth );
24 use JSON qw( to_json );
25 use Koha::List::Patron;
26
27 my $input = new CGI;
28
29 my ( $auth_status, $sessionID ) = check_cookie_auth(
30     $input->cookie('CGISESSID'),
31     { tools => 'manage_patron_lists' },
32 );
33
34 exit 0 if $auth_status ne "ok";
35 my $add_to_patron_list       = $input->param('add_to_patron_list');
36 my $new_patron_list          = $input->param('new_patron_list');
37 my @borrowernumbers          = $input->param('borrowernumbers[]');
38
39 my $response;
40 if ($add_to_patron_list) {
41     my $patron_list = [];
42
43     if ( $add_to_patron_list eq 'new' ) {
44         $patron_list = AddPatronList( { name => $new_patron_list } );
45     }
46     else {
47         $patron_list =
48           [ GetPatronLists( { patron_list_id => $add_to_patron_list } ) ]->[0];
49     }
50
51     my @patrons_added_to_list = AddPatronsToList( { list => $patron_list, borrowernumbers => \@borrowernumbers } );
52
53     $response->{patron_list} = { patron_list_id => $patron_list->patron_list_id, name => $patron_list->name };
54     $response->{patrons_added_to_list} = scalar( @patrons_added_to_list );
55 }
56
57 binmode STDOUT, ":encoding(UTF-8)";
58 print $input->header(
59     -type => 'application/json',
60     -charset => 'UTF-8'
61 );
62
63 print to_json( $response );