|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Symbiote\AdvancedWorkflow\DataObjects; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
6
|
|
|
use SilverStripe\Forms\Form; |
|
7
|
|
|
use SilverStripe\Forms\ReadonlyField; |
|
8
|
|
|
use SilverStripe\Forms\TextareaField; |
|
9
|
|
|
use SilverStripe\ORM\ArrayList; |
|
10
|
|
|
use SilverStripe\ORM\DataObject; |
|
11
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
|
12
|
|
|
use SilverStripe\Security\Member; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* A workflow action attached to a {@link WorkflowInstance} that has been run, |
|
16
|
|
|
* and is either currently running, or has finished. |
|
17
|
|
|
* |
|
18
|
|
|
* Each step of the workflow has one of these created for it - it refers back |
|
19
|
|
|
* to the original action definition, but is unique for each step of the |
|
20
|
|
|
* workflow to ensure re-entrant behaviour. |
|
21
|
|
|
* |
|
22
|
|
|
* @license BSD License (http://silverstripe.org/bsd-license/) |
|
23
|
|
|
* @package advancedworkflow |
|
24
|
|
|
*/ |
|
25
|
|
|
class WorkflowActionInstance extends DataObject |
|
26
|
|
|
{ |
|
27
|
|
|
private static $db = array( |
|
|
|
|
|
|
28
|
|
|
'Comment' => 'Text', |
|
29
|
|
|
'Finished' => 'Boolean' |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
private static $has_one = array( |
|
|
|
|
|
|
33
|
|
|
'Workflow' => WorkflowInstance::class, |
|
34
|
|
|
'BaseAction' => WorkflowAction::class, |
|
35
|
|
|
'Member' => Member::class, |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
private static $summary_fields = array( |
|
|
|
|
|
|
39
|
|
|
'BaseAction.Title', |
|
40
|
|
|
'Comment', |
|
41
|
|
|
'Created', |
|
42
|
|
|
'Member.Name', |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
private static $table_name = 'WorkflowActionInstance'; |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
public function fieldLabels($includerelations = true) |
|
48
|
|
|
{ |
|
49
|
|
|
$labels = parent::fieldLabels($includerelations); |
|
50
|
|
|
$labels['BaseAction.Title'] = _t('WorkflowActionInstance.Title', 'Title'); |
|
51
|
|
|
$labels['Comment'] = _t('WorkflowAction.CommentLabel', 'Comment'); |
|
52
|
|
|
$labels['Member.Name'] = _t('WorkflowAction.Author', 'Author'); |
|
53
|
|
|
$labels['Finished'] = _t('WorkflowAction.FinishedLabel', 'Finished'); |
|
54
|
|
|
$labels['BaseAction.Title'] = _t('WorkflowAction.TITLE', 'Title'); |
|
55
|
|
|
|
|
56
|
|
|
return $labels; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Gets fields for when this is part of an active workflow |
|
61
|
|
|
*/ |
|
62
|
|
|
public function updateWorkflowFields($fields) |
|
63
|
|
|
{ |
|
64
|
|
|
$fieldDiff = $this->Workflow()->getTargetDiff(); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
foreach ($fieldDiff as $field) { |
|
67
|
|
|
$display = ReadonlyField::create( |
|
68
|
|
|
'workflow-' . $field->Name, |
|
69
|
|
|
$field->Title, |
|
70
|
|
|
DBField::create_field('HTMLText', $field->Diff) |
|
71
|
|
|
) |
|
72
|
|
|
->addExtraClass('workflow-field-diff') |
|
73
|
|
|
->setTemplate(__CLASS__ . '/ReadonlyField'); |
|
74
|
|
|
$fields->push($display); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if ($this->BaseAction()->AllowCommenting) { |
|
|
|
|
|
|
78
|
|
|
$fields->push(new TextareaField('Comment', _t('WorkflowAction.COMMENT', 'Comment'))); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function updateFrontendWorkflowFields($fields) |
|
83
|
|
|
{ |
|
84
|
|
|
if ($this->BaseAction()->AllowCommenting) { |
|
|
|
|
|
|
85
|
|
|
$fields->push(new TextareaField( |
|
86
|
|
|
'WorkflowActionInstanceComment', |
|
87
|
|
|
_t('WorkflowAction.FRONTENDCOMMENT', 'Comment') |
|
88
|
|
|
)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$ba = $this->BaseAction(); |
|
|
|
|
|
|
92
|
|
|
$fields = $ba->updateFrontendWorkflowFields($fields, $this->Workflow()); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Gets Front-End DataObject |
|
97
|
|
|
* |
|
98
|
|
|
* Use the DataObject as defined in the WorkflowAction, otherwise fall back to the |
|
99
|
|
|
* context object. |
|
100
|
|
|
* |
|
101
|
|
|
* Useful for situations where front end workflow deals with multiple data objects |
|
102
|
|
|
* |
|
103
|
|
|
* @return DataObject |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getFrontEndDataObject() |
|
106
|
|
|
{ |
|
107
|
|
|
$obj = null; |
|
|
|
|
|
|
108
|
|
|
$ba = $this->BaseAction(); |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
if ($ba->hasMethod('getFrontEndDataObject')) { |
|
111
|
|
|
$obj = $ba->getFrontEndDataObject(); |
|
112
|
|
|
} else { |
|
113
|
|
|
$obj = $this->Workflow()->getTarget(); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $obj; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function updateFrontEndWorkflowActions($actions) |
|
120
|
|
|
{ |
|
121
|
|
|
$ba = $this->BaseAction(); |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
if ($ba->hasMethod('updateFrontEndWorkflowActions')) { |
|
124
|
|
|
$ba->updateFrontEndWorkflowActions($actions); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function getRequiredFields() |
|
129
|
|
|
{ |
|
130
|
|
|
$validator = null; |
|
131
|
|
|
$ba = $this->BaseAction(); |
|
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
if ($ba->hasMethod('getRequiredFields')) { |
|
134
|
|
|
$validator = $ba->getRequiredFields(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $validator; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function setFrontendFormRequirements() |
|
141
|
|
|
{ |
|
142
|
|
|
$ba = $this->BaseAction(); |
|
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
if ($ba->hasMethod('setFrontendFormRequirements')) { |
|
145
|
|
|
$ba->setFrontendFormRequirements(); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function doFrontEndAction(array $data, Form $form, HTTPRequest $request) |
|
150
|
|
|
{ |
|
151
|
|
|
//Save Front End Workflow notes, then hand over to Workflow Action |
|
152
|
|
|
if (isset($data["WorkflowActionInstanceComment"])) { |
|
153
|
|
|
$this->Comment = $data["WorkflowActionInstanceComment"]; |
|
|
|
|
|
|
154
|
|
|
$this->write(); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$ba = $this->BaseAction(); |
|
|
|
|
|
|
158
|
|
|
if ($ba->hasMethod('doFrontEndAction')) { |
|
159
|
|
|
$ba->doFrontEndAction($data, $form, $request); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Gets the title of this active action instance |
|
166
|
|
|
* |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getTitle() |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->BaseAction()->Title; |
|
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Returns all the valid transitions that lead out from this action. |
|
176
|
|
|
* |
|
177
|
|
|
* This is called if this action has finished, and the workflow engine wants |
|
178
|
|
|
* to run the next action. |
|
179
|
|
|
* |
|
180
|
|
|
* If this action returns only one valid transition it will be immediately |
|
181
|
|
|
* followed; otherwise the user will decide which transition to follow. |
|
182
|
|
|
* |
|
183
|
|
|
* @return ArrayList |
|
184
|
|
|
*/ |
|
185
|
|
|
public function getValidTransitions() |
|
186
|
|
|
{ |
|
187
|
|
|
$available = $this->BaseAction()->Transitions(); |
|
|
|
|
|
|
188
|
|
|
$valid = new ArrayList(); |
|
189
|
|
|
|
|
190
|
|
|
// iterate through the transitions and see if they're valid for the current state of the item being |
|
191
|
|
|
// workflowed |
|
192
|
|
|
if ($available) { |
|
193
|
|
|
foreach ($available as $transition) { |
|
194
|
|
|
if ($transition->isValid($this->Workflow())) { |
|
|
|
|
|
|
195
|
|
|
$valid->push($transition); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$this->extend('updateValidTransitions', $valid); |
|
201
|
|
|
|
|
202
|
|
|
return $valid; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Called when this instance is started within the workflow |
|
207
|
|
|
*/ |
|
208
|
|
|
public function actionStart(WorkflowTransition $transition) |
|
209
|
|
|
{ |
|
210
|
|
|
$this->extend('onActionStart', $transition); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Called when this action has been completed within the workflow |
|
215
|
|
|
*/ |
|
216
|
|
|
public function actionComplete(WorkflowTransition $transition) |
|
217
|
|
|
{ |
|
218
|
|
|
$this->MemberID = Member::currentUserID(); |
|
|
|
|
|
|
219
|
|
|
$this->write(); |
|
220
|
|
|
$this->extend('onActionComplete', $transition); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Can documents in the current workflow state be edited? |
|
226
|
|
|
* |
|
227
|
|
|
* @param DataObject $target |
|
228
|
|
|
* @return bool |
|
229
|
|
|
*/ |
|
230
|
|
|
public function canEditTarget(DataObject $target) |
|
231
|
|
|
{ |
|
232
|
|
|
$absolute = $this->BaseAction()->canEditTarget($target); |
|
|
|
|
|
|
233
|
|
|
if (!is_null($absolute)) { |
|
234
|
|
|
return $absolute; |
|
235
|
|
|
} |
|
236
|
|
|
switch ($this->BaseAction()->AllowEditing) { |
|
|
|
|
|
|
237
|
|
|
case 'By Assignees': |
|
238
|
|
|
return $this->Workflow()->canEdit(); |
|
|
|
|
|
|
239
|
|
|
case 'No': |
|
240
|
|
|
return false; |
|
241
|
|
|
case 'Content Settings': |
|
242
|
|
|
default: |
|
243
|
|
|
return null; |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Does this action restrict viewing of the document? |
|
249
|
|
|
* |
|
250
|
|
|
* @param DataObject $target |
|
251
|
|
|
* @return bool |
|
252
|
|
|
*/ |
|
253
|
|
|
public function canViewTarget(DataObject $target) |
|
254
|
|
|
{ |
|
255
|
|
|
return $this->BaseAction()->canViewTarget($target); |
|
|
|
|
|
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Does this action restrict the publishing of a document? |
|
260
|
|
|
* |
|
261
|
|
|
* @param DataObject $target |
|
262
|
|
|
* @return bool |
|
263
|
|
|
*/ |
|
264
|
|
|
public function canPublishTarget(DataObject $target) |
|
265
|
|
|
{ |
|
266
|
|
|
$absolute = $this->BaseAction()->canPublishTarget($target); |
|
|
|
|
|
|
267
|
|
|
if (!is_null($absolute)) { |
|
268
|
|
|
return $absolute; |
|
269
|
|
|
} |
|
270
|
|
|
return false; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
public function canView($member = null) |
|
274
|
|
|
{ |
|
275
|
|
|
return $this->Workflow()->canView($member); |
|
|
|
|
|
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
public function canEdit($member = null) |
|
279
|
|
|
{ |
|
280
|
|
|
return $this->Workflow()->canEdit($member); |
|
|
|
|
|
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
public function canDelete($member = null) |
|
284
|
|
|
{ |
|
285
|
|
|
return $this->Workflow()->canDelete($member); |
|
|
|
|
|
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
|