28436e033e035c02e1f55ae47bc81fa614debccf
[koha.git] / svc / creator_batches
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2017 Aleisha Amohia <aleisha@catalyst.net.nz>
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use JSON qw( encode_json );
23 use CGI;
24 use C4::Service;
25 use C4::Context;
26 use C4::Auth qw /check_cookie_auth/;
27 use C4::Output qw(:DEFAULT :ajax);
28 use C4::Patroncards::Batch;
29 use C4::Labels::Batch;
30
31 =head1 NAME
32
33 svc/creator_batches - Web service for managing AJAX functionality for patroncards and label batches
34
35 =head1 DESCRIPTION
36
37 =cut
38
39 # AJAX requests
40 my $is_ajax = is_ajax();
41 my $cgi = new CGI;
42 my ( $auth_status, $sessionID ) = check_cookie_auth( $cgi->cookie('CGISESSID'), { catalogue => 1 } );
43 if ( $auth_status ne "ok" ) {
44     exit 0;
45 }
46
47 if ($is_ajax) {
48     my $batch_id = $cgi->param('batch_id');
49     my $description = $cgi->param('newdescription');
50     my $status = '';
51     my $dbh = C4::Context->dbh;
52     my $query = "UPDATE creator_batches SET description = ? WHERE batch_id = ?";
53     my $sth = $dbh->prepare($query);
54     $sth->execute($description, $batch_id);
55
56     # Check for success
57     my $creator = $cgi->param('creator');
58     if ( $creator eq 'patroncard' ) {
59         my $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
60         if ( $batch->{description} eq $description ) {
61             $status = 'success';
62         }
63     } elsif ( $creator eq 'label' ) {
64         my $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
65         if ( $batch->{description} eq $description ) {
66             $status = 'success';
67         }
68     }
69
70     my $json = encode_json ( { status => $status, newdesc => $description } );
71     output_with_http_headers $cgi, undef, $json, 'js';
72     exit;
73 }