Bug 17411: Remove 3 other occurrences of exit 1
[koha.git] / tools / quotes / quotes-upload_ajax.pl
1 #!/usr/bin/perl
2
3 # Copyright 2012 Foundations Bible College Inc.
4 #
5 # This file is part of Koha.
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 strict;
21 use warnings;
22
23 use CGI qw ( -utf8 );
24 use JSON;
25 use URI::Escape;
26 use autouse 'Data::Dumper' => qw(Dumper);
27
28 use C4::Auth;
29 use C4::Koha;
30 use C4::Context;
31 use C4::Output;
32
33 my $cgi = new CGI;
34 my $dbh = C4::Context->dbh;
35
36 my ( $status, $cookie, $sessionID ) = C4::Auth::check_api_auth( $cgi, { tools => 'edit_quotes' } );
37 unless ($status eq "ok") {
38     print $cgi->header(-type => 'application/json', -status => '403 Forbidden');
39     print to_json({ auth_status => $status });
40     exit 0;
41 }
42
43 my $success = 'true';
44 my $quotes_tmp = uri_unescape( $cgi->param('quote' ) );
45 my $quotes = decode_json( $quotes_tmp );
46
47 my $action = $cgi->param('action');
48
49 my $sth = $dbh->prepare('INSERT INTO quotes (source, text) VALUES (?, ?);');
50
51 my $insert_count = 0;
52
53 foreach my $quote (@$quotes) {
54     $insert_count++ if $sth->execute($quote->[1], $quote->[2]);
55     if ($sth->err) {
56         warn sprintf('Database returned the following error: %s', $sth->errstr);
57         $success = 'false';
58     }
59 }
60
61 print $cgi->header('application/json');
62
63 print to_json({
64                 success => $success,
65                 records => $insert_count,
66 });