From aa71a52d0faed4403b3a7d16aab294ab47a13b00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nu=C3=B1o=20L=C3=B3pez=20Ans=C3=B3tegui?= Date: Wed, 19 Jun 2013 14:35:41 +0200 Subject: [PATCH] Bug 27981: Add option to automatically set 001 to the biblionumber 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 Signed-off-by: Thomas Klausner --- C4/Biblio.pm | 7 +++++ cataloguing/addbiblio.pl | 6 ++++- installer/data/mysql/mandatory/sysprefs.sql | 1 + .../lib/koha/cateditor/koha-backend.js | 5 ++-- .../prog/en/includes/cateditor-ui.inc | 8 ++++-- .../admin/preferences/cataloguing.pref | 6 +++++ t/db_dependent/Biblio.t | 26 ++++++++++++++++++- 7 files changed, 53 insertions(+), 6 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index c4de6b064d..f66b478bb5 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -2562,6 +2562,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 diff --git a/cataloguing/addbiblio.pl b/cataloguing/addbiblio.pl index 036764417c..bc7d498841 100755 --- a/cataloguing/addbiblio.pl +++ b/cataloguing/addbiblio.pl @@ -806,7 +806,11 @@ if (($biblionumber) && !($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"); diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql index aab22108b0..e039bb04f6 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -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'), 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 eeca779440..d3556bbc1a 100644 --- a/koha-tmpl/intranet-tmpl/lib/koha/cateditor/koha-backend.js +++ b/koha-tmpl/intranet-tmpl/lib/koha/cateditor/koha-backend.js @@ -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' ) { 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 9fd33640e8..4fe7515f9e 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-ui.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-ui.inc @@ -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 ) { 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 86840eff5a..5b966ba2dd 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 @@ -165,6 +165,12 @@ Cataloging: - and record's last modifier name in MARC subfield - pref: MarcFieldForModifierName - ".
NOTE: 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 ' diff --git a/t/db_dependent/Biblio.t b/t/db_dependent/Biblio.t index ff0857a296..507b3b98e6 100755 --- a/t/db_dependent/Biblio.t +++ b/t/db_dependent/Biblio.t @@ -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'); my $isbn = '0590353403'; my $title = 'Foundation'; @@ -815,6 +816,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; -- 2.30.2