|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WorkflowApplicable extension specifically for File objects, which don't have the same CMS |
|
4
|
|
|
* UI structure so need to be handled a little differently. Additionally, it doesn't really |
|
5
|
|
|
* work without custom code to handle the triggering of workflow, and in general is not |
|
6
|
|
|
* ready for production use just yet. |
|
7
|
|
|
* |
|
8
|
|
|
* @author [email protected] |
|
9
|
|
|
* @license BSD License (http://silverstripe.org/bsd-license/) |
|
10
|
|
|
* @package advancedworkflow |
|
11
|
|
|
*/ |
|
12
|
|
|
class FileWorkflowApplicable extends WorkflowApplicable { |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
public function updateSummaryFields(&$fields) { |
|
15
|
|
|
$fields['ID'] = 'ID'; |
|
16
|
|
|
$fields['ParentID'] = 'ParentID'; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function updateCMSFields(FieldList $fields) { |
|
20
|
|
|
if (!$this->owner->ID) { |
|
21
|
|
|
return $fields; |
|
22
|
|
|
} |
|
23
|
|
|
parent::updateCMSFields($fields); |
|
24
|
|
|
|
|
25
|
|
|
// add the workflow fields directly. It's a requirement of workflow on file objects |
|
26
|
|
|
// that CMS admins mark the workflow step as being editable for files to be administerable |
|
27
|
|
|
$active = $this->workflowService->getWorkflowFor($this->owner); |
|
28
|
|
|
if ($active) { |
|
29
|
|
|
$current = $active->CurrentAction(); |
|
30
|
|
|
$wfFields = $active->getWorkflowFields(); |
|
31
|
|
|
|
|
32
|
|
|
// loading data in a somewhat hack way |
|
33
|
|
|
$form = new Form($this, 'DummyForm', $wfFields, new FieldList()); |
|
|
|
|
|
|
34
|
|
|
$form->loadDataFrom($current); |
|
35
|
|
|
|
|
36
|
|
|
$fields->findOrMakeTab( |
|
37
|
|
|
'Root.WorkflowActions', |
|
38
|
|
|
_t('Workflow.WorkflowActionsTabTitle', 'Workflow Actions') |
|
39
|
|
|
); |
|
40
|
|
|
$fields->addFieldsToTab('Root.WorkflowActions', $wfFields); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function onAfterWrite() { |
|
45
|
|
|
parent::onAfterWrite(); |
|
46
|
|
|
|
|
47
|
|
|
$workflow = $this->workflowService->getWorkflowFor($this->owner); |
|
48
|
|
|
$rawData = $this->owner->toMap(); |
|
49
|
|
|
if ($workflow && $this->owner->TransitionID) { |
|
50
|
|
|
// we want to transition, so do so if that's a valid transition to take. |
|
51
|
|
|
$action = $workflow->CurrentAction(); |
|
52
|
|
|
if (!$this->canEditWorkflow()) { |
|
53
|
|
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$allowedFields = $workflow->getWorkflowFields()->saveableFields(); |
|
57
|
|
|
unset($allowedFields['TransitionID']); |
|
58
|
|
|
|
|
59
|
|
|
$allowed = array_keys($allowedFields); |
|
60
|
|
|
|
|
61
|
|
|
foreach ($allowed as $field) { |
|
62
|
|
|
if (isset($rawData[$field])) { |
|
63
|
|
|
$action->$field = $rawData[$field]; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$action->write(); |
|
68
|
|
|
|
|
69
|
|
|
if (isset($rawData['TransitionID']) && $rawData['TransitionID']) { |
|
70
|
|
|
// unset the transition ID so this doesn't get re-executed |
|
71
|
|
|
$this->owner->TransitionID = null; |
|
72
|
|
|
$this->workflowService->executeTransition($this->owner, $rawData['TransitionID']); |
|
73
|
|
|
} else { |
|
74
|
|
|
// otherwise, just try to execute the current workflow to see if it |
|
75
|
|
|
// can now proceed based on user input |
|
76
|
|
|
$workflow->execute(); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.