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>.
27 use vars
qw($VERSION @ISA @EXPORT);
29 use constant DEBUG => 0;
32 $VERSION = 1.00; # set the version for version checking
35 &GetModificationTemplates
36 &AddModificationTemplate
37 &DelModificationTemplate
39 &GetModificationTemplateAction
40 &GetModificationTemplateActions
42 &AddModificationTemplateAction
43 &ModModificationTemplateAction
44 &DelModificationTemplateAction
45 &MoveModificationTemplateAction
47 &ModifyRecordsWithTemplate
48 &ModifyRecordWithTemplate
55 C4::MarcModificationTemplates - Module to manage MARC Modification Templates
59 MARC Modification Templates are a tool for marc batch imports,
60 so that librarians can set up templates for various vendors'
61 files telling Koha what fields to insert data into.
67 =head2 GetModificationTemplates
69 my @templates = GetModificationTemplates( [ $template_id ] );
71 Passing a $template_id will mark the given id as the selected template.
74 sub GetModificationTemplates
{
75 my ( $template_id ) = @_;
76 warn("C4::MarcModificationTemplates::GetModificationTemplates( $template_id )") if DEBUG
;
78 my $dbh = C4
::Context
->dbh;
79 my $sth = $dbh->prepare("SELECT * FROM marc_modification_templates");
83 while ( my $template = $sth->fetchrow_hashref() ) {
84 $template->{'selected'} = 1 if ( $template->{'template_id'} eq $template_id );
85 push( @templates, $template );
92 AddModificationTemplate
94 $template_id = AddModificationTemplate( $template_name[, $template_id ] );
96 If $template_id is supplied, the actions from that template will be copied
97 into the newly created template.
100 sub AddModificationTemplate
{
101 my ( $template_name, $template_id_copy ) = @_;
103 my $dbh = C4
::Context
->dbh;
104 my $sth = $dbh->prepare("INSERT INTO marc_modification_templates ( name ) VALUES ( ? )");
105 $sth->execute( $template_name );
107 $sth = $dbh->prepare("SELECT * FROM marc_modification_templates WHERE name = ?");
108 $sth->execute( $template_name );
109 my $row = $sth->fetchrow_hashref();
110 my $template_id = $row->{'template_id'};
112 if ( $template_id_copy ) {
113 my @actions = GetModificationTemplateActions
( $template_id_copy );
114 foreach my $action ( @actions ) {
115 AddModificationTemplateAction
(
118 $action->{'field_number'},
119 $action->{'from_field'},
120 $action->{'from_subfield'},
121 $action->{'field_value'},
122 $action->{'to_field'},
123 $action->{'to_subfield'},
124 $action->{'to_regex_search'},
125 $action->{'to_regex_replace'},
126 $action->{'to_regex_modifiers'},
127 $action->{'conditional'},
128 $action->{'conditional_field'},
129 $action->{'conditional_subfield'},
130 $action->{'conditional_comparison'},
131 $action->{'conditional_value'},
132 $action->{'conditional_regex'},
133 $action->{'description'},
143 DelModificationTemplate
145 DelModificationTemplate( $template_id );
148 sub DelModificationTemplate
{
149 my ( $template_id ) = @_;
151 my $dbh = C4
::Context
->dbh;
152 my $sth = $dbh->prepare("DELETE FROM marc_modification_templates WHERE template_id = ?");
153 $sth->execute( $template_id );
157 GetModificationTemplateAction
159 my $action = GetModificationTemplateAction( $mmta_id );
162 sub GetModificationTemplateAction
{
163 my ( $mmta_id ) = @_;
165 my $dbh = C4
::Context
->dbh;
166 my $sth = $dbh->prepare("SELECT * FROM marc_modification_template_actions WHERE mmta_id = ?");
167 $sth->execute( $mmta_id );
168 my $action = $sth->fetchrow_hashref();
174 GetModificationTemplateActions
176 my @actions = GetModificationTemplateActions( $template_id );
179 sub GetModificationTemplateActions
{
180 my ( $template_id ) = @_;
182 warn( "C4::MarcModificationTemplates::GetModificationTemplateActions( $template_id )" ) if DEBUG
;
184 my $dbh = C4
::Context
->dbh;
185 my $sth = $dbh->prepare("SELECT * FROM marc_modification_template_actions WHERE template_id = ? ORDER BY ordering");
186 $sth->execute( $template_id );
189 while ( my $action = $sth->fetchrow_hashref() ) {
190 push( @actions, $action );
193 warn( Data
::Dumper
::Dumper
( @actions ) ) if DEBUG
> 4;
199 AddModificationTemplateAction
201 AddModificationTemplateAction(
202 $template_id, $action, $field_number,
203 $from_field, $from_subfield, $field_value,
204 $to_field, $to_subfield, $to_regex_search, $to_regex_replace, $to_regex_modifiers
205 $conditional, $conditional_field, $conditional_subfield,
206 $conditional_comparison, $conditional_value,
207 $conditional_regex, $description
210 Adds a new action to the given modification template.
214 sub AddModificationTemplateAction
{
229 $conditional_subfield,
230 $conditional_comparison,
236 warn( "C4::MarcModificationTemplates::AddModificationTemplateAction( $template_id, $action,
237 $field_number, $from_field, $from_subfield, $field_value, $to_field, $to_subfield,
238 $to_regex_search, $to_regex_replace, $to_regex_modifiers, $conditional, $conditional_field, $conditional_subfield, $conditional_comparison,
239 $conditional_value, $conditional_regex, $description )" ) if DEBUG
;
241 $conditional_regex ||= '0';
243 my $dbh = C4
::Context
->dbh;
244 my $sth = $dbh->prepare( 'SELECT MAX(ordering) + 1 AS next_ordering FROM marc_modification_template_actions WHERE template_id = ?' );
245 $sth->execute( $template_id );
246 my $row = $sth->fetchrow_hashref;
247 my $ordering = $row->{'next_ordering'} || 1;
250 INSERT INTO marc_modification_template_actions (
266 conditional_subfield,
267 conditional_comparison,
272 VALUES ( NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";
274 $sth = $dbh->prepare( $query );
291 $conditional_subfield,
292 $conditional_comparison,
300 ModModificationTemplateAction
302 ModModificationTemplateAction(
303 $mmta_id, $action, $field_number, $from_field,
304 $from_subfield, $field_value, $to_field,
305 $to_subfield, $to_regex_search, $to_regex_replace, $to_regex_modifiers, $conditional,
306 $conditional_field, $conditional_subfield,
307 $conditional_comparison, $conditional_value,
308 $conditional_regex, $description
311 Modifies an existing action.
315 sub ModModificationTemplateAction
{
330 $conditional_subfield,
331 $conditional_comparison,
337 my $dbh = C4
::Context
->dbh;
340 UPDATE marc_modification_template_actions SET
349 to_regex_replace = ?,
350 to_regex_modifiers = ?,
352 conditional_field = ?,
353 conditional_subfield = ?,
354 conditional_comparison = ?,
355 conditional_value = ?,
356 conditional_regex = ?,
360 my $sth = $dbh->prepare( $query );
375 $conditional_subfield,
376 $conditional_comparison,
386 DelModificationTemplateAction
388 DelModificationTemplateAction( $mmta_id );
390 Deletes the given template action.
393 sub DelModificationTemplateAction
{
394 my ( $mmta_id ) = @_;
396 my $action = GetModificationTemplateAction
( $mmta_id );
398 my $dbh = C4
::Context
->dbh;
399 my $sth = $dbh->prepare("DELETE FROM marc_modification_template_actions WHERE mmta_id = ?");
400 $sth->execute( $mmta_id );
402 $sth = $dbh->prepare("UPDATE marc_modification_template_actions SET ordering = ordering - 1 WHERE template_id = ? AND ordering > ?");
403 $sth->execute( $action->{'template_id'}, $action->{'ordering'} );
407 MoveModificationTemplateAction
409 MoveModificationTemplateAction( $mmta_id, $where );
411 Changes the order for the given action.
412 Options for $where are 'up', 'down', 'top' and 'bottom'
414 sub MoveModificationTemplateAction
{
415 my ( $mmta_id, $where ) = @_;
417 my $action = GetModificationTemplateAction
( $mmta_id );
419 return if ( $action->{'ordering'} eq '1' && ( $where eq 'up' || $where eq 'top' ) );
420 return if ( $action->{'ordering'} eq GetModificationTemplateActions
( $action->{'template_id'} ) && ( $where eq 'down' || $where eq 'bottom' ) );
422 my $dbh = C4
::Context
->dbh;
425 if ( $where eq 'up' || $where eq 'down' ) {
427 ## For up and down, we just swap the ordering number with the one above or below it.
429 ## Change the ordering for the other action
430 $query = "UPDATE marc_modification_template_actions SET ordering = ? WHERE template_id = ? AND ordering = ?";
432 my $ordering = $action->{'ordering'};
433 $ordering-- if ( $where eq 'up' );
434 $ordering++ if ( $where eq 'down' );
436 $sth = $dbh->prepare( $query );
437 $sth->execute( $action->{'ordering'}, $action->{'template_id'}, $ordering );
439 ## Change the ordering for this action
440 $query = "UPDATE marc_modification_template_actions SET ordering = ? WHERE mmta_id = ?";
441 $sth = $dbh->prepare( $query );
442 $sth->execute( $ordering, $action->{'mmta_id'} );
444 } elsif ( $where eq 'top' ) {
446 $sth = $dbh->prepare('UPDATE marc_modification_template_actions SET ordering = ordering + 1 WHERE template_id = ? AND ordering < ?');
447 $sth->execute( $action->{'template_id'}, $action->{'ordering'} );
449 $sth = $dbh->prepare('UPDATE marc_modification_template_actions SET ordering = 1 WHERE mmta_id = ?');
450 $sth->execute( $mmta_id );
452 } elsif ( $where eq 'bottom' ) {
454 my $ordering = GetModificationTemplateActions
( $action->{'template_id'} );
456 $sth = $dbh->prepare('UPDATE marc_modification_template_actions SET ordering = ordering - 1 WHERE template_id = ? AND ordering > ?');
457 $sth->execute( $action->{'template_id'}, $action->{'ordering'} );
459 $sth = $dbh->prepare('UPDATE marc_modification_template_actions SET ordering = ? WHERE mmta_id = ?');
460 $sth->execute( $ordering, $mmta_id );
467 ModifyRecordsWithTemplate
469 ModifyRecordsWithTemplate( $template_id, $batch );
471 Accepts a template id and a MARC::Batch object.
474 sub ModifyRecordsWithTemplate
{
475 my ( $template_id, $batch ) = @_;
476 warn( "C4::MarcModificationTemplates::ModifyRecordsWithTemplate( $template_id, $batch )" ) if DEBUG
;
478 while ( my $record = $batch->next() ) {
479 ModifyRecordWithTemplate
( $template_id, $record );
484 ModifyRecordWithTemplate
486 ModifyRecordWithTemplate( $template_id, $record )
488 Accepts a MARC::Record object ( $record ) and modifies
489 it based on the actions for the given $template_id
492 sub ModifyRecordWithTemplate
{
493 my ( $template_id, $record ) = @_;
494 warn( "C4::MarcModificationTemplates::ModifyRecordWithTemplate( $template_id, $record )" ) if DEBUG
;
495 warn( "Unmodified Record:\n" . $record->as_formatted() ) if DEBUG
>= 10;
497 my $current_date = DateTime
->now()->ymd();
499 $branchcode = C4
::Context
->userenv->{branch
} if C4
::Context
->userenv;
501 my @actions = GetModificationTemplateActions
( $template_id );
503 foreach my $a ( @actions ) {
504 my $action = $a->{'action'};
505 my $field_number = $a->{'field_number'};
506 my $from_field = $a->{'from_field'};
507 my $from_subfield = $a->{'from_subfield'};
508 my $field_value = $a->{'field_value'};
509 my $to_field = $a->{'to_field'};
510 my $to_subfield = $a->{'to_subfield'};
511 my $to_regex_search = $a->{'to_regex_search'};
512 my $to_regex_replace = $a->{'to_regex_replace'};
513 my $to_regex_modifiers = $a->{'to_regex_modifiers'};
514 my $conditional = $a->{'conditional'};
515 my $conditional_field = $a->{'conditional_field'};
516 my $conditional_subfield = $a->{'conditional_subfield'};
517 my $conditional_comparison = $a->{'conditional_comparison'};
518 my $conditional_value = $a->{'conditional_value'};
519 my $conditional_regex = $a->{'conditional_regex'};
521 if ( $field_value ) {
522 $field_value =~ s/__CURRENTDATE__/$current_date/g;
523 $field_value =~ s/__BRANCHCODE__/$branchcode/g;
526 my @params = ( $record, $from_field, $from_subfield );
527 if ( $action eq 'update_field' ) {
530 ?
( undef, $field_value )
541 ( ( not $field_value and $to_field )
542 ?
( $to_field, $to_subfield, { search
=> $to_regex_search, replace
=> $to_regex_replace, modifiers
=> $to_regex_modifiers} )
550 if ( $conditional ) {
551 for ( $conditional_comparison ) {
552 when ( /^exists$/ ) {
553 my $exists = field_exists
( $record, $conditional_field, $conditional_subfield );
554 $do = $conditional eq 'if'
558 when ( /^not_exists$/ ) {
559 my $exists = field_exists
( $record, $conditional_field, $conditional_subfield );
560 $do = $conditional eq 'if'
564 when ( /^equals$/ ) {
565 my $equals = field_equals
( $record, $conditional_value, $conditional_field, $conditional_subfield, $conditional_regex );
566 $do = $conditional eq 'if'
570 when ( /^not_equals$/ ) {
571 my $equals = field_equals
( $record, $conditional_value, $conditional_field, $conditional_subfield, $conditional_regex );
572 $do = $conditional eq 'if'
581 when ( /^copy_field$/ ) {
582 copy_field
( @params );
584 when ( /^update_field$/ ) {
585 update_field
( @params );
587 when ( /^move_field$/ ) {
588 move_field
( @params );
590 when ( /^delete_field$/ ) {
591 delete_field
( @params );
596 warn( $record->as_formatted() ) if DEBUG
>= 10;