1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Symbiote\AdvancedWorkflow\Templates; |
4
|
|
|
|
5
|
|
|
use ArrayObject; |
6
|
|
|
use Exception; |
7
|
|
|
use SilverStripe\Core\Convert; |
8
|
|
|
use SilverStripe\ORM\DataObject; |
9
|
|
|
use SilverStripe\Security\Group; |
10
|
|
|
use SilverStripe\Security\Member; |
11
|
|
|
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowDefinition; |
12
|
|
|
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowTransition; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A class that wraps around an array description of a workflow |
16
|
|
|
* |
17
|
|
|
* array( |
18
|
|
|
* 'Step Name' = array( |
19
|
|
|
* 'type' => class name |
20
|
|
|
* 'transitions' => array( |
21
|
|
|
* 'transition name' => 'target step', |
22
|
|
|
* 'next name' => 'other step' |
23
|
|
|
* ) |
24
|
|
|
* ), |
25
|
|
|
* 'Next Step' = array( |
26
|
|
|
* |
27
|
|
|
* ), |
28
|
|
|
* ) |
29
|
|
|
* |
30
|
|
|
* This can be defined in config yml as follows |
31
|
|
|
* |
32
|
|
|
* SilverStripe\Core\Injector\Injector: |
33
|
|
|
* SimpleReviewApprove: |
34
|
|
|
* class: Symbiote\AdvancedWorkflow\Templates\WorkflowTemplate |
35
|
|
|
* constructor: |
36
|
|
|
* - Review and Approve |
37
|
|
|
* - Description of the workflow template |
38
|
|
|
* - 0.1 (version number) |
39
|
|
|
* properties: |
40
|
|
|
* structure: |
41
|
|
|
* Apply for approval: |
42
|
|
|
* type: AssignUsersToWorkflowAction |
43
|
|
|
* transitions: |
44
|
|
|
* notify: Notify users |
45
|
|
|
* Notify users: |
46
|
|
|
* type: NotifyUsersWorkflowAction |
47
|
|
|
* transitions: |
48
|
|
|
* approval: Approval |
49
|
|
|
* Approval: |
50
|
|
|
* type: SimpleApprovalWorkflowAction |
51
|
|
|
* transitions: |
52
|
|
|
* Approve: Publish |
53
|
|
|
* Reject: Reject |
54
|
|
|
* Publish: |
55
|
|
|
* type: PublishItemWorkflowAction |
56
|
|
|
* Reject: |
57
|
|
|
* type: CancelWorkflowAction |
58
|
|
|
* Symbiote\AdvancedWorkflow\Services\WorkflowService: |
59
|
|
|
* properties: |
60
|
|
|
* templates: |
61
|
|
|
* - %$SimpleReviewApprove |
62
|
|
|
* |
63
|
|
|
* When updating a template, there's a few things that can be done to assist |
64
|
|
|
* the system when changing things around |
65
|
|
|
* |
66
|
|
|
* 1. Update the 'version' number |
67
|
|
|
* |
68
|
|
|
* @author [email protected] |
69
|
|
|
* @license BSD License http://silverstripe.org/bsd-license/ |
70
|
|
|
*/ |
71
|
|
|
class WorkflowTemplate |
72
|
|
|
{ |
73
|
|
|
protected $name; |
74
|
|
|
protected $description; |
75
|
|
|
protected $version; |
76
|
|
|
protected $remindDays; |
77
|
|
|
protected $sort; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* An array representation of the structure of this workflow template |
81
|
|
|
* |
82
|
|
|
* @var array |
83
|
|
|
*/ |
84
|
|
|
protected $structure; |
85
|
|
|
|
86
|
|
|
public function __construct($name, $description = '', $version = '0.0', $remindDays = 0, $sort = 0) |
87
|
|
|
{ |
88
|
|
|
$this->name = $name; |
89
|
|
|
$this->description = $description; |
90
|
|
|
$this->version = $version; |
91
|
|
|
$this->remindDays = $remindDays; |
92
|
|
|
$this->sort = $sort; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getName() |
96
|
|
|
{ |
97
|
|
|
return $this->name; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getVersion() |
101
|
|
|
{ |
102
|
|
|
return $this->version; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getDescription() |
106
|
|
|
{ |
107
|
|
|
return $this->description; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getRemindDays() |
111
|
|
|
{ |
112
|
|
|
return $this->remindDays; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getSort() |
116
|
|
|
{ |
117
|
|
|
return $this->sort; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set the structure for this template |
122
|
|
|
* |
123
|
|
|
* @param array $structure |
124
|
|
|
*/ |
125
|
|
|
public function setStructure($structure) |
126
|
|
|
{ |
127
|
|
|
$this->structure = $structure; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Creates the relevant data objects for this structure, returning an array |
132
|
|
|
* of actions in the order they've been created |
133
|
|
|
* |
134
|
|
|
* @param WorkflowDefinition $definitino |
|
|
|
|
135
|
|
|
* An optional workflow definition to bind the actions into |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function createRelations($definition = null) |
139
|
|
|
{ |
140
|
|
|
$actions = array(); |
141
|
|
|
$transitions = new ArrayObject(); |
142
|
|
|
$sort = 1; |
143
|
|
|
foreach ($this->structure as $relationName => $relationTemplate) { |
144
|
|
|
$isAction = isset($relationTemplate['type']); |
145
|
|
|
$isUsers = ($relationName == 'users'); |
146
|
|
|
$isGroups = ($relationName == 'groups'); |
147
|
|
|
|
148
|
|
|
// Process actions on WorkflowDefinition from the template |
149
|
|
|
if ($isAction) { |
150
|
|
|
$action = $this->createAction($relationName, $relationTemplate, $definition); |
151
|
|
|
// add a sort value in! |
152
|
|
|
$action->Sort = $sort++; |
153
|
|
|
$action->write(); |
154
|
|
|
|
155
|
|
|
$actions[$relationName] = $action; |
156
|
|
|
|
157
|
|
|
$newTransitions = $this->updateActionTransitions($relationTemplate, $action); |
158
|
|
|
foreach ($newTransitions as $t) { |
159
|
|
|
$transitions->append($t); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
// Process users on WorkflowDefinition from the template |
163
|
|
|
if ($isUsers) { |
164
|
|
|
$this->createUsers($relationTemplate, $definition); |
165
|
|
|
} |
166
|
|
|
// Process groups on WorkflowDefinition from the template |
167
|
|
|
if ($isGroups) { |
168
|
|
|
$this->createGroups($relationTemplate, $definition); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
View Code Duplication |
foreach ($transitions as $transition) { |
|
|
|
|
173
|
|
|
if (isset($actions[$transition->Target])) { |
174
|
|
|
$transition->NextActionID = $actions[$transition->Target]->ID; |
175
|
|
|
} |
176
|
|
|
$transition->write(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $actions; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Create a workflow action based on a template |
184
|
|
|
* |
185
|
|
|
* @param string $name |
186
|
|
|
* @param array $template |
|
|
|
|
187
|
|
|
* @param WorkflowDefinition $definition |
188
|
|
|
* @return WorkflowAction |
189
|
|
|
* @throws Exception |
190
|
|
|
*/ |
191
|
|
|
protected function createAction($name, $actionTemplate, WorkflowDefinition $definition = null) |
192
|
|
|
{ |
193
|
|
|
$type = $actionTemplate['type']; |
194
|
|
|
if (!$type || !class_exists($type)) { |
195
|
|
|
throw new Exception(_t( |
196
|
|
|
'WorkflowTemplate.INVALID_TEMPLATE_ACTION', |
197
|
|
|
'Invalid action class specified in template' |
198
|
|
|
)); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$action = $type::create(); |
202
|
|
|
|
203
|
|
|
$action->Title = $name; |
204
|
|
|
|
205
|
|
|
if (isset($actionTemplate['properties']) && is_array($actionTemplate['properties'])) { |
206
|
|
|
foreach ($actionTemplate['properties'] as $prop => $val) { |
207
|
|
|
$action->$prop = $val; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
// Deal with User + Group many_many relations on an action |
212
|
|
|
$this->addManyManyToObject($action, $actionTemplate); |
213
|
|
|
|
214
|
|
|
if ($definition) { |
215
|
|
|
$action->WorkflowDefID = $definition->ID; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$action->write(); |
219
|
|
|
|
220
|
|
|
return $action; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Create a WorkflowDefinition->Users relation based on template data. But only if the related groups from the |
225
|
|
|
* export, can be foud in the target environment's DB. |
226
|
|
|
* |
227
|
|
|
* Note: The template gives us a Member Email to work with rather than an ID as it's arguably |
228
|
|
|
* more likely that Member Emails will be the same between environments than their IDs. |
229
|
|
|
* |
230
|
|
|
* @param array $users |
231
|
|
|
* @param WorkflowDefinition $definition |
232
|
|
|
* @param boolean $clear |
233
|
|
|
* @return void |
234
|
|
|
*/ |
235
|
|
|
protected function createUsers($users, WorkflowDefinition $definition, $clear = false) |
236
|
|
|
{ |
237
|
|
|
// Create the necessary relation in WorkflowDefinition_Users |
238
|
|
|
$source = array('users' => $users); |
239
|
|
|
$this->addManyManyToObject($definition, $source, $clear); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Create a WorkflowDefinition->Groups relation based on template data, But only if the related groups from the |
244
|
|
|
* export, can be foud in the target environment's DB. |
245
|
|
|
* |
246
|
|
|
* Note: The template gives us a Group Title to work with rther than an ID as it's arguably |
247
|
|
|
* more likely that Group titles will be the same between environments than their IDs. |
248
|
|
|
* |
249
|
|
|
* @param array $groups |
250
|
|
|
* @param WorkflowDefinition $definition |
251
|
|
|
* @param boolean $clear |
252
|
|
|
* @return void |
253
|
|
|
*/ |
254
|
|
|
protected function createGroups($groups, WorkflowDefinition $definition, $clear = false) |
255
|
|
|
{ |
256
|
|
|
// Create the necessary relation in WorkflowDefinition_Groups |
257
|
|
|
$source = array('groups' => $groups); |
258
|
|
|
$this->addManyManyToObject($definition, $source, $clear); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Update the transitions for a given action |
263
|
|
|
* |
264
|
|
|
* @param array $actionTemplate |
265
|
|
|
* @param WorkflowAction $action |
266
|
|
|
* |
267
|
|
|
* @return array |
268
|
|
|
*/ |
269
|
|
|
protected function updateActionTransitions($actionTemplate, $action) |
270
|
|
|
{ |
271
|
|
|
$transitions = array(); |
272
|
|
|
if (isset($actionTemplate['transitions']) && is_array($actionTemplate['transitions'])) { |
273
|
|
|
$existing = $action->Transitions(); |
274
|
|
|
$transitionMap = array(); |
275
|
|
|
foreach ($existing as $transition) { |
276
|
|
|
$transitionMap[$transition->Title] = $transition; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
foreach ($actionTemplate['transitions'] as $transitionName => $transitionTemplate) { |
280
|
|
|
$target = $transitionTemplate; |
281
|
|
|
if (is_array($transitionTemplate)) { |
282
|
|
|
$to = array_keys($transitionTemplate); |
283
|
|
|
$transitionName = $to[0]; |
284
|
|
|
$target = $transitionTemplate[$transitionName]; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
if (isset($transitionMap[$transitionName])) { |
288
|
|
|
$transition = $transitionMap[$transitionName]; |
289
|
|
|
} else { |
290
|
|
|
$transition = WorkflowTransition::create(); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
// Add Member and Group relations to this Transition |
294
|
|
|
$this->addManyManyToObject($transition, $transitionTemplate); |
295
|
|
|
|
296
|
|
|
$transition->Title = $transitionName; |
297
|
|
|
$transition->ActionID = $action->ID; |
298
|
|
|
// we don't have the NextAction yet other than the target name, so we store that against |
299
|
|
|
// the transition and do a second pass later on to match things up |
300
|
|
|
$transition->Target = $target; |
301
|
|
|
$transitions[] = $transition; |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
return $transitions; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Update a workflow definition |
310
|
|
|
* |
311
|
|
|
* @param WorkflowDefinition $definition The definition to update |
312
|
|
|
*/ |
313
|
|
|
public function updateDefinition(WorkflowDefinition $definition) |
314
|
|
|
{ |
315
|
|
|
$existingActions = array(); |
316
|
|
|
|
317
|
|
|
$existing = $definition->Actions()->column('Title'); |
|
|
|
|
318
|
|
|
$structure = array_keys($this->structure); |
319
|
|
|
|
320
|
|
|
$removeNames = array_diff($existing, $structure); |
321
|
|
|
|
322
|
|
|
foreach ($definition->Actions() as $action) { |
|
|
|
|
323
|
|
|
if (in_array($action->Title, $removeNames)) { |
324
|
|
|
$action->delete(); |
325
|
|
|
continue; |
326
|
|
|
} |
327
|
|
|
$existingActions[$action->Title] = $action; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
$actions = array(); |
331
|
|
|
$transitions = new ArrayObject; |
332
|
|
|
$sort = 1; |
333
|
|
|
// now, go through the structure and create/realign things |
334
|
|
|
foreach ($this->structure as $relationName => $relationTemplate) { |
335
|
|
|
$isAction = isset($relationTemplate['type']); |
336
|
|
|
$isUsers = ($relationName == 'users'); |
337
|
|
|
$isGroups = ($relationName == 'groups'); |
338
|
|
|
|
339
|
|
|
if ($isAction) { |
340
|
|
|
$action = null; |
|
|
|
|
341
|
|
|
if (isset($existingActions[$relationName])) { |
342
|
|
|
$action = $existingActions[$relationName]; |
343
|
|
|
} else { |
344
|
|
|
$action = $this->createAction($relationName, $relationTemplate, $definition, $transitions); |
|
|
|
|
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
// add a sort value in! |
348
|
|
|
$action->Sort = $sort++; |
349
|
|
|
$action->write(); |
350
|
|
|
|
351
|
|
|
$actions[$relationName] = $action; |
352
|
|
|
|
353
|
|
|
$newTransitions = $this->updateActionTransitions($relationTemplate, $action); |
354
|
|
|
foreach ($newTransitions as $t) { |
355
|
|
|
$transitions->append($t); |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
// Process users on WorkflowDefinition from the template |
359
|
|
|
if ($isUsers) { |
360
|
|
|
$this->createUsers($relationTemplate, $definition, true); |
361
|
|
|
} |
362
|
|
|
// Process groups on WorkflowDefinition from the template |
363
|
|
|
if ($isGroups) { |
364
|
|
|
$this->createGroups($relationTemplate, $definition, true); |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
|
368
|
|
View Code Duplication |
foreach ($transitions as $transition) { |
|
|
|
|
369
|
|
|
if (isset($actions[$transition->Target])) { |
370
|
|
|
$transition->NextActionID = $actions[$transition->Target]->ID; |
371
|
|
|
} |
372
|
|
|
$transition->write(); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
// Set the version and do the write at the end so that we don't trigger an infinite loop!! |
376
|
|
|
$definition->Description = $this->getDescription(); |
|
|
|
|
377
|
|
|
$definition->TemplateVersion = $this->getVersion(); |
|
|
|
|
378
|
|
|
$definition->RemindDays = $this->getRemindDays(); |
|
|
|
|
379
|
|
|
$definition->Sort = $this->getSort(); |
|
|
|
|
380
|
|
|
$definition->write(); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Given an object, first check it has a ManyMany relation on it and add() Member and Group relations as required. |
385
|
|
|
* |
386
|
|
|
* @param Object $object (e.g. WorkflowDefinition, WorkflowAction, WorkflowTransition) |
387
|
|
|
* @param array $source Usually data taken from a YAML template |
388
|
|
|
* @param boolean $clear Lose/keep Group/Member relations on $object (useful for reloading/refreshing definition) |
389
|
|
|
* @return void |
390
|
|
|
*/ |
391
|
|
|
protected function addManyManyToObject($object, $source, $clear = false) |
392
|
|
|
{ |
393
|
|
|
// Check incoming |
394
|
|
|
if (!is_object($object) || !is_array($source)) { |
395
|
|
|
return; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
// Only some target class variants actually have Group/User relations |
399
|
|
|
$hasUsers = false; |
400
|
|
|
$hasGroups = false; |
401
|
|
|
if ($manyMany = $object->stat('many_many')) { |
402
|
|
|
if (in_array(Member::class, $manyMany)) { |
403
|
|
|
$hasUsers = true; |
404
|
|
|
$userRelationName = array_keys($manyMany); |
405
|
|
|
} |
406
|
|
|
if (in_array(Group::class, $manyMany)) { |
407
|
|
|
$hasGroups = true; |
408
|
|
|
$groupRelationName = array_keys($manyMany); |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
// Deal with User relations on target object |
413
|
|
View Code Duplication |
if ($hasUsers) { |
|
|
|
|
414
|
|
|
if ($clear) { |
415
|
|
|
$relName = $userRelationName[0]; |
|
|
|
|
416
|
|
|
$object->$relName()->removeAll(); |
417
|
|
|
} |
418
|
|
|
if (isset($source['users']) && is_array($source['users'])) { |
419
|
|
|
foreach ($source['users'] as $user) { |
420
|
|
|
$email = Convert::raw2sql($user['email']); |
421
|
|
|
if ($_user = DataObject::get_one(Member::class, "Email = '".$email."'")) { |
422
|
|
|
$object->Users()->add($_user); |
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
// Deal with Group relations on target object |
429
|
|
View Code Duplication |
if ($hasGroups) { |
|
|
|
|
430
|
|
|
if ($clear) { |
431
|
|
|
$relName = $groupRelationName[0]; |
|
|
|
|
432
|
|
|
$object->$relName()->removeAll(); |
433
|
|
|
} |
434
|
|
|
if (isset($source['groups']) && is_array($source['groups'])) { |
435
|
|
|
foreach ($source['groups'] as $group) { |
436
|
|
|
$title = Convert::raw2sql($group['title']); |
437
|
|
|
if ($_group = DataObject::get_one(Group::class, "Title = '".$title."'")) { |
438
|
|
|
$object->Groups()->add($_group); |
439
|
|
|
} |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
} |
443
|
|
|
} |
444
|
|
|
} |
445
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.