1 package C4
::MarcModificationTemplates
;
3 # This file is part of Koha.
5 # Copyright 2010 Kyle M Hall <kyle.m.hall@gmail.com>
7 # Koha is free software; you can redistribute it and/or modify it
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.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
29 use vars
qw(@ISA @EXPORT);
31 use constant DEBUG => 0;
36 &GetModificationTemplates
37 &AddModificationTemplate
38 &DelModificationTemplate
40 &GetModificationTemplateAction
41 &GetModificationTemplateActions
43 &AddModificationTemplateAction
44 &ModModificationTemplateAction
45 &DelModificationTemplateAction
46 &MoveModificationTemplateAction
48 &ModifyRecordsWithTemplate
49 &ModifyRecordWithTemplate
56 C4::MarcModificationTemplates - Module to manage MARC Modification Templates
60 MARC Modification Templates are a tool for marc batch imports,
61 so that librarians can set up templates for various vendors'
62 files telling Koha what fields to insert data into.
68 =head2 GetModificationTemplates
70 my @templates = GetModificationTemplates( $template_id );
72 Passing optional $template_id marks it as the selected template.
76 sub GetModificationTemplates
{
77 my ( $template_id ) = @_;
78 warn("C4::MarcModificationTemplates::GetModificationTemplates( $template_id )") if DEBUG
;
80 my $dbh = C4
::Context
->dbh;
81 my $sth = $dbh->prepare("SELECT * FROM marc_modification_templates ORDER BY name");
85 while ( my $template = $sth->fetchrow_hashref() ) {
86 $template->{'selected'} = 1
87 if $template_id && $template->{'template_id'} eq $template_id;
88 push( @templates, $template );
95 AddModificationTemplate
97 $template_id = AddModificationTemplate( $template_name[, $template_id ] );
99 If $template_id is supplied, the actions from that template will be copied
100 into the newly created template.
103 sub AddModificationTemplate
{
104 my ( $template_name, $template_id_copy ) = @_;
106 my $dbh = C4
::Context
->dbh;
107 my $sth = $dbh->prepare("INSERT INTO marc_modification_templates ( name ) VALUES ( ? )");
108 $sth->execute( $template_name );
110 $sth = $dbh->prepare("SELECT * FROM marc_modification_templates WHERE name = ?");
111 $sth->execute( $template_name );
112 my $row = $sth->fetchrow_hashref();
113 my $template_id = $row->{'template_id'};
115 if ( $template_id_copy ) {
116 my @actions = GetModificationTemplateActions
( $template_id_copy );
117 foreach my $action ( @actions ) {
118 AddModificationTemplateAction
(
121 $action->{'field_number'},
122 $action->{'from_field'},
123 $action->{'from_subfield'},
124 $action->{'field_value'},
125 $action->{'to_field'},
126 $action->{'to_subfield'},
127 $action->{'to_regex_search'},
128 $action->{'to_regex_replace'},
129 $action->{'to_regex_modifiers'},
130 $action->{'conditional'},
131 $action->{'conditional_field'},
132 $action->{'conditional_subfield'},
133 $action->{'conditional_comparison'},
134 $action->{'conditional_value'},
135 $action->{'conditional_regex'},
136 $action->{'description'},
146 DelModificationTemplate
148 DelModificationTemplate( $template_id );
151 sub DelModificationTemplate
{
152 my ( $template_id ) = @_;
154 my $dbh = C4
::Context
->dbh;
155 my $sth = $dbh->prepare("DELETE FROM marc_modification_templates WHERE template_id = ?");
156 $sth->execute( $template_id );
160 GetModificationTemplateAction
162 my $action = GetModificationTemplateAction( $mmta_id );
165 sub GetModificationTemplateAction
{
166 my ( $mmta_id ) = @_;
168 my $dbh = C4
::Context
->dbh;
169 my $sth = $dbh->prepare("SELECT * FROM marc_modification_template_actions WHERE mmta_id = ?");
170 $sth->execute( $mmta_id );
171 my $action = $sth->fetchrow_hashref();
177 GetModificationTemplateActions
179 my @actions = GetModificationTemplateActions( $template_id );
182 sub GetModificationTemplateActions
{
183 my ( $template_id ) = @_;
185 warn( "C4::MarcModificationTemplates::GetModificationTemplateActions( $template_id )" ) if DEBUG
;
187 my $dbh = C4
::Context
->dbh;
188 my $sth = $dbh->prepare("SELECT * FROM marc_modification_template_actions WHERE template_id = ? ORDER BY ordering");
189 $sth->execute( $template_id );
192 while ( my $action = $sth->fetchrow_hashref() ) {
193 push( @actions, $action );
196 warn( Data
::Dumper
::Dumper
( @actions ) ) if DEBUG
> 4;
202 AddModificationTemplateAction
204 AddModificationTemplateAction(
205 $template_id, $action, $field_number,
206 $from_field, $from_subfield, $field_value,
207 $to_field, $to_subfield, $to_regex_search, $to_regex_replace, $to_regex_modifiers
208 $conditional, $conditional_field, $conditional_subfield,
209 $conditional_comparison, $conditional_value,
210 $conditional_regex, $description
213 Adds a new action to the given modification template.
217 sub AddModificationTemplateAction
{
232 $conditional_subfield,
233 $conditional_comparison,
239 warn( "C4::MarcModificationTemplates::AddModificationTemplateAction( $template_id, $action,
240 $field_number, $from_field, $from_subfield, $field_value, $to_field, $to_subfield,
241 $to_regex_search, $to_regex_replace, $to_regex_modifiers, $conditional, $conditional_field, $conditional_subfield, $conditional_comparison,
242 $conditional_value, $conditional_regex, $description )" ) if DEBUG
;
244 $conditional ||= undef;
245 $conditional_comparison ||= undef;
246 $conditional_regex ||= '0';
248 my $dbh = C4
::Context
->dbh;
249 my $sth = $dbh->prepare( 'SELECT MAX(ordering) + 1 AS next_ordering FROM marc_modification_template_actions WHERE template_id = ?' );
250 $sth->execute( $template_id );
251 my $row = $sth->fetchrow_hashref;
252 my $ordering = $row->{'next_ordering'} || 1;
255 INSERT INTO marc_modification_template_actions (
271 conditional_subfield,
272 conditional_comparison,
277 VALUES ( NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";
279 $sth = $dbh->prepare( $query );
296 $conditional_subfield,
297 $conditional_comparison,
305 ModModificationTemplateAction
307 ModModificationTemplateAction(
308 $mmta_id, $action, $field_number, $from_field,
309 $from_subfield, $field_value, $to_field,
310 $to_subfield, $to_regex_search, $to_regex_replace, $to_regex_modifiers, $conditional,
311 $conditional_field, $conditional_subfield,
312 $conditional_comparison, $conditional_value,
313 $conditional_regex, $description
316 Modifies an existing action.
320 sub ModModificationTemplateAction
{
335 $conditional_subfield,
336 $conditional_comparison,
342 my $dbh = C4
::Context
->dbh;
343 $conditional ||= undef;
344 $conditional_comparison ||= undef;
345 $conditional_regex ||= '0';
348 UPDATE marc_modification_template_actions SET
357 to_regex_replace = ?,
358 to_regex_modifiers = ?,
360 conditional_field = ?,
361 conditional_subfield = ?,
362 conditional_comparison = ?,
363 conditional_value = ?,
364 conditional_regex = ?,
368 my $sth = $dbh->prepare( $query );
383 $conditional_subfield,
384 $conditional_comparison,
394 DelModificationTemplateAction
396 DelModificationTemplateAction( $mmta_id );
398 Deletes the given template action.
401 sub DelModificationTemplateAction
{
402 my ( $mmta_id ) = @_;
404 my $action = GetModificationTemplateAction
( $mmta_id );
406 my $dbh = C4
::Context
->dbh;
407 my $sth = $dbh->prepare("DELETE FROM marc_modification_template_actions WHERE mmta_id = ?");
408 $sth->execute( $mmta_id );
410 $sth = $dbh->prepare("UPDATE marc_modification_template_actions SET ordering = ordering - 1 WHERE template_id = ? AND ordering > ?");
411 $sth->execute( $action->{'template_id'}, $action->{'ordering'} );
415 MoveModificationTemplateAction
417 MoveModificationTemplateAction( $mmta_id, $where );
419 Changes the order for the given action.
420 Options for $where are 'up', 'down', 'top' and 'bottom'
422 sub MoveModificationTemplateAction
{
423 my ( $mmta_id, $where ) = @_;
425 my $action = GetModificationTemplateAction
( $mmta_id );
427 return if ( $action->{'ordering'} eq '1' && ( $where eq 'up' || $where eq 'top' ) );
428 return if ( $action->{'ordering'} eq GetModificationTemplateActions
( $action->{'template_id'} ) && ( $where eq 'down' || $where eq 'bottom' ) );
430 my $dbh = C4
::Context
->dbh;
433 if ( $where eq 'up' || $where eq 'down' ) {
435 ## For up and down, we just swap the ordering number with the one above or below it.
437 ## Change the ordering for the other action
438 $query = "UPDATE marc_modification_template_actions SET ordering = ? WHERE template_id = ? AND ordering = ?";
440 my $ordering = $action->{'ordering'};
441 $ordering-- if ( $where eq 'up' );
442 $ordering++ if ( $where eq 'down' );
444 $sth = $dbh->prepare( $query );
445 $sth->execute( $action->{'ordering'}, $action->{'template_id'}, $ordering );
447 ## Change the ordering for this action
448 $query = "UPDATE marc_modification_template_actions SET ordering = ? WHERE mmta_id = ?";
449 $sth = $dbh->prepare( $query );
450 $sth->execute( $ordering, $action->{'mmta_id'} );
452 } elsif ( $where eq 'top' ) {
454 $sth = $dbh->prepare('UPDATE marc_modification_template_actions SET ordering = ordering + 1 WHERE template_id = ? AND ordering < ?');
455 $sth->execute( $action->{'template_id'}, $action->{'ordering'} );
457 $sth = $dbh->prepare('UPDATE marc_modification_template_actions SET ordering = 1 WHERE mmta_id = ?');
458 $sth->execute( $mmta_id );
460 } elsif ( $where eq 'bottom' ) {
462 my $ordering = GetModificationTemplateActions
( $action->{'template_id'} );
464 $sth = $dbh->prepare('UPDATE marc_modification_template_actions SET ordering = ordering - 1 WHERE template_id = ? AND ordering > ?');
465 $sth->execute( $action->{'template_id'}, $action->{'ordering'} );
467 $sth = $dbh->prepare('UPDATE marc_modification_template_actions SET ordering = ? WHERE mmta_id = ?');
468 $sth->execute( $ordering, $mmta_id );
475 ModifyRecordsWithTemplate
477 ModifyRecordsWithTemplate( $template_id, $batch );
479 Accepts a template id and a MARC::Batch object.
482 sub ModifyRecordsWithTemplate
{
483 my ( $template_id, $batch ) = @_;
484 warn( "C4::MarcModificationTemplates::ModifyRecordsWithTemplate( $template_id, $batch )" ) if DEBUG
;
486 while ( my $record = $batch->next() ) {
487 ModifyRecordWithTemplate
( $template_id, $record );
492 ModifyRecordWithTemplate
494 ModifyRecordWithTemplate( $template_id, $record )
496 Accepts a MARC::Record object ( $record ) and modifies
497 it based on the actions for the given $template_id
500 sub ModifyRecordWithTemplate
{
501 my ( $template_id, $record ) = @_;
502 warn( "C4::MarcModificationTemplates::ModifyRecordWithTemplate( $template_id, $record )" ) if DEBUG
;
503 warn( "Unmodified Record:\n" . $record->as_formatted() ) if DEBUG
>= 10;
505 my $current_date = dt_from_string
()->ymd();
507 $branchcode = C4
::Context
->userenv->{branch
} if C4
::Context
->userenv;
509 my @actions = GetModificationTemplateActions
( $template_id );
511 foreach my $a ( @actions ) {
512 my $action = $a->{'action'};
513 my $field_number = $a->{'field_number'} // 1;
514 my $from_field = $a->{'from_field'};
515 my $from_subfield = $a->{'from_subfield'};
516 my $field_value = $a->{'field_value'};
517 my $to_field = $a->{'to_field'};
518 my $to_subfield = $a->{'to_subfield'};
519 my $to_regex_search = $a->{'to_regex_search'};
520 my $to_regex_replace = $a->{'to_regex_replace'};
521 my $to_regex_modifiers = $a->{'to_regex_modifiers'};
522 my $conditional = $a->{'conditional'};
523 my $conditional_field = $a->{'conditional_field'};
524 my $conditional_subfield = $a->{'conditional_subfield'};
525 my $conditional_comparison = $a->{'conditional_comparison'};
526 my $conditional_value = $a->{'conditional_value'};
527 my $conditional_regex = $a->{'conditional_regex'};
529 if ( $field_value ) {
530 $field_value =~ s/__CURRENTDATE__/$current_date/g;
531 $field_value =~ s/__BRANCHCODE__/$branchcode/g;
535 my $field_numbers = [];
536 if ( $conditional ) {
537 if ( $conditional_comparison eq 'exists' ) {
538 $field_numbers = field_exists
({
540 field
=> $conditional_field,
541 subfield
=> $conditional_subfield,
543 $do = $conditional eq 'if'
545 : not @
$field_numbers;
547 elsif ( $conditional_comparison eq 'not_exists' ) {
548 $field_numbers = field_exists
({
550 field
=> $conditional_field,
551 subfield
=> $conditional_subfield
553 $do = $conditional eq 'if'
554 ?
not @
$field_numbers
557 elsif ( $conditional_comparison eq 'equals' ) {
558 $field_numbers = field_equals
({
560 value
=> $conditional_value,
561 field
=> $conditional_field,
562 subfield
=> $conditional_subfield,
563 is_regex
=> $conditional_regex,
565 $do = $conditional eq 'if'
567 : not @
$field_numbers;
569 elsif ( $conditional_comparison eq 'not_equals' ) {
570 $field_numbers = field_equals
({
572 value
=> $conditional_value,
573 field
=> $conditional_field,
574 subfield
=> $conditional_subfield,
575 is_regex
=> $conditional_regex,
582 field
=> $conditional_field,
583 subfield
=> $conditional_subfield
588 $field_numbers = [Koha
::MoreUtils
::singleton
( @
$field_numbers, @
$all_fields ) ];
589 if ( $from_field == $conditional_field ){
590 $do = $conditional eq 'if'
592 : not @
$field_numbers;
594 $do = $conditional eq 'if'
595 ?
not @
$field_numbers
603 # field_number == 0 if all field need to be updated
604 # or 1 if only the first field need to be updated
606 # A condition has been given
607 if ( @
$field_numbers > 0 ) {
608 if ( $field_number == 1 ) {
609 # We want only the first
610 if ( $from_field == $conditional_field ){
611 # want first field matching condition
612 $field_numbers = [ $field_numbers->[0] ];
614 # condition doesn't match, so just want first occurrence of from field
615 $field_numbers = [ 1 ];
618 unless ( $from_field == $conditional_field ){
619 # condition doesn't match from fields so need all occurrences of from fields for action
620 $field_numbers = field_exists
({
622 field
=> $from_field,
623 subfield
=> $from_subfield,
628 # There was no condition
630 if ( $field_number == 1 ) {
631 # We want to process the first field
632 $field_numbers = [ 1 ];
636 if ( $action eq 'copy_field' ) {
639 from_field
=> $from_field,
640 from_subfield
=> $from_subfield,
641 to_field
=> $to_field,
642 to_subfield
=> $to_subfield,
644 search
=> $to_regex_search,
645 replace
=> $to_regex_replace,
646 modifiers
=> $to_regex_modifiers
648 field_numbers
=> $field_numbers,
651 elsif ( $action eq 'copy_and_replace_field' ) {
652 copy_and_replace_field
({
654 from_field
=> $from_field,
655 from_subfield
=> $from_subfield,
656 to_field
=> $to_field,
657 to_subfield
=> $to_subfield,
659 search
=> $to_regex_search,
660 replace
=> $to_regex_replace,
661 modifiers
=> $to_regex_modifiers
663 field_numbers
=> $field_numbers,
666 elsif ( $action eq 'add_field' ) {
669 field
=> $from_field,
670 subfield
=> $from_subfield,
671 values => [ $field_value ],
672 field_numbers
=> $field_numbers,
675 elsif ( $action eq 'update_field' ) {
678 field
=> $from_field,
679 subfield
=> $from_subfield,
680 values => [ $field_value ],
681 field_numbers
=> $field_numbers,
684 elsif ( $action eq 'move_field' ) {
687 from_field
=> $from_field,
688 from_subfield
=> $from_subfield,
689 to_field
=> $to_field,
690 to_subfield
=> $to_subfield,
692 search
=> $to_regex_search,
693 replace
=> $to_regex_replace,
694 modifiers
=> $to_regex_modifiers
696 field_numbers
=> $field_numbers,
699 elsif ( $action eq 'delete_field' ) {
702 field
=> $from_field,
703 subfield
=> $from_subfield,
704 field_numbers
=> $field_numbers,
709 warn( $record->as_formatted() ) if DEBUG
>= 10;