From 38675798263e4a0af26022abb34386d40ed6d926 Mon Sep 17 00:00:00 2001
From: Cori Lynn Arnold <carnold@dgiinc.com>
Date: Wed, 4 Dec 2019 10:49:30 +0000
Subject: [PATCH] Bug 24108: Advanced Editor: Save .mrc save with a
 configurable value

Creates a new systempref "SaveRecordbyControlNumber" which is initially
set to 0 (off/no). By default saving .mrc (and .xml) will be saved by the
biblionumber.

To test:

    Apply the patches.
    1. Open a record in the advanced editor
    2. Save the record using the drop down box selecting "Save as MARC
    (.mrc) file.
    3. Note that the file saved is the same as the biblionumber at the top
    of the Advanced Editor interface.
    <Repeat steps 1-3 for the .xml save option>

    4. Update the preference:
        Search for "SaveRecordbyControlNumber" in system preferences. Change to "Save by ControlNumber"

    5. Restart Koha

    6. Verify that there is not a 001 field in the record (delete it if
            there is)

    Repeat steps 1, 2, & 3 for both .mrc and .xml options (the file should still be saved as the
            biblionumber if there is no control field)

    7. Add a 001 field to the record such as "001  abc00012345"

    Repeat steps 1, 2 for both .mrc and .xml Verify that the files saved
    are named abc00012345.mrc and abc00012345.xml
---
 ...bug_24108-add_SaveRecordbyControlNumber_syspref.sql |  1 +
 installer/data/mysql/sysprefs.sql                      |  1 +
 .../intranet-tmpl/prog/en/includes/cateditor-ui.inc    | 18 ++++++++++++++++--
 .../prog/en/modules/admin/preferences/cataloguing.pref |  8 ++++++++
 4 files changed, 26 insertions(+), 2 deletions(-)
 create mode 100644 installer/data/mysql/atomicupdate/bug_24108-add_SaveRecordbyControlNumber_syspref.sql

diff --git a/installer/data/mysql/atomicupdate/bug_24108-add_SaveRecordbyControlNumber_syspref.sql b/installer/data/mysql/atomicupdate/bug_24108-add_SaveRecordbyControlNumber_syspref.sql
new file mode 100644
index 0000000000..db11c9907f
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug_24108-add_SaveRecordbyControlNumber_syspref.sql
@@ -0,0 +1 @@
+INSERT IGNORE INTO systempreferences (variable, value, explanation, options, type) VALUES ('SaveRecordbyControlNumber', '0', 'If set, advanced editor (Rancor) will use the control number field to populate the name of the save file, otherwise it uses the biblionumber.', NULL, 'YesNo');
diff --git a/installer/data/mysql/sysprefs.sql b/installer/data/mysql/sysprefs.sql
index 612dcc20c6..96aa0b0eca 100644
--- a/installer/data/mysql/sysprefs.sql
+++ b/installer/data/mysql/sysprefs.sql
@@ -549,6 +549,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `
 ('RoutingListAddReserves','0','','If ON the patrons on routing lists are automatically added to holds on the issue.','YesNo'),
 ('RoutingListNote','To change this note edit <a href=\"/cgi-bin/koha/admin/preferences.pl?op=search&searchfield=RoutingListNote#jumped\">RoutingListNote</a> system preference.','70|10','Define a note to be shown on all routing lists','Textarea'),
 ('RoutingSerials','1',NULL,'If ON, serials routing is enabled','YesNo'),
+('SaveRecordbyControlNumber','0',NULL,'If set, advanced editor (Rancor) will use the control number field to populate the name of the save file, otherwise it uses the biblionumber.','YesNo'),
 ('SCOMainUserBlock','','70|10','Add a block of HTML that will display on the self checkout screen','Textarea'),
 ('SCOUserCSS','',NULL,'Add CSS to be included in the SCO module in an embedded <style> tag.','free'),
 ('SCOUserJS','',NULL,'Define custom javascript for inclusion in the SCO module','free'),
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 0130a63979..17544bf558 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-ui.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-ui.inc
@@ -286,7 +286,14 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
         'iso2709': {
             saveLabel: _("Save as MARC (.mrc) file"),
             save: function( id, record, done ) {
-                saveAs( new Blob( [record.toISO2709()], { 'type': 'application/octet-stream;charset=utf-8' } ), 'record.mrc' );
+                var recname = state.recordID+'.mrc';
+                [% IF (Koha.Preference('SaveRecordbyControlNumber') == '1') %]
+                var controlnumfield = record.field('001');
+                if(controlnumfield) {
+                        recname = controlnumfield.subfield('@')+'.mrc';
+                }
+                [% END %]
+                saveAs( new Blob( [record.toISO2709()], { 'type': 'application/octet-stream;charset=utf-8' } ), recname );
 
                 done( {} );
             }
@@ -294,7 +301,14 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
         'marcxml': {
             saveLabel: _("Save as MARCXML (.xml) file"),
             save: function( id, record, done ) {
-                saveAs( new Blob( [record.toXML()], { 'type': 'application/octet-stream;charset=utf-8' } ), 'record.xml' );
+                var recname = state.recordID+'.xml';
+                [% IF (Koha.Preference('SaveRecordbyControlNumber') == '1') %]
+                var controlnumfield = record.field('001');
+                if(controlnumfield) {
+                        recname = controlnumfield.subfield('@')+'.xml';
+                }
+                [% END %]
+                saveAs( new Blob( [record.toXML()], { 'type': 'application/octet-stream;charset=utf-8' } ), recname );
 
                 done( {} );
             }
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref
index 20587fa310..ea7579de48 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref
@@ -27,6 +27,14 @@ Cataloging:
             - the advanced cataloging editor.
             - "<br/> NOTE:"
             - Currently does not include support for UNIMARC or NORMARC fixed fields.
+        -
+            - pref: SaveRecordbyControlNumber
+              default: 0
+              choices:
+                  yes: "Save by ControlNumber" 
+                  no: "Save by Biblionumber"
+            - "<br/> NOTE:"
+            - Save file in Advanced Editor (Rancor) by Control Number, default is by Biblionumer.
     Spine Labels:
         -
             - When using the quick spine label printer,
-- 
2.11.0