Bug 11944: use CGI( -utf8 ) everywhere
[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 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 strict;
21 use warnings;
22
23 use CGI qw ( -utf8 );
24 use JSON;
25 use autouse 'Data::Dumper' => qw(Dumper);
26
27 use C4::Auth;
28 use C4::Koha;
29 use C4::Context;
30 use C4::Output;
31
32 my $cgi = new CGI;
33 my $dbh = C4::Context->dbh;
34
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36     {
37         template_name   => "",
38         query           => $cgi,
39         type            => "intranet",
40         authnotrequired => 0,
41         flagsrequired   => { tools => 'edit_quotes' },
42         debug           => 1,
43     }
44 );
45
46 my $success = 'true';
47
48 my $quotes = decode_json($cgi->param('quote'));
49 my $action = $cgi->param('action');
50
51 my $sth = $dbh->prepare('INSERT INTO quotes (source, text) VALUES (?, ?);');
52
53 my $insert_count = 0;
54
55 foreach my $quote (@$quotes) {
56     $insert_count++ if $sth->execute($quote->[1], $quote->[2]);
57     if ($sth->err) {
58         warn sprintf('Database returned the following error: %s', $sth->errstr);
59         $success = 'false';
60     }
61 }
62
63 print $cgi->header('application/json');
64
65 print to_json({
66                 success => $success,
67                 records => $insert_count,
68 });