Bug 13560: Add an 'Add' option for marc modification templates
authorNick Clemens <nick@bywatersolutions.com>
Tue, 16 Jan 2018 16:16:40 +0000 (16:16 +0000)
committerNick Clemens <nick@bywatersolutions.com>
Thu, 19 Jul 2018 17:22:18 +0000 (17:22 +0000)
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

Signed-off-by: Victor Grousset <victor.grousset@biblibre.com>
Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

C4/MarcModificationTemplates.pm
Koha/SimpleMARC.pm
koha-tmpl/intranet-tmpl/prog/en/modules/tools/marc_modification_templates.tt
koha-tmpl/intranet-tmpl/prog/js/marc_modification_templates.js
tools/marc_modification_templates.pl

index 3932e80..da8f0a4 100644 (file)
@@ -644,6 +644,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,
index aaab6b6..de76cd3 100644 (file)
@@ -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};
index b281356..e364588 100644 (file)
                                         <td>[% ActionsLoo.ordering %]</td>
                                         <td>
                                             [% 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 %]
 
                             <select name="action" id="action" onchange="onActionChange(this);">
                                 <option value="delete_field">Delete</option>
-                                <option value="update_field">Add/Update</option>
+                                <option value="add_field">Add new</option>
+                                <option value="update_field">Update existing or add new</option>
                                 <option value="move_field">Move</option>
                                 <option value="copy_field">Copy</option>
                                 <option value="copy_and_replace_field">Copy and replace</option>
index 8ca256b..93b2a43 100644 (file)
@@ -105,6 +105,12 @@ function onActionChange(selectObj) {
             hide('to_field_block');
             break;
 
+        case 'add_field':
+            hide('field_number_block');
+            show('with_value_block');
+            hide('to_field_block');
+            break;
+
         case 'update_field':
             hide('field_number_block');
             show('with_value_block');
index c115429..f88f7d5 100755 (executable)
@@ -116,6 +116,7 @@ my @templates = GetModificationTemplates( $template_id );
 my @actions = GetModificationTemplateActions( $template_id );
 foreach my $action ( @actions ) {
   $action->{'action_delete_field'} = ( $action->{'action'} eq 'delete_field' );
+  $action->{'action_add_field'} = ( $action->{'action'} eq 'add_field' );
   $action->{'action_update_field'} = ( $action->{'action'} eq 'update_field' );
   $action->{'action_move_field'} = ( $action->{'action'} eq 'move_field' );
   $action->{'action_copy_field'} = ( $action->{'action'} eq 'copy_field' );