Bug 17411: Remove 3 other occurrences of exit 1
[koha.git] / tools / quotes / quotes_ajax.pl
blob9948313dbf0ae3bcb623e3a48960cf5e96bdd086
1 #!/usr/bin/perl
3 # Copyright 2012 Foundations Bible College Inc.
5 # This file is part of Koha.
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.
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.
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>.
20 use strict;
21 use warnings;
23 use CGI qw ( -utf8 );
24 use JSON;
25 use autouse 'Data::Dumper' => qw(Dumper);
27 use C4::Auth;
28 use C4::Context;
30 my $cgi = CGI->new;
31 my $dbh = C4::Context->dbh;
32 my $sort_columns = ["id", "source", "text", "timestamp"];
34 my ( $status, $cookie, $sessionID ) = C4::Auth::check_api_auth( $cgi, { tools => 'edit_quotes' } );
35 unless ($status eq "ok") {
36 print $cgi->header(-type => 'application/json', -status => '403 Forbidden');
37 print to_json({ auth_status => $status });
38 exit 0;
41 # NOTE: This is a collection of ajax functions for use with tools/quotes.pl
43 my $params = $cgi->Vars; # NOTE: Multivalue parameters NOT allowed!!
45 print $cgi->header('application/json; charset=utf-8');
47 my $action = $params->{'action'} || 'get';
48 if ($action eq 'add') {
49 my $sth = $dbh->prepare('INSERT INTO quotes (source, text) VALUES (?, ?);');
50 $sth->execute($params->{'source'}, $params->{'text'});
51 if ($sth->err) {
52 warn sprintf('Database returned the following error: %s', $sth->errstr);
53 exit 0;
55 my $new_quote_id = $dbh->{q{mysql_insertid}}; # ALERT: mysqlism here
56 $sth = $dbh->prepare('SELECT * FROM quotes WHERE id = ?;');
57 $sth->execute($new_quote_id);
58 print to_json($sth->fetchall_arrayref, {utf8 =>1});
59 exit 0;
61 elsif ($action eq 'edit') {
62 my $aaData = [];
63 my $editable_columns = [qw(source text)]; # pay attention to element order; these columns match the quotes table columns
64 my $sth = $dbh->prepare("UPDATE quotes SET $editable_columns->[$params->{'column'}-1] = ? WHERE id = ?;");
65 $sth->execute($params->{'value'}, $params->{'id'});
66 if ($sth->err) {
67 warn sprintf('Database returned the following error: %s', $sth->errstr);
68 exit 1;
70 $sth = $dbh->prepare("SELECT $editable_columns->[$params->{'column'}-1] FROM quotes WHERE id = ?;");
71 $sth->execute($params->{'id'});
72 $aaData = $sth->fetchrow_array();
73 print Encode::encode('utf8', $aaData);
75 exit 0;
77 elsif ($action eq 'delete') {
78 my $sth = $dbh->prepare("DELETE FROM quotes WHERE id = ?;");
79 $sth->execute($params->{'id'});
80 if ($sth->err) {
81 warn sprintf('Database returned the following error: %s', $sth->errstr);
82 exit 1;
84 exit 0;
86 else {
87 my $aaData = [];
88 my $iTotalRecords = '';
89 my $sth = '';
91 $iTotalRecords = $dbh->selectrow_array('SELECT count(*) FROM quotes;');
92 $sth = $dbh->prepare("SELECT * FROM quotes;");
94 $sth->execute();
95 if ($sth->err) {
96 warn sprintf('Database returned the following error: %s', $sth->errstr);
97 exit 1;
100 $aaData = $sth->fetchall_arrayref;
101 my $iTotalDisplayRecords = $iTotalRecords; # no filtering happening here
104 print to_json({
105 iTotalRecords => $iTotalRecords,
106 iTotalDisplayRecords=> $iTotalDisplayRecords,
107 sEcho => $params->{'sEcho'},
108 aaData => $aaData,
109 }, {utf8 =>1});