Bug 14997: Remove C4::Dates from tools (import / export)
[koha.git] / tools / marc_modification_templates.pl
1 #!/usr/bin/perl
2 # This file is part of Koha.
3 #
4 # Copyright 2010 Kyle M Hall <kyle.m.hall@gmail.com>
5 #
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20
21 use CGI qw ( -utf8 );
22
23 use C4::Auth;
24 use C4::Koha;
25 use C4::Output;
26 use C4::MarcModificationTemplates;
27
28 my $cgi = new CGI;
29
30 my $op = $cgi->param('op') || q{};
31 my $template_id = $cgi->param('template_id');
32
33 my ($template, $loggedinuser, $cookie)
34     = get_template_and_user({
35             template_name => "tools/marc_modification_templates.tt",
36             query => $cgi,
37             type => "intranet",
38             authnotrequired => 0,
39             flagsrequired => { tools => 'marc_modfication_templates' },
40             debug => 1,
41     });
42
43 if ( $op eq "create_template" ) {
44   $template_id = '' unless $cgi->param('duplicate_current_template');
45   $template_id = AddModificationTemplate( $cgi->param('template_name'), $template_id );
46
47 } elsif ( $op eq "delete_template" ) {
48
49   DelModificationTemplate( $template_id );
50   $template_id = '';
51
52 } elsif ( $op eq "add_action" ) {
53
54   my $mmta_id = $cgi->param('mmta_id');
55   my $action = $cgi->param('action');
56   my $field_number = $cgi->param('field_number');
57   my $from_field = $cgi->param('from_field');
58   my $from_subfield = $cgi->param('from_subfield');
59   my $field_value = $cgi->param('field_value');
60   my $to_field = $cgi->param('to_field');
61   my $to_subfield = $cgi->param('to_subfield');
62   my $to_regex_search = $cgi->param('to_regex_search');
63   my $to_regex_replace = $cgi->param('to_regex_replace');
64   my $to_regex_modifiers = $cgi->param('to_regex_modifiers');
65   my $conditional = $cgi->param('conditional');
66   my $conditional_field = $cgi->param('conditional_field');
67   my $conditional_subfield = $cgi->param('conditional_subfield');
68   my $conditional_comparison = $cgi->param('conditional_comparison');
69   my $conditional_value = $cgi->param('conditional_value');
70   my $conditional_regex = ( $cgi->param('conditional_regex') eq 'on' ) ? 1 : 0;
71   my $description = $cgi->param('description');
72
73     if ($from_field) {
74         unless ($mmta_id) {
75             AddModificationTemplateAction(
76                 $template_id,            $action,
77                 $field_number,           $from_field,
78                 $from_subfield,          $field_value,
79                 $to_field,               $to_subfield,
80                 $to_regex_search,        $to_regex_replace,
81                 $to_regex_modifiers,     $conditional,
82                 $conditional_field,      $conditional_subfield,
83                 $conditional_comparison, $conditional_value,
84                 $conditional_regex,      $description
85             );
86         }
87         else {
88             ModModificationTemplateAction(
89                 $mmta_id,                $action,
90                 $field_number,           $from_field,
91                 $from_subfield,          $field_value,
92                 $to_field,               $to_subfield,
93                 $to_regex_search,        $to_regex_replace,
94                 $to_regex_modifiers,     $conditional,
95                 $conditional_field,      $conditional_subfield,
96                 $conditional_comparison, $conditional_value,
97                 $conditional_regex,      $description
98             );
99         }
100     }
101     else {
102         $template->param( error => 'no_from_field' );
103     }
104
105 } elsif ( $op eq "delete_action" ) {
106   DelModificationTemplateAction( $cgi->param('mmta_id') );
107
108 } elsif ( $op eq "move_action" ) {
109
110   MoveModificationTemplateAction( $cgi->param('mmta_id'), $cgi->param('where') );
111
112 }
113
114 my @templates = GetModificationTemplates( $template_id );
115
116 unless ( $template_id ) {
117   $template_id = $templates[0]->{'template_id'};
118   @templates = GetModificationTemplates( $template_id );
119 }
120
121 my @actions = GetModificationTemplateActions( $template_id );
122 foreach my $action ( @actions ) {
123   $action->{'action_delete_field'} = ( $action->{'action'} eq 'delete_field' );
124   $action->{'action_update_field'} = ( $action->{'action'} eq 'update_field' );
125   $action->{'action_move_field'} = ( $action->{'action'} eq 'move_field' );
126   $action->{'action_copy_field'} = ( $action->{'action'} eq 'copy_field' );
127   $action->{'action_copy_and_replace_field'} = ( $action->{'action'} eq 'copy_and_replace_field' );
128
129   $action->{'conditional_if'} = ( $action->{'conditional'} eq 'if' );
130   $action->{'conditional_unless'} = ( $action->{'conditional'} eq 'unless' );
131
132   $action->{'conditional_comparison_exists'} = ( $action->{'conditional_comparison'} eq 'exists' );
133   $action->{'conditional_comparison_not_exists'} = ( $action->{'conditional_comparison'} eq 'not_exists' );
134   $action->{'conditional_comparison_equals'} = ( $action->{'conditional_comparison'} eq 'equals' );
135   $action->{'conditional_comparison_not_equals'} = ( $action->{'conditional_comparison'} eq 'not_equals' );
136 }
137
138 $template->param(
139   TemplatesLoop => \@templates,
140   ActionsLoop => \@actions,
141
142   template_id => $template_id,
143 );
144
145 output_html_with_http_headers $cgi, $cookie, $template->output;