From 79a451bf2176dd36a50b206c2d74a2923639f2b0 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 19 Mar 2024 15:33:28 +0000 Subject: [PATCH] Bug 36351: Adjust saveRecord and _fromXMLStruct Using the new API Client means we need to handle some calls differently. the http-client is returning only the response, not the text, so we need to handle getting this out with a new async function (or promises, but this works) We also need to adjust _fromXMLStruct as we have reduced the levels in the response by the time it is called This now adds a new 'update' function to the cataloguing client as well. Eventually, this should all be using the REST API, but I think for now handling the non-standard responses gets it working again To test: To test: 1 - Browse to Cataloguing->Advanced editor 2 - New Record 3 - Enter values and save - confirm it works 4 - Confirm url now ends in : editor.pl#catalog/{biblionumber} and not editor.pl#new 5 - Save the record again, confirm biblio is updated and not saved as new Signed-off-by: Nick Clemens --- .../lib/koha/cateditor/koha-backend.js | 59 ++++++++++++------- .../prog/js/fetch/cataloguing-api-client.js | 9 +++ 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/lib/koha/cateditor/koha-backend.js b/koha-tmpl/intranet-tmpl/lib/koha/cateditor/koha-backend.js index 8586206ade0..0ac49808726 100644 --- a/koha-tmpl/intranet-tmpl/lib/koha/cateditor/koha-backend.js +++ b/koha-tmpl/intranet-tmpl/lib/koha/cateditor/koha-backend.js @@ -23,10 +23,16 @@ define( [ '/cgi-bin/koha/svc/cataloguing/framework?frameworkcode=&callback=defin var _framework_mappings = {}; var _framework_kohafields = {}; + async function _readStream(stream){ + let xml_response = new Response(stream); + let the_xml = await xml_response.text(); + return the_xml; + } + function _fromXMLStruct( data ) { result = {}; - $(data).children().eq(0).children().each( function() { + $(data).children().each( function() { var $contents = $(this).contents(); if ( $contents.length == 1 && $contents[0].nodeType == Node.TEXT_NODE ) { result[ this.localName ] = $contents[0].data; @@ -137,14 +143,21 @@ define( [ '/cgi-bin/koha/svc/cataloguing/framework?frameworkcode=&callback=defin _removeBiblionumberFields( record ); const client = APIClient.cataloguing; - client.catalog_bib.create({ frameworkcode, record }).then( + client.catalog_bib.create({ frameworkcode: frameworkcode, record: record }).then( success => { - var record = _fromXMLStruct( success ); - if ( record.marcxml ) { - record.marcxml[0].frameworkcode = frameworkcode; + _readStream( success.body ).then( + success=> { + var xml_response = success; + var record =_fromXMLStruct( xml_response ); + if ( record.marcxml ) { + record.marcxml[0].frameworkcode = frameworkcode; + } + callback( record ); + }, + error => { + callback( { error: _('Could not parse response') } ); } - callback( record ); - }, + );}, error => { callback( { error: _('Could not save record') } ); } @@ -156,20 +169,26 @@ define( [ '/cgi-bin/koha/svc/cataloguing/framework?frameworkcode=&callback=defin record = record.clone(); _removeBiblionumberFields( record ); - $.ajax( { - type: 'POST', - url: '/cgi-bin/koha/svc/bib/' + id + '?frameworkcode=' + encodeURIComponent(frameworkcode), - data: record.toXML(), - contentType: 'text/xml' - } ).done( function( data ) { - var record = _fromXMLStruct( data ); - if ( record.marcxml ) { - record.marcxml[0].frameworkcode = frameworkcode; + const client = APIClient.cataloguing; + client.catalog_bib.update({ frameworkcode: frameworkcode, record: record, id: id }).then( + success => { + _readStream( success.body ).then( + success=> { + var xml_response = success; + var record =_fromXMLStruct( xml_response ); + if ( record.marcxml ) { + record.marcxml[0].frameworkcode = frameworkcode; + } + callback( record ); + }, + error => { + callback( { error: _('Could not parse response') } ); + } + );}, + error => { + callback( { error: _('Could not save record') } ); } - callback( record ); - } ).fail( function( data ) { - callback( { error: _('Could not save record') } ); - } ); + ); }, GetTagsBy: function( frameworkcode, field, value ) { diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/cataloguing-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/cataloguing-api-client.js index 435be5b834d..d6d1d1b45df 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/fetch/cataloguing-api-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/cataloguing-api-client.js @@ -18,6 +18,15 @@ export class CataloguingAPIClient extends HttpClient { "text/xml", }, }), + update: bib_info => + this.post({ + endpoint: "bib/%s?frameworkcode=%s".format( bib_info.id, bib_info.frameworkcode ), + body: bib_info.record.toXML(), + headers: { + "Content-Type": + "text/xml", + }, + }), }; } -- 2.30.2