Bug 9300 - filtering Export (MARC) data by accession date does not work
[koha.git] / tools / quotes / quotes_ajax.pl
blobda5dc01ef91c83b98e66635eb7b8db2b27318a10
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 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.
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.
20 use strict;
21 use warnings;
23 use CGI;
24 use JSON;
25 use autouse 'Data::Dumper' => qw(Dumper);
27 use C4::Auth;
28 use C4::Context;
30 my $cgi = CGI->new;
31 my $dbh = C4::Context->dbh;
32 my $sort_columns = ["id", "source", "text", "timestamp"];
34 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36 template_name => "",
37 query => $cgi,
38 type => "intranet",
39 authnotrequired => 0,
40 flagsrequired => { tools => 'edit_quotes' },
41 debug => 1,
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 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;
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;
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;
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);
78 exit 1;
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;
87 exit 1;
89 else {
90 my $aaData = [];
91 my $iTotalRecords = '';
92 my $sth = '';
94 $iTotalRecords = $dbh->selectrow_array('SELECT count(*) FROM quotes;');
95 $sth = $dbh->prepare("SELECT * FROM quotes;");
97 $sth->execute();
98 if ($sth->err) {
99 warn sprintf('Database returned the following error: %s', $sth->errstr);
100 exit 0;
103 $aaData = $sth->fetchall_arrayref;
104 my $iTotalDisplayRecords = $iTotalRecords; # no filtering happening here
107 print to_json({
108 iTotalRecords => $iTotalRecords,
109 iTotalDisplayRecords=> $iTotalDisplayRecords,
110 sEcho => $params->{'sEcho'},
111 aaData => $aaData,
112 }, {utf8 =>1});