Bug 17600: Standardize our EXPORT_OK
[srvgit] / C4 / MarcModificationTemplates.pm
index bbfb1c6..13b81ca 100644 (file)
@@ -22,30 +22,38 @@ use Modern::Perl;
 use DateTime;
 
 use C4::Context;
-use Koha::SimpleMARC;
+use Koha::SimpleMARC qw(
+    add_field
+    copy_and_replace_field
+    copy_field
+    delete_field
+    field_equals
+    field_exists
+    move_field
+    update_field
+);
 use Koha::MoreUtils;
+use Koha::DateUtils qw( dt_from_string );
 
 use vars qw(@ISA @EXPORT);
 
-use constant DEBUG => 0;
-
 BEGIN {
-    @ISA = qw(Exporter);
+    @ISA    = qw(Exporter);
     @EXPORT = qw(
-        &GetModificationTemplates
-        &AddModificationTemplate
-        &DelModificationTemplate
+      GetModificationTemplates
+      AddModificationTemplate
+      DelModificationTemplate
 
-        &GetModificationTemplateAction
-        &GetModificationTemplateActions
+      GetModificationTemplateAction
+      GetModificationTemplateActions
 
-        &AddModificationTemplateAction
-        &ModModificationTemplateAction
-        &DelModificationTemplateAction
-        &MoveModificationTemplateAction
+      AddModificationTemplateAction
+      ModModificationTemplateAction
+      DelModificationTemplateAction
+      MoveModificationTemplateAction
 
-        &ModifyRecordsWithTemplate
-        &ModifyRecordWithTemplate
+      ModifyRecordsWithTemplate
+      ModifyRecordWithTemplate
     );
 }
 
@@ -74,7 +82,6 @@ files telling Koha what fields to insert data into.
 
 sub GetModificationTemplates {
   my ( $template_id ) = @_;
-  warn("C4::MarcModificationTemplates::GetModificationTemplates( $template_id )") if DEBUG;
 
   my $dbh = C4::Context->dbh;
   my $sth = $dbh->prepare("SELECT * FROM marc_modification_templates ORDER BY name");
@@ -181,8 +188,6 @@ sub GetModificationTemplateAction {
 sub GetModificationTemplateActions {
   my ( $template_id ) = @_;
 
-  warn( "C4::MarcModificationTemplates::GetModificationTemplateActions( $template_id )" ) if DEBUG;
-
   my $dbh = C4::Context->dbh;
   my $sth = $dbh->prepare("SELECT * FROM marc_modification_template_actions WHERE template_id = ? ORDER BY ordering");
   $sth->execute( $template_id );
@@ -192,8 +197,6 @@ sub GetModificationTemplateActions {
     push( @actions, $action );
   }
 
-  warn( Data::Dumper::Dumper( @actions ) ) if DEBUG > 4;
-
   return @actions;
 }
 
@@ -235,11 +238,8 @@ sub AddModificationTemplateAction {
     $description
   ) = @_;
 
-  warn( "C4::MarcModificationTemplates::AddModificationTemplateAction( $template_id, $action,
-                    $field_number, $from_field, $from_subfield, $field_value, $to_field, $to_subfield,
-                    $to_regex_search, $to_regex_replace, $to_regex_modifiers, $conditional, $conditional_field, $conditional_subfield, $conditional_comparison,
-                    $conditional_value, $conditional_regex, $description )" ) if DEBUG;
-
+  $conditional ||= undef;
+  $conditional_comparison ||= undef;
   $conditional_regex ||= '0';
 
   my $dbh = C4::Context->dbh;
@@ -337,6 +337,9 @@ sub ModModificationTemplateAction {
   ) = @_;
 
   my $dbh = C4::Context->dbh;
+  $conditional ||= undef;
+  $conditional_comparison ||= undef;
+  $conditional_regex ||= '0';
 
   my $query = "
   UPDATE marc_modification_template_actions SET
@@ -475,7 +478,6 @@ sub MoveModificationTemplateAction {
 
 sub ModifyRecordsWithTemplate {
   my ( $template_id, $batch ) = @_;
-  warn( "C4::MarcModificationTemplates::ModifyRecordsWithTemplate( $template_id, $batch )" ) if DEBUG;
 
   while ( my $record = $batch->next() ) {
     ModifyRecordWithTemplate( $template_id, $record );
@@ -493,10 +495,8 @@ sub ModifyRecordsWithTemplate {
 
 sub ModifyRecordWithTemplate {
     my ( $template_id, $record ) = @_;
-    warn( "C4::MarcModificationTemplates::ModifyRecordWithTemplate( $template_id, $record )" ) if DEBUG;
-    warn( "Unmodified Record:\n" . $record->as_formatted() ) if DEBUG >= 10;
 
-    my $current_date = DateTime->now()->ymd();
+    my $current_date = dt_from_string()->ymd();
     my $branchcode = '';
     $branchcode = C4::Context->userenv->{branch} if C4::Context->userenv;
 
@@ -580,9 +580,15 @@ sub ModifyRecordWithTemplate {
                     }
                 ];
                 $field_numbers = [Koha::MoreUtils::singleton ( @$field_numbers, @$all_fields ) ];
-                $do = $conditional eq 'if'
-                    ? @$field_numbers
-                    : not @$field_numbers;
+                if ( $from_field == $conditional_field ){
+                    $do = $conditional eq 'if'
+                        ? @$field_numbers
+                        : not @$field_numbers;
+                } else {
+                    $do = $conditional eq 'if'
+                        ? not @$field_numbers
+                        : @$field_numbers;
+                }
             }
         }
 
@@ -594,8 +600,23 @@ sub ModifyRecordWithTemplate {
             # A condition has been given
             if ( @$field_numbers > 0 ) {
                 if ( $field_number == 1 ) {
-                    # We want only the first matching
-                    $field_numbers = [ $field_numbers->[0] ];
+                    # We want only the first
+                    if ( $from_field == $conditional_field ){
+                        # want first field matching condition
+                        $field_numbers = [ $field_numbers->[0] ];
+                    } else {
+                        # condition doesn't match, so just want first occurrence of from field
+                        $field_numbers = [ 1 ];
+                    }
+                } else {
+                    unless ( $from_field == $conditional_field ){
+                        # condition doesn't match from fields so need all occurrences of from fields for action
+                        $field_numbers = field_exists({
+                            record => $record,
+                            field => $from_field,
+                            subfield => $from_subfield,
+                        });
+                    }
                 }
             }
             # There was no condition
@@ -603,9 +624,6 @@ sub ModifyRecordWithTemplate {
                 if ( $field_number == 1 ) {
                     # We want to process the first field
                     $field_numbers = [ 1 ];
-                } elsif ( $to_field and $from_field ne $to_field ) {
-                    # If the from and to fields are not the same, we only process the first field.
-                    $field_numbers = [ 1 ];
                 }
             }
 
@@ -639,6 +657,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,
@@ -672,8 +699,6 @@ sub ModifyRecordWithTemplate {
                 });
             }
         }
-
-        warn( $record->as_formatted() ) if DEBUG >= 10;
     }
 
     return;