Bug 21290: Updating documentation for ModItem
[koha.git] / C4 / MarcModificationTemplates.pm
index d26b76d..da8f0a4 100644 (file)
@@ -17,24 +17,19 @@ package C4::MarcModificationTemplates;
 # You should have received a copy of the GNU General Public License
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-## NOTE:
-## Parts of this module are used from cgi scripts that are detached from apache before
-## execution. For this reason, the C4::Koha::Log function has been used to capture
-## output for debugging purposes.
-
 use Modern::Perl;
 
 use DateTime;
 
 use C4::Context;
 use Koha::SimpleMARC;
+use Koha::MoreUtils;
 
-use vars qw($VERSION @ISA @EXPORT);
+use vars qw(@ISA @EXPORT);
 
 use constant DEBUG => 0;
 
 BEGIN {
-    $VERSION = 1.00;    # set the version for version checking
     @ISA = qw(Exporter);
     @EXPORT = qw(
         &GetModificationTemplates
@@ -71,23 +66,24 @@ files telling Koha what fields to insert data into.
 
 =head2 GetModificationTemplates
 
-  my @templates = GetModificationTemplates( [ $template_id ] );
+  my @templates = GetModificationTemplates( $template_id );
+
+  Passing optional $template_id marks it as the selected template.
 
-  Passing a $template_id will mark the given id as the selected template.
 =cut
 
 sub GetModificationTemplates {
   my ( $template_id ) = @_;
-  C4::Koha::Log("C4::MarcModificationTemplates::GetModificationTemplates( $template_id )") if DEBUG;
   warn("C4::MarcModificationTemplates::GetModificationTemplates( $template_id )") if DEBUG;
 
   my $dbh = C4::Context->dbh;
-  my $sth = $dbh->prepare("SELECT * FROM marc_modification_templates");
+  my $sth = $dbh->prepare("SELECT * FROM marc_modification_templates ORDER BY name");
   $sth->execute();
 
   my @templates;
   while ( my $template = $sth->fetchrow_hashref() ) {
-    $template->{'selected'} = 1 if ( $template->{'template_id'} eq $template_id );
+    $template->{'selected'} = 1
+        if $template_id && $template->{'template_id'} eq $template_id;
     push( @templates, $template );
   }
 
@@ -185,7 +181,6 @@ sub GetModificationTemplateAction {
 sub GetModificationTemplateActions {
   my ( $template_id ) = @_;
 
-  C4::Koha::Log( "C4::MarcModificationTemplates::GetModificationTemplateActions( $template_id )" ) if DEBUG;
   warn( "C4::MarcModificationTemplates::GetModificationTemplateActions( $template_id )" ) if DEBUG;
 
   my $dbh = C4::Context->dbh;
@@ -197,7 +192,6 @@ sub GetModificationTemplateActions {
     push( @actions, $action );
   }
 
-  C4::Koha::Log( Data::Dumper::Dumper( @actions ) ) if DEBUG > 4;
   warn( Data::Dumper::Dumper( @actions ) ) if DEBUG > 4;
 
   return @actions;
@@ -241,15 +235,13 @@ sub AddModificationTemplateAction {
     $description
   ) = @_;
 
-  C4::Koha::Log( "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;
   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;
@@ -347,6 +339,9 @@ sub ModModificationTemplateAction {
   ) = @_;
 
   my $dbh = C4::Context->dbh;
+  $conditional ||= undef;
+  $conditional_comparison ||= undef;
+  $conditional_regex ||= '0';
 
   my $query = "
   UPDATE marc_modification_template_actions SET
@@ -485,7 +480,6 @@ sub MoveModificationTemplateAction {
 
 sub ModifyRecordsWithTemplate {
   my ( $template_id, $batch ) = @_;
-  C4::Koha::Log( "C4::MarcModificationTemplates::ModifyRecordsWithTemplate( $template_id, $batch )" ) if DEBUG;
   warn( "C4::MarcModificationTemplates::ModifyRecordsWithTemplate( $template_id, $batch )" ) if DEBUG;
 
   while ( my $record = $batch->next() ) {
@@ -504,19 +498,18 @@ sub ModifyRecordsWithTemplate {
 
 sub ModifyRecordWithTemplate {
     my ( $template_id, $record ) = @_;
-    C4::Koha::Log( "C4::MarcModificationTemplates::ModifyRecordWithTemplate( $template_id, $record )" ) if DEBUG;
     warn( "C4::MarcModificationTemplates::ModifyRecordWithTemplate( $template_id, $record )" ) if DEBUG;
-    C4::Koha::Log( "Unmodified Record:\n" . $record->as_formatted() ) if DEBUG >= 10;
     warn( "Unmodified Record:\n" . $record->as_formatted() ) if DEBUG >= 10;
 
     my $current_date = DateTime->now()->ymd();
-    my $branchcode = C4::Context->userenv->{branch};
+    my $branchcode = '';
+    $branchcode = C4::Context->userenv->{branch} if C4::Context->userenv;
 
     my @actions = GetModificationTemplateActions( $template_id );
 
     foreach my $a ( @actions ) {
         my $action = $a->{'action'};
-        my $field_number = $a->{'field_number'};
+        my $field_number = $a->{'field_number'} // 1;
         my $from_field = $a->{'from_field'};
         my $from_subfield = $a->{'from_subfield'};
         my $field_value = $a->{'field_value'};
@@ -537,79 +530,167 @@ sub ModifyRecordWithTemplate {
             $field_value =~ s/__BRANCHCODE__/$branchcode/g;
         }
 
-        my @params = ( $record, $from_field, $from_subfield );
-        if ( $action eq 'update_field' ) {
-            push @params,
-                ( $field_value
-                    ? ( undef, $field_value )
-                    : ()
-                );
-        } else {
-            push @params,
-                ( $field_value
-                    ? $field_value
-                    : ()
-                );
-        }
-        push @params, (
-                ( ( not $field_value and $to_field )
-                    ? ( $to_field, $to_subfield, { search => $to_regex_search, replace => $to_regex_replace, modifiers => $to_regex_modifiers} )
-                    : () ),
-                ( $field_number
-                    ? $field_number
-                    : () )
-        );
-
         my $do = 1;
+        my $field_numbers = [];
         if ( $conditional ) {
-            for ( $conditional_comparison ) {
-                when ( /^exists$/ ) {
-                    my $exists = field_exists( $record, $conditional_field, $conditional_subfield );
-                    $do = $conditional eq 'if'
-                        ? $exists
-                        : not $exists;
-                }
-                when ( /^not_exists$/ ) {
-                    my $exists = field_exists( $record, $conditional_field, $conditional_subfield );
-                    $do = $conditional eq 'if'
-                        ? not $exists
-                        : $exists;
-                }
-                when ( /^equals$/ ) {
-                    my $equals = field_equals( $record, $conditional_value, $conditional_field, $conditional_subfield, $conditional_regex );
-                    $do = $conditional eq 'if'
-                        ? $equals
-                        : not $equals;
-                }
-                when ( /^not_equals$/ ) {
-                    my $equals = field_equals( $record, $conditional_value, $conditional_field, $conditional_subfield, $conditional_regex );
-                    $do = $conditional eq 'if'
-                        ? not $equals
-                        : $equals;
-                }
+            if ( $conditional_comparison eq 'exists' ) {
+                $field_numbers = field_exists({
+                        record => $record,
+                        field => $conditional_field,
+                        subfield => $conditional_subfield,
+                    });
+                $do = $conditional eq 'if'
+                    ? @$field_numbers
+                    : not @$field_numbers;
+            }
+            elsif ( $conditional_comparison eq 'not_exists' ) {
+                $field_numbers = field_exists({
+                        record => $record,
+                        field => $conditional_field,
+                        subfield => $conditional_subfield
+                    });
+                $do = $conditional eq 'if'
+                    ? not @$field_numbers
+                    : @$field_numbers;
+            }
+            elsif ( $conditional_comparison eq 'equals' ) {
+                $field_numbers = field_equals({
+                    record => $record,
+                    value => $conditional_value,
+                    field => $conditional_field,
+                    subfield => $conditional_subfield,
+                    is_regex => $conditional_regex,
+                });
+                $do = $conditional eq 'if'
+                    ? @$field_numbers
+                    : not @$field_numbers;
+            }
+            elsif ( $conditional_comparison eq 'not_equals' ) {
+                $field_numbers = field_equals({
+                    record => $record,
+                    value => $conditional_value,
+                    field => $conditional_field,
+                    subfield => $conditional_subfield,
+                    is_regex => $conditional_regex,
+                });
+                my $all_fields = [
+                    1 .. scalar @{
+                        field_exists(
+                            {
+                                record   => $record,
+                                field    => $conditional_field,
+                                subfield => $conditional_subfield
+                            }
+                        )
+                    }
+                ];
+                $field_numbers = [Koha::MoreUtils::singleton ( @$field_numbers, @$all_fields ) ];
+                $do = $conditional eq 'if'
+                    ? @$field_numbers
+                    : not @$field_numbers;
             }
         }
 
         if ( $do ) {
-            for ( $action ) {
-                when ( /^copy_field$/ ) {
-                    copy_field( @params );
-                }
-                when ( /^update_field$/ ) {
-                    update_field( @params );
-                }
-                when ( /^move_field$/ ) {
-                    move_field( @params );
+
+            # field_number == 0 if all field need to be updated
+            # or 1 if only the first field need to be updated
+
+            # A condition has been given
+            if ( @$field_numbers > 0 ) {
+                if ( $field_number == 1 ) {
+                    # We want only the first matching
+                    $field_numbers = [ $field_numbers->[0] ];
                 }
-                when ( /^delete_field$/ ) {
-                    delete_field( @params );
+            }
+            # There was no condition
+            else {
+                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 ];
                 }
             }
+
+            if ( $action eq 'copy_field' ) {
+                copy_field({
+                    record => $record,
+                    from_field => $from_field,
+                    from_subfield => $from_subfield,
+                    to_field => $to_field,
+                    to_subfield => $to_subfield,
+                    regex => {
+                        search => $to_regex_search,
+                        replace => $to_regex_replace,
+                        modifiers => $to_regex_modifiers
+                    },
+                    field_numbers => $field_numbers,
+                });
+            }
+            elsif ( $action eq 'copy_and_replace_field' ) {
+                copy_and_replace_field({
+                    record => $record,
+                    from_field => $from_field,
+                    from_subfield => $from_subfield,
+                    to_field => $to_field,
+                    to_subfield => $to_subfield,
+                    regex => {
+                        search => $to_regex_search,
+                        replace => $to_regex_replace,
+                        modifiers => $to_regex_modifiers
+                    },
+                    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,
+                    field => $from_field,
+                    subfield => $from_subfield,
+                    values => [ $field_value ],
+                    field_numbers => $field_numbers,
+                });
+            }
+            elsif ( $action eq 'move_field' ) {
+                move_field({
+                    record => $record,
+                    from_field => $from_field,
+                    from_subfield => $from_subfield,
+                    to_field => $to_field,
+                    to_subfield => $to_subfield,
+                    regex => {
+                        search => $to_regex_search,
+                        replace => $to_regex_replace,
+                        modifiers => $to_regex_modifiers
+                    },
+                    field_numbers => $field_numbers,
+                });
+            }
+            elsif ( $action eq 'delete_field' ) {
+                delete_field({
+                    record => $record,
+                    field => $from_field,
+                    subfield => $from_subfield,
+                    field_numbers => $field_numbers,
+                });
+            }
         }
 
-        C4::Koha::Log( $record->as_formatted() ) if DEBUG >= 10;
         warn( $record->as_formatted() ) if DEBUG >= 10;
     }
+
+    return;
 }
 1;
 __END__