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