From 4bf78fd8e285f5f41038f1312810f51907807898 Mon Sep 17 00:00:00 2001 From: Johanna Raisa Date: Wed, 12 Jun 2024 12:26:01 +0300 Subject: [PATCH] Bug 37061: allow to configure how is data copied from host to component parts This patch adds a new system preference to allow to configure how is data copied from host to component parts. The rules are defined in YAML format. If left empty, the default behavior is to copy the host data to the component parts as hard coded in the code. Test plan: 1) Apply the patch 2) Run updatedatabase.pl 3) Go to Administration -> Global system preferences 4) Search for PrepareHostField 5) Fill the rules in YAML format as in the example in the description 6) Save 7) Create a new child record 8) See that the data is copied from host to the component parts according to the rules Sponsored-by: Koha-Suomi Oy --- C4/Biblio.pm | 202 ++++++++++-------- .../data/mysql/atomicupdate/bug_37061.pl | 17 ++ .../admin/preferences/cataloguing.pref | 14 ++ 3 files changed, 144 insertions(+), 89 deletions(-) create mode 100755 installer/data/mysql/atomicupdate/bug_37061.pl diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 4b32ad43d5..4a95aa3604 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -2948,116 +2948,140 @@ sub prepare_host_field { my $biblio = Koha::Biblios->find($hostbiblio); my $host = $biblio->metadata->record; + my $prepared_host_field = C4::Context->yaml_preference('PrepareHostField') || q{}; # unfortunately as_string does not 'do the right thing' # if field returns undef my %sfd; my $field; my $host_field; - if ( $marcflavour eq 'MARC21' ) { - if ( $field = $host->field('100') || $host->field('110') || $host->field('11') ) { - my $s = $field->as_string('ab'); - if ($s) { - $sfd{a} = $s; + if ( $prepared_host_field ) { + foreach my $key (keys %$prepared_host_field) { + my ($tag, $subfields) = split(/\$/, $key); + my ($part_field, $part_subfield) = split(/\$/, $prepared_host_field->{$key}); + my $field = $host->field($tag); + if ( $field ) { + if ($field->is_control_field) { + $sfd{$part_subfield} = $field->data(); + } else { + my $s = $field->as_string($subfields); + if ($s) { + $sfd{$part_subfield} = $s; + } + } } - } - if ( $field = $host->field('245') ) { - my $s = $field->as_string('a'); - if ($s) { - $sfd{t} = $s; + if (%sfd) { + $host_field = MARC::Field->new( $part_field, '0', ' ', %sfd ); } } - if ( $field = $host->field('260') ) { - my $s = $field->as_string('abc'); - if ($s) { - $sfd{d} = $s; + + return $host_field; + } else { + if ( $marcflavour eq 'MARC21' ) { + if ( $field = $host->field('100') || $host->field('110') || $host->field('11') ) { + my $s = $field->as_string('ab'); + if ($s) { + $sfd{a} = $s; + } } - } - if ( $field = $host->field('240') ) { - my $s = $field->as_string(); - if ($s) { - $sfd{b} = $s; + if ( $field = $host->field('245') ) { + my $s = $field->as_string('a'); + if ($s) { + $sfd{t} = $s; + } } - } - if ( $field = $host->field('022') ) { - my $s = $field->as_string('a'); - if ($s) { - $sfd{x} = $s; + if ( $field = $host->field('260') ) { + my $s = $field->as_string('abc'); + if ($s) { + $sfd{d} = $s; + } } - } - if ( $field = $host->field('020') ) { - my $s = $field->as_string('a'); - if ($s) { - $sfd{z} = $s; + if ( $field = $host->field('240') ) { + my $s = $field->as_string(); + if ($s) { + $sfd{b} = $s; + } } - } - if ( $field = $host->field('001') ) { - $sfd{w} = $field->data(),; - } - $host_field = MARC::Field->new( 773, '0', ' ', %sfd ); - return $host_field; - } - elsif ( $marcflavour eq 'UNIMARC' ) { - #author - if ( $field = $host->field('700') || $host->field('710') || $host->field('720') ) { - my $s = $field->as_string('ab'); - if ($s) { - $sfd{a} = $s; + if ( $field = $host->field('022') ) { + my $s = $field->as_string('a'); + if ($s) { + $sfd{x} = $s; + } } - } - #title - if ( $field = $host->field('200') ) { - my $s = $field->as_string('a'); - if ($s) { - $sfd{t} = $s; + if ( $field = $host->field('020') ) { + my $s = $field->as_string('a'); + if ($s) { + $sfd{z} = $s; + } } - } - #place of publicaton - if ( $field = $host->field('210') ) { - my $s = $field->as_string('a'); - if ($s) { - $sfd{c} = $s; + if ( $field = $host->field('001') ) { + $sfd{w} = $field->data(),; } - } - #date of publication - if ( $field = $host->field('210') ) { - my $s = $field->as_string('d'); - if ($s) { - $sfd{d} = $s; + $host_field = MARC::Field->new( 773, '0', ' ', %sfd ); + return $host_field; + } + elsif ( $marcflavour eq 'UNIMARC' ) { + #author + if ( $field = $host->field('700') || $host->field('710') || $host->field('720') ) { + my $s = $field->as_string('ab'); + if ($s) { + $sfd{a} = $s; + } } - } - #edition statement - if ( $field = $host->field('205') ) { - my $s = $field->as_string(); - if ($s) { - $sfd{e} = $s; + #title + if ( $field = $host->field('200') ) { + my $s = $field->as_string('a'); + if ($s) { + $sfd{t} = $s; + } } - } - #URL - if ( $field = $host->field('856') ) { - my $s = $field->as_string('u'); - if ($s) { - $sfd{u} = $s; + #place of publicaton + if ( $field = $host->field('210') ) { + my $s = $field->as_string('a'); + if ($s) { + $sfd{c} = $s; + } } - } - #ISSN - if ( $field = $host->field('011') ) { - my $s = $field->as_string('a'); - if ($s) { - $sfd{x} = $s; + #date of publication + if ( $field = $host->field('210') ) { + my $s = $field->as_string('d'); + if ($s) { + $sfd{d} = $s; + } } - } - #ISBN - if ( $field = $host->field('010') ) { - my $s = $field->as_string('a'); - if ($s) { - $sfd{y} = $s; + #edition statement + if ( $field = $host->field('205') ) { + my $s = $field->as_string(); + if ($s) { + $sfd{e} = $s; + } } + #URL + if ( $field = $host->field('856') ) { + my $s = $field->as_string('u'); + if ($s) { + $sfd{u} = $s; + } + } + #ISSN + if ( $field = $host->field('011') ) { + my $s = $field->as_string('a'); + if ($s) { + $sfd{x} = $s; + } + } + #ISBN + if ( $field = $host->field('010') ) { + my $s = $field->as_string('a'); + if ($s) { + $sfd{y} = $s; + } + } + if ( $field = $host->field('001') ) { + $sfd{0} = $field->data(),; + } + $host_field = MARC::Field->new( 461, '0', ' ', %sfd ); + return $host_field; } - if ( $field = $host->field('001') ) { - $sfd{0} = $field->data(),; - } - $host_field = MARC::Field->new( 461, '0', ' ', %sfd ); - return $host_field; } return; } diff --git a/installer/data/mysql/atomicupdate/bug_37061.pl b/installer/data/mysql/atomicupdate/bug_37061.pl new file mode 100755 index 0000000000..37b673cd6e --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_37061.pl @@ -0,0 +1,17 @@ +use Modern::Perl; + +return { + bug_number => "37061", + description => "Add a new system preference PrepareHostField", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + # Do you stuffs here + $dbh->do(q{ + INSERT INTO systempreferences (variable, value, options, type, explanation) + VALUES ('PrepareHostField', '', '', 'Textarea', 'Define YAML rules for copying host data to component parts.');}); + + say $out "Added new system preference 'PrepareHostField'"; + }, +}; 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 ffd9b7e549..7a1ccc344c 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 @@ -346,6 +346,20 @@ Cataloging: dsc: descending. az: from A to Z. za: from Z to A. + - + - "Define rules for copying host data to component parts.
" + - pref: PrepareHostField + type: textarea + syntax: text/x-yaml + class: code + - "Example:
" + - "001: 773$w
" + - "020$a: 773$z
" + - "022$a: 773$x
" + - "028$ba: 773$o
" + - "245$abnpc: 773$t
" + - "264$abc: 773$d
" + - "300$ae: 773$h" Importing: - - When matching on ISBN with the record import tool, -- 2.34.1