Bug 10694: (follow-up) fix various issues
[koha.git] / tools / quotes / quotes_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;
24 use JSON;
25 use autouse 'Data::Dumper' => qw(Dumper);
26
27 use C4::Auth;
28 use C4::Context;
29
30 my $cgi = CGI->new;
31 my $dbh = C4::Context->dbh;
32 my $sort_columns = ["id", "source", "text", "timestamp"];
33
34 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
35     {
36         template_name   => "",
37         query           => $cgi,
38         type            => "intranet",
39         authnotrequired => 0,
40         flagsrequired   => { tools => 'edit_quotes' },
41         debug           => 1,
42     }
43 );
44
45 # NOTE: This is a collection of ajax functions for use with tools/quotes.pl
46
47 my $params = $cgi->Vars; # NOTE: Multivalue parameters NOT allowed!!
48
49 print $cgi->header('application/json; charset=utf-8');
50
51 if ($params->{'action'} eq 'add') {
52     my $sth = $dbh->prepare('INSERT INTO quotes (source, text) VALUES (?, ?);');
53     $sth->execute($params->{'source'}, $params->{'text'});
54     if ($sth->err) {
55         warn sprintf('Database returned the following error: %s', $sth->errstr);
56         exit 0;
57     }
58     my $new_quote_id = $dbh->{q{mysql_insertid}}; # ALERT: mysqlism here
59     $sth = $dbh->prepare('SELECT * FROM quotes WHERE id = ?;');
60     $sth->execute($new_quote_id);
61     print to_json($sth->fetchall_arrayref, {utf8 =>1});
62     exit 1;
63 }
64 elsif ($params->{'action'} eq 'edit') {
65     my $aaData = [];
66     my $editable_columns = [qw(source text)]; # pay attention to element order; these columns match the quotes table columns
67     my $sth = $dbh->prepare("UPDATE quotes SET $editable_columns->[$params->{'column'}-1]  = ? WHERE id = ?;");
68     $sth->execute($params->{'value'}, $params->{'id'});
69     if ($sth->err) {
70         warn sprintf('Database returned the following error: %s', $sth->errstr);
71         exit 0;
72     }
73     $sth = $dbh->prepare("SELECT $editable_columns->[$params->{'column'}-1] FROM quotes WHERE id = ?;");
74     $sth->execute($params->{'id'});
75     $aaData = $sth->fetchrow_array();
76     print Encode::encode('utf8', $aaData);
77
78     exit 1;
79 }
80 elsif ($params->{'action'} eq 'delete') {
81     my $sth = $dbh->prepare("DELETE FROM quotes WHERE id = ?;");
82     $sth->execute($params->{'id'});
83     if ($sth->err) {
84         warn sprintf('Database returned the following error: %s', $sth->errstr);
85         exit 0;
86     }
87     exit 1;
88 }
89 else {
90     my $aaData = [];
91     my $iTotalRecords = '';
92     my $sth = '';
93
94     $iTotalRecords = $dbh->selectrow_array('SELECT count(*) FROM quotes;');
95     $sth = $dbh->prepare("SELECT * FROM quotes;");
96
97     $sth->execute();
98     if ($sth->err) {
99         warn sprintf('Database returned the following error: %s', $sth->errstr);
100         exit 0;
101     }
102
103     $aaData = $sth->fetchall_arrayref;
104     my $iTotalDisplayRecords = $iTotalRecords; # no filtering happening here
105
106
107     print to_json({
108                     iTotalRecords       =>  $iTotalRecords,
109                     iTotalDisplayRecords=>  $iTotalDisplayRecords,
110                     sEcho               =>  $params->{'sEcho'},
111                     aaData              =>  $aaData,
112                   }, {utf8 =>1});
113 }