Bug 8015: Add MARC Modifications Templates
[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 Koha::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({template_name => "tools/marc_modification_templates.tmpl",
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 = $cgi->param('to_regex');
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';
69   my $description = $cgi->param('description');
70
71   unless ( $mmta_id ) {
72     AddModificationTemplateAction(
73       $template_id,
74       $action,
75       $field_number,
76       $from_field,
77       $from_subfield,
78       $field_value,
79       $to_field,
80       $to_subfield,
81       $to_regex,
82       $conditional,
83       $conditional_field,
84       $conditional_subfield,
85       $conditional_comparison,
86       $conditional_value,
87       $conditional_regex,
88       $description
89     );
90   } else {
91     ModModificationTemplateAction(
92       $mmta_id,
93       $action,
94       $field_number,
95       $from_field,
96       $from_subfield,
97       $field_value,
98       $to_field,
99       $to_subfield,
100       $to_regex,
101       $conditional,
102       $conditional_field,
103       $conditional_subfield,
104       $conditional_comparison,
105       $conditional_value,
106       $conditional_regex,
107       $description
108     );
109
110   }
111
112 } elsif ( $op eq "delete_action" ) {
113   DelModificationTemplateAction( $cgi->param('mmta_id') );
114
115 } elsif ( $op eq "move_action" ) {
116
117   MoveModificationTemplateAction( $cgi->param('mmta_id'), $cgi->param('where') );
118
119 }
120
121 my @templates = GetModificationTemplates( $template_id );
122
123 unless ( $template_id ) {
124   $template_id = $templates[0]->{'template_id'};
125   @templates = GetModificationTemplates( $template_id );
126 }
127
128 my @actions = GetModificationTemplateActions( $template_id );
129 foreach my $action ( @actions ) {
130   $action->{'action_delete_field'} = ( $action->{'action'} eq 'delete_field' );
131   $action->{'action_update_field'} = ( $action->{'action'} eq 'update_field' );
132   $action->{'action_move_field'} = ( $action->{'action'} eq 'move_field' );
133   $action->{'action_copy_field'} = ( $action->{'action'} eq 'copy_field' );
134
135   $action->{'conditional_if'} = ( $action->{'conditional'} eq 'if' );
136   $action->{'conditional_unless'} = ( $action->{'conditional'} eq 'unless' );
137
138   $action->{'conditional_comparison_exists'} = ( $action->{'conditional_comparison'} eq 'exists' );
139   $action->{'conditional_comparison_not_exists'} = ( $action->{'conditional_comparison'} eq 'not_exists' );
140   $action->{'conditional_comparison_equals'} = ( $action->{'conditional_comparison'} eq 'equals' );
141   $action->{'conditional_comparison_not_equals'} = ( $action->{'conditional_comparison'} eq 'not_equals' );
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;