Bug 9978: Replace license header with the correct license (GPLv3+)
[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
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 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 my $action = $params->{'action'} || 'get';
52 if ($action eq 'add') {
53     my $sth = $dbh->prepare('INSERT INTO quotes (source, text) VALUES (?, ?);');
54     $sth->execute($params->{'source'}, $params->{'text'});
55     if ($sth->err) {
56         warn sprintf('Database returned the following error: %s', $sth->errstr);
57         exit 1;
58     }
59     my $new_quote_id = $dbh->{q{mysql_insertid}}; # ALERT: mysqlism here
60     $sth = $dbh->prepare('SELECT * FROM quotes WHERE id = ?;');
61     $sth->execute($new_quote_id);
62     print to_json($sth->fetchall_arrayref, {utf8 =>1});
63     exit 0;
64 }
65 elsif ($action eq 'edit') {
66     my $aaData = [];
67     my $editable_columns = [qw(source text)]; # pay attention to element order; these columns match the quotes table columns
68     my $sth = $dbh->prepare("UPDATE quotes SET $editable_columns->[$params->{'column'}-1]  = ? WHERE id = ?;");
69     $sth->execute($params->{'value'}, $params->{'id'});
70     if ($sth->err) {
71         warn sprintf('Database returned the following error: %s', $sth->errstr);
72         exit 1;
73     }
74     $sth = $dbh->prepare("SELECT $editable_columns->[$params->{'column'}-1] FROM quotes WHERE id = ?;");
75     $sth->execute($params->{'id'});
76     $aaData = $sth->fetchrow_array();
77     print Encode::encode('utf8', $aaData);
78
79     exit 0;
80 }
81 elsif ($action eq 'delete') {
82     my $sth = $dbh->prepare("DELETE FROM quotes WHERE id = ?;");
83     $sth->execute($params->{'id'});
84     if ($sth->err) {
85         warn sprintf('Database returned the following error: %s', $sth->errstr);
86         exit 1;
87     }
88     exit 0;
89 }
90 else {
91     my $aaData = [];
92     my $iTotalRecords = '';
93     my $sth = '';
94
95     $iTotalRecords = $dbh->selectrow_array('SELECT count(*) FROM quotes;');
96     $sth = $dbh->prepare("SELECT * FROM quotes;");
97
98     $sth->execute();
99     if ($sth->err) {
100         warn sprintf('Database returned the following error: %s', $sth->errstr);
101         exit 1;
102     }
103
104     $aaData = $sth->fetchall_arrayref;
105     my $iTotalDisplayRecords = $iTotalRecords; # no filtering happening here
106
107
108     print to_json({
109                     iTotalRecords       =>  $iTotalRecords,
110                     iTotalDisplayRecords=>  $iTotalDisplayRecords,
111                     sEcho               =>  $params->{'sEcho'},
112                     aaData              =>  $aaData,
113                   }, {utf8 =>1});
114 }