Bug 9048 - fix quote editor under Plack
authorRobin Sheat <robin@catalyst.net.nz>
Wed, 30 Apr 2014 03:33:44 +0000 (15:33 +1200)
committerGalen Charlton <gmc@esilibrary.com>
Thu, 8 May 2014 16:15:27 +0000 (16:15 +0000)
The ajax responder for the quote editor was using the wrong error codes.
These have been fixed. Also, a small fixup to get rid of some annoying
warnings.

To test:
* Under plack,
* Add/edit/delete a quote.
* Make sure that things don't crash.

Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
tools/quotes/quotes_ajax.pl

index da5dc01..5f44a7e 100755 (executable)
@@ -48,43 +48,44 @@ my $params = $cgi->Vars; # NOTE: Multivalue parameters NOT allowed!!
 
 print $cgi->header('application/json; charset=utf-8');
 
-if ($params->{'action'} eq 'add') {
+my $action = $params->{'action'} || 'get';
+if ($action eq 'add') {
     my $sth = $dbh->prepare('INSERT INTO quotes (source, text) VALUES (?, ?);');
     $sth->execute($params->{'source'}, $params->{'text'});
     if ($sth->err) {
         warn sprintf('Database returned the following error: %s', $sth->errstr);
-        exit 0;
+        exit 1;
     }
     my $new_quote_id = $dbh->{q{mysql_insertid}}; # ALERT: mysqlism here
     $sth = $dbh->prepare('SELECT * FROM quotes WHERE id = ?;');
     $sth->execute($new_quote_id);
     print to_json($sth->fetchall_arrayref, {utf8 =>1});
-    exit 1;
+    exit 0;
 }
-elsif ($params->{'action'} eq 'edit') {
+elsif ($action eq 'edit') {
     my $aaData = [];
     my $editable_columns = [qw(source text)]; # pay attention to element order; these columns match the quotes table columns
     my $sth = $dbh->prepare("UPDATE quotes SET $editable_columns->[$params->{'column'}-1]  = ? WHERE id = ?;");
     $sth->execute($params->{'value'}, $params->{'id'});
     if ($sth->err) {
         warn sprintf('Database returned the following error: %s', $sth->errstr);
-        exit 0;
+        exit 1;
     }
     $sth = $dbh->prepare("SELECT $editable_columns->[$params->{'column'}-1] FROM quotes WHERE id = ?;");
     $sth->execute($params->{'id'});
     $aaData = $sth->fetchrow_array();
     print Encode::encode('utf8', $aaData);
 
-    exit 1;
+    exit 0;
 }
-elsif ($params->{'action'} eq 'delete') {
+elsif ($action eq 'delete') {
     my $sth = $dbh->prepare("DELETE FROM quotes WHERE id = ?;");
     $sth->execute($params->{'id'});
     if ($sth->err) {
         warn sprintf('Database returned the following error: %s', $sth->errstr);
-        exit 0;
+        exit 1;
     }
-    exit 1;
+    exit 0;
 }
 else {
     my $aaData = [];
@@ -97,7 +98,7 @@ else {
     $sth->execute();
     if ($sth->err) {
         warn sprintf('Database returned the following error: %s', $sth->errstr);
-        exit 0;
+        exit 1;
     }
 
     $aaData = $sth->fetchall_arrayref;