Bug 6810: Send membership expiry reminder notices.
[koha.git] / C4 / MarcModificationTemplates.pm
1 package C4::MarcModificationTemplates;
2
3 # This file is part of Koha.
4 #
5 # Copyright 2010 Kyle M Hall <kyle.m.hall@gmail.com>
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use DateTime;
23
24 use C4::Context;
25 use Koha::SimpleMARC;
26
27 use vars qw($VERSION @ISA @EXPORT);
28
29 use constant DEBUG => 0;
30
31 BEGIN {
32     $VERSION = 1.00;    # set the version for version checking
33     @ISA = qw(Exporter);
34     @EXPORT = qw(
35         &GetModificationTemplates
36         &AddModificationTemplate
37         &DelModificationTemplate
38
39         &GetModificationTemplateAction
40         &GetModificationTemplateActions
41
42         &AddModificationTemplateAction
43         &ModModificationTemplateAction
44         &DelModificationTemplateAction
45         &MoveModificationTemplateAction
46
47         &ModifyRecordsWithTemplate
48         &ModifyRecordWithTemplate
49     );
50 }
51
52
53 =head1 NAME
54
55 C4::MarcModificationTemplates - Module to manage MARC Modification Templates
56
57 =head1 DESCRIPTION
58
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.
62
63 =head1 FUNCTIONS
64
65 =cut
66
67 =head2 GetModificationTemplates
68
69   my @templates = GetModificationTemplates( $template_id );
70
71   Passing optional $template_id marks it as the selected template.
72
73 =cut
74
75 sub GetModificationTemplates {
76   my ( $template_id ) = @_;
77   warn("C4::MarcModificationTemplates::GetModificationTemplates( $template_id )") if DEBUG;
78
79   my $dbh = C4::Context->dbh;
80   my $sth = $dbh->prepare("SELECT * FROM marc_modification_templates");
81   $sth->execute();
82
83   my @templates;
84   while ( my $template = $sth->fetchrow_hashref() ) {
85     $template->{'selected'} = 1
86         if $template_id && $template->{'template_id'} eq $template_id;
87     push( @templates, $template );
88   }
89
90   return @templates;
91 }
92
93 =head2
94   AddModificationTemplate
95
96   $template_id = AddModificationTemplate( $template_name[, $template_id ] );
97
98   If $template_id is supplied, the actions from that template will be copied
99   into the newly created template.
100 =cut
101
102 sub AddModificationTemplate {
103   my ( $template_name, $template_id_copy ) = @_;
104
105   my $dbh = C4::Context->dbh;
106   my $sth = $dbh->prepare("INSERT INTO marc_modification_templates ( name ) VALUES ( ? )");
107   $sth->execute( $template_name );
108
109   $sth = $dbh->prepare("SELECT * FROM marc_modification_templates WHERE name = ?");
110   $sth->execute( $template_name );
111   my $row = $sth->fetchrow_hashref();
112   my $template_id = $row->{'template_id'};
113
114   if ( $template_id_copy ) {
115     my @actions = GetModificationTemplateActions( $template_id_copy );
116     foreach my $action ( @actions ) {
117       AddModificationTemplateAction(
118         $template_id,
119         $action->{'action'},
120         $action->{'field_number'},
121         $action->{'from_field'},
122         $action->{'from_subfield'},
123         $action->{'field_value'},
124         $action->{'to_field'},
125         $action->{'to_subfield'},
126         $action->{'to_regex_search'},
127         $action->{'to_regex_replace'},
128         $action->{'to_regex_modifiers'},
129         $action->{'conditional'},
130         $action->{'conditional_field'},
131         $action->{'conditional_subfield'},
132         $action->{'conditional_comparison'},
133         $action->{'conditional_value'},
134         $action->{'conditional_regex'},
135         $action->{'description'},
136       );
137
138     }
139   }
140
141   return $template_id;
142 }
143
144 =head2
145   DelModificationTemplate
146
147   DelModificationTemplate( $template_id );
148 =cut
149
150 sub DelModificationTemplate {
151   my ( $template_id ) = @_;
152
153   my $dbh = C4::Context->dbh;
154   my $sth = $dbh->prepare("DELETE FROM marc_modification_templates WHERE template_id = ?");
155   $sth->execute( $template_id );
156 }
157
158 =head2
159   GetModificationTemplateAction
160
161   my $action = GetModificationTemplateAction( $mmta_id );
162 =cut
163
164 sub GetModificationTemplateAction {
165   my ( $mmta_id ) = @_;
166
167   my $dbh = C4::Context->dbh;
168   my $sth = $dbh->prepare("SELECT * FROM marc_modification_template_actions WHERE mmta_id = ?");
169   $sth->execute( $mmta_id );
170   my $action = $sth->fetchrow_hashref();
171
172   return $action;
173 }
174
175 =head2
176   GetModificationTemplateActions
177
178   my @actions = GetModificationTemplateActions( $template_id );
179 =cut
180
181 sub GetModificationTemplateActions {
182   my ( $template_id ) = @_;
183
184   warn( "C4::MarcModificationTemplates::GetModificationTemplateActions( $template_id )" ) if DEBUG;
185
186   my $dbh = C4::Context->dbh;
187   my $sth = $dbh->prepare("SELECT * FROM marc_modification_template_actions WHERE template_id = ? ORDER BY ordering");
188   $sth->execute( $template_id );
189
190   my @actions;
191   while ( my $action = $sth->fetchrow_hashref() ) {
192     push( @actions, $action );
193   }
194
195   warn( Data::Dumper::Dumper( @actions ) ) if DEBUG > 4;
196
197   return @actions;
198 }
199
200 =head2
201   AddModificationTemplateAction
202
203   AddModificationTemplateAction(
204     $template_id, $action, $field_number,
205     $from_field, $from_subfield, $field_value,
206     $to_field, $to_subfield, $to_regex_search, $to_regex_replace, $to_regex_modifiers
207     $conditional, $conditional_field, $conditional_subfield,
208     $conditional_comparison, $conditional_value,
209     $conditional_regex, $description
210   );
211
212   Adds a new action to the given modification template.
213
214 =cut
215
216 sub AddModificationTemplateAction {
217   my (
218     $template_id,
219     $action,
220     $field_number,
221     $from_field,
222     $from_subfield,
223     $field_value,
224     $to_field,
225     $to_subfield,
226     $to_regex_search,
227     $to_regex_replace,
228     $to_regex_modifiers,
229     $conditional,
230     $conditional_field,
231     $conditional_subfield,
232     $conditional_comparison,
233     $conditional_value,
234     $conditional_regex,
235     $description
236   ) = @_;
237
238   warn( "C4::MarcModificationTemplates::AddModificationTemplateAction( $template_id, $action,
239                     $field_number, $from_field, $from_subfield, $field_value, $to_field, $to_subfield,
240                     $to_regex_search, $to_regex_replace, $to_regex_modifiers, $conditional, $conditional_field, $conditional_subfield, $conditional_comparison,
241                     $conditional_value, $conditional_regex, $description )" ) if DEBUG;
242
243   $conditional_regex ||= '0';
244
245   my $dbh = C4::Context->dbh;
246   my $sth = $dbh->prepare( 'SELECT MAX(ordering) + 1 AS next_ordering FROM marc_modification_template_actions WHERE template_id = ?' );
247   $sth->execute( $template_id );
248   my $row = $sth->fetchrow_hashref;
249   my $ordering = $row->{'next_ordering'} || 1;
250
251   my $query = "
252   INSERT INTO marc_modification_template_actions (
253   mmta_id,
254   template_id,
255   ordering,
256   action,
257   field_number,
258   from_field,
259   from_subfield,
260   field_value,
261   to_field,
262   to_subfield,
263   to_regex_search,
264   to_regex_replace,
265   to_regex_modifiers,
266   conditional,
267   conditional_field,
268   conditional_subfield,
269   conditional_comparison,
270   conditional_value,
271   conditional_regex,
272   description
273   )
274   VALUES ( NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";
275
276   $sth = $dbh->prepare( $query );
277
278   $sth->execute(
279     $template_id,
280     $ordering,
281     $action,
282     $field_number,
283     $from_field,
284     $from_subfield,
285     $field_value,
286     $to_field,
287     $to_subfield,
288     $to_regex_search,
289     $to_regex_replace,
290     $to_regex_modifiers,
291     $conditional,
292     $conditional_field,
293     $conditional_subfield,
294     $conditional_comparison,
295     $conditional_value,
296     $conditional_regex,
297     $description
298   );
299 }
300
301 =head2
302   ModModificationTemplateAction
303
304   ModModificationTemplateAction(
305     $mmta_id, $action, $field_number, $from_field,
306     $from_subfield, $field_value, $to_field,
307     $to_subfield, $to_regex_search, $to_regex_replace, $to_regex_modifiers, $conditional,
308     $conditional_field, $conditional_subfield,
309     $conditional_comparison, $conditional_value,
310     $conditional_regex, $description
311   );
312
313   Modifies an existing action.
314
315 =cut
316
317 sub ModModificationTemplateAction {
318   my (
319     $mmta_id,
320     $action,
321     $field_number,
322     $from_field,
323     $from_subfield,
324     $field_value,
325     $to_field,
326     $to_subfield,
327     $to_regex_search,
328     $to_regex_replace,
329     $to_regex_modifiers,
330     $conditional,
331     $conditional_field,
332     $conditional_subfield,
333     $conditional_comparison,
334     $conditional_value,
335     $conditional_regex,
336     $description
337   ) = @_;
338
339   my $dbh = C4::Context->dbh;
340
341   my $query = "
342   UPDATE marc_modification_template_actions SET
343   action = ?,
344   field_number = ?,
345   from_field = ?,
346   from_subfield = ?,
347   field_value = ?,
348   to_field = ?,
349   to_subfield = ?,
350   to_regex_search = ?,
351   to_regex_replace = ?,
352   to_regex_modifiers = ?,
353   conditional = ?,
354   conditional_field = ?,
355   conditional_subfield = ?,
356   conditional_comparison = ?,
357   conditional_value = ?,
358   conditional_regex = ?,
359   description = ?
360   WHERE mmta_id = ?";
361
362   my $sth = $dbh->prepare( $query );
363
364   $sth->execute(
365     $action,
366     $field_number,
367     $from_field,
368     $from_subfield,
369     $field_value,
370     $to_field,
371     $to_subfield,
372     $to_regex_search,
373     $to_regex_replace,
374     $to_regex_modifiers,
375     $conditional,
376     $conditional_field,
377     $conditional_subfield,
378     $conditional_comparison,
379     $conditional_value,
380     $conditional_regex,
381     $description,
382     $mmta_id
383   );
384 }
385
386
387 =head2
388   DelModificationTemplateAction
389
390   DelModificationTemplateAction( $mmta_id );
391
392   Deletes the given template action.
393 =cut
394
395 sub DelModificationTemplateAction {
396   my ( $mmta_id ) = @_;
397
398   my $action = GetModificationTemplateAction( $mmta_id );
399
400   my $dbh = C4::Context->dbh;
401   my $sth = $dbh->prepare("DELETE FROM marc_modification_template_actions WHERE mmta_id = ?");
402   $sth->execute( $mmta_id );
403
404   $sth = $dbh->prepare("UPDATE marc_modification_template_actions SET ordering = ordering - 1 WHERE template_id = ? AND ordering > ?");
405   $sth->execute( $action->{'template_id'}, $action->{'ordering'} );
406 }
407
408 =head2
409   MoveModificationTemplateAction
410
411   MoveModificationTemplateAction( $mmta_id, $where );
412
413   Changes the order for the given action.
414   Options for $where are 'up', 'down', 'top' and 'bottom'
415 =cut
416 sub MoveModificationTemplateAction {
417   my ( $mmta_id, $where ) = @_;
418
419   my $action = GetModificationTemplateAction( $mmta_id );
420
421   return if ( $action->{'ordering'} eq '1' && ( $where eq 'up' || $where eq 'top' ) );
422   return if ( $action->{'ordering'} eq GetModificationTemplateActions( $action->{'template_id'} ) && ( $where eq 'down' || $where eq 'bottom' ) );
423
424   my $dbh = C4::Context->dbh;
425   my ( $sth, $query );
426
427   if ( $where eq 'up' || $where eq 'down' ) {
428
429     ## For up and down, we just swap the ordering number with the one above or below it.
430
431     ## Change the ordering for the other action
432     $query = "UPDATE marc_modification_template_actions SET ordering = ? WHERE template_id = ? AND ordering = ?";
433
434     my $ordering = $action->{'ordering'};
435     $ordering-- if ( $where eq 'up' );
436     $ordering++ if ( $where eq 'down' );
437
438     $sth = $dbh->prepare( $query );
439     $sth->execute( $action->{'ordering'}, $action->{'template_id'}, $ordering );
440
441     ## Change the ordering for this action
442     $query = "UPDATE marc_modification_template_actions SET ordering = ? WHERE mmta_id = ?";
443     $sth = $dbh->prepare( $query );
444     $sth->execute( $ordering, $action->{'mmta_id'} );
445
446   } elsif ( $where eq 'top' ) {
447
448     $sth = $dbh->prepare('UPDATE marc_modification_template_actions SET ordering = ordering + 1 WHERE template_id = ? AND ordering < ?');
449     $sth->execute( $action->{'template_id'}, $action->{'ordering'} );
450
451     $sth = $dbh->prepare('UPDATE marc_modification_template_actions SET ordering = 1 WHERE mmta_id = ?');
452     $sth->execute( $mmta_id );
453
454   } elsif ( $where eq 'bottom' ) {
455
456     my $ordering = GetModificationTemplateActions( $action->{'template_id'} );
457
458     $sth = $dbh->prepare('UPDATE marc_modification_template_actions SET ordering = ordering - 1 WHERE template_id = ? AND ordering > ?');
459     $sth->execute( $action->{'template_id'}, $action->{'ordering'} );
460
461     $sth = $dbh->prepare('UPDATE marc_modification_template_actions SET ordering = ? WHERE mmta_id = ?');
462     $sth->execute( $ordering, $mmta_id );
463
464   }
465
466 }
467
468 =head2
469   ModifyRecordsWithTemplate
470
471   ModifyRecordsWithTemplate( $template_id, $batch );
472
473   Accepts a template id and a MARC::Batch object.
474 =cut
475
476 sub ModifyRecordsWithTemplate {
477   my ( $template_id, $batch ) = @_;
478   warn( "C4::MarcModificationTemplates::ModifyRecordsWithTemplate( $template_id, $batch )" ) if DEBUG;
479
480   while ( my $record = $batch->next() ) {
481     ModifyRecordWithTemplate( $template_id, $record );
482   }
483 }
484
485 =head2
486   ModifyRecordWithTemplate
487
488   ModifyRecordWithTemplate( $template_id, $record )
489
490   Accepts a MARC::Record object ( $record ) and modifies
491   it based on the actions for the given $template_id
492 =cut
493
494 sub ModifyRecordWithTemplate {
495     my ( $template_id, $record ) = @_;
496     warn( "C4::MarcModificationTemplates::ModifyRecordWithTemplate( $template_id, $record )" ) if DEBUG;
497     warn( "Unmodified Record:\n" . $record->as_formatted() ) if DEBUG >= 10;
498
499     my $current_date = DateTime->now()->ymd();
500     my $branchcode = '';
501     $branchcode = C4::Context->userenv->{branch} if C4::Context->userenv;
502
503     my @actions = GetModificationTemplateActions( $template_id );
504
505     foreach my $a ( @actions ) {
506         my $action = $a->{'action'};
507         my $field_number = $a->{'field_number'} // 1;
508         my $from_field = $a->{'from_field'};
509         my $from_subfield = $a->{'from_subfield'};
510         my $field_value = $a->{'field_value'};
511         my $to_field = $a->{'to_field'};
512         my $to_subfield = $a->{'to_subfield'};
513         my $to_regex_search = $a->{'to_regex_search'};
514         my $to_regex_replace = $a->{'to_regex_replace'};
515         my $to_regex_modifiers = $a->{'to_regex_modifiers'};
516         my $conditional = $a->{'conditional'};
517         my $conditional_field = $a->{'conditional_field'};
518         my $conditional_subfield = $a->{'conditional_subfield'};
519         my $conditional_comparison = $a->{'conditional_comparison'};
520         my $conditional_value = $a->{'conditional_value'};
521         my $conditional_regex = $a->{'conditional_regex'};
522
523         if ( $field_value ) {
524             $field_value =~ s/__CURRENTDATE__/$current_date/g;
525             $field_value =~ s/__BRANCHCODE__/$branchcode/g;
526         }
527
528         my $do = 1;
529         my $field_numbers = [];
530         if ( $conditional ) {
531             if ( $conditional_comparison eq 'exists' ) {
532                 $field_numbers = field_exists({
533                         record => $record,
534                         field => $conditional_field,
535                         subfield => $conditional_subfield,
536                     });
537                 $do = $conditional eq 'if'
538                     ? @$field_numbers
539                     : not @$field_numbers;
540             }
541             elsif ( $conditional_comparison eq 'not_exists' ) {
542                 $field_numbers = field_exists({
543                         record => $record,
544                         field => $conditional_field,
545                         subfield => $conditional_subfield
546                     });
547                 $do = $conditional eq 'if'
548                     ? not @$field_numbers
549                     : @$field_numbers;
550             }
551             elsif ( $conditional_comparison eq 'equals' ) {
552                 $field_numbers = field_equals({
553                     record => $record,
554                     value => $conditional_value,
555                     field => $conditional_field,
556                     subfield => $conditional_subfield,
557                     is_regex => $conditional_regex,
558                 });
559                 $do = $conditional eq 'if'
560                     ? @$field_numbers
561                     : not @$field_numbers;
562             }
563             elsif ( $conditional_comparison eq 'not_equals' ) {
564                 $field_numbers = field_equals({
565                     record => $record,
566                     value => $conditional_value,
567                     field => $conditional_field,
568                     subfield => $conditional_subfield,
569                     is_regex => $conditional_regex,
570                 });
571                 $do = $conditional eq 'if'
572                     ? not @$field_numbers
573                     : @$field_numbers;
574             }
575         }
576
577         if ( $do ) {
578
579             # field_number == 0 if all field need to be updated
580             # or 1 if only the first field need to be updated
581
582             # A condition has been given
583             if ( @$field_numbers > 0 ) {
584                 if ( $field_number == 1 ) {
585                     # We want only the first matching
586                     $field_numbers = [ $field_numbers->[0] ];
587                 }
588             }
589             # There was no condition
590             else {
591                 if ( $field_number == 1 ) {
592                     # We want to process the first field
593                     $field_numbers = [ 1 ];
594                 } elsif ( $to_field and $from_field ne $to_field ) {
595                     # If the from and to fields are not the same, we only process the first field.
596                     $field_numbers = [ 1 ];
597                 }
598             }
599
600             if ( $action eq 'copy_field' ) {
601                 copy_field({
602                     record => $record,
603                     from_field => $from_field,
604                     from_subfield => $from_subfield,
605                     to_field => $to_field,
606                     to_subfield => $to_subfield,
607                     regex => {
608                         search => $to_regex_search,
609                         replace => $to_regex_replace,
610                         modifiers => $to_regex_modifiers
611                     },
612                     field_numbers => $field_numbers,
613                 });
614             }
615             elsif ( $action eq 'copy_and_replace_field' ) {
616                 copy_and_replace_field({
617                     record => $record,
618                     from_field => $from_field,
619                     from_subfield => $from_subfield,
620                     to_field => $to_field,
621                     to_subfield => $to_subfield,
622                     regex => {
623                         search => $to_regex_search,
624                         replace => $to_regex_replace,
625                         modifiers => $to_regex_modifiers
626                     },
627                     field_numbers => $field_numbers,
628                 });
629             }
630             elsif ( $action eq 'update_field' ) {
631                 update_field({
632                     record => $record,
633                     field => $from_field,
634                     subfield => $from_subfield,
635                     values => [ $field_value ],
636                     field_numbers => $field_numbers,
637                 });
638             }
639             elsif ( $action eq 'move_field' ) {
640                 move_field({
641                     record => $record,
642                     from_field => $from_field,
643                     from_subfield => $from_subfield,
644                     to_field => $to_field,
645                     to_subfield => $to_subfield,
646                     regex => {
647                         search => $to_regex_search,
648                         replace => $to_regex_replace,
649                         modifiers => $to_regex_modifiers
650                     },
651                     field_numbers => $field_numbers,
652                 });
653             }
654             elsif ( $action eq 'delete_field' ) {
655                 delete_field({
656                     record => $record,
657                     field => $from_field,
658                     subfield => $from_subfield,
659                     field_numbers => $field_numbers,
660                 });
661             }
662         }
663
664         warn( $record->as_formatted() ) if DEBUG >= 10;
665     }
666
667     return;
668 }
669 1;
670 __END__
671
672 =head1 AUTHOR
673
674 Kyle M Hall
675
676 =cut