From 8ebb11b244cc272e1d94e7a15fc15a3c75395a40 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 21 Feb 2024 13:30:29 +0100 Subject: [PATCH] Bug 36084: svc - creator_batches No desire to have a nice api client for this. This patch also: Improve failure handling Remove unecessary code in svc script Remove duplicated on click binding Signed-off-by: Jonathan Druart --- .../en/modules/labels/label-edit-batch.tt | 7 ++-- .../prog/en/modules/patroncards/edit-batch.tt | 32 +++---------------- svc/creator_batches | 32 ++++++++----------- 3 files changed, 23 insertions(+), 48 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-edit-batch.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-edit-batch.tt index 4c7d235d56..73855dbef5 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-edit-batch.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-edit-batch.tt @@ -375,8 +375,8 @@ var ajaxData = { 'newdescription': newdescription, 'batch_id': batch_id, - 'card_element': "batch", - 'creator': "label", + op: 'cud-set_permission', + csrf_token: $('meta[name="csrf-token"]').attr("content"), }; $.ajax({ @@ -385,7 +385,6 @@ dataType: 'json', data: ajaxData, }) - .done(function(data){ if (data.status == 'success') { $("input[name='description']").text(data.newdesc); @@ -393,6 +392,8 @@ } else { $("#change-status").text(_("Unable to save description")); } + }).fail(function(){ + $("#change-status").text(_("Unable to save description")); }); }); }); diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/patroncards/edit-batch.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/patroncards/edit-batch.tt index cc92019c8a..11270294ec 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/patroncards/edit-batch.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/patroncards/edit-batch.tt @@ -353,40 +353,16 @@ var batch_id = $(this).data("batch-id"); GB_showCenter( _("Export single card"),'/cgi-bin/koha/patroncards/print.pl?batch_id=' + batch_id + '&label_id=' + label_id, 400, 800); }); - $("#savedesc").click(function(event){ - var newdescription = $(this).siblings('input[name="description"]').val(); - var batch_id = $(this).data('batch_id'); - var ajaxData = { - 'newdescription': newdescription, - 'batch_id': batch_id, - 'card_element': "batch", - 'creator': "patroncard", - }; - - $.ajax({ - url: '/cgi-bin/koha/svc/creator_batches', - type: 'POST', - dataType: 'json', - data: ajaxData, - }) - .done(function(data){ - if (data.status == 'success') { - $("input[name='description']").text(data.newdesc); - $("#change-status").text(_("Saved")); - } else { - $("#change-status").text(_("Unable to save description")); - } - }); - }); $("#savedesc").click(function(event){ + event.preventDefault(); // prevent form submission var newdescription = $('input[name="description"]').val(); var batch_id = $(this).data('batch_id'); var ajaxData = { 'newdescription': newdescription, 'batch_id': batch_id, - 'card_element': "batch", - 'creator': "patroncard", + op: 'cud-set_permission', + csrf_token: $('meta[name="csrf-token"]').attr("content"), }; $.ajax({ @@ -403,6 +379,8 @@ } else { $("#change-status").text(_("Unable to save description")); } + }).fail(function(){ + $("#change-status").text(_("Unable to save description")); }); }); }); diff --git a/svc/creator_batches b/svc/creator_batches index 1e2e4b3eb7..8db67423a7 100755 --- a/svc/creator_batches +++ b/svc/creator_batches @@ -44,30 +44,26 @@ if ( $auth_status ne "ok" ) { exit 0; } -if ($is_ajax) { +my $op = $cgi->param('op') || q{}; +if ($is_ajax && $op eq 'cud-set_permission') { my $batch_id = $cgi->param('batch_id'); my $description = $cgi->param('newdescription'); - my $status = ''; my $dbh = C4::Context->dbh; - my $query = "UPDATE creator_batches SET description = ? WHERE batch_id = ?"; - my $sth = $dbh->prepare($query); - $sth->execute($description, $batch_id); - - # Check for success - my $creator = $cgi->param('creator'); - if ( $creator eq 'patroncard' ) { - my $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id); - if ( $batch->{description} eq $description ) { - $status = 'success'; - } - } elsif ( $creator eq 'label' ) { - my $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id); - if ( $batch->{description} eq $description ) { - $status = 'success'; - } + my $status = 'success'; + eval { + my $query = "UPDATE creator_batches SET description = ? WHERE batch_id = ?"; + my $sth = $dbh->prepare($query); + $sth->execute($description, $batch_id); + }; + if ($@) { + warn $@; + undef $status; } my $json = encode_json ( { status => $status, newdesc => $description } ); output_with_http_headers $cgi, undef, $json, 'js'; exit; +} else { + # FIXME be nicer + exit 0; } -- 2.20.1