ffzg/recall_notices.pl: added --interval and --dedup
[koha.git] / C4 / MarcModificationTemplates.pm
index 3af0013..da8f0a4 100644 (file)
@@ -23,13 +23,13 @@ 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
@@ -77,7 +77,7 @@ sub GetModificationTemplates {
   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;
@@ -240,6 +240,8 @@ sub AddModificationTemplateAction {
                     $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 +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
@@ -568,9 +573,21 @@ sub ModifyRecordWithTemplate {
                     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'
-                    ? not @$field_numbers
-                    : @$field_numbers;
+                    ? @$field_numbers
+                    : not @$field_numbers;
             }
         }
 
@@ -612,6 +629,30 @@ sub ModifyRecordWithTemplate {
                     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,