also prevent some edits to auto-created scrips
[freeside.git] / rt / share / html / Admin / Elements / EditScrip
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2013 Best Practical Solutions, LLC
6 %#                                          <sales@bestpractical.com>
7 %#
8 %# (Except where explicitly superseded by other copyright notices)
9 %#
10 %#
11 %# LICENSE:
12 %#
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
16 %# from www.gnu.org.
17 %#
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 %# General Public License for more details.
22 %#
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 %# 02110-1301 or visit their web page on the internet at
27 %# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 %#
29 %#
30 %# CONTRIBUTION SUBMISSION POLICY:
31 %#
32 %# (The following paragraph is not intended to limit the rights granted
33 %# to you to modify and distribute this software under the terms of
34 %# the GNU General Public License and is only of importance to you if
35 %# you choose to contribute your changes and enhancements to the
36 %# community by submitting them to Best Practical Solutions, LLC.)
37 %#
38 %# By intentionally submitting any modifications, corrections or
39 %# derivatives to this work, or any other work intended for use with
40 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 %# you are the copyright holder for those contributions and you grant
42 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 %# royalty-free, perpetual, license to use, copy, create derivative
44 %# works based on those contributions, and sublicense and distribute
45 %# those contributions and any derivatives thereof.
46 %#
47 %# END BPS TAGGED BLOCK }}}
48 <& /Elements/ListActions, actions => \@actions &>
49   
50 <form method="post" action="Scrip.html" id="ModifyScrip" name="ModifyScrip">
51 <input type="hidden" class="hidden" name="id" value="<% $id %>" />
52 <input type="hidden" class="hidden" name="Queue" value="<% $Queue %>" />
53
54 <&| /Widgets/TitleBox, title => loc('Scrip Fields') &>
55 <table>
56
57 <tr><td class="label"><&|/l&>Description</&>:</td><td class="value">\
58 % if ( $immutable ) {
59 <% $scrip->Description %> <i>(system scrip)</i>
60 <input type="hidden" name="Scrip-<% $id %>-Description" value="<% $scrip->Description %>">
61 % } else {
62 <input name="Scrip-<% $id %>-Description" \
63     size="60" \
64     value="<% $ARGS{"Scrip-$id-Description"} || $scrip->Description || '' %>" />
65 % }
66 </td></tr>
67
68 <tr><td class="label"><&|/l&>Condition</&>:</td><td class="value">\
69 % if ( $immutable ) {
70 <% loc( $scrip->ConditionObj->Name ) %>
71 <input type="hidden" name="Scrip-<%$id%>-Condition" value="<% $scrip->ConditionObj->Id %>">
72 % } else {
73 <& /Admin/Elements/SelectScripCondition,
74     Name => "Scrip-$id-ScripCondition",
75     Default => $ARGS{"Scrip-$id-ScripCondition"} || $scrip->ConditionObj->Id,
76     ScripObj => $scrip,
77     Queue => $Queue,
78 &>
79 % }
80 </td></tr>
81
82 <& /Admin/Elements/EditScripOptions,
83     Name => "Condition",
84     Default => $ARGS{"Scrip-$id-ConditionRules"} || $scrip->ConditionRules,
85     Queue => $Queue,
86     ScripX => $ARGS{"Scrip-$id-ScripCondition"} || $scrip->ConditionObj->Id,
87 &>
88
89 <tr><td class="label"><&|/l&>Action</&>:</td><td class="value">\
90 % if ( $immutable ) {
91 <% loc( $scrip->ActionObj->Name ) %>
92 <input type="hidden" name="Scrip-<%$id%>-Action" value="<% $scrip->ActionObj->Id %>">
93 % } else {
94 <& /Admin/Elements/SelectScripAction,
95     Name => "Scrip-$id-ScripAction",
96     Default => $ARGS{"Scrip-$id-ScripAction"} || $scrip->ActionObj->Id,
97     ScripObj => $scrip,
98     Queue => $Queue,
99 &>
100 % }
101 </td></tr>
102
103 <& /Admin/Elements/EditScripOptions,
104     Name => "Action",
105     Default => $ARGS{"Scrip-$id-ActionRules"} || $scrip->ActionRules,
106     Queue => $Queue,
107     ScripX => $ARGS{"Scrip-$id-ScripAction"} || $scrip->ActionObj->Id,
108 &>
109
110 <tr><td class="label"><&|/l&>Template</&>:</td><td class="value">\
111 % if ( $immutable ) {
112 <% $scrip->TemplateObj->Name %>
113 <input type="hidden" name="Scrip-<%$id%>-Template" value="<% $scrip->TemplateObj->Id %>">
114 % } else {
115 <& /Admin/Elements/SelectTemplate,
116     Name => "Scrip-$id-Template",
117     Default => $ARGS{"Scrip-$id-Template"} || $scrip->TemplateObj->Id,
118     Queue => $Queue,
119 &>
120 % }
121 </td></tr>
122
123 <tr><td class="label"><&|/l&>Stage</&>:</td><td class="value">\
124 <& /Admin/Elements/SelectStage,
125     Name => "Scrip-$id-Stage",
126     Default => $ARGS{"Scrip-$id-Stage"} || $scrip->Stage,
127 &></td></tr>
128
129 </table>
130 </&>
131
132 % if ($session{CurrentUser}->HasRight(Object => $RT::System, Right => 'ExecuteCode')) {
133 <& /Elements/Submit,
134     Label => $SubmitLabel,
135     Reset => 1,
136 &><br />
137
138 <&| /Widgets/TitleBox, title => loc('User Defined conditions and actions') &>
139 <table>
140 <tr><td colspan="2">
141 <i><&|/l&>(Use these fields when you choose 'User Defined' for a condition or action)</&></i>
142 </td></tr>
143
144 <tr><td class="labeltop"><&|/l&>Custom condition</&>:</td><td class="value">
145 % my $code = $ARGS{"Scrip-$id-CustomIsApplicableCode"} || $scrip->CustomIsApplicableCode || '';
146 % my $lines = @{[ $code =~ /\n/gs ]} + 3;
147 % $lines = $min_lines if $lines < $min_lines;
148 <textarea cols="80" rows="<% $lines %>" name="Scrip-<% $id %>-CustomIsApplicableCode"><% $code %></textarea>
149 </td></tr>
150
151 <tr><td class="labeltop"><&|/l&>Custom action preparation code</&>:</td><td class="value">
152 % $code = $ARGS{"Scrip-$id-CustomPrepareCode"} || $scrip->CustomPrepareCode || '';
153 % $lines = @{[ $code =~ /\n/gs ]} + 3;
154 % $lines = $min_lines if $lines < $min_lines;
155 <textarea cols="80" rows="<% $lines %>" name="Scrip-<% $id %>-CustomPrepareCode"><% $code %></textarea>
156 </td></tr>
157
158 <tr><td class="labeltop"><&|/l&>Custom action cleanup code</&>:</td><td class="value">
159 % $code = $ARGS{"Scrip-$id-CustomCommitCode"} || $scrip->CustomCommitCode || '';
160 % $lines = @{[ $code =~ /\n/gs ]} + 3;
161 % $lines = $min_lines if $lines < $min_lines;
162 <textarea cols="80" rows="<% $lines %>" name="Scrip-<% $id || '' %>-CustomCommitCode"><% $code %></textarea>
163 </td></tr>
164
165 </table>
166 </&>
167
168 % }
169
170 <& /Elements/Submit,
171     Label => $SubmitLabel,
172     Reset => 1,
173 &>
174
175 </form>
176 <%init>
177
178 my (@actions, $SubmitLabel);
179 my $scrip = RT::Scrip->new( $session{'CurrentUser'} );
180 my $immutable = 0;
181
182 if ( $id ) {
183     $scrip->Load( $id );
184     unless ( $id = $scrip->id ) {
185         push @actions, loc("Couldn't load scrip #[_1]", $id);
186     }
187     $SubmitLabel = loc('Save Changes');
188     my $a = $scrip->FirstAttribute('Immutable');
189     if ( defined($a) and $a->Content ) {
190         $immutable = 1;
191     }
192 }
193
194 unless ( $id ) {
195     $id = 'new';
196     $SubmitLabel = loc('Create');
197 }
198
199 my $min_lines = 10;
200
201 my ($ok, $msg) = $scrip->CompileCheck;
202 push @actions, $msg if !$ok;
203
204 </%init>
205
206 <%ARGS>
207 $id => undef
208 $title => undef
209 $Queue => 0
210 </%ARGS>
211
212 <%METHOD Process>
213 <%ARGS>
214 $id => undef
215 $Queue => undef
216 </%ARGS>
217 <%INIT>
218 return ($id) unless $id;
219
220 my @rules = ('ConditionRules', 'ActionRules');
221 if ( exists($ARGS{"Scrip-$id-ScripCondition"}) ) {
222   foreach my $rules (@rules) {
223     my $prefix = join('-', 'Scrip', $id, $rules);
224     $ARGS{$prefix} = join("\n", map {
225       $_ =~ /^$rules-(.*)$/ ? 
226             ($1, $ARGS{$_}) : ()
227     } keys(%ARGS)
228     );
229   }
230 }
231
232 my $scrip = RT::Scrip->new( $session{'CurrentUser'} );
233 if ( $id eq 'new' ) {
234     return $scrip->Create(
235         Queue                  => $Queue,
236         ScripAction            => $ARGS{"Scrip-new-ScripAction"},
237         ScripCondition         => $ARGS{"Scrip-new-ScripCondition"},
238         Template               => $ARGS{"Scrip-new-Template"},
239         Description            => $ARGS{"Scrip-new-Description"},
240         CustomPrepareCode      => $ARGS{"Scrip-new-CustomPrepareCode"},
241         CustomCommitCode       => $ARGS{"Scrip-new-CustomCommitCode"},
242         CustomIsApplicableCode => $ARGS{"Scrip-new-CustomIsApplicableCode"},
243         Stage                  => $ARGS{"Scrip-new-Stage"},
244         ConditionRules         => $ARGS{"Scrip-new-ConditionRules"},
245         ActionRules            => $ARGS{"Scrip-new-ActionRules"},
246     );
247 }
248 else {
249     $scrip->Load( $id );
250     return (undef, loc("Couldn't load scrip #[_1]", $id))
251         unless $scrip->id;
252
253     my @attribs = qw(Queue ScripAction ScripCondition Template Stage
254         Description CustomPrepareCode CustomCommitCode CustomIsApplicableCode
255         ConditionRules ActionRules);
256     my @results = UpdateRecordObject(
257         AttributesRef   => \@attribs,
258         AttributePrefix => 'Scrip-'.$scrip->Id,
259         Object          => $scrip,
260         ARGSRef         => \%ARGS
261     );
262     return ($scrip->id, @results);
263 }
264 </%INIT>
265 </%METHOD>