Bug 7977: Followup patch addressing items pointed out by Jonathan Druart
[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');
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);
62     exit 1;
63 }
64 elsif ($params->{'action'} eq 'edit') {
65     my $editable_columns = [qw(source text)]; # pay attention to element order; these columns match the quotes table columns
66     my $sth = $dbh->prepare("UPDATE quotes SET $editable_columns->[$params->{'column'}-1]  = ? WHERE id = ?;");
67     $sth->execute($params->{'value'}, $params->{'id'});
68     if ($sth->err) {
69         warn sprintf('Database returned the following error: %s', $sth->errstr);
70         exit 0;
71     }
72     $sth = $dbh->prepare("SELECT $editable_columns->[$params->{'column'}-1] FROM quotes WHERE id = ?;");
73     $sth->execute($params->{'id'});
74     print $sth->fetchrow_array();
75     exit 1;
76 }
77 elsif ($params->{'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 0;
83     }
84     exit 1;
85 }
86 else {
87     my $aaData = [];
88     my $iTotalRecords = '';
89     my $sth = '';
90
91     if (my $filter = $params->{'sSearch'}) {
92         # This seems a bit brute force and ungraceful, but it provides a very general, simple search on all fields
93         $iTotalRecords = $dbh->selectrow_array("SELECT count(*) FROM quotes WHERE id LIKE %?% OR source LIKE %?% OR text LIKE %?% OR timestamp LIKE %?%;",undef,($filter, $filter, $filter, $filter));
94         $sth = $dbh->prepare("SELECT * FROM quotes;");
95     }
96     else {
97         $iTotalRecords = $dbh->selectrow_array('SELECT count(*) FROM quotes;');
98         $sth = $dbh->prepare("SELECT * FROM quotes;");
99     }
100
101     $sth->execute();
102     if ($sth->err) {
103         warn sprintf('Database returned the following error: %s', $sth->errstr);
104         exit 0;
105     }
106
107     $aaData = $sth->fetchall_arrayref;
108     my $iTotalDisplayRecords = $iTotalRecords; # no filtering happening here
109
110
111     print to_json({
112                     iTotalRecords       =>  $iTotalRecords,
113                     iTotalDisplayRecords=>  $iTotalDisplayRecords,
114                     sEcho               =>  $params->{'sEcho'},
115                     aaData              =>  $aaData,
116                   });
117 }