From e9424427e0686a31fb1a460a577be5da45e4998b 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 If set to 'OFF' the 001 field wil not be touched When duplicating a record the 001 will be removed if autoControlNumber is set A 'duplication' option is add to svc/bib and rancor passes the parameter, the basic editor simply removes the field 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 testi 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 --- C4/Biblio.pm | 7 +++++ cataloguing/addbiblio.pl | 6 ++++- .../data/mysql/atomicupdate/bug_9921.perl | 11 ++++++++ installer/data/mysql/mandatory/sysprefs.sql | 1 + .../lib/koha/cateditor/koha-backend.js | 4 +-- .../prog/en/includes/cateditor-ui.inc | 4 +-- .../admin/preferences/cataloguing.pref | 6 +++++ svc/bib | 13 +++++++--- t/db_dependent/Biblio.t | 26 ++++++++++++++++++- 9 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_9921.perl diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 45d1b31887..efc0f11e69 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -2627,6 +2627,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 7eba09f55d..0033c8f9eb 100755 --- a/cataloguing/addbiblio.pl +++ b/cataloguing/addbiblio.pl @@ -790,7 +790,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/atomicupdate/bug_9921.perl b/installer/data/mysql/atomicupdate/bug_9921.perl new file mode 100644 index 0000000000..7358523c82 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_9921.perl @@ -0,0 +1,11 @@ +$DBversion = 'XXX'; # will be replaced by the RM +if( CheckVersion( $DBversion ) ) { + # $dbh->do( "ALTER TABLE biblio ADD COLUMN badtaste int" ); + $dbh->do( + q{INSERT INTO systempreferences (variable, value, options, explanation, type) VALUES ('autoControlNumber','OFF','biblionumber|OFF', + 'Used to autogenerate a Control Number: biblionumber will be as biblionumber; OFF will leave it as is','Choice');} + ); + + # Always end with this (adjust the bug info) + NewVersion( $DBversion, 9921, "Make it possible to force 001 = biblionumber"); +} diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql index 3430bc26e7..73dc6534fa 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -70,6 +70,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('AuthoritySeparator','--','10','Used to separate a list of authorities in a display. Usually --','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'), ('AutoEmailOpacUser','0',NULL,'Sends notification emails containing new account details to patrons - when account is created.','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..93fc5c492c 100644 --- a/koha-tmpl/intranet-tmpl/lib/koha/cateditor/koha-backend.js +++ b/koha-tmpl/intranet-tmpl/lib/koha/cateditor/koha-backend.js @@ -103,9 +103,9 @@ define( [ '/cgi-bin/koha/svc/cataloguing/framework?frameworkcode=&callback=defin return _framework_kohafields[kohafield]; }, - GetRecord: function( id, callback ) { + GetRecord: function( id, duplicate, callback ) { $.get( - '/cgi-bin/koha/svc/bib/' + id + '/cgi-bin/koha/svc/bib/'+ duplicate + id ).done( function( metadata ) { $.get( '/cgi-bin/koha/svc/bib_framework/' + id 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 352e9a0c53..8ed5016df1 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-ui.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-ui.inc @@ -252,7 +252,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, "d", callback ); }, save: function( id, record, done ) { function finishCb( data ) { @@ -272,7 +272,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 5ffd9a22f3..a740bd0a68 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 @@ -158,6 +158,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/svc/bib b/svc/bib index 43fc6f6857..2a500254eb 100755 --- a/svc/bib +++ b/svc/bib @@ -40,16 +40,18 @@ unless ($status eq "ok") { # do initial validation my $path_info = $query->path_info(); +my $duplicate = undef; my $biblionumber = undef; -if ($path_info =~ m!^/(\d+)$!) { - $biblionumber = $1; +if ($path_info =~ m!^/(d?)(\d+)$!) { + $duplicate = $1; + $biblionumber = $2; } else { print $query->header(-type => 'text/xml', -status => '400 Bad Request'); } # are we retrieving, updating or deleting a bib? if ($query->request_method eq "GET") { - fetch_bib($query, $biblionumber); + fetch_bib($query, $biblionumber, $duplicate); } elsif ($query->request_method eq "POST") { update_bib($query, $biblionumber); } elsif ($query->request_method eq "DELETE") { @@ -65,10 +67,15 @@ exit 0; sub fetch_bib { my $query = shift; my $biblionumber = shift; + my $duplicate = shift; my $record = GetMarcBiblio({ biblionumber => $biblionumber, embed_items => scalar $query->param('items') }); if (defined $record) { + if( $duplicate && C4::Context->preference('autoControlNumber') eq 'biblionumber' ){ + my @control_nums = $record->field('001'); + $record->delete_fields(@control_nums); + } print $query->header(-type => 'text/xml',-charset => 'utf-8',); print $record->as_xml_record(); } else { diff --git a/t/db_dependent/Biblio.t b/t/db_dependent/Biblio.t index feac71d8eb..0c4f44a081 100755 --- a/t/db_dependent/Biblio.t +++ b/t/db_dependent/Biblio.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 15; +use Test::More tests => 16; use Test::MockModule; use Test::Warn; use List::MoreUtils qw( uniq ); @@ -246,6 +246,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'; @@ -821,6 +822,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.20.1