From 76b5b7aa83c95c2e9a5d87568da26ddaff090384 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 16 Jan 2018 16:16:40 +0000 Subject: [PATCH] Bug 13560: Add an 'Add' option for marc modification templates Add/Update would update a field or create new if it existed, but didn't allow for creating new if the field existed. This patchset splits the options to 'Add & Update' so that 'Add' will always add a field and 'Update' will operate as it always has To test: 1 - Have a record with a known existing field (make a copy) 2 - Define a marc modification template that 'Add/update' on that field 3 - Define an 'Add/Update' on a field that doesn't exist 4 - Batch modify the copy of record using the above template 5 - Verify the existing field was updated 6 - Verify the non-existing field was updated 7 - Apply patch and update database 8 - Make another copy 9 - Modify the copy with the same template as above 10 - Should match initial modification 11 - Add a new rule to add a new field 12 - Modify using the updated template 13 - Ensure your new field is created 14 - Test various options in the modification tool 15 - prove t/db_dependent/MarcModificationTemplates.t --- C4/MarcModificationTemplates.pm | 9 +++++ Koha/SimpleMARC.pm | 40 ++++++++++++++++++++++ .../modules/tools/marc_modification_templates.tt | 6 ++-- .../prog/js/marc_modification_templates.js | 6 ++++ tools/marc_modification_templates.pl | 1 + 5 files changed, 60 insertions(+), 2 deletions(-) diff --git a/C4/MarcModificationTemplates.pm b/C4/MarcModificationTemplates.pm index bbfb1c6..852f7b3 100644 --- a/C4/MarcModificationTemplates.pm +++ b/C4/MarcModificationTemplates.pm @@ -639,6 +639,15 @@ sub ModifyRecordWithTemplate { field_numbers => $field_numbers, }); } + elsif ( $action eq 'add_field' ) { + add_field({ + record => $record, + field => $from_field, + subfield => $from_subfield, + values => [ $field_value ], + field_numbers => $field_numbers, + }); + } elsif ( $action eq 'update_field' ) { update_field({ record => $record, diff --git a/Koha/SimpleMARC.pm b/Koha/SimpleMARC.pm index aaab6b6..de76cd3 100644 --- a/Koha/SimpleMARC.pm +++ b/Koha/SimpleMARC.pm @@ -17,6 +17,7 @@ our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( read_field + add_field update_field copy_field copy_and_replace_field @@ -172,6 +173,45 @@ sub update_field { } } +=head2 add_field + + add_field({ + record => $record, + field => $fieldName, + subfield => $subfieldName, + values => \@values, + field_numbers => $field_numbers, + }); + + Adds a new field/subfield with supplied value(s). + This function always add a new field as opposed to 'update_field' which will + either update if field exists and add if it does not. + +=cut + + +sub add_field { + my ( $params ) = @_; + my $record = $params->{record}; + my $fieldName = $params->{field}; + my $subfieldName = $params->{subfield}; + my @values = @{ $params->{values} }; + my $field_numbers = $params->{field_numbers} // []; + + if ( ! ( $record && $fieldName ) ) { return; } + if ( $fieldName > 10 ) { + foreach my $value ( @values ) { + my $field = MARC::Field->new( $fieldName, '', '', "$subfieldName" => $value ); + $record->append_fields( $field ); + } + } else { + foreach my $value ( @values ) { + my $field = MARC::Field->new( $fieldName, $value ); + $record->append_fields( $field ); + } + } +} + sub _update_field { my ( $params ) = @_; my $record = $params->{record}; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/marc_modification_templates.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/marc_modification_templates.tt index 228c8a5..1c43cea 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/marc_modification_templates.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/marc_modification_templates.tt @@ -116,7 +116,8 @@ [% ActionsLoo.ordering %] [% IF ( ActionsLoo.action_delete_field ) %] Delete [% END %] - [% IF ( ActionsLoo.action_update_field ) %] Update [% END %] + [% IF ( ActionsLoo.action_add_field ) %] Add new [% END %] + [% IF ( ActionsLoo.action_update_field ) %] Update existing or add new [% END %] [% IF ( ActionsLoo.action_move_field ) %] Move [% END %] [% IF ( ActionsLoo.action_copy_field ) %] Copy [% END %] [% IF ( ActionsLoo.action_copy_and_replace_field ) %] Copy and replace [% END %] @@ -202,7 +203,8 @@