From 374db28eee937cc363a5dfef7884a3ff4302c6f6 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 21 Feb 2024 11:46:42 +0100 Subject: [PATCH] Bug 36084: svc - cover_images Removing the ability to remove several cover images, it was not used. FIXME - Note that I am getting randomly a failure when uploading an image and seeing the following error in the log: [Wed Feb 21 10:43:59.168934 2024] [cgi:error] [pid 24037] [client 172.18.0.1:45074] AH01215: Use of uninitialized value $fileID in bitwise and (&) at /kohadevbox/koha/tools/upload-cover-image.pl line 101.: /kohadevbox/koha/tools/upload-cover-image.pl, referer: http://localhost:8081/cgi-bin/koha/catalogue/detail.pl?biblionumber=1 Signed-off-by: Jonathan Druart --- .../prog/en/modules/catalogue/detail.tt | 31 +++++++------- .../prog/en/modules/catalogue/imageviewer.tt | 29 +++++++------- .../intranet-tmpl/prog/js/fetch/api-client.js | 2 + .../prog/js/fetch/cover-image-api-client.js | 25 ++++++++++++ .../intranet-tmpl/prog/js/upload-images.js | 28 ++++++------- svc/cover_images | 40 +++++++++---------- 6 files changed, 89 insertions(+), 66 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/js/fetch/cover-image-api-client.js diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt index 565f2b7015..863ae272f2 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt @@ -1596,26 +1596,25 @@ var copy = thumbnail.html(); thumbnail.find("img").css("opacity", ".2"); thumbnail.find("a.remove").html(""); - $.ajax({ - url: "/cgi-bin/koha/svc/cover_images?action=delete&imagenumber=" + imagenumber, - success: function(data) { - $(data).each( function(i) { - if ( this.deleted == 1 ) { - thumbnail.remove(); - } else { - thumbnail.html( copy ); - alert(_("An error occurred on deleting this image")); - } - if ( $('ul.thumbnails > li').length == 0 ) { - showNoImageMessage(); - } - }); + const client = APIClient.cover_image; + client.cover_images.delete(imagenumber).then( + success => { + if ( success.deleted == 1 ) { + thumbnail.remove(); + } else { + thumbnail.html( copy ); + alert(_("An error occurred on deleting this image")); + } + if ( $('ul.thumbnails > li').length == 0 ) { + showNoImageMessage(); + } }, - error: function(data) { + error => { thumbnail.html( copy ); alert(_("An error occurred on deleting this image")); + console.warn("Something wrong happened: %s".format(error)); } - }); + ); } function showNoImageMessage() { diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/imageviewer.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/imageviewer.tt index 02a0d0e8ae..2a32fa8768 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/imageviewer.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/imageviewer.tt @@ -185,27 +185,26 @@ thumbnail.find("img").css("opacity", ".2"); thumbnail.find("a.remove").html(""); - $.ajax({ - url: "/cgi-bin/koha/svc/cover_images?action=delete&imagenumber=" + imagenumber, - success: function(data) { - $(data).each( function() { - if ( this.deleted == 1 ) { - if( itemnumber ) { - location.href="/cgi-bin/koha/catalogue/imageviewer.pl?itemnumber=" + itemnumber; - } else { - location.href="/cgi-bin/koha/catalogue/imageviewer.pl?biblionumber=" + biblionumber; - } + const client = APIClient.cover_image; + client.cover_images.delete(imagenumber).then( + success => { + if ( success.deleted == 1 ) { + if( itemnumber ) { + location.href="/cgi-bin/koha/catalogue/imageviewer.pl?itemnumber=" + itemnumber; } else { - thumbnail.html( copy ); - alert(_("An error occurred on deleting this image")); + location.href="/cgi-bin/koha/catalogue/imageviewer.pl?biblionumber=" + biblionumber; } - }); + } else { + thumbnail.html( copy ); + alert(_("An error occurred on deleting this image")); + } }, - error: function(data) { + error => { thumbnail.html( copy ); alert(_("An error occurred on deleting this image")); + console.warn("Something wrong happened: %s".format(error)); } - }); + ); } function showCover(img) { diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js index 40abda2dc6..03be692d8f 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js @@ -2,6 +2,7 @@ import ArticleRequestAPIClient from "./article-request-api-client.js"; import AVAPIClient from "./authorised-value-api-client.js"; import CirculationAPIClient from "./circulation-api-client.js"; import ClubAPIClient from "./club-api-client.js"; +import CoverImageAPIClient from "./cover-image-api-client.js"; import SysprefAPIClient from "./system-preferences-api-client.js"; export const APIClient = { @@ -9,5 +10,6 @@ export const APIClient = { authorised_value: new AVAPIClient(), circulation: new CirculationAPIClient(), club: new ClubAPIClient(), + cover_image: new CoverImageAPIClient(), syspref: new SysprefAPIClient(), }; diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/cover-image-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/cover-image-api-client.js new file mode 100644 index 0000000000..2d6c0a03cd --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/cover-image-api-client.js @@ -0,0 +1,25 @@ +import HttpClient from "./http-client.js"; + +export class CoverImageAPIClient extends HttpClient { + constructor() { + super({ + baseURL: "/cgi-bin/koha/svc/cover_images", + }); + } + + get cover_images() { + return { + delete: image_id => + this.post({ + endpoint: "", + body: "imagenumber=%s&op=%s".format(image_id, "cud-delete"), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + }; + } +} + +export default CoverImageAPIClient; diff --git a/koha-tmpl/intranet-tmpl/prog/js/upload-images.js b/koha-tmpl/intranet-tmpl/prog/js/upload-images.js index ee478e6047..b2349a55ef 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/upload-images.js +++ b/koha-tmpl/intranet-tmpl/prog/js/upload-images.js @@ -124,6 +124,7 @@ function AjaxUpload ( formData, progressbar, xtra, callback ) { progressbar.val( 0 ); progressbar.next('.fileuploadpercent').text( '0' ); xhr.open('POST', url, true); + xhr.setRequestHeader("csrf_token", $('meta[name="csrf-token"]').attr("content")); xhr.upload.onprogress = function (e) { var p = Math.round( (e.loaded/e.total) * 100 ); progressbar.val( p ); @@ -203,23 +204,22 @@ function removeLocalImage(imagenumber) { thumbnail.find("img").css("opacity", ".2"); thumbnail.find("a.remove").html(""); - $.ajax({ - url: "/cgi-bin/koha/svc/cover_images?action=delete&biblionumber=" + biblionumber + "&imagenumber=" + imagenumber, - success: function(data) { - $(data).each( function() { - if ( this.deleted == 1 ) { - location.href="/cgi-bin/koha/tools/upload-cover-image.pl?biblionumber=" + biblionumber; - } else { - thumbnail.html( copy ); - alert(__("An error occurred on deleting this image")); - } - }); + const client = APIClient.cover_image; + client.cover_images.delete(imagenumber).then( + success => { + if ( success.deleted == 1 ) { + location.href="/cgi-bin/koha/tools/upload-cover-image.pl?biblionumber=" + biblionumber; + } else { + thumbnail.html( copy ); + alert(_("An error occurred on deleting this image")); + } }, - error: function() { + error => { thumbnail.html( copy ); - alert(__("An error occurred on deleting this image")); + alert(_("An error occurred on deleting this image")); + console.warn("Something wrong happened: %s".format(error)); } - }); + ); } function resetForm(){ diff --git a/svc/cover_images b/svc/cover_images index a1d487a664..629c19368c 100755 --- a/svc/cover_images +++ b/svc/cover_images @@ -36,33 +36,31 @@ if ( $auth_status ne "ok" ) { exit 0; } -my $action = $input->param('action'); -my @imagenumbers = $input->multi_param('imagenumber'); +my $op = $input->param('op') || q{}; +my $imagenumber = $input->param('imagenumber'); -# Array to store the reponse JSON -my $response = []; +my $response = q{}; -if ( $action eq "delete" ) { +if ( $op eq "cud-delete" ) { - foreach my $imagenumber ( @imagenumbers ) { - eval { - Koha::CoverImages->find($imagenumber)->delete; + eval { + Koha::CoverImages->find($imagenumber)->delete; + }; + if ( $@ ) { + $response = { + imagenumber => $imagenumber, + deleted => 0, + error => "MSG_INVALID_IMAGENUMBER" + }; + } else { + $response = { + imagenumber => $imagenumber, + deleted => 1 }; - if ( $@ ) { - push @$response, { - imagenumber => $imagenumber, - deleted => 0, - error => "MSG_INVALID_IMAGENUMBER" - }; - } else { - push @$response, { - imagenumber => $imagenumber, - deleted => 1 - }; - } } } else { - # invalid action + # FIXME be nicer! + # invalid op exit 0; } -- 2.39.5