Lines 492-573
sub ModifyRecordsWithTemplate {
Link Here
|
492 |
=cut |
492 |
=cut |
493 |
|
493 |
|
494 |
sub ModifyRecordWithTemplate { |
494 |
sub ModifyRecordWithTemplate { |
495 |
my ( $template_id, $record ) = @_; |
495 |
my ( $template_id, $record ) = @_; |
496 |
C4::Koha::Log( "C4::MarcModificationTemplates::ModifyRecordWithTemplate( $template_id, $record )" ) if DEBUG; |
496 |
C4::Koha::Log( "C4::MarcModificationTemplates::ModifyRecordWithTemplate( $template_id, $record )" ) if DEBUG; |
497 |
warn( "C4::MarcModificationTemplates::ModifyRecordWithTemplate( $template_id, $record )" ) if DEBUG; |
497 |
warn( "C4::MarcModificationTemplates::ModifyRecordWithTemplate( $template_id, $record )" ) if DEBUG; |
498 |
C4::Koha::Log( "Unmodified Record:\n" . $record->as_formatted() ) if DEBUG >= 10; |
498 |
C4::Koha::Log( "Unmodified Record:\n" . $record->as_formatted() ) if DEBUG >= 10; |
499 |
warn( "Unmodified Record:\n" . $record->as_formatted() ) if DEBUG >= 10; |
499 |
warn( "Unmodified Record:\n" . $record->as_formatted() ) if DEBUG >= 10; |
500 |
|
500 |
|
501 |
my $current_date = DateTime->now()->ymd(); |
501 |
my $current_date = DateTime->now()->ymd(); |
502 |
my $branchcode = C4::Context->userenv->{branch}; |
502 |
my $branchcode = C4::Context->userenv->{branch}; |
503 |
|
503 |
|
504 |
my @actions = GetModificationTemplateActions( $template_id ); |
504 |
my @actions = GetModificationTemplateActions( $template_id ); |
505 |
|
505 |
|
506 |
foreach my $a ( @actions ) { |
506 |
foreach my $a ( @actions ) { |
507 |
my $action = $a->{'action'}; |
507 |
my $action = $a->{'action'}; |
508 |
my $field_number = $a->{'field_number'}; |
508 |
my $field_number = $a->{'field_number'}; |
509 |
my $from_field = $a->{'from_field'}; |
509 |
my $from_field = $a->{'from_field'}; |
510 |
my $from_subfield = $a->{'from_subfield'}; |
510 |
my $from_subfield = $a->{'from_subfield'}; |
511 |
my $field_value = $a->{'field_value'}; |
511 |
my $field_value = $a->{'field_value'}; |
512 |
my $to_field = $a->{'to_field'}; |
512 |
my $to_field = $a->{'to_field'}; |
513 |
my $to_subfield = $a->{'to_subfield'}; |
513 |
my $to_subfield = $a->{'to_subfield'}; |
514 |
my $to_regex = $a->{'to_regex'}; |
514 |
my $to_regex = $a->{'to_regex'}; |
515 |
my $conditional = $a->{'conditional'}; |
515 |
my $conditional = $a->{'conditional'}; |
516 |
my $conditional_field = $a->{'conditional_field'}; |
516 |
my $conditional_field = $a->{'conditional_field'}; |
517 |
my $conditional_subfield = $a->{'conditional_subfield'}; |
517 |
my $conditional_subfield = $a->{'conditional_subfield'}; |
518 |
my $conditional_comparison = $a->{'conditional_comparison'}; |
518 |
my $conditional_comparison = $a->{'conditional_comparison'}; |
519 |
my $conditional_value = $a->{'conditional_value'}; |
519 |
my $conditional_value = $a->{'conditional_value'}; |
520 |
my $conditional_regex = $a->{'conditional_regex'}; |
520 |
my $conditional_regex = $a->{'conditional_regex'}; |
521 |
|
521 |
|
522 |
my $eval = "$action( \$record, '$from_field', '$from_subfield', "; |
522 |
if ( $field_value ) { |
523 |
|
523 |
$field_value =~ s/__CURRENTDATE__/$current_date/g; |
524 |
if ( $field_value ) { |
524 |
$field_value =~ s/__BRANCHCODE__/$branchcode/g; |
525 |
C4::Koha::Log( "Field value before replacements: $field_value" ) if ( DEBUG >= 3 ); |
525 |
} |
526 |
warn( "Field value before replacements: $field_value" ) if ( DEBUG >= 3 ); |
526 |
|
527 |
|
527 |
my @params = ( $record, $from_field, $from_subfield ); |
528 |
$field_value =~ s/__CURRENTDATE__/$current_date/g; |
528 |
if ( $action eq 'update_field' ) { |
529 |
$field_value =~ s/__BRANCHCODE__/$branchcode/g; |
529 |
push @params, |
530 |
|
530 |
( $field_value |
531 |
$eval .= " undef, " if ( $action eq 'update_field' ); |
531 |
? ( undef, $field_value ) |
532 |
$eval .= " '$field_value' "; |
532 |
: () |
533 |
|
533 |
); |
534 |
C4::Koha::Log( "Field value after replacements: $field_value" ) if ( DEBUG >= 3 ); |
534 |
} else { |
535 |
warn( "Field value after replacements: $field_value" ) if ( DEBUG >= 3 ); |
535 |
push @params, |
536 |
} elsif ( $to_field ) { |
536 |
( $field_value |
537 |
$eval .= " '$to_field', '$to_subfield', '$to_regex' "; |
537 |
? $field_value |
538 |
} |
538 |
: () |
539 |
|
539 |
); |
540 |
$eval .= ", '$field_number' " if ( $field_number ); |
540 |
} |
541 |
$eval .= ') '; |
541 |
push @params, ( |
542 |
|
542 |
( ( not $field_value and $to_field ) |
543 |
if ( $conditional ) { |
543 |
? ( $to_field, $to_subfield, $to_regex ) |
544 |
$eval .= " $conditional ( "; |
544 |
: () ), |
545 |
|
545 |
( $field_number |
546 |
if ( $conditional_comparison eq 'exists' ) { |
546 |
? $field_number |
547 |
$eval .= "field_exists( \$record, '$conditional_field', '$conditional_subfield' )"; |
547 |
: () ) |
548 |
|
548 |
); |
549 |
} elsif ( $conditional_comparison eq 'not_exists' ) { |
549 |
|
550 |
$eval .= "!field_exists( \$record, '$conditional_field', '$conditional_subfield' )"; |
550 |
my $do = 1; |
551 |
|
551 |
if ( $conditional ) { |
552 |
} elsif ( $conditional_comparison eq 'equals' ) { |
552 |
for ( $conditional_comparison ) { |
553 |
$eval .= "field_equals( \$record, '$conditional_value', '$conditional_field', '$conditional_subfield', '$conditional_regex' ) "; |
553 |
when ( /^exists$/ ) { |
554 |
|
554 |
my $exists = field_exists( $record, $conditional_field, $conditional_subfield ); |
555 |
} elsif ( $conditional_comparison eq 'not_equals' ) { |
555 |
$do = $conditional eq 'if' |
556 |
$eval .= "!field_equals( \$record, '$conditional_value', '$conditional_field', '$conditional_subfield', '$conditional_regex' ) "; |
556 |
? $exists |
557 |
} |
557 |
: not $exists; |
558 |
|
558 |
} |
559 |
$eval .= " )"; |
559 |
when ( /^not_exists$/ ) { |
|
|
560 |
my $exists = field_exists( $record, $conditional_field, $conditional_subfield ); |
561 |
$do = $conditional eq 'if' |
562 |
? not $exists |
563 |
: $exists; |
564 |
} |
565 |
when ( /^equals$/ ) { |
566 |
my $equals = field_equals( $record, $conditional_value, $conditional_field, $conditional_subfield, $conditional_regex ); |
567 |
$do = $conditional eq 'if' |
568 |
? $equals |
569 |
: not $equals; |
570 |
} |
571 |
when ( /^not_equals$/ ) { |
572 |
my $equals = field_equals( $record, $conditional_value, $conditional_field, $conditional_subfield, $conditional_regex ); |
573 |
$do = $conditional eq 'if' |
574 |
? not $equals |
575 |
: $equals; |
576 |
} |
577 |
} |
578 |
} |
579 |
|
580 |
if ( $do ) { |
581 |
for ( $action ) { |
582 |
when ( /^copy_field$/ ) { |
583 |
copy_field( @params ); |
584 |
} |
585 |
when ( /^update_field$/ ) { |
586 |
update_field( @params ); |
587 |
} |
588 |
when ( /^move_field$/ ) { |
589 |
move_field( @params ); |
590 |
} |
591 |
when ( /^delete_field$/ ) { |
592 |
delete_field( @params ); |
593 |
} |
594 |
} |
595 |
} |
596 |
|
597 |
C4::Koha::Log( $record->as_formatted() ) if DEBUG >= 10; |
598 |
warn( $record->as_formatted() ) if DEBUG >= 10; |
560 |
} |
599 |
} |
561 |
|
|
|
562 |
$eval .= ";"; |
563 |
|
564 |
C4::Koha::Log("eval $eval") if DEBUG >= 2; |
565 |
warn("eval $eval") if DEBUG >= 2; |
566 |
eval {$eval}; |
567 |
C4::Koha::Log( $record->as_formatted() ) if DEBUG >= 10; |
568 |
warn( $record->as_formatted() ) if DEBUG >= 10; |
569 |
|
570 |
} |
571 |
} |
600 |
} |
572 |
1; |
601 |
1; |
573 |
__END__ |
602 |
__END__ |