1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
<& /Admin/Elements/Header, Title => $title &>
<& /Elements/Tabs &>
<& /Elements/ListActions, actions => \@results &>
<form action="Tasks.html" method="post">
<input type="hidden" name="Queue" value="<% $Queue %>" />
<table>
% my (@links, @postponed); # not really used here
% my $idx = 1;
% foreach my $task_id (@task_ids, 'new') {
% # simulate creating the tickets, but don't evaluate any perl inclusions
% # in the content (_ActiveContent => 0 earlier)
% my ($ticket, $ticketargs);
% if ( $task_id eq 'new' ) {
% $ticket = RT::Ticket->new($session{'CurrentUser'});
% $ticketargs = {
% Queue => $Queue,
% # any other defaults make sense here?
% };
% } else {
% ($ticket, $ticketargs) =
% $Action->ParseLines($task_id, \@links, \@postponed);
% }
% my $subject = $ticketargs->{Subject};
% my $subjectprefix = 0;
% if ( $subject =~ s/^\Q$SUBJECT_PREFIX\E// ) {
% $subjectprefix = 1;
% }
<tr>
<td colspan="2">
<h2>
<label for="task_id"><&|/l&>Task #</&><% $idx %>
% # each time these are edited, replace all task IDs with sequential numbers.
% # no point in letting them be anything else, at least yet.
<input type="hidden" name="task_id" value="<% $idx %>">
</h2>
</td>
</tr>
<tr>
<td class="label"><&|/l&>Subject</&>:</td>
<td class="value">
<input name="<% $idx %>-Subject" value="<% $subject |h %>" />
<input type="checkbox" name="<% $idx %>-SubjectPrefix" <% $subjectprefix ? 'checked' : '' %> /> <&|/l&>Prefix with main subject</&>
</td>
</tr>
<tr>
<td class="label"><&|/l&>In queue</&>:</td>
<td class="value"><& /Elements/SelectQueue,
Name => "$idx-Queue",
ShowNullOption => 0,
Default => ($ticketargs->{Queue} || $Queue),
&></td>
</tr>
<tr>
<td class="label"><&|/l&>Content</&>:</td>
<td class="value"><textarea name="<% $idx %>-Content" rows="10" cols="80" wrap="soft"><%
( $ticketargs->{MIMEObj} ? $ticketargs->{MIMEObj}->body_as_string : '' )
%></textarea>
</td>
</tr>
% $idx++;
% }
</table>
<& /Elements/Submit, Label => 'Save Changes' &>
</form>
<%init>
my @results;
my $QueueObj = RT::Queue->new($session{'CurrentUser'});
$QueueObj->Load($Queue);
Abort(loc("Queue [_1] not found",$Queue)) unless $QueueObj->Id;
my $title = loc("Set up subtasks for queue [_1]", $QueueObj->Name);
my $TEMPLATE_NAME = '[Subtask]';
my $SCRIPCONDITION_NAME = '[Subtask] Queue='.$Queue;
my $SUBJECT_PREFIX = q({ $TOP->Subject }-);
my $CUSTOMFIELD_NAME = 'Create subtasks';
my ($Scrip, $ScripCondition, $Template, $CustomField);
# SystemUser for the scrip so that the user doesn't need ACLs to edit scrips
# as such. all the scrip parameters are hardcoded anyway...
$ScripCondition = RT::ScripCondition->new($RT::SystemUser);
$ScripCondition->LoadByCol('Name', $SCRIPCONDITION_NAME);
$Template = RT::Template->new($session{'CurrentUser'});
$Template->LoadByName(
Name => $TEMPLATE_NAME,
Queue => $Queue,
);
$Scrip = RT::Scrip->new($RT::SystemUser);
{
my $Scrips = RT::Scrips->new($RT::SystemUser);
$Scrips->LimitToQueue($Queue);
$Scrips->Limit( FIELD => 'Template', VALUE => $Template->Name );
if ( $Scrips->Count > 0 ) {
$Scrip = $Scrips->First;
}
}
my $CustomField = RT::CustomField->new($RT::SystemUser);
$CustomField->LoadByName(
Name => $CUSTOMFIELD_NAME,
LookupType => 'RT::Queue-RT::Ticket',
);
# if there's input from the form, process it into a new template content
my $new_content = '';
if ( $ARGS{task_id} ) { # actually contains numeric indices
my @task_ids = $ARGS{task_id};
@task_ids = @{ $task_ids[0] } if ref($task_ids[0]);
foreach my $task_id (@task_ids) {
# find the inputs for this task_id
my %task_opts = map { $_ => $ARGS{$_} }
grep /^$task_id-/, keys(%ARGS);
my $task_content = "===Create-Ticket: $task_id
CF-$CUSTOMFIELD_NAME" . q[
Depended-On-By: TOP
Owner: { $TOP->Owner }
{ join("\n", map { "Requestor: $_" }
$TOP->Requestors->MemberEmailAddresses) }
];
# any other attributes to put on subtask tickets should go here also.
my $has_content = 0;
# special case: automate prefixing the main ticket subject
if ( $task_opts{"$task_id-SubjectPrefix"} ) {
$task_opts{"$task_id-Subject"} =
$SUBJECT_PREFIX . $task_opts{"$task_id-Subject"};
}
foreach my $key (sort keys %task_opts) {
$key =~ /^$task_id-(.*)/;
my $tag = $1;
my $value = $task_opts{$key};
$value =~ s/^\s*//;
$value =~ s/\s*$//;
$value =~ s/\r//g;
$task_content .= "$tag: $value\n";
# only create a task if the ticket has non-whitespace content
if ( lc($tag) eq 'content' and length($value) > 0 ) {
$task_content .= "ENDOFCONTENT\n";
$has_content = 1;
}
}
if ( $has_content ) {
$new_content .= $task_content;
}
}
# set up custom field, if necessary
if ( ! $CustomField->Id ) {
# should be RenderType 'Checkbox', but there isn't one yet...
my ($val, $msg) = $CustomField->Create(
Name => $CUSTOMFIELD_NAME,
Type => 'Select',
MaxValues => 1,
LookupType => 'RT::Queue-RT::Ticket',
Description => 'Start subtasks for this ticket',
RenderType => 'Dropdown',
);
if ($val) {
# should be impossible for this to fail
($val, $msg) = $CustomField->AddValue(Name => 'Yes');
}
if (!$val) {
push @results, loc("Could not create ticket custom field: [_1]", $msg);
} else {
push @results, loc("Custom field created");
}
}
# apply CF to the queue, iff there are any tasks set up
if ( length($new_content) and ! $CustomField->IsAdded($Queue) ) {
my ($val, $msg) = $CustomField->AddToObject($QueueObj);
if (!$val) {
push @results, loc("Could not apply custom field to this queue: [_1]", $msg);
} else {
push @results, loc("Applied custom field to this queue");
}
} elsif ( ! length($new_content) and $CustomField->IsAdded($Queue) ) {
my ($val, $msg) = $CustomField->RemoveFromObject($QueueObj);
if (!$val) {
push @results, loc("Could not remove custom field from this queue: [_1]", $msg);
} else {
push @results, loc("Removed custom field from this queue");
}
}
if ( ! $Template->Id ) {
my ( $val, $msg ) = $Template->Create(
Queue => $Queue,
Name => $TEMPLATE_NAME,
Description => 'Subtask tickets',
Type => 'Perl',
Content => $new_content,
);
if (!$val) {
push @results, loc("Could not create template: [_1]", $msg);
} else {
push @results, loc("Template created");
}
} elsif ( $Template->Content ne $new_content ) { # template needs updating
my ( $val, $msg ) = $Template->SetContent($new_content);
if (!$val) {
push @results, loc("Could not update template: [_1]", $msg);
} else {
push @results, loc("Template updated");
}
}
# Set up ScripCondition
if ( ! $ScripCondition->Id ) {
my ( $val, $msg ) = $ScripCondition->Create(
Name => $SCRIPCONDITION_NAME,
Description => "When CF.[$CUSTOMFIELD_NAME] equals 'Yes'",
ExecModule => 'CustomFieldEquals',
Argument => "$CUSTOMFIELD_NAME=Yes",
ApplicableTransTypes => 'Any',
);
if (!$val) {
push @results, loc("Could not create custom field condition: [_1]", $msg);
} else {
push @results, loc("Custom field condition created");
}
} elsif ( $ScripCondition->Argument ne "$CUSTOMFIELD_NAME=Yes" ) {
my ( $val, $msg ) = $ScripCondition->SetArgument("$CUSTOMFIELD_NAME=Yes");
if (!$val) {
push @results, loc("Could not set custom field condition: [_1]", $msg);
} else {
push @results, loc("Custom field condition set");
}
}
# Set up Scrip
if ( $Template->Id and ! $Scrip->Id ) {
my ($val, $msg) = $Scrip->Create(
Queue => $Queue,
Template => $Template->Id,
Description => 'Create subtasks for ' . $QueueObj->Name,
ScripCondition => $ScripCondition->Id,
ScripAction => 'Create Tickets',
);
if (!$val) {
push @results, loc("Could not create scrip: [_1]", $msg);
} else {
push @results, loc("Scrip created");
}
} # else don't need to create the scrip
# even if $new_content is empty, there's no harm in letting the scrip and
# template exist with empty content. they just won't do anything.
}
# CHANGES HAVE BEEN SAVED.
# Now prepare to (re-)display the form.
# ask RT::Action::CreateTickets how it will parse the template
my $action_class = 'RT::Action::CreateTickets';
$action_class->require;
my $Action = $action_class->new(
CurrentUser => $session{'CurrentUser'},
);
# this will populate $Action with the 'create_tickets' hash
$Action->Parse(
Content => $Template->Content,
_ActiveContent => 0,
);
my @task_ids;
@task_ids = @{ $Action->{create_tickets} } if exists $Action->{create_tickets};
</%init>
<%ARGS>
$Queue => undef #queue id
</%ARGS>
|