Bug 27981: Add option to automatically set 001 to the biblionumber
authorNuño López Ansótegui <nunyo@masmedios.com>
Wed, 19 Jun 2013 12:35:41 +0000 (14:35 +0200)
committerTomas Cohen Arazi <tomascohen@theke.io>
Mon, 12 Sep 2022 18:17:36 +0000 (15:17 -0300)
This patch adds a new system preference:
autoControlNumber

The option "biblionumber" will set field 001 to the biblionumber when you create a new record
or edit an existing record and clear the 001 field.

If a value is present in 001 when saving the record it will be preserved.

If set to 'OFF' the 001 field wil not be touched

When duplicating a record the 001 will be removed if autoControlNumber is set

To test:
 1 - Apply patches and updatedatabase
 2 - Create a new record with no 001 field
 3 - Save and view the MAC, confirm there is no 001
 4 - Set the system preference to 'biblionumber'
 5 - Edit the record you created previously
 6 - Note the 001 is prepopulated with the biblionumber
 7 - Delete the field
 8 - Save the record
 9 - View the MARC, the 001 is filled with biblionumber
10 - Edit the record
11 - Set the 001 to a different value "Not the biblionumber"
12 - Save
13 - View the marc and confirm the value you entered is retained
14 - Edit a record with an existing 001 that is not the biblionumber
15 - Save and confirm 001 is not updated

To test duplication:
1 - Edit a record as duplicate when using the advanced editor
2 - Confirm the 001 does not load, but record saves correctly
3 - Edit the record
4 - Switch to 'basic editor'
5 - Save, then view record
6 - Edit as duplicate in basic editor
7 - Confirm the 001 is removed
8 - Confirm the 001 is added on save

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Thomas Klausner <domm@plix.at>
Signed-off-by: Andrew Nugged <nugged@gmail.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
C4/Biblio.pm
cataloguing/addbiblio.pl
installer/data/mysql/mandatory/sysprefs.sql
koha-tmpl/intranet-tmpl/lib/koha/cateditor/koha-backend.js
koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-ui.inc
koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref
t/db_dependent/Biblio.t

index e2df601..84337c5 100644 (file)
@@ -2413,6 +2413,13 @@ sub _koha_marc_update_bib_ids {
     } else {
         C4::Biblio::UpsertMarcSubfield($record, $biblioitem_tag, $biblioitem_subfield, $biblioitemnumber);
     }
+
+    # update the control number (001) in MARC
+    if(C4::Context->preference('autoControlNumber') eq 'biblionumber'){
+        unless($record->field('001')){
+            $record->insert_fields_ordered(MARC::Field->new('001', $biblionumber));
+        }
+    }
 }
 
 =head2 _koha_marc_update_biblioitem_cn_sort
index 92abe81..b73d973 100755 (executable)
@@ -807,7 +807,11 @@ if ( $biblio && !$breedingid ) {
 if ($breedingid) {
     ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
 }
-
+if ( $record && $op eq 'duplicate' &&
+     C4::Context->preference('autoControlNumber') eq 'biblionumber' ){
+    my @control_num = $record->field('001');
+    $record->delete_fields(@control_num);
+}
 #populate hostfield if hostbiblionumber is available
 if ($hostbiblionumber) {
     my $marcflavour = C4::Context->preference("marcflavour");
index 3ee50c6..d60605e 100644 (file)
@@ -73,6 +73,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `
 ('AuthorityXSLTResultsDisplay','','','Enable XSL stylesheet control over authority results page display on intranet','Free'),
 ('AuthSuccessLog','0',NULL,'If enabled, log successful authentications','YesNo'),
 ('autoBarcode','OFF','incremental|annual|hbyymmincr|EAN13|OFF','Used to autogenerate a barcode: incremental will be of the form 1, 2, 3; annual of the form 2007-0001, 2007-0002; hbyymmincr of the form HB08010001 where HB=Home Branch','Choice'),
+('autoControlNumber','OFF','biblionumber|OFF','Used to autogenerate a Control Number: biblionumber will be as biblionumber, OFF will leave the field as it is;','Choice'),
 ('AutoCreateAuthorities','0',NULL,'Automatically create authorities that do not exist when cataloging records.','YesNo'),
 ('AutoCreditNumber', '', '', 'Automatically generate a number for account credits', 'Choice'),
 ('AutoEmailNewUser','0',NULL,'Send an email to newly created patrons.','YesNo'),
index eeca779..d3556bb 100644 (file)
@@ -103,15 +103,16 @@ define( [ '/cgi-bin/koha/svc/cataloguing/framework?frameworkcode=&callback=defin
             return _framework_kohafields[kohafield];
         },
 
-        GetRecord: function( id, callback ) {
+        GetRecord: function( id, remove_control_num, callback ) {
             $.get(
-                '/cgi-bin/koha/svc/bib/' + id
+                '/cgi-bin/koha/svc/bib/'+ id
             ).done( function( metadata ) {
                 $.get(
                     '/cgi-bin/koha/svc/bib_framework/' + id
                 ).done( function( frameworkcode ) {
                     var record = new MARC.Record();
                     record.loadMARCXML(metadata);
+                    if( remove_control_num ) { record.removeField("001"); }
                     record.frameworkcode = $(frameworkcode).find('frameworkcode').text();
                     initFramework( record.frameworkcode, function( error ) {
                         if ( typeof error === 'undefined' ) {
index 9fd3364..4fe7515 100644 (file)
@@ -251,13 +251,17 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
             saveLabel: _("Duplicate"),
             get: function( id, callback ) {
                 if ( !id ) return false;
+                var remove_control_num = [% IF Koha.Preference('autoControlNumber') == 'OFF' %] 0 [% ELSE %] 1 [% END %];
 
-                KohaBackend.GetRecord( id, callback );
+                KohaBackend.GetRecord( id, remove_control_num, callback );
             },
             save: function( id, record, done ) {
                 function finishCb( data ) {
                     done( { error: data.error, newRecord: data.marcxml && data.marcxml[0], newId: data.biblionumber && [ 'catalog', data.biblionumber ] } );
                 }
+                [% IF Koha.Preference('autoControlNumber') != 'OFF' %]
+                    record.removeField("001");
+                [% END %]
 
                 KohaBackend.CreateRecord( record, finishCb );
             }
@@ -272,7 +276,7 @@ require( [ 'koha-backend', 'search', 'macros', 'marc-editor', 'marc-record', 'pr
             get: function( id, callback ) {
                 if ( !id ) return false;
 
-                KohaBackend.GetRecord( id, callback );
+                KohaBackend.GetRecord( id, "", callback );
             },
             save: function( id, record, done ) {
                 function finishCb( data ) {
index 4aad6fc..4b397e1 100644 (file)
@@ -165,6 +165,12 @@ Cataloging:
             - and record's last modifier name in MARC subfield
             - pref: MarcFieldForModifierName
             - ". <br/><strong>NOTE:</strong> Use a dollar sign between field and subfield like 123$a."
+        -
+            - Control Number (001) is
+            - pref: autoControlNumber
+              choices:
+                  biblionumber: generated as biblionumber.
+                  "OFF": not generated automatically.
     Display:
         -
             - 'Separate main entry and subdivisions with '
index ea8a824..2037a6b 100755 (executable)
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 14;
+use Test::More tests => 15;
 use Test::MockModule;
 use Test::Warn;
 use List::MoreUtils qw( uniq );
@@ -250,6 +250,7 @@ sub run_tests {
     # Authority tests don't interact well with Elasticsearch at the moment due to the fact that there's currently no way to
     # roll back ES index changes.
     t::lib::Mocks::mock_preference('SearchEngine', 'Zebra');
+    t::lib::Mocks::mock_preference('autoControlNumber', 'OFF');
 
     t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
 
@@ -823,6 +824,29 @@ subtest "LinkBibHeadingsToAuthorities record generation tests" => sub {
     );
 };
 
+subtest 'autoControlNumber tests' => sub {
+    plan tests => 3;
+
+    t::lib::Mocks::mock_preference('autoControlNumber', 'OFF');
+
+    my $record = MARC::Record->new();
+    my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
+    $record = GetMarcBiblio({biblionumber => $biblionumber});
+    is($record->field('001'), undef, '001 not set when pref is off');
+
+    t::lib::Mocks::mock_preference('autoControlNumber', 'biblionumber');
+    C4::Biblio::ModBiblio($record, $biblionumber, "", 1);
+    $record = GetMarcBiblio({biblionumber => $biblionumber});
+    is($record->field('001')->as_string(), $biblionumber, '001 set to biblionumber when pref set and field is blank');
+
+    $record->field('001')->update('Not biblionumber');
+    C4::Biblio::ModBiblio($record, $biblionumber, "", 1);
+    $record = GetMarcBiblio({biblionumber => $biblionumber});
+    is($record->field('001')->as_string(), 'Not biblionumber', '001 not set to biblionumber when pref set and field exists');
+
+};
+
+
 # Cleanup
 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
 $schema->storage->txn_rollback;