Bug 13885: Cataloging search header should include more options
[koha.git] / tools / quotes / quotes_ajax.pl
blobd08178f9136bf919420386e6dfe5ef5dd52de583
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
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.
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.
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>.
20 use strict;
21 use warnings;
23 use CGI qw ( -utf8 );
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 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;
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;
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;
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);
79 exit 0;
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;
88 exit 0;
90 else {
91 my $aaData = [];
92 my $iTotalRecords = '';
93 my $sth = '';
95 $iTotalRecords = $dbh->selectrow_array('SELECT count(*) FROM quotes;');
96 $sth = $dbh->prepare("SELECT * FROM quotes;");
98 $sth->execute();
99 if ($sth->err) {
100 warn sprintf('Database returned the following error: %s', $sth->errstr);
101 exit 1;
104 $aaData = $sth->fetchall_arrayref;
105 my $iTotalDisplayRecords = $iTotalRecords; # no filtering happening here
108 print to_json({
109 iTotalRecords => $iTotalRecords,
110 iTotalDisplayRecords=> $iTotalDisplayRecords,
111 sEcho => $params->{'sEcho'},
112 aaData => $aaData,
113 }, {utf8 =>1});