fix some problems with creation of subtask tickets, #34061
[freeside.git] / rt / share / html / Admin / Queues / Tasks.html
1 <& /Admin/Elements/Header, Title => $title &>
2 <& /Elements/Tabs &>
3 <& /Elements/ListActions, actions => \@results &>
4
5 <form action="Tasks.html" method="post">
6 <input type="hidden" name="Queue" value="<% $Queue %>" />
7 <h2>
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>
13 %   }
14   </select>
15   <label for="ConditionValue"><&|/l&>equals</&></label>
16   <input name="ConditionValue" value="<% $cfvalue %>" />
17 % } else {
18   <select name="no_cfs" disabled>
19     <option value="1">(no custom fields defined)</option>
20   </select>
21 % }
22 </h2>
23 <table>
24 % my (@links, @postponed); # not really used here
25 % my $idx = 1;
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'});
32 %     $ticketargs = {
33 %       Queue => $Queue,
34 %       # any other defaults make sense here?
35 %     };
36 %   } else {
37 %     ($ticket, $ticketargs) =
38 %       $Action->ParseLines($task_id, \@links, \@postponed);
39 %   }
40 %   my $subject = $ticketargs->{Subject};
41 %   my $subjectprefix = 0;
42 %   if ( $subject =~ s/^\Q$SUBJECT_PREFIX\E// ) {
43 %     $subjectprefix = 1;
44 %   }
45
46   <tr>
47     <td colspan="2">
48       <h2>
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 %>">
53       </h2>
54     </td>
55   </tr>
56   <tr>
57     <td class="label"><&|/l&>Subject</&>:</td>
58     <td class="value">
59       <input name="<% $idx %>-Subject" value="<% $subject |h %>" />
60       <input type="checkbox" name="<% $idx %>-SubjectPrefix" <% $subjectprefix ? 'checked' : '' %> /> <&|/l&>Prefix with main subject</&>
61     </td>
62   </tr>
63   <tr>
64     <td class="label"><&|/l&>In queue</&>:</td>
65     <td class="value"><& /Elements/SelectQueue,
66       Name => "$idx-Queue",
67       ShowNullOption => 0,
68       Default => ($ticketargs->{Queue} || $Queue),
69     &></td>
70   </tr>
71   <tr>
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 : '' )
75     %></textarea>
76     </td>
77   </tr>
78
79 %   $idx++;
80 % }
81 </table>
82 <& /Elements/Submit, Label => 'Save Changes' &>
83 </form>
84 <%init>
85 my @results;
86
87 my $QueueObj = RT::Queue->new($session{'CurrentUser'});
88 $QueueObj->Load($Queue);
89 Abort(loc("Queue [_1] not found",$Queue)) unless $QueueObj->Id;
90
91 my $title = loc("Set up subtasks for queue [_1]", $QueueObj->Name);
92
93 my $TEMPLATE_NAME = '[Subtask]';
94 my $SCRIPCONDITION_NAME = '[Subtask] Queue='.$Queue;
95 my $SUBJECT_PREFIX = q({ $TOP->Subject }-);
96
97 my ($Scrip, $ScripCondition, $Template, $CustomField);
98
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...
101
102 $ScripCondition = RT::ScripCondition->new($RT::SystemUser);
103 $ScripCondition->LoadByCol('Name', $SCRIPCONDITION_NAME);
104
105 $Template = RT::Template->new($session{'CurrentUser'});
106 $Template->LoadByName(
107   Name  => $TEMPLATE_NAME,
108   Queue => $Queue,
109 );
110
111 $Scrip = RT::Scrip->new($RT::SystemUser);
112 {
113   my $Scrips = RT::Scrips->new($RT::SystemUser);
114   $Scrips->LimitToQueue($Queue);
115   $Scrips->Limit( FIELD => 'Template', VALUE => $Template->Id );
116   if ( $Scrips->Count > 0 ) {
117     $Scrip = $Scrips->First;
118   }
119 }
120
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'});
125 if ( $cfid ) {
126   $CustomField->Load($cfid);
127 }
128 my $cfname = $CustomField->Name;
129
130 # if there's input from the form, process it into a new template content
131 my $new_content = '';
132
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
141 CF-$cfname:" . q[
142 Depended-On-By: TOP
143 Owner: { $TOP->Owner }
144 { join("\n", map { "Requestor: $_" }
145   $TOP->Requestors->MemberEmailAddresses) }
146 ];
147     # any other attributes to put on subtask tickets should go here also.
148
149     my $has_content = 0;
150
151     # special case: automate prefixing the main ticket subject
152     if ( $task_opts{"$task_id-SubjectPrefix"} ) {
153       $task_opts{"$task_id-Subject"} =
154         $SUBJECT_PREFIX . $task_opts{"$task_id-Subject"};
155     }
156     
157     foreach my $key (sort keys %task_opts) {
158       $key =~ /^$task_id-(.*)/;
159       my $tag = $1;
160       my $value = $task_opts{$key};
161       $value =~ s/^\s*//;
162       $value =~ s/\s*$//;
163       $value =~ s/\r//g;
164       $task_content .= "$tag: $value\n";
165       # only create a task if the ticket has non-whitespace content
166       if ( lc($tag) eq 'content' and length($value) > 0 ) {
167         $task_content .= "ENDOFCONTENT\n";
168         $has_content = 1;
169       }
170     }
171     if ( $has_content ) {
172       $new_content .= $task_content;
173     }
174   }
175
176   if ( ! $Template->Id ) {
177     my ( $val, $msg ) = $Template->Create(
178       Queue           => $Queue,
179       Name            => $TEMPLATE_NAME,
180       Description     => 'Subtask tickets',
181       Type            => 'Perl',
182       Content         => $new_content,
183     );
184     if (!$val) {
185       push @results, loc("Could not create template: [_1]", $msg);
186     } else {
187       push @results, loc("Template created");
188     }
189   } elsif ( $Template->Content ne $new_content ) { # template needs updating
190     my ( $val, $msg ) = $Template->SetContent($new_content);
191     if (!$val) {
192       push @results, loc("Could not update template: [_1]", $msg);
193     } else {
194       push @results, loc("Template updated");
195     }
196   }
197
198   # Set up ScripCondition
199   if ( !$cfname ) {
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',
210     );
211     if (!$val) {
212       push @results, loc("Could not create custom field condition: [_1]", $msg);
213     } else {
214       push @results, loc("Custom field condition created");
215     }
216   } elsif ( $ScripCondition->Argument ne "$cfname=$cfvalue" ) {
217     my ( $val, $msg ) = $ScripCondition->SetArgument("$cfname=$cfvalue");
218     if (!$val) {
219       push @results, loc("Could not set custom field condition: [_1]", $msg);
220     } else {
221       push @results, loc("Custom field condition set");
222     }
223   }
224
225   # Set up Scrip
226   if ( $Template->Id and ! $Scrip->Id ) {
227     my ($val, $msg) = $Scrip->Create(
228       Queue           => $Queue,
229       Template        => $Template->Id,
230       Description     => 'Create subtasks for ' . $QueueObj->Name,
231       ScripCondition  => $ScripCondition->Id,
232       ScripAction     => 'Create Tickets',
233     );
234     if (!$val) {
235       push @results, loc("Could not create scrip: [_1]", $msg);
236     } else {
237       push @results, loc("Scrip created");
238     }
239   } # else don't need to create the scrip
240
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.
243 }
244
245 # CHANGES HAVE BEEN SAVED.
246 # Now prepare to (re-)display the form.
247
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'},
253 );
254 # this will populate $Action with the 'create_tickets' hash
255 $Action->Parse(
256   Content         => $Template->Content,
257   _ActiveContent  => 0,
258 );
259 my @task_ids;
260 @task_ids = @{ $Action->{create_tickets} } if exists $Action->{create_tickets};
261
262 my $PossibleCustomFields = $QueueObj->TicketCustomFields;
263
264 </%init>
265 <%ARGS>
266 $Queue => undef         #queue id
267 </%ARGS>