Bug 24594: Rewrite authorities_normal_marc21 to YAML
[koha.git] / tools / quotes / quotes_ajax.pl
blob79fbe6fb01e323d83868023581dbaf0584f3b3ef
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 Modern::Perl;
22 use CGI qw ( -utf8 );
23 use JSON;
24 use autouse 'Data::Dumper' => qw(Dumper);
26 use C4::Auth;
27 use C4::Context;
29 my $cgi = CGI->new;
30 my $dbh = C4::Context->dbh;
31 my $sort_columns = ["id", "source", "text", "timestamp"];
33 my ( $status, $cookie, $sessionID ) = C4::Auth::check_api_auth( $cgi, { tools => 'edit_quotes' } );
34 unless ($status eq "ok") {
35 print $cgi->header(-type => 'application/json', -status => '403 Forbidden');
36 print to_json({ auth_status => $status });
37 exit 0;
40 # NOTE: This is a collection of ajax functions for use with tools/quotes.pl
42 my $params = $cgi->Vars; # NOTE: Multivalue parameters NOT allowed!!
44 print $cgi->header('application/json; charset=utf-8');
46 my $action = $params->{'action'} || 'get';
47 if ($action eq 'add') {
48 my $sth = $dbh->prepare('INSERT INTO quotes (source, text) VALUES (?, ?);');
49 $sth->execute($params->{'source'}, $params->{'text'});
50 if ($sth->err) {
51 warn sprintf('Database returned the following error: %s', $sth->errstr);
52 exit 0;
54 my $new_quote_id = $dbh->{q{mysql_insertid}}; # ALERT: mysqlism here
55 $sth = $dbh->prepare('SELECT * FROM quotes WHERE id = ?;');
56 $sth->execute($new_quote_id);
57 print to_json($sth->fetchall_arrayref, {utf8 =>1});
58 exit 0;
60 elsif ($action eq 'edit') {
61 my $aaData = [];
62 my $editable_columns = [qw(source text)]; # pay attention to element order; these columns match the quotes table columns
63 my $sth = $dbh->prepare("UPDATE quotes SET $editable_columns->[$params->{'column'}-1] = ? WHERE id = ?;");
64 $sth->execute($params->{'value'}, $params->{'id'});
65 if ($sth->err) {
66 warn sprintf('Database returned the following error: %s', $sth->errstr);
67 exit 1;
69 $sth = $dbh->prepare("SELECT $editable_columns->[$params->{'column'}-1] FROM quotes WHERE id = ?;");
70 $sth->execute($params->{'id'});
71 $aaData = $sth->fetchrow_array();
72 print Encode::encode('utf8', $aaData);
74 exit 0;
76 elsif ($action eq 'delete') {
77 my $sth = $dbh->prepare("DELETE FROM quotes WHERE id = ?;");
78 $sth->execute($params->{'id'});
79 if ($sth->err) {
80 warn sprintf('Database returned the following error: %s', $sth->errstr);
81 exit 1;
83 exit 0;
85 else {
86 my $aaData = [];
87 my $iTotalRecords = '';
88 my $sth = '';
90 $iTotalRecords = $dbh->selectrow_array('SELECT count(*) FROM quotes;');
91 $sth = $dbh->prepare("SELECT * FROM quotes;");
93 $sth->execute();
94 if ($sth->err) {
95 warn sprintf('Database returned the following error: %s', $sth->errstr);
96 exit 1;
99 $aaData = $sth->fetchall_arrayref;
100 my $iTotalDisplayRecords = $iTotalRecords; # no filtering happening here
103 print to_json({
104 iTotalRecords => $iTotalRecords,
105 iTotalDisplayRecords=> $iTotalDisplayRecords,
106 sEcho => $params->{'sEcho'},
107 aaData => $aaData,
108 }, {utf8 =>1});