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