Bug 22544: Move del_opac_new to Koha namespace
[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 = CGI->new;
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             flagsrequired => { tools => 'marc_modification_templates' },
39     });
40
41 if ( $op eq "create_template" ) {
42   $template_id = '' unless $cgi->param('duplicate_current_template');
43   $template_id = AddModificationTemplate( scalar $cgi->param('template_name'), $template_id );
44
45 } elsif ( $op eq "delete_template" ) {
46
47   DelModificationTemplate( $template_id );
48   $template_id = '';
49
50 } elsif ( $op eq "add_action" ) {
51
52   my $mmta_id = $cgi->param('mmta_id');
53   my $action = $cgi->param('action');
54   my $field_number = $cgi->param('field_number');
55   my $from_field = $cgi->param('from_field');
56   my $from_subfield = $cgi->param('from_subfield');
57   my $field_value = $cgi->param('field_value');
58   my $to_field = $cgi->param('to_field');
59   my $to_subfield = $cgi->param('to_subfield');
60   my $to_regex_search = $cgi->param('to_regex_search');
61   my $to_regex_replace = $cgi->param('to_regex_replace');
62   my $to_regex_modifiers = $cgi->param('to_regex_modifiers');
63   my $conditional = $cgi->param('conditional');
64   my $conditional_field = $cgi->param('conditional_field');
65   my $conditional_subfield = $cgi->param('conditional_subfield');
66   my $conditional_comparison = $cgi->param('conditional_comparison');
67   my $conditional_value = $cgi->param('conditional_value');
68   my $conditional_regex = ( $cgi->param('conditional_regex') eq 'on' ) ? 1 : 0;
69   my $description = $cgi->param('description');
70
71     if ($from_field) {
72         unless ($mmta_id) {
73             AddModificationTemplateAction(
74                 $template_id,            $action,
75                 $field_number,           $from_field,
76                 $from_subfield,          $field_value,
77                 $to_field,               $to_subfield,
78                 $to_regex_search,        $to_regex_replace,
79                 $to_regex_modifiers,     $conditional,
80                 $conditional_field,      $conditional_subfield,
81                 $conditional_comparison, $conditional_value,
82                 $conditional_regex,      $description
83             );
84         }
85         else {
86             ModModificationTemplateAction(
87                 $mmta_id,                $action,
88                 $field_number,           $from_field,
89                 $from_subfield,          $field_value,
90                 $to_field,               $to_subfield,
91                 $to_regex_search,        $to_regex_replace,
92                 $to_regex_modifiers,     $conditional,
93                 $conditional_field,      $conditional_subfield,
94                 $conditional_comparison, $conditional_value,
95                 $conditional_regex,      $description
96             );
97         }
98     }
99     else {
100         $template->param( error => 'no_from_field' );
101     }
102
103 } elsif ( $op eq "delete_action" ) {
104   DelModificationTemplateAction( scalar $cgi->param('mmta_id') );
105
106 } elsif ( $op eq "move_action" ) {
107
108   MoveModificationTemplateAction( scalar $cgi->param('mmta_id'), scalar $cgi->param('where') );
109
110 }
111
112 my @templates = GetModificationTemplates( $template_id );
113
114 my @actions = GetModificationTemplateActions( $template_id );
115 foreach my $action ( @actions ) {
116   $action->{'action_delete_field'} = ( $action->{'action'} eq 'delete_field' );
117   $action->{'action_add_field'} = ( $action->{'action'} eq 'add_field' );
118   $action->{'action_update_field'} = ( $action->{'action'} eq 'update_field' );
119   $action->{'action_move_field'} = ( $action->{'action'} eq 'move_field' );
120   $action->{'action_copy_field'} = ( $action->{'action'} eq 'copy_field' );
121   $action->{'action_copy_and_replace_field'} = ( $action->{'action'} eq 'copy_and_replace_field' );
122
123   if( defined $action->{'conditional'} ){
124       $action->{'conditional_if'} = ( $action->{'conditional'} eq 'if' );
125       $action->{'conditional_unless'} = ( $action->{'conditional'} eq 'unless' );
126   }
127
128   if( defined $action->{'conditional_comparison'} ){
129       $action->{'conditional_comparison_exists'} = ( $action->{'conditional_comparison'} eq 'exists' );
130       $action->{'conditional_comparison_not_exists'} = ( $action->{'conditional_comparison'} eq 'not_exists' );
131       $action->{'conditional_comparison_equals'} = ( $action->{'conditional_comparison'} eq 'equals' );
132       $action->{'conditional_comparison_not_equals'} = ( $action->{'conditional_comparison'} eq 'not_equals' );
133   }
134 }
135
136 $template->param(
137   TemplatesLoop => \@templates,
138   ActionsLoop => \@actions,
139
140   template_id => $template_id,
141 );
142
143 output_html_with_http_headers $cgi, $cookie, $template->output;