1 <& /Admin/Elements/Header, Title => $title &>
3 <& /Elements/ListActions, actions => \@results &>
5 <form action="Tasks.html" method="post">
6 <input type="hidden" name="Queue" value="<% $Queue %>" />
8 <label for="ConditionCF"><&|/l&>Enabled if</&>:</label>
9 % if ( $PossibleCustomFields->Count > 0 ) {
10 <select name="ConditionCF">
11 % while ( my $thiscf = $PossibleCustomFields->Next ) {
12 <option value="<% $thiscf->Id %>" <% $thiscf->Id == $cfid ? 'selected' : '' %>><% $thiscf->Name %></option>
15 <label for="ConditionValue"><&|/l&>equals</&></label>
16 <input name="ConditionValue" value="<% $cfvalue %>" />
18 <select name="no_cfs" disabled>
19 <option value="1">(no custom fields defined)</option>
24 % my (@links, @postponed); # not really used here
26 % foreach my $task_id (@task_ids, 'new') {
27 % # simulate creating the tickets, but don't evaluate any perl inclusions
28 % # in the content (_ActiveContent => 0 earlier)
29 % my ($ticket, $ticketargs);
30 % if ( $task_id eq 'new' ) {
31 % $ticket = RT::Ticket->new($session{'CurrentUser'});
34 % # any other defaults make sense here?
37 % ($ticket, $ticketargs) =
38 % $Action->ParseLines($task_id, \@links, \@postponed);
40 % my $subject = $ticketargs->{Subject};
41 % my $subjectprefix = 0;
42 % if ( $subject =~ s/^\Q$SUBJECT_PREFIX\E// ) {
49 <label for="task_id"><&|/l&>Task #</&><% $idx %>
50 % # each time these are edited, replace all task IDs with sequential numbers.
51 % # no point in letting them be anything else, at least yet.
52 <input type="hidden" name="task_id" value="<% $idx %>">
57 <td class="label"><&|/l&>Subject</&>:</td>
59 <input name="<% $idx %>-Subject" value="<% $subject |h %>" />
60 <input type="checkbox" name="<% $idx %>-SubjectPrefix" <% $subjectprefix ? 'checked' : '' %> /> <&|/l&>Prefix with main subject</&>
64 <td class="label"><&|/l&>In queue</&>:</td>
65 <td class="value"><& /Elements/SelectQueue,
68 Default => ($ticketargs->{Queue} || $Queue),
72 <td class="label"><&|/l&>Content</&>:</td>
73 <td class="value"><textarea name="<% $idx %>-Content" rows="10" cols="80" wrap="soft"><%
74 ( $ticketargs->{MIMEObj} ? $ticketargs->{MIMEObj}->body_as_string : '' )
82 <& /Elements/Submit, Label => 'Save Changes' &>
87 my $QueueObj = RT::Queue->new($session{'CurrentUser'});
88 $QueueObj->Load($Queue);
89 Abort(loc("Queue [_1] not found",$Queue)) unless $QueueObj->Id;
91 my $title = loc("Set up subtasks for queue [_1]", $QueueObj->Name);
93 my $TEMPLATE_NAME = '[Subtask]';
94 my $SCRIPCONDITION_NAME = '[Subtask] Queue='.$Queue;
95 my $SUBJECT_PREFIX = q({ $Tickets{'TOP'}->Subject }-);
97 my ($Scrip, $ScripCondition, $Template, $CustomField);
99 # SystemUser for the scrip so that the user doesn't need ACLs to edit scrips
100 # as such. all the scrip parameters are hardcoded anyway...
102 $ScripCondition = RT::ScripCondition->new($RT::SystemUser);
103 $ScripCondition->LoadByCol('Name', $SCRIPCONDITION_NAME);
105 $Template = RT::Template->new($session{'CurrentUser'});
106 $Template->LoadByName(
107 Name => $TEMPLATE_NAME,
111 $Scrip = RT::Scrip->new($RT::SystemUser);
113 my $Scrips = RT::Scrips->new($RT::SystemUser);
114 $Scrips->LimitToQueue($Queue);
115 $Scrips->Limit( FIELD => 'Template', VALUE => $TEMPLATE_NAME );
116 if ( $Scrips->Count > 0 ) {
117 $Scrip = $Scrips->First;
121 # The CF name to test, and the value it must have to trigger the scrip.
122 my $cfid = $ARGS{ConditionCF};
123 my $cfvalue = $ARGS{ConditionValue};
124 $CustomField = RT::CustomField->new($session{'CurrentUser'});
126 $CustomField->Load($cfid);
128 my $cfname = $CustomField->Name;
130 # if there's input from the form, process it into a new template content
131 my $new_content = '';
133 if ( $ARGS{task_id} ) { # actually contains numeric indices
134 my @task_ids = $ARGS{task_id};
135 @task_ids = @{ $task_ids[0] } if ref($task_ids[0]);
136 foreach my $task_id (@task_ids) {
137 # find the inputs for this task_id
138 my %task_opts = map { $_ => $ARGS{$_} }
139 grep /^$task_id-/, keys(%ARGS);
140 my $task_content = "===Create-Ticket: $task_id
144 # any other static content can go here, but we always want the child
145 # ticket relationship, and we want to force the ConditionCF to be empty
146 # to avoid recursion.
150 # special case: automate prefixing the main ticket subject
151 if ( $task_opts{"$task_id-SubjectPrefix"} ) {
152 $task_opts{"$task_id-Subject"} =
153 $SUBJECT_PREFIX . $task_opts{"$task_id-Subject"};
156 foreach my $key (sort keys %task_opts) {
157 $key =~ /^$task_id-(.*)/;
159 my $value = $task_opts{$key};
163 $task_content .= "$tag: $value\n";
164 # only create a task if the ticket has non-whitespace content
165 if ( lc($tag) eq 'content' and length($value) > 0 ) {
166 $task_content .= "ENDOFCONTENT\n";
170 if ( $has_content ) {
171 $new_content .= $task_content;
174 warn "NEW CONTENT:\n$new_content\n\n"; # XXX
176 if ( ! $Template->Id ) {
177 my ( $val, $msg ) = $Template->Create(
179 Name => $TEMPLATE_NAME,
180 Description => 'Subtask tickets',
182 Content => $new_content,
185 push @results, loc("Could not create template: [_1]", $msg);
187 push @results, loc("Template created");
189 } elsif ( $Template->Content ne $new_content ) { # template needs updating
190 my ( $val, $msg ) = $Template->SetContent($new_content);
192 push @results, loc("Could not update template: [_1]", $msg);
194 push @results, loc("Template updated");
198 # Set up ScripCondition
200 push @results, loc("No custom field selected");
201 } elsif ( length($cfvalue) == 0 ) {
202 push @results, loc("Custom field value is required");
203 } elsif ( ! $ScripCondition->Id ) {
204 my ( $val, $msg ) = $ScripCondition->Create(
205 Name => $SCRIPCONDITION_NAME,
206 Description => "When CF.[$cfname] equals '$cfvalue'",
207 ExecModule => 'CustomFieldEquals',
208 Argument => "$cfname=$cfvalue",
209 ApplicableTransTypes => 'Any',
212 push @results, loc("Could not create custom field condition: [_1]", $msg);
214 push @results, loc("Custom field condition created");
216 } elsif ( $ScripCondition->Argument ne "$cfname=$cfvalue" ) {
217 my ( $val, $msg ) = $ScripCondition->SetArgument("$cfname=$cfvalue");
219 push @results, loc("Could not set custom field condition: [_1]", $msg);
221 push @results, loc("Custom field condition set");
226 if ( $Template->Id and ! $Scrip->Id ) {
227 my ($val, $msg) = $Scrip->Create(
229 Template => $Template->Id,
230 Description => 'Create subtasks for ' . $QueueObj->Name,
231 ScripCondition => $ScripCondition->Id,
232 ScripAction => 'Create Tickets',
235 push @results, loc("Could not create scrip: [_1]", $msg);
237 push @results, loc("Scrip created");
239 } # else don't need to create the scrip
241 # even if $new_content is empty, there's no harm in letting the scrip and
242 # template exist with empty content. they just won't do anything.
245 # CHANGES HAVE BEEN SAVED.
246 # Now prepare to (re-)display the form.
248 # ask RT::Action::CreateTickets how it will parse the template
249 my $action_class = 'RT::Action::CreateTickets';
250 $action_class->require;
251 my $Action = $action_class->new(
252 CurrentUser => $session{'CurrentUser'},
254 # this will populate $Action with the 'create_tickets' hash
255 warn $Template->Content;
257 Content => $Template->Content,
260 warn Dumper \$Action;
262 @task_ids = @{ $Action->{create_tickets} } if exists $Action->{create_tickets};
264 my $PossibleCustomFields = $QueueObj->TicketCustomFields;
268 $Queue => undef #queue id