From 579df1b93f5fd3292e01ae38d5eb89cdca496c8a Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 16 Dec 2020 12:34:43 +0100 Subject: [PATCH] Bug 27251: Rewrite QOTD with the Koha REST API This patch replace the QOTD editor with our new way to CRUD the adminitration page (like libraries and STMP servers) Test plan: Play with the QOTD by adding, removing, updating quotes Try to find bugs :) Bug 27251: Fix capitalization Signed-off-by: Andrew Fuerste-Henry Signed-off-by: Katrin Fischer --- Koha/Quote.pm | 18 +- Koha/REST/V1/Quotes.pm | 139 ++++++++ api/v1/swagger/definitions.json | 3 + api/v1/swagger/definitions/quote.json | 22 ++ api/v1/swagger/parameters.json | 3 + api/v1/swagger/parameters/quote.json | 9 + api/v1/swagger/paths.json | 6 + api/v1/swagger/paths/quotes.json | 327 ++++++++++++++++++ api/v1/swagger/x-primitives.json | 6 +- .../prog/en/includes/quotes-toolbar.inc | 5 - .../prog/en/includes/quotes-upload-toolbar.inc | 5 - .../intranet-tmpl/prog/en/modules/intranet-main.tt | 2 +- .../prog/en/modules/tools/quotes-upload.tt | 84 ++--- .../intranet-tmpl/prog/en/modules/tools/quotes.tt | 378 +++++++++++---------- tools/quotes.pl | 77 ++++- tools/quotes/quotes-upload_ajax.pl | 65 ---- tools/quotes/quotes_ajax.pl | 109 ------ 17 files changed, 833 insertions(+), 425 deletions(-) create mode 100644 Koha/REST/V1/Quotes.pm create mode 100644 api/v1/swagger/definitions/quote.json create mode 100644 api/v1/swagger/parameters/quote.json create mode 100644 api/v1/swagger/paths/quotes.json delete mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/quotes-toolbar.inc delete mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/quotes-upload-toolbar.inc delete mode 100755 tools/quotes/quotes-upload_ajax.pl delete mode 100755 tools/quotes/quotes_ajax.pl diff --git a/Koha/Quote.pm b/Koha/Quote.pm index acb844d3af..49868aa2a3 100644 --- a/Koha/Quote.pm +++ b/Koha/Quote.pm @@ -33,6 +33,22 @@ Koha::Quote - Koha Quote object class =cut +=head3 to_api_mapping + +This method returns the mapping for representing a Koha::Quote object +on the API. + +=cut + +sub to_api_mapping { + return { + id => 'quote_id', + source => 'source', + text => 'text', + timestamp => 'displayed_on', + }; +} + =head3 _type =cut @@ -41,4 +57,4 @@ sub _type { return 'Quote'; } -1; \ No newline at end of file +1; diff --git a/Koha/REST/V1/Quotes.pm b/Koha/REST/V1/Quotes.pm new file mode 100644 index 0000000000..49c6e4cab6 --- /dev/null +++ b/Koha/REST/V1/Quotes.pm @@ -0,0 +1,139 @@ +package Koha::REST::V1::Quotes; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Controller'; + +use Koha::Quotes; + +use Try::Tiny; + +=head1 API + +=head2 Methods + +=head3 list + +=cut + +sub list { + my $c = shift->openapi->valid_input or return; + + return try { + my $quotes_set = Koha::Quotes->new; + my $quotes = $c->objects->search( $quotes_set ); + return $c->render( status => 200, openapi => $quotes ); + } + catch { + $c->unhandled_exception($_); + }; + +} + +=head3 get + +=cut + +sub get { + my $c = shift->openapi->valid_input or return; + + return try { + my $quote = Koha::Quotes->find( $c->validation->param('quote_id') ); + unless ($quote) { + return $c->render( status => 404, + openapi => { error => "quote not found" } ); + } + + return $c->render( status => 200, openapi => $quote->to_api ); + } + catch { + $c->unhandled_exception($_); + } +} + +=head3 add + +=cut + +sub add { + my $c = shift->openapi->valid_input or return; + + return try { + my $quote = Koha::Quote->new_from_api( $c->validation->param('body') ); + $quote->store; + $c->res->headers->location( $c->req->url->to_string . '/' . $quote->id ); + return $c->render( + status => 201, + openapi => $quote->to_api + ); + } + catch { + $c->unhandled_exception($_); + }; +} + +=head3 update + +=cut + +sub update { + my $c = shift->openapi->valid_input or return; + + my $quote = Koha::Quotes->find( $c->validation->param('quote_id') ); + + if ( not defined $quote ) { + return $c->render( status => 404, + openapi => { error => "Object not found" } ); + } + + return try { + $quote->set_from_api( $c->validation->param('body') ); + $quote->store(); + return $c->render( status => 200, openapi => $quote->to_api ); + } + catch { + $c->unhandled_exception($_); + }; +} + +=head3 delete + +=cut + +sub delete { + my $c = shift->openapi->valid_input or return; + + my $quote = Koha::Quotes->find( $c->validation->param('quote_id') ); + if ( not defined $quote ) { + return $c->render( status => 404, + openapi => { error => "Object not found" } ); + } + + return try { + $quote->delete; + return $c->render( + status => 204, + openapi => q{} + ); + } + catch { + $c->unhandled_exception($_); + }; +} + +1; diff --git a/api/v1/swagger/definitions.json b/api/v1/swagger/definitions.json index e35054a456..bc844bdc8f 100644 --- a/api/v1/swagger/definitions.json +++ b/api/v1/swagger/definitions.json @@ -65,6 +65,9 @@ "patron_balance": { "$ref": "definitions/patron_balance.json" }, + "quote": { + "$ref": "definitions/quote.json" + }, "allows_renewal": { "$ref": "definitions/allows_renewal.json" }, diff --git a/api/v1/swagger/definitions/quote.json b/api/v1/swagger/definitions/quote.json new file mode 100644 index 0000000000..8033f2af66 --- /dev/null +++ b/api/v1/swagger/definitions/quote.json @@ -0,0 +1,22 @@ +{ + "type": "object", + "properties": { + "quote_id": { + "$ref": "../x-primitives.json#/quote_id" + }, + "source": { + "description": "source of the quote", + "type": "string" + }, + "text": { + "description": "text", + "type": ["string", "null"] + }, + "displayed_on": { + "description": "Last display date", + "type": ["string", "null"] + } + }, + "additionalProperties": false, + "required": ["quote_id", "source", "text"] +} diff --git a/api/v1/swagger/parameters.json b/api/v1/swagger/parameters.json index ddced814d0..8fc892a6af 100644 --- a/api/v1/swagger/parameters.json +++ b/api/v1/swagger/parameters.json @@ -32,6 +32,9 @@ "order_id_pp": { "$ref": "parameters/order.json#/order_id_pp" }, + "quote_id_pp": { + "$ref": "parameters/quote.json#/quote_id_pp" + }, "smtp_server_id_pp": { "$ref": "parameters/smtp_server.json#/smtp_server_id_pp" }, diff --git a/api/v1/swagger/parameters/quote.json b/api/v1/swagger/parameters/quote.json new file mode 100644 index 0000000000..c7f36fcbc3 --- /dev/null +++ b/api/v1/swagger/parameters/quote.json @@ -0,0 +1,9 @@ +{ + "quote_id_pp": { + "name": "quote_id", + "in": "path", + "description": "Quote internal identifier", + "required": true, + "type": "integer" + } +} diff --git a/api/v1/swagger/paths.json b/api/v1/swagger/paths.json index 3b5dc352d5..e046c57a22 100644 --- a/api/v1/swagger/paths.json +++ b/api/v1/swagger/paths.json @@ -134,6 +134,12 @@ "/public/patrons/{patron_id}/guarantors/can_see_checkouts": { "$ref": "paths/public_patrons.json#/~1public~1patrons~1{patron_id}~1guarantors~1can_see_checkouts" }, + "/quotes": { + "$ref": "paths/quotes.json#/~1quotes" + }, + "/quotes/{quote_id}": { + "$ref": "paths/quotes.json#/~1quotes~1{quote_id}" + }, "/return_claims": { "$ref": "paths/return_claims.json#/~1return_claims" }, diff --git a/api/v1/swagger/paths/quotes.json b/api/v1/swagger/paths/quotes.json new file mode 100644 index 0000000000..ac2a503fe0 --- /dev/null +++ b/api/v1/swagger/paths/quotes.json @@ -0,0 +1,327 @@ +{ + "/quotes": { + "get": { + "x-mojo-to": "Quotes#list", + "operationId": "listQuotes", + "tags": [ + "quotes" + ], + "produces": [ + "application/json" + ], + "parameters": [ + { + "name": "quote_id", + "in": "query", + "description": "Case insensitive search on quote id", + "required": false, + "type": "string" + }, + { + "name": "source", + "in": "query", + "description": "Case insensitive search on source", + "required": false, + "type": "string" + }, + { + "name": "text", + "in": "query", + "description": "Case insensitive search on text", + "required": false, + "type": "string" + }, + { + "name": "displayed_on", + "in": "query", + "description": "Case Insensative search on last displayed date", + "required": false, + "type": "string" + }, + { + "$ref": "../parameters.json#/match" + }, + { + "$ref": "../parameters.json#/order_by" + }, + { + "$ref": "../parameters.json#/page" + }, + { + "$ref": "../parameters.json#/per_page" + }, + { + "$ref": "../parameters.json#/q_param" + }, + { + "$ref": "../parameters.json#/q_body" + }, + { + "$ref": "../parameters.json#/q_header" + } + ], + "responses": { + "200": { + "description": "A list of quotes", + "schema": { + "type": "array", + "items": { + "$ref": "../definitions.json#/quote" + } + } + }, + "403": { + "description": "Access forbidden", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "500": { + "description": "Internal error", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "503": { + "description": "Under maintenance", + "schema": { + "$ref": "../definitions.json#/error" + } + } + }, + "x-koha-authorization": { + "permissions": { + "catalogue": "1" + } + } + }, + "post": { + "x-mojo-to": "Quotes#add", + "operationId": "addQuote", + "tags": [ + "quotes" + ], + "parameters": [ + { + "name": "body", + "in": "body", + "description": "A JSON object containing informations about the new quote", + "required": true, + "schema": { + "$ref": "../definitions.json#/quote" + } + } + ], + "produces": [ + "application/json" + ], + "responses": { + "201": { + "description": "Quote added", + "schema": { + "$ref": "../definitions.json#/quote" + } + }, + "401": { + "description": "Authentication required", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "403": { + "description": "Access forbidden", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "500": { + "description": "Internal error", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "503": { + "description": "Under maintenance", + "schema": { + "$ref": "../definitions.json#/error" + } + } + }, + "x-koha-authorization": { + "permissions": { + "tools": "edit_quotes" + } + } + } + }, + "/quotes/{quote_id}": { + "get": { + "x-mojo-to": "Quotes#get", + "operationId": "getQuote", + "tags": [ + "quotes" + ], + "parameters": [ + { + "$ref": "../parameters.json#/quote_id_pp" + } + ], + "produces": [ + "application/json" + ], + "responses": { + "200": { + "description": "A Quote", + "schema": { + "$ref": "../definitions.json#/quote" + } + }, + "404": { + "description": "Quote not found", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "500": { + "description": "Internal error", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "503": { + "description": "Under maintenance", + "schema": { + "$ref": "../definitions.json#/error" + } + } + }, + "x-koha-authorization": { + "permissions": { + "catalogue": "1" + } + } + }, + "put": { + "x-mojo-to": "Quotes#update", + "operationId": "updateQuote", + "tags": [ + "quotes" + ], + "parameters": [ + { + "$ref": "../parameters.json#/quote_id_pp" + }, + { + "name": "body", + "in": "body", + "description": "a quote object", + "required": true, + "schema": { + "$ref": "../definitions.json#/quote" + } + } + ], + "produces": [ + "application/json" + ], + "responses": { + "200": { + "description": "A quote", + "schema": { + "$ref": "../definitions.json#/quote" + } + }, + "401": { + "description": "Authentication required", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "403": { + "description": "Access forbidden", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "404": { + "description": "Quote not found", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "500": { + "description": "Internal error", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "503": { + "description": "Under maintenance", + "schema": { + "$ref": "../definitions.json#/error" + } + } + }, + "x-koha-authorization": { + "permissions": { + "tools": "edit_quotes" + } + } + }, + "delete": { + "x-mojo-to": "Quotes#delete", + "operationId": "deleteQuote", + "tags": [ + "quotes" + ], + "parameters": [ + { + "$ref": "../parameters.json#/quote_id_pp" + } + ], + "produces": [ + "application/json" + ], + "responses": { + "204": { + "description": "Quote deleted" + }, + "401": { + "description": "Authentication required", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "403": { + "description": "Access forbidden", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "404": { + "description": "Quote not found", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "500": { + "description": "Internal error", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "503": { + "description": "Under maintenance", + "schema": { + "$ref": "../definitions.json#/error" + } + } + }, + "x-koha-authorization": { + "permissions": { + "tools": "edit_quotes" + } + } + } + } +} diff --git a/api/v1/swagger/x-primitives.json b/api/v1/swagger/x-primitives.json index 0594264be9..13cc1da4fc 100644 --- a/api/v1/swagger/x-primitives.json +++ b/api/v1/swagger/x-primitives.json @@ -52,6 +52,10 @@ "type": "integer", "description": "internally assigned fund identifier", "readOnly": true + }, + "quote_id": { + "type": "integer", + "description": "internally assigned quote identifier", + "readOnly": true } - } diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/quotes-toolbar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/quotes-toolbar.inc deleted file mode 100644 index e63ad8b450..0000000000 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/quotes-toolbar.inc +++ /dev/null @@ -1,5 +0,0 @@ - diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/quotes-upload-toolbar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/quotes-upload-toolbar.inc deleted file mode 100644 index b531be0452..0000000000 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/quotes-upload-toolbar.inc +++ /dev/null @@ -1,5 +0,0 @@ - diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/intranet-main.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/intranet-main.tt index 373c312dd5..0f06f70797 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/intranet-main.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/intranet-main.tt @@ -37,7 +37,7 @@ [% END %] [% IF ( daily_quote ) %]
-

Quote of the Day

+

Quote of the day

[% daily_quote.text | html %] ~ [% daily_quote.source | html %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/quotes-upload.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/quotes-upload.tt index dc5da4b0f2..d5fc8f7585 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/quotes-upload.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/quotes-upload.tt @@ -18,9 +18,18 @@
+ - [% INCLUDE 'quotes-upload-toolbar.inc' %]

Quote uploader

+ + +
Instructions @@ -34,12 +43,12 @@
  • Click on any field to edit the contents; Press the <Enter> key to save edit.
  • -
  • Click on one or more quote numbers to select entire quotes for deletion; Click the 'Delete Quote(s)' button to delete selected quotes.
  • Click the 'Save Quotes' button in the toolbar to save the entire batch of quotes.
+
Upload quotes
@@ -232,7 +241,6 @@ $('#file_uploader').css("top","-150px"); $('#quotes_editor').css("visibility","visible"); $("#save_quotes").on("click", yuiGetData); - $("#delete_quote").on("click", fnClickDeleteRow); oTable = $('#quotes_editor').dataTable( { "bAutoWidth" : false, @@ -262,8 +270,6 @@ /* do foo on various cells in the current row */ var quoteNum = $('td', nRow)[0].innerHTML; $(nRow).attr("id", quoteNum); /* set row ids to quote number */ - $('td:eq(0)', nRow).click(function() {$(this.parentNode).toggleClass('selected',this.clicked);}); /* add row selectors */ - $('td:eq(0)', nRow).attr("title", _("Click ID to select/deselect quote")); /* apply no_edit id to noEditFields */ noEditFields = [0]; /* number */ for (i=0; i [% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/quotes.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/quotes.tt index 5339069a0f..c42839bfd4 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/quotes.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/quotes.tt @@ -1,10 +1,10 @@ [% USE raw %] [% USE Asset %] [% SET footerjs = 1 %] - [% INCLUDE 'doc-head-open.inc' %] - Koha › Tools › Quote editor - [% INCLUDE 'doc-head-close.inc' %] - [% Asset.css("css/quotes.css") | $raw %] +[% INCLUDE 'doc-head-open.inc' %] +Koha › Tools › Quote editor +[% INCLUDE 'doc-head-close.inc' %] +[% Asset.css("css/quotes.css") | $raw %] @@ -18,43 +18,110 @@
- [% INCLUDE 'quotes-toolbar.inc' %] -

Quote editor

-
-
- Instructions -
-
    -
  • Click on the 'Add quote' button to add a single quote; Press the <Enter> key to save the quote.
    - Note: Both the 'source' and 'text' fields must have content in order for the quote to be saved.
  • -
  • Click on any field to edit the contents; Press the <Enter> key to save edit.
  • -
  • Click on one or more quote numbers to select entire quotes for deletion; Click the 'Delete quote(s)' button to delete selected quotes.
  • -
  • Click the 'Import quotes' button in the toolbar to import a CSV file of quotes.
  • -
-
-
+[% FOREACH m IN messages %] +
+ [% SWITCH m.code %] + [% CASE 'error_on_update' %] + An error occurred when updating this quote. Perhaps it already exists. + [% CASE 'error_on_insert' %] + An error occurred when adding this quote. + [% CASE 'success_on_update' %] + Quote updated successfully. + [% CASE 'success_on_insert' %] + Quote added successfully. + [% CASE %] + [% m.code | html %] + [% END %] +
+[% END %] + + + + +[% IF op == 'list' %] + +[% END %] + +[% IF op == 'add_form' %] +

[% IF quote %]Modify quote[% ELSE %]New quote[% END %]

+
+
+ +
    +
  1. + + + Required +
  2. +
  3. + + + Required +
  4. +
+
+
+ + + Cancel +
+
+[% END %] + +[% IF op == 'delete_confirm' %] +
+
+

Are you sure you want to delete the following quote?

+ [% quote.source | html %] - [% quote.text | html %] + + + +
+
+ +
+
+[% END %] + +[% IF op == 'list' %] +

Quotes

+ [% IF quotes_count > 0 %] + + + + + + + + + + +
IDSourceTextLast displayActions
+ [% ELSE %] +
There are no quotes defined. Start defining quotes.
+ [% END %] + + +[% END %]
@@ -68,164 +135,99 @@ [% MACRO jsinclude BLOCK %] [% Asset.js("js/tools-menu.js") | $raw %] + [% INCLUDE 'js-date-format.inc' %] [% INCLUDE 'datatables.inc' %] - [% Asset.js("lib/jquery/plugins/dataTables.fnReloadAjax.js") | $raw %] - [% Asset.js("lib/jquery/plugins/jquery.jeditable.mini.js") | $raw %] + [% END %] diff --git a/tools/quotes.pl b/tools/quotes.pl index a10e5747f7..57859f14b2 100755 --- a/tools/quotes.pl +++ b/tools/quotes.pl @@ -1,7 +1,5 @@ #!/usr/bin/perl -# Copyright 2012 Foundations Bible College Inc. -# # This file is part of Koha. # # Koha is free software; you can redistribute it and/or modify it @@ -20,23 +18,80 @@ use Modern::Perl; use CGI qw ( -utf8 ); -use autouse 'Data::Dumper' => qw(Dumper); +use Try::Tiny; use C4::Auth; -use C4::Koha; use C4::Context; use C4::Output; +use Koha::Quotes; -my $cgi = CGI->new; +my $input = CGI->new; my ( $template, $borrowernumber, $cookie ) = get_template_and_user( { - template_name => "tools/quotes.tt", - query => $cgi, - type => "intranet", - flagsrequired => { tools => 'edit_quotes' }, - debug => 1, + template_name => "tools/quotes.tt", + query => $input, + type => "intranet", + flagsrequired => { tools => 'edit_quotes' }, + debug => 1, } ); -output_html_with_http_headers $cgi, $cookie, $template->output; +my $id = $input->param('id'); +my $op = $input->param('op') || 'list'; +my @messages; + +if ( $op eq 'add_form' ) { + $template->param( quote => Koha::Quotes->find($id), ); +} +elsif ( $op eq 'add_validate' ) { + my @fields = qw( + source + text + ); + + if ($id) { + my $quote = Koha::Quotes->find($id); + for my $field (@fields) { + $quote->$field( scalar $input->param($field) ); + } + + try { + $quote->store; + push @messages, { type => 'message', code => 'success_on_update' }; + } + catch { + push @messages, { type => 'alert', code => 'error_on_update' }; + } + } + else { + my $quote = Koha::Quote->new( + { + id => $id, + ( map { $_ => scalar $input->param($_) || undef } @fields ) + } + ); + + try { + $quote->store; + push @messages, { type => 'message', code => 'success_on_insert' }; + } + catch { + push @messages, { type => 'alert', code => 'error_on_insert' }; + }; + } + $op = 'list'; +} +else { + $op = 'list'; +} + +$template->param( quotes_count => Koha::Quotes->search->count ) + if $op eq 'list'; + +$template->param( + messages => \@messages, + op => $op, +); + +output_html_with_http_headers $input, $cookie, $template->output; diff --git a/tools/quotes/quotes-upload_ajax.pl b/tools/quotes/quotes-upload_ajax.pl deleted file mode 100755 index c988e7c540..0000000000 --- a/tools/quotes/quotes-upload_ajax.pl +++ /dev/null @@ -1,65 +0,0 @@ -#!/usr/bin/perl - -# Copyright 2012 Foundations Bible College Inc. -# -# This file is part of Koha. -# -# Koha is free software; you can redistribute it and/or modify it -# under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3 of the License, or -# (at your option) any later version. -# -# Koha is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Koha; if not, see . - -use Modern::Perl; - -use CGI qw ( -utf8 ); -use JSON; -use URI::Escape; -use autouse 'Data::Dumper' => qw(Dumper); - -use C4::Auth; -use C4::Koha; -use C4::Context; -use C4::Output; - -my $cgi = CGI->new; -my $dbh = C4::Context->dbh; - -my ( $status, $cookie, $sessionID ) = C4::Auth::check_api_auth( $cgi, { tools => 'edit_quotes' } ); -unless ($status eq "ok") { - print $cgi->header(-type => 'application/json', -status => '403 Forbidden'); - print to_json({ auth_status => $status }); - exit 0; -} - -my $success = 'true'; -my $quotes_tmp = uri_unescape( $cgi->param('quote' ) ); -my $quotes = decode_json( $quotes_tmp ); - -my $action = $cgi->param('action'); - -my $sth = $dbh->prepare('INSERT INTO quotes (source, text) VALUES (?, ?);'); - -my $insert_count = 0; - -foreach my $quote (@$quotes) { - $insert_count++ if $sth->execute($quote->[1], $quote->[2]); - if ($sth->err) { - warn sprintf('Database returned the following error: %s', $sth->errstr); - $success = 'false'; - } -} - -print $cgi->header('application/json'); - -print to_json({ - success => $success, - records => $insert_count, -}); diff --git a/tools/quotes/quotes_ajax.pl b/tools/quotes/quotes_ajax.pl deleted file mode 100755 index 79fbe6fb01..0000000000 --- a/tools/quotes/quotes_ajax.pl +++ /dev/null @@ -1,109 +0,0 @@ -#!/usr/bin/perl - -# Copyright 2012 Foundations Bible College Inc. -# -# This file is part of Koha. -# -# Koha is free software; you can redistribute it and/or modify it -# under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3 of the License, or -# (at your option) any later version. -# -# Koha is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Koha; if not, see . - -use Modern::Perl; - -use CGI qw ( -utf8 ); -use JSON; -use autouse 'Data::Dumper' => qw(Dumper); - -use C4::Auth; -use C4::Context; - -my $cgi = CGI->new; -my $dbh = C4::Context->dbh; -my $sort_columns = ["id", "source", "text", "timestamp"]; - -my ( $status, $cookie, $sessionID ) = C4::Auth::check_api_auth( $cgi, { tools => 'edit_quotes' } ); -unless ($status eq "ok") { - print $cgi->header(-type => 'application/json', -status => '403 Forbidden'); - print to_json({ auth_status => $status }); - exit 0; -} - -# NOTE: This is a collection of ajax functions for use with tools/quotes.pl - -my $params = $cgi->Vars; # NOTE: Multivalue parameters NOT allowed!! - -print $cgi->header('application/json; charset=utf-8'); - -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; - } - 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 0; -} -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 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 0; -} -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 1; - } - exit 0; -} -else { - my $aaData = []; - my $iTotalRecords = ''; - my $sth = ''; - - $iTotalRecords = $dbh->selectrow_array('SELECT count(*) FROM quotes;'); - $sth = $dbh->prepare("SELECT * FROM quotes;"); - - $sth->execute(); - if ($sth->err) { - warn sprintf('Database returned the following error: %s', $sth->errstr); - exit 1; - } - - $aaData = $sth->fetchall_arrayref; - my $iTotalDisplayRecords = $iTotalRecords; # no filtering happening here - - - print to_json({ - iTotalRecords => $iTotalRecords, - iTotalDisplayRecords=> $iTotalDisplayRecords, - sEcho => $params->{'sEcho'}, - aaData => $aaData, - }, {utf8 =>1}); -} -- 2.11.0