From 2074421ba3a32979d797479cf124ae6bfb333d36 Mon Sep 17 00:00:00 2001 From: Ere Maijala Date: Fri, 10 Nov 2017 08:28:01 +0200 Subject: [PATCH] Bug 16424: Update default values in advanced editor when changing frameworks. Signed-off-by: Michal Denar Signed-off-by: Katrin Fischer Signed-off-by: Nick Clemens --- .../lib/koha/cateditor/koha-backend.js | 8 +++++-- .../lib/koha/cateditor/marc-editor.js | 24 ++++++++++++------- .../prog/en/includes/cateditor-ui.inc | 1 + 3 files changed, 23 insertions(+), 10 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 10e9337caa..eeca779440 100644 --- a/koha-tmpl/intranet-tmpl/lib/koha/cateditor/koha-backend.js +++ b/koha-tmpl/intranet-tmpl/lib/koha/cateditor/koha-backend.js @@ -222,10 +222,14 @@ define( [ '/cgi-bin/koha/svc/cataloguing/framework?frameworkcode=&callback=defin $.each( taginfo.subfields, function( undef, subfield ) { var subfieldcode = subfield[0], subfieldinfo = subfield[1]; - if ( subfieldinfo.mandatory != "1" && !allTags ) return; + if ( subfieldinfo.mandatory != "1" && !subfieldinfo.defaultvalue && !allTags ) return; $.each( fields, function( undef, field ) { - if ( !field.hasSubfield(subfieldcode) ) field.addSubfieldGrouped( [ subfieldcode, subfieldinfo.defaultvalue || '' ] ); + if ( !field.hasSubfield(subfieldcode) ) { + field.addSubfieldGrouped( [ subfieldcode, subfieldinfo.defaultvalue || '' ] ); + } else if ( subfieldinfo.defaultvalue && field.subfield( subfieldcode ) === '' ) { + field.subfield( subfieldcode, subfieldinfo.defaultvalue ); + } } ); } ); } ); diff --git a/koha-tmpl/intranet-tmpl/lib/koha/cateditor/marc-editor.js b/koha-tmpl/intranet-tmpl/lib/koha/cateditor/marc-editor.js index 046ff71da5..7fe7062edf 100644 --- a/koha-tmpl/intranet-tmpl/lib/koha/cateditor/marc-editor.js +++ b/koha-tmpl/intranet-tmpl/lib/koha/cateditor/marc-editor.js @@ -537,30 +537,38 @@ define( [ 'marc-record', 'koha-backend', 'preferences', 'text-marc', 'widget' ], this.cm.refresh(); }, - setFrameworkCode: function( code, callback ) { + setFrameworkCode: function( code, updateFields, callback ) { this.frameworkcode = code; $( 'a.change-framework i.selected' ).addClass( 'hidden' ); $( 'a.change-framework i.unselected' ).removeClass( 'hidden' ); $( 'a.change-framework[data-frameworkcode="' + code + '"] i.unselected' ).addClass( 'hidden' ); $( 'a.change-framework[data-frameworkcode="' + code + '"] i.selected' ).removeClass( 'hidden '); - KohaBackend.InitFramework( code, callback ); + var cm = this.cm; + KohaBackend.InitFramework( code, function ( error ) { + cm.setOption( 'mode', { + name: 'marc', + nonRepeatableTags: KohaBackend.GetTagsBy( code, 'repeatable', '0' ), + nonRepeatableSubfields: KohaBackend.GetSubfieldsBy( code, 'repeatable', '0' ) + }); + if ( updateFields ) { + var record = TextMARC.TextToRecord( cm.getValue() ); + KohaBackend.FillRecord( code, record ); + cm.setValue( TextMARC.RecordToText(record) ); + } + callback( error ); + } ); }, displayRecord: function( record ) { this.cm.setValue( TextMARC.RecordToText(record) ); this.modified = false; - var cm = this.cm; this.setFrameworkCode( typeof record.frameworkcode !== 'undefined' ? record.frameworkcode : '', + false, function ( error ) { if ( typeof error !== 'undefined' ) { humanMsg.displayAlert( _(error), { className: 'humanError' } ); } - cm.setOption( 'mode', { - name: 'marc', - nonRepeatableTags: KohaBackend.GetTagsBy( this.frameworkcode, 'repeatable', '0' ), - nonRepeatableSubfields: KohaBackend.GetSubfieldsBy( this.frameworkcode, 'repeatable', '0' ) - }); } ); }, diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-ui.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-ui.inc index a20bbe6eed..82d200ff28 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-ui.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-ui.inc @@ -1098,6 +1098,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr $("#loading").show(); editor.setFrameworkCode( $(this).data( 'frameworkcode' ), + true, function ( error ) { if ( typeof error !== 'undefined' ) { humanMsg.displayAlert( _("Failed to change framework"), { className: 'humanError' } ); -- 2.39.5