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