|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Provides a front end Form view of the defined Workflow Actions and Transitions |
|
4
|
|
|
* |
|
5
|
|
|
* @author [email protected] [email protected] |
|
6
|
|
|
* @license BSD License (http://silverstripe.org/bsd-license/) |
|
7
|
|
|
* @package advancedworkflow |
|
8
|
|
|
*/ |
|
9
|
|
|
abstract class FrontEndWorkflowController extends Controller { |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
protected $transitionID; |
|
12
|
|
|
protected $contextObj; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The title to be displayed on the page |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
public $Title; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @return string ClassName of object that Workflow is applied to |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract function getContextType(); |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @return object Context Object |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getContextObject() { |
|
30
|
|
|
if (!$this->contextObj) { |
|
31
|
|
|
if ($id = $this->getContextID()) { |
|
32
|
|
|
$cType = $this->getContextType(); |
|
33
|
|
|
$cObj = DataObject::get_by_id($cType, $id); |
|
34
|
|
|
if ($cObj) { |
|
35
|
|
|
$this->contextObj = $cObj->canView() ? $cObj : null; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
return $this->contextObj; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return int ID of Context Object |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function getContextID() { |
|
46
|
|
|
$id = $this->contextObj ? $this->contextObj->ID : null; |
|
47
|
|
|
if (!$id) { |
|
|
|
|
|
|
48
|
|
|
if ($this->request->param('ID')) { |
|
49
|
|
|
$id = (int) $this->request->param('ID'); |
|
50
|
|
|
} else if ($this->request->requestVar('ID')) { |
|
51
|
|
|
$id = (int) $this->request->requestVar('ID'); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
return $id; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Specifies the Workflow Definition to be used, |
|
59
|
|
|
* ie. retrieve from SiteConfig - or wherever it's defined |
|
60
|
|
|
* |
|
61
|
|
|
* @return WorkflowDefinition |
|
62
|
|
|
*/ |
|
63
|
|
|
abstract function getWorkflowDefinition(); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Handle the Form Action |
|
67
|
|
|
* - FrontEndWorkflowForm contains the logic for this |
|
68
|
|
|
* |
|
69
|
|
|
* @param SS_HTTPRequest $request |
|
70
|
|
|
* @todo - is this even required??? |
|
71
|
|
|
*/ |
|
72
|
|
|
public function handleAction($request, $action){ |
|
73
|
|
|
return parent::handleAction($request, $action); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Create the Form containing: |
|
78
|
|
|
* - fields from the Context Object |
|
79
|
|
|
* - required fields from the Context Object |
|
80
|
|
|
* - Actions from the connected WorkflowTransitions |
|
81
|
|
|
* |
|
82
|
|
|
* @return Form |
|
83
|
|
|
*/ |
|
84
|
|
|
public function Form(){ |
|
85
|
|
|
|
|
86
|
|
|
$svc = singleton('WorkflowService'); |
|
87
|
|
|
$active = $svc->getWorkflowFor($this->getContextObject()); |
|
88
|
|
|
|
|
89
|
|
|
if (!$active){ |
|
90
|
|
|
return; |
|
91
|
|
|
//throw new Exception('Workflow not found, or not specified for Context Object'); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$wfFields = $active->getFrontEndWorkflowFields(); |
|
95
|
|
|
$wfActions = $active->getFrontEndWorkflowActions(); |
|
96
|
|
|
$wfValidator = $active->getFrontEndRequiredFields(); |
|
97
|
|
|
|
|
98
|
|
|
//Get DataObject for Form (falls back to ContextObject if not defined in WorkflowAction) |
|
99
|
|
|
$wfDataObject = $active->getFrontEndDataObject(); |
|
100
|
|
|
|
|
101
|
|
|
// set any requirements spcific to this contextobject |
|
102
|
|
|
$active->setFrontendFormRequirements(); |
|
103
|
|
|
|
|
104
|
|
|
// hooks for decorators |
|
105
|
|
|
$this->extend('updateFrontEndWorkflowFields', $wfFields); |
|
106
|
|
|
$this->extend('updateFrontEndWorkflowActions', $wfActions); |
|
107
|
|
|
$this->extend('updateFrontEndRequiredFields', $wfValidator); |
|
108
|
|
|
$this->extend('updateFrontendFormRequirements'); |
|
109
|
|
|
|
|
110
|
|
|
$form = new FrontendWorkflowForm($this, 'Form/' . $this->getContextID(), $wfFields, $wfActions, $wfValidator); |
|
111
|
|
|
|
|
112
|
|
|
$form->addExtraClass("fwf"); |
|
113
|
|
|
|
|
114
|
|
|
if($wfDataObject) { |
|
115
|
|
|
$form->loadDataFrom($wfDataObject); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $form; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return WorkflowTransition |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getCurrentTransition() { |
|
125
|
|
|
$trans = null; |
|
126
|
|
|
if ($this->transitionID) { |
|
127
|
|
|
$trans = DataObject::get_by_id('WorkflowTransition',$this->transitionID); |
|
128
|
|
|
} |
|
129
|
|
|
return $trans; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Save the Form Data to the defined Context Object |
|
134
|
|
|
* |
|
135
|
|
|
* @param array $data |
|
136
|
|
|
* @param Form $form |
|
137
|
|
|
* @param SS_HTTPRequest $request |
|
138
|
|
|
* @throws Exception |
|
139
|
|
|
*/ |
|
140
|
|
|
public function doFrontEndAction(array $data, Form $form, SS_HTTPRequest $request) { |
|
141
|
|
|
if (!$obj = $this->getContextObject()) { |
|
142
|
|
|
throw new Exception( |
|
143
|
|
|
_t( |
|
144
|
|
|
'FrontEndWorkflowController.FRONTENDACTION_CONTEXT_EXCEPTION', |
|
145
|
|
|
'Context Object Not Found' |
|
146
|
|
|
) |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
if(!$this->getCurrentTransition()->canExecute($this->contextObj->getWorkflowInstance())){ |
|
151
|
|
|
throw new Exception( |
|
152
|
|
|
_t( |
|
153
|
|
|
'FrontEndWorkflowController.FRONTENDACTION_TRANSITION_EXCEPTION', |
|
154
|
|
|
'You do not have permission to execute this action' |
|
155
|
|
|
) |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
//Only Save data when Transition is 'Active' |
|
160
|
|
|
if ($this->getCurrentTransition()->Type == 'Active') { |
|
|
|
|
|
|
161
|
|
|
//Hand off to WorkflowAction to perform Save |
|
162
|
|
|
$svc = singleton('WorkflowService'); |
|
163
|
|
|
$active = $svc->getWorkflowFor($obj); |
|
164
|
|
|
|
|
165
|
|
|
$active->doFrontEndAction($data, $form, $request); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
//run execute on WorkflowInstance instance |
|
169
|
|
|
$action = $this->contextObj->getWorkflowInstance()->currentAction(); |
|
170
|
|
|
$action->BaseAction()->execute($this->contextObj->getWorkflowInstance()); |
|
171
|
|
|
|
|
172
|
|
|
//get valid transitions |
|
173
|
|
|
$transitions = $action->getValidTransitions(); |
|
174
|
|
|
|
|
175
|
|
|
//tell instance to execute transition if it's in the permitted list |
|
176
|
|
|
if ($transitions->find('ID',$this->transitionID)) { |
|
177
|
|
|
$this->contextObj->getWorkflowInstance()->performTransition($this->getCurrentTransition()); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* checks to see if there is a title set on the current workflow action |
|
183
|
|
|
* uses that or falls back to controller->Title |
|
184
|
|
|
*/ |
|
185
|
|
|
public function Title(){ |
|
186
|
|
|
if (!$this->Title) { |
|
187
|
|
|
if($this->getContextObject()){ |
|
188
|
|
|
if($workflow = $this->contextObj->getWorkflowInstance()){ |
|
189
|
|
|
$this->Title = $workflow->currentAction()->BaseAction()->PageTitle ? $workflow->currentAction()->BaseAction()->PageTitle : $workflow->currentAction()->Title; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
return $this->Title; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
} |
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.