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 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
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.
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.
25 use autouse 'Data::Dumper' => qw(Dumper);
31 my $dbh = C4::Context->dbh;
32 my $sort_columns = ["id", "source", "text", "timestamp"];
34 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
40 flagsrequired => { tools => 'edit_quotes' },
45 # NOTE: This is a collection of ajax functions for use with tools/quotes.pl
47 my $params = $cgi->Vars; # NOTE: Multivalue parameters NOT allowed!!
49 print $cgi->header('application/json; charset=utf-8');
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'});
56 warn sprintf('Database returned the following error: %s', $sth->errstr);
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});
65 elsif ($action eq 'edit') {
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'});
71 warn sprintf('Database returned the following error: %s', $sth->errstr);
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);
81 elsif ($action eq 'delete') {
82 my $sth = $dbh->prepare("DELETE FROM quotes WHERE id = ?;");
83 $sth->execute($params->{'id'});
85 warn sprintf('Database returned the following error: %s', $sth->errstr);
92 my $iTotalRecords = '';
95 $iTotalRecords = $dbh->selectrow_array('SELECT count(*) FROM quotes;');
96 $sth = $dbh->prepare("SELECT * FROM quotes;");
100 warn sprintf('Database returned the following error: %s', $sth->errstr);
104 $aaData = $sth->fetchall_arrayref;
105 my $iTotalDisplayRecords = $iTotalRecords; # no filtering happening here
109 iTotalRecords => $iTotalRecords,
110 iTotalDisplayRecords=> $iTotalDisplayRecords,
111 sEcho => $params->{'sEcho'},