-
Notifications
You must be signed in to change notification settings - Fork 70
/
FileWorkflowApplicable.php
89 lines (75 loc) · 3.09 KB
/
FileWorkflowApplicable.php
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
<?php
namespace Symbiote\AdvancedWorkflow\Extensions;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
/**
* WorkflowApplicable extension specifically for File objects, which don't have the same CMS
* UI structure so need to be handled a little differently. Additionally, it doesn't really
* work without custom code to handle the triggering of workflow, and in general is not
* ready for production use just yet.
*
* @author marcus@symbiote.com.au
* @license BSD License (http://silverstripe.org/bsd-license/)
* @package advancedworkflow
*/
class FileWorkflowApplicable extends WorkflowApplicable
{
public function updateSummaryFields(&$fields)
{
$fields['ID'] = 'ID';
$fields['ParentID'] = 'ParentID';
}
public function updateCMSFields(FieldList $fields)
{
if (!$this->owner->ID) {
return $fields;
}
parent::updateCMSFields($fields);
// add the workflow fields directly. It's a requirement of workflow on file objects
// that CMS admins mark the workflow step as being editable for files to be administerable
$active = $this->getWorkflowService()->getWorkflowFor($this->owner);
if ($active) {
$current = $active->CurrentAction();
$wfFields = $active->getWorkflowFields();
// loading data in a somewhat hack way
$form = new Form($this, 'DummyForm', $wfFields, new FieldList());
$form->loadDataFrom($current);
$fields->findOrMakeTab(
'Root.WorkflowActions',
_t('Workflow.WorkflowActionsTabTitle', 'Workflow Actions')
);
$fields->addFieldsToTab('Root.WorkflowActions', $wfFields);
}
}
public function onAfterWrite()
{
parent::onAfterWrite();
$workflow = $this->getWorkflowService()->getWorkflowFor($this->owner);
$rawData = $this->owner->toMap();
if ($workflow && $this->owner->TransitionID) {
// we want to transition, so do so if that's a valid transition to take.
$action = $workflow->CurrentAction();
if (!$this->canEditWorkflow()) {
return;
}
$allowedFields = $workflow->getWorkflowFields()->saveableFields();
unset($allowedFields['TransitionID']);
$allowed = array_keys($allowedFields ?? []);
foreach ($allowed as $field) {
if (isset($rawData[$field])) {
$action->$field = $rawData[$field];
}
}
$action->write();
if (isset($rawData['TransitionID']) && $rawData['TransitionID']) {
// unset the transition ID so this doesn't get re-executed
$this->owner->TransitionID = null;
$this->getWorkflowService()->executeTransition($this->owner, $rawData['TransitionID']);
} else {
// otherwise, just try to execute the current workflow to see if it
// can now proceed based on user input
$workflow->execute();
}
}
}
}