Lines 1-29
Link Here
|
1 |
package C4::MarcModificationTemplates; |
1 |
package C4::MarcModificationTemplates; |
2 |
|
2 |
|
3 |
# Copyright 2010 Kyle M Hall <kyle.m.hall@gmail.com> |
|
|
4 |
# |
5 |
# This file is part of Koha. |
3 |
# This file is part of Koha. |
6 |
# |
4 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
5 |
# Copyright 2010 Kyle M Hall <kyle.m.hall@gmail.com> |
8 |
# terms of the GNU General Public License as published by the Free Software |
6 |
# |
9 |
# Foundation; either version 2 of the License, or (at your option) any later |
7 |
# Koha is free software; you can redistribute it and/or modify it |
10 |
# version. |
8 |
# under the terms of the GNU General Public License as published by |
|
|
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
15 |
# GNU General Public License for more details. |
15 |
# |
16 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# You should have received a copy of the GNU General Public License |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
|
|
19 |
|
19 |
|
20 |
## NOTE: |
20 |
## NOTE: |
21 |
## Parts of this module are used from cgi scripts that are detached from apache before |
21 |
## Parts of this module are used from cgi scripts that are detached from apache before |
22 |
## execution. For this reason, the C4::Koha::Log function has been used to capture |
22 |
## execution. For this reason, the C4::Koha::Log function has been used to capture |
23 |
## output for debugging purposes. |
23 |
## output for debugging purposes. |
24 |
|
24 |
|
25 |
use strict; |
25 |
use Modern::Perl; |
26 |
use warnings; |
|
|
27 |
|
26 |
|
28 |
use DateTime; |
27 |
use DateTime; |
29 |
|
28 |
|
Lines 128-134
sub AddModificationTemplate {
Link Here
|
128 |
$action->{'field_value'}, |
127 |
$action->{'field_value'}, |
129 |
$action->{'to_field'}, |
128 |
$action->{'to_field'}, |
130 |
$action->{'to_subfield'}, |
129 |
$action->{'to_subfield'}, |
131 |
$action->{'to_regex'}, |
130 |
$action->{'to_regex_search'}, |
|
|
131 |
$action->{'to_regex_replace'}, |
132 |
$action->{'to_regex_modifiers'}, |
132 |
$action->{'conditional'}, |
133 |
$action->{'conditional'}, |
133 |
$action->{'conditional_field'}, |
134 |
$action->{'conditional_field'}, |
134 |
$action->{'conditional_subfield'}, |
135 |
$action->{'conditional_subfield'}, |
Lines 156-164
sub DelModificationTemplate {
Link Here
|
156 |
my $dbh = C4::Context->dbh; |
157 |
my $dbh = C4::Context->dbh; |
157 |
my $sth = $dbh->prepare("DELETE FROM marc_modification_templates WHERE template_id = ?"); |
158 |
my $sth = $dbh->prepare("DELETE FROM marc_modification_templates WHERE template_id = ?"); |
158 |
$sth->execute( $template_id ); |
159 |
$sth->execute( $template_id ); |
159 |
|
|
|
160 |
$sth = $dbh->prepare("DELETE FROM marc_modification_template_actions WHERE template_id = ?"); |
161 |
$sth->execute( $template_id ); |
162 |
} |
160 |
} |
163 |
|
161 |
|
164 |
=head2 |
162 |
=head2 |
Lines 211-217
sub GetModificationTemplateActions {
Link Here
|
211 |
AddModificationTemplateAction( |
209 |
AddModificationTemplateAction( |
212 |
$template_id, $action, $field_number, |
210 |
$template_id, $action, $field_number, |
213 |
$from_field, $from_subfield, $field_value, |
211 |
$from_field, $from_subfield, $field_value, |
214 |
$to_field, $to_subfield, $to_regex, |
212 |
$to_field, $to_subfield, $to_regex_search, $to_regex_replace, $to_regex_modifiers |
215 |
$conditional, $conditional_field, $conditional_subfield, |
213 |
$conditional, $conditional_field, $conditional_subfield, |
216 |
$conditional_comparison, $conditional_value, |
214 |
$conditional_comparison, $conditional_value, |
217 |
$conditional_regex, $description |
215 |
$conditional_regex, $description |
Lines 231-237
sub AddModificationTemplateAction {
Link Here
|
231 |
$field_value, |
229 |
$field_value, |
232 |
$to_field, |
230 |
$to_field, |
233 |
$to_subfield, |
231 |
$to_subfield, |
234 |
$to_regex, |
232 |
$to_regex_search, |
|
|
233 |
$to_regex_replace, |
234 |
$to_regex_modifiers, |
235 |
$conditional, |
235 |
$conditional, |
236 |
$conditional_field, |
236 |
$conditional_field, |
237 |
$conditional_subfield, |
237 |
$conditional_subfield, |
Lines 243-253
sub AddModificationTemplateAction {
Link Here
|
243 |
|
243 |
|
244 |
C4::Koha::Log( "C4::MarcModificationTemplates::AddModificationTemplateAction( $template_id, $action, |
244 |
C4::Koha::Log( "C4::MarcModificationTemplates::AddModificationTemplateAction( $template_id, $action, |
245 |
$field_number, $from_field, $from_subfield, $field_value, $to_field, $to_subfield, |
245 |
$field_number, $from_field, $from_subfield, $field_value, $to_field, $to_subfield, |
246 |
$to_regex, $conditional, $conditional_field, $conditional_subfield, $conditional_comparison, |
246 |
$to_regex_search, $to_regex_replace, $to_regex_modifiers, $conditional, $conditional_field, $conditional_subfield, $conditional_comparison, |
247 |
$conditional_value, $conditional_regex, $description )" ) if DEBUG; |
247 |
$conditional_value, $conditional_regex, $description )" ) if DEBUG; |
248 |
warn( "C4::MarcModificationTemplates::AddModificationTemplateAction( $template_id, $action, |
248 |
warn( "C4::MarcModificationTemplates::AddModificationTemplateAction( $template_id, $action, |
249 |
$field_number, $from_field, $from_subfield, $field_value, $to_field, $to_subfield, |
249 |
$field_number, $from_field, $from_subfield, $field_value, $to_field, $to_subfield, |
250 |
$to_regex, $conditional, $conditional_field, $conditional_subfield, $conditional_comparison, |
250 |
$to_regex_search, $to_regex_replace, $to_regex_modifiers, $conditional, $conditional_field, $conditional_subfield, $conditional_comparison, |
251 |
$conditional_value, $conditional_regex, $description )" ) if DEBUG; |
251 |
$conditional_value, $conditional_regex, $description )" ) if DEBUG; |
252 |
|
252 |
|
253 |
$conditional_regex ||= '0'; |
253 |
$conditional_regex ||= '0'; |
Lines 270-276
sub AddModificationTemplateAction {
Link Here
|
270 |
field_value, |
270 |
field_value, |
271 |
to_field, |
271 |
to_field, |
272 |
to_subfield, |
272 |
to_subfield, |
273 |
to_regex, |
273 |
to_regex_search, |
|
|
274 |
to_regex_replace, |
275 |
to_regex_modifiers, |
274 |
conditional, |
276 |
conditional, |
275 |
conditional_field, |
277 |
conditional_field, |
276 |
conditional_subfield, |
278 |
conditional_subfield, |
Lines 279-285
sub AddModificationTemplateAction {
Link Here
|
279 |
conditional_regex, |
281 |
conditional_regex, |
280 |
description |
282 |
description |
281 |
) |
283 |
) |
282 |
VALUES ( NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )"; |
284 |
VALUES ( NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )"; |
283 |
|
285 |
|
284 |
$sth = $dbh->prepare( $query ); |
286 |
$sth = $dbh->prepare( $query ); |
285 |
|
287 |
|
Lines 293-299
sub AddModificationTemplateAction {
Link Here
|
293 |
$field_value, |
295 |
$field_value, |
294 |
$to_field, |
296 |
$to_field, |
295 |
$to_subfield, |
297 |
$to_subfield, |
296 |
$to_regex, |
298 |
$to_regex_search, |
|
|
299 |
$to_regex_replace, |
300 |
$to_regex_modifiers, |
297 |
$conditional, |
301 |
$conditional, |
298 |
$conditional_field, |
302 |
$conditional_field, |
299 |
$conditional_subfield, |
303 |
$conditional_subfield, |
Lines 310-316
sub AddModificationTemplateAction {
Link Here
|
310 |
ModModificationTemplateAction( |
314 |
ModModificationTemplateAction( |
311 |
$mmta_id, $action, $field_number, $from_field, |
315 |
$mmta_id, $action, $field_number, $from_field, |
312 |
$from_subfield, $field_value, $to_field, |
316 |
$from_subfield, $field_value, $to_field, |
313 |
$to_subfield, $to_regex, $conditional, |
317 |
$to_subfield, $to_regex_search, $to_regex_replace, $to_regex_modifiers, $conditional, |
314 |
$conditional_field, $conditional_subfield, |
318 |
$conditional_field, $conditional_subfield, |
315 |
$conditional_comparison, $conditional_value, |
319 |
$conditional_comparison, $conditional_value, |
316 |
$conditional_regex, $description |
320 |
$conditional_regex, $description |
Lines 330-336
sub ModModificationTemplateAction {
Link Here
|
330 |
$field_value, |
334 |
$field_value, |
331 |
$to_field, |
335 |
$to_field, |
332 |
$to_subfield, |
336 |
$to_subfield, |
333 |
$to_regex, |
337 |
$to_regex_search, |
|
|
338 |
$to_regex_replace, |
339 |
$to_regex_modifiers, |
334 |
$conditional, |
340 |
$conditional, |
335 |
$conditional_field, |
341 |
$conditional_field, |
336 |
$conditional_subfield, |
342 |
$conditional_subfield, |
Lines 351-357
sub ModModificationTemplateAction {
Link Here
|
351 |
field_value = ?, |
357 |
field_value = ?, |
352 |
to_field = ?, |
358 |
to_field = ?, |
353 |
to_subfield = ?, |
359 |
to_subfield = ?, |
354 |
to_regex = ?, |
360 |
to_regex_search = ?, |
|
|
361 |
to_regex_replace = ?, |
362 |
to_regex_modifiers = ?, |
355 |
conditional = ?, |
363 |
conditional = ?, |
356 |
conditional_field = ?, |
364 |
conditional_field = ?, |
357 |
conditional_subfield = ?, |
365 |
conditional_subfield = ?, |
Lines 371-377
sub ModModificationTemplateAction {
Link Here
|
371 |
$field_value, |
379 |
$field_value, |
372 |
$to_field, |
380 |
$to_field, |
373 |
$to_subfield, |
381 |
$to_subfield, |
374 |
$to_regex, |
382 |
$to_regex_search, |
|
|
383 |
$to_regex_replace, |
384 |
$to_regex_modifiers, |
375 |
$conditional, |
385 |
$conditional, |
376 |
$conditional_field, |
386 |
$conditional_field, |
377 |
$conditional_subfield, |
387 |
$conditional_subfield, |
Lines 493-574
sub ModifyRecordsWithTemplate {
Link Here
|
493 |
=cut |
503 |
=cut |
494 |
|
504 |
|
495 |
sub ModifyRecordWithTemplate { |
505 |
sub ModifyRecordWithTemplate { |
496 |
my ( $template_id, $record ) = @_; |
506 |
my ( $template_id, $record ) = @_; |
497 |
C4::Koha::Log( "C4::MarcModificationTemplates::ModifyRecordWithTemplate( $template_id, $record )" ) if DEBUG; |
507 |
C4::Koha::Log( "C4::MarcModificationTemplates::ModifyRecordWithTemplate( $template_id, $record )" ) if DEBUG; |
498 |
warn( "C4::MarcModificationTemplates::ModifyRecordWithTemplate( $template_id, $record )" ) if DEBUG; |
508 |
warn( "C4::MarcModificationTemplates::ModifyRecordWithTemplate( $template_id, $record )" ) if DEBUG; |
499 |
C4::Koha::Log( "Unmodified Record:\n" . $record->as_formatted() ) if DEBUG >= 10; |
509 |
C4::Koha::Log( "Unmodified Record:\n" . $record->as_formatted() ) if DEBUG >= 10; |
500 |
warn( "Unmodified Record:\n" . $record->as_formatted() ) if DEBUG >= 10; |
510 |
warn( "Unmodified Record:\n" . $record->as_formatted() ) if DEBUG >= 10; |
501 |
|
511 |
|
502 |
my $current_date = DateTime->now()->ymd(); |
512 |
my $current_date = DateTime->now()->ymd(); |
503 |
my $branchcode = C4::Context->userenv->{branch}; |
513 |
my $branchcode = C4::Context->userenv->{branch}; |
504 |
|
514 |
|
505 |
my @actions = GetModificationTemplateActions( $template_id ); |
515 |
my @actions = GetModificationTemplateActions( $template_id ); |
506 |
|
516 |
|
507 |
foreach my $a ( @actions ) { |
517 |
foreach my $a ( @actions ) { |
508 |
my $action = $a->{'action'}; |
518 |
my $action = $a->{'action'}; |
509 |
my $field_number = $a->{'field_number'}; |
519 |
my $field_number = $a->{'field_number'}; |
510 |
my $from_field = $a->{'from_field'}; |
520 |
my $from_field = $a->{'from_field'}; |
511 |
my $from_subfield = $a->{'from_subfield'}; |
521 |
my $from_subfield = $a->{'from_subfield'}; |
512 |
my $field_value = $a->{'field_value'}; |
522 |
my $field_value = $a->{'field_value'}; |
513 |
my $to_field = $a->{'to_field'}; |
523 |
my $to_field = $a->{'to_field'}; |
514 |
my $to_subfield = $a->{'to_subfield'}; |
524 |
my $to_subfield = $a->{'to_subfield'}; |
515 |
my $to_regex = $a->{'to_regex'}; |
525 |
my $to_regex_search = $a->{'to_regex_search'}; |
516 |
my $conditional = $a->{'conditional'}; |
526 |
my $to_regex_replace = $a->{'to_regex_replace'}; |
517 |
my $conditional_field = $a->{'conditional_field'}; |
527 |
my $to_regex_modifiers = $a->{'to_regex_modifiers'}; |
518 |
my $conditional_subfield = $a->{'conditional_subfield'}; |
528 |
my $conditional = $a->{'conditional'}; |
519 |
my $conditional_comparison = $a->{'conditional_comparison'}; |
529 |
my $conditional_field = $a->{'conditional_field'}; |
520 |
my $conditional_value = $a->{'conditional_value'}; |
530 |
my $conditional_subfield = $a->{'conditional_subfield'}; |
521 |
my $conditional_regex = $a->{'conditional_regex'}; |
531 |
my $conditional_comparison = $a->{'conditional_comparison'}; |
522 |
|
532 |
my $conditional_value = $a->{'conditional_value'}; |
523 |
my $eval = "$action( \$record, '$from_field', '$from_subfield', "; |
533 |
my $conditional_regex = $a->{'conditional_regex'}; |
524 |
|
534 |
|
525 |
if ( $field_value ) { |
535 |
if ( $field_value ) { |
526 |
C4::Koha::Log( "Field value before replacements: $field_value" ) if ( DEBUG >= 3 ); |
536 |
$field_value =~ s/__CURRENTDATE__/$current_date/g; |
527 |
warn( "Field value before replacements: $field_value" ) if ( DEBUG >= 3 ); |
537 |
$field_value =~ s/__BRANCHCODE__/$branchcode/g; |
528 |
|
538 |
} |
529 |
$field_value =~ s/__CURRENTDATE__/$current_date/g; |
539 |
|
530 |
$field_value =~ s/__BRANCHCODE__/$branchcode/g; |
540 |
my @params = ( $record, $from_field, $from_subfield ); |
531 |
|
541 |
if ( $action eq 'update_field' ) { |
532 |
$eval .= " undef, " if ( $action eq 'update_field' ); |
542 |
push @params, |
533 |
$eval .= " '$field_value' "; |
543 |
( $field_value |
534 |
|
544 |
? ( undef, $field_value ) |
535 |
C4::Koha::Log( "Field value after replacements: $field_value" ) if ( DEBUG >= 3 ); |
545 |
: () |
536 |
warn( "Field value after replacements: $field_value" ) if ( DEBUG >= 3 ); |
546 |
); |
537 |
} elsif ( $to_field ) { |
547 |
} else { |
538 |
$eval .= " '$to_field', '$to_subfield', '$to_regex' "; |
548 |
push @params, |
539 |
} |
549 |
( $field_value |
540 |
|
550 |
? $field_value |
541 |
$eval .= ", '$field_number' " if ( $field_number ); |
551 |
: () |
542 |
$eval .= ') '; |
552 |
); |
543 |
|
553 |
} |
544 |
if ( $conditional ) { |
554 |
push @params, ( |
545 |
$eval .= " $conditional ( "; |
555 |
( ( not $field_value and $to_field ) |
546 |
|
556 |
? ( $to_field, $to_subfield, { search => $to_regex_search, replace => $to_regex_replace, modifiers => $to_regex_modifiers} ) |
547 |
if ( $conditional_comparison eq 'exists' ) { |
557 |
: () ), |
548 |
$eval .= "field_exists( \$record, '$conditional_field', '$conditional_subfield' )"; |
558 |
( $field_number |
549 |
|
559 |
? $field_number |
550 |
} elsif ( $conditional_comparison eq 'not_exists' ) { |
560 |
: () ) |
551 |
$eval .= "!field_exists( \$record, '$conditional_field', '$conditional_subfield' )"; |
561 |
); |
552 |
|
562 |
|
553 |
} elsif ( $conditional_comparison eq 'equals' ) { |
563 |
my $do = 1; |
554 |
$eval .= "field_equals( \$record, '$conditional_value', '$conditional_field', '$conditional_subfield', '$conditional_regex' ) "; |
564 |
if ( $conditional ) { |
555 |
|
565 |
for ( $conditional_comparison ) { |
556 |
} elsif ( $conditional_comparison eq 'not_equals' ) { |
566 |
when ( /^exists$/ ) { |
557 |
$eval .= "!field_equals( \$record, '$conditional_value', '$conditional_field', '$conditional_subfield', '$conditional_regex' ) "; |
567 |
my $exists = field_exists( $record, $conditional_field, $conditional_subfield ); |
558 |
} |
568 |
$do = $conditional eq 'if' |
559 |
|
569 |
? $exists |
560 |
$eval .= " )"; |
570 |
: not $exists; |
|
|
571 |
} |
572 |
when ( /^not_exists$/ ) { |
573 |
my $exists = field_exists( $record, $conditional_field, $conditional_subfield ); |
574 |
$do = $conditional eq 'if' |
575 |
? not $exists |
576 |
: $exists; |
577 |
} |
578 |
when ( /^equals$/ ) { |
579 |
my $equals = field_equals( $record, $conditional_value, $conditional_field, $conditional_subfield, $conditional_regex ); |
580 |
$do = $conditional eq 'if' |
581 |
? $equals |
582 |
: not $equals; |
583 |
} |
584 |
when ( /^not_equals$/ ) { |
585 |
my $equals = field_equals( $record, $conditional_value, $conditional_field, $conditional_subfield, $conditional_regex ); |
586 |
$do = $conditional eq 'if' |
587 |
? not $equals |
588 |
: $equals; |
589 |
} |
590 |
} |
591 |
} |
592 |
|
593 |
if ( $do ) { |
594 |
for ( $action ) { |
595 |
when ( /^copy_field$/ ) { |
596 |
copy_field( @params ); |
597 |
} |
598 |
when ( /^update_field$/ ) { |
599 |
update_field( @params ); |
600 |
} |
601 |
when ( /^move_field$/ ) { |
602 |
move_field( @params ); |
603 |
} |
604 |
when ( /^delete_field$/ ) { |
605 |
delete_field( @params ); |
606 |
} |
607 |
} |
608 |
} |
609 |
|
610 |
C4::Koha::Log( $record->as_formatted() ) if DEBUG >= 10; |
611 |
warn( $record->as_formatted() ) if DEBUG >= 10; |
561 |
} |
612 |
} |
562 |
|
|
|
563 |
$eval .= ";"; |
564 |
|
565 |
C4::Koha::Log("eval $eval") if DEBUG >= 2; |
566 |
warn("eval $eval") if DEBUG >= 2; |
567 |
eval {$eval}; |
568 |
C4::Koha::Log( $record->as_formatted() ) if DEBUG >= 10; |
569 |
warn( $record->as_formatted() ) if DEBUG >= 10; |
570 |
|
571 |
} |
572 |
} |
613 |
} |
573 |
1; |
614 |
1; |
574 |
__END__ |
615 |
__END__ |