Bug 16424: Update default values in advanced editor when changing frameworks.
authorEre Maijala <ere.maijala@helsinki.fi>
Fri, 10 Nov 2017 06:28:01 +0000 (08:28 +0200)
committerNick Clemens <nick@bywatersolutions.com>
Fri, 28 Sep 2018 19:01:51 +0000 (19:01 +0000)
Signed-off-by: Michal Denar <black23@gmail.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

koha-tmpl/intranet-tmpl/lib/koha/cateditor/koha-backend.js
koha-tmpl/intranet-tmpl/lib/koha/cateditor/marc-editor.js
koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-ui.inc

index 10e9337..eeca779 100644 (file)
@@ -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 );
+                        }
                     } );
                 } );
             } );
index 046ff71..7fe7062 100644 (file)
@@ -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' )
-                    });
                 }
             );
         },
index a20bbe6..82d200f 100644 (file)
@@ -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' } );