From 1b41f98199f67421eb5418e943dd1a7370c778b4 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Wed, 5 Jul 2017 16:12:13 +0000 Subject: [PATCH] Bug 13560 - need an add option in 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 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 | 25 ++++++++ installer/data/mysql/atomicupdate/bug13560.perl | 6 ++ .../modules/tools/marc_modification_templates.tt | 4 +- .../prog/js/marc_modification_templates.js | 6 ++ t/db_dependent/MarcModificationTemplates.t | 75 ++++++++++++++++++---- tools/marc_modification_templates.pl | 1 + 7 files changed, 114 insertions(+), 12 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug13560.perl diff --git a/C4/MarcModificationTemplates.pm b/C4/MarcModificationTemplates.pm index cbd4204..e245784 100644 --- a/C4/MarcModificationTemplates.pm +++ b/C4/MarcModificationTemplates.pm @@ -626,6 +626,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..81b92a7 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,30 @@ sub update_field { } } +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 ); + warn Data::Dumper::Dumper( $field ); + $record->append_fields( $field ); + } + } else { + foreach my $value ( @values ) { + my $field = MARC::Field->new( $fieldName, $value ); + warn Data::Dumper::Dumper( $field ); + $record->append_fields( $field ); + } + } +} + sub _update_field { my ( $params ) = @_; my $record = $params->{record}; diff --git a/installer/data/mysql/atomicupdate/bug13560.perl b/installer/data/mysql/atomicupdate/bug13560.perl new file mode 100644 index 0000000..81e39bc --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug13560.perl @@ -0,0 +1,6 @@ +$DBversion = 'XXX'; # will be replaced by the RM +if( CheckVersion( $DBversion ) ) { + $dbh->do( "ALTER TABLE marc_modification_template_actions CHANGE action action ENUM('delete_field','add_field','update_field','move_field','copy_field','copy_and_replace_field')" ); + SetVersion( $DBversion ); + print "Upgrade to $DBversion done (Bug 13560 - need an add option in marc modification templates)\n"; +} 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 6d32500..36936d5 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 @@ -132,6 +132,7 @@ [% ActionsLoo.ordering %] [% IF ( ActionsLoo.action_delete_field ) %] Delete [% END %] + [% IF ( ActionsLoo.action_add_field ) %] Add [% END %] [% IF ( ActionsLoo.action_update_field ) %] Update [% END %] [% IF ( ActionsLoo.action_move_field ) %] Move [% END %] [% IF ( ActionsLoo.action_copy_field ) %] Copy [% END %] @@ -218,7 +219,8 @@