1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Symbiote\AdvancedWorkflow\DataObjects; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\CheckboxSetField; |
6
|
|
|
use SilverStripe\Forms\DropdownField; |
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
use SilverStripe\Forms\TabSet; |
9
|
|
|
use SilverStripe\Forms\TextField; |
10
|
|
|
use SilverStripe\Forms\TreeMultiselectField; |
11
|
|
|
use SilverStripe\ORM\ArrayList; |
12
|
|
|
use SilverStripe\ORM\DataObject; |
13
|
|
|
use SilverStripe\ORM\DB; |
14
|
|
|
use SilverStripe\Security\Group; |
15
|
|
|
use SilverStripe\Security\Member; |
16
|
|
|
use SilverStripe\Security\Permission; |
17
|
|
|
use SilverStripe\Security\Security; |
18
|
|
|
use Symbiote\AdvancedWorkflow\Forms\AWRequiredFields; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* A workflow transition. |
22
|
|
|
* |
23
|
|
|
* When used within the context of a workflow, the transition will have its |
24
|
|
|
* "isValid()" method call. This must return true or false to indicate whether |
25
|
|
|
* this transition is valid for the state of the workflow that it a part of. |
26
|
|
|
* |
27
|
|
|
* Therefore, any logic around whether the workflow can proceed should be |
28
|
|
|
* managed within this method. |
29
|
|
|
* |
30
|
|
|
* @method WorkflowAction Action() |
31
|
|
|
* @method WorkflowAction NextAction() |
32
|
|
|
* @author [email protected] |
33
|
|
|
* @license BSD License (http://silverstripe.org/bsd-license/) |
34
|
|
|
* @package advancedworkflow |
35
|
|
|
*/ |
36
|
|
|
class WorkflowTransition extends DataObject |
37
|
|
|
{ |
38
|
|
|
private static $db = array( |
|
|
|
|
39
|
|
|
'Title' => 'Varchar(128)', |
40
|
|
|
'Sort' => 'Int', |
41
|
|
|
'Type' => "Enum('Active, Passive', 'Active')" |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
private static $default_sort = 'Sort'; |
|
|
|
|
45
|
|
|
|
46
|
|
|
private static $has_one = array( |
|
|
|
|
47
|
|
|
'Action' => WorkflowAction::class, |
48
|
|
|
'NextAction' => WorkflowAction::class, |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
private static $many_many = array( |
|
|
|
|
52
|
|
|
'Users' => Member::class, |
53
|
|
|
'Groups' => Group::class, |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
private static $icon = 'symbiote/silverstripe-advancedworkflow:images/transition.png'; |
|
|
|
|
57
|
|
|
|
58
|
|
|
private static $table_name = 'WorkflowTransition'; |
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* |
62
|
|
|
* @var array $extendedMethodReturn A basic extended validation routine method return format |
63
|
|
|
*/ |
64
|
|
|
public static $extendedMethodReturn = array( |
65
|
|
|
'fieldName' => null, |
66
|
|
|
'fieldField' => null, |
67
|
|
|
'fieldMsg' => null, |
68
|
|
|
'fieldValid' => true, |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns true if it is valid for this transition to be followed given the |
73
|
|
|
* current state of a workflow. |
74
|
|
|
* |
75
|
|
|
* @param WorkflowInstance $workflow |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function isValid(WorkflowInstance $workflow) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Before saving, make sure we're not in an infinite loop |
85
|
|
|
*/ |
86
|
|
|
public function onBeforeWrite() |
87
|
|
|
{ |
88
|
|
|
if (!$this->Sort) { |
|
|
|
|
89
|
|
|
$this->Sort = DB::query('SELECT MAX("Sort") + 1 FROM "WorkflowTransition"')->value(); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
parent::onBeforeWrite(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/* CMS FUNCTIONS */ |
96
|
|
|
|
97
|
|
|
public function getCMSFields() |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$fields = new FieldList(new TabSet('Root')); |
100
|
|
|
$fields->addFieldToTab('Root.Main', new TextField('Title', $this->fieldLabel('Title'))); |
101
|
|
|
|
102
|
|
|
$filter = ''; |
103
|
|
|
|
104
|
|
|
$reqParent = isset($_REQUEST['ParentID']) ? (int) $_REQUEST['ParentID'] : 0; |
105
|
|
|
$attachTo = $this->ActionID ? $this->ActionID : $reqParent; |
|
|
|
|
106
|
|
|
|
107
|
|
|
if ($attachTo) { |
108
|
|
|
$action = DataObject::get_by_id(WorkflowAction::class, $attachTo); |
109
|
|
|
if ($action && $action->ID) { |
110
|
|
|
$filter = '"WorkflowDefID" = '.((int) $action->WorkflowDefID); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$actions = DataObject::get(WorkflowAction::class, $filter); |
115
|
|
|
$options = array(); |
116
|
|
|
if ($actions) { |
117
|
|
|
$options = $actions->map(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$defaultAction = $action ? $action->ID : ""; |
|
|
|
|
121
|
|
|
|
122
|
|
|
$typeOptions = array( |
123
|
|
|
'Active' => _t('WorkflowTransition.Active', 'Active'), |
124
|
|
|
'Passive' => _t('WorkflowTransition.Passive', 'Passive'), |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$fields->addFieldToTab('Root.Main', new DropdownField( |
128
|
|
|
'ActionID', |
129
|
|
|
$this->fieldLabel('ActionID'), |
130
|
|
|
$options, |
131
|
|
|
$defaultAction |
132
|
|
|
)); |
133
|
|
|
$fields->addFieldToTab('Root.Main', $nextActionDropdownField = new DropdownField( |
134
|
|
|
'NextActionID', |
135
|
|
|
$this->fieldLabel('NextActionID'), |
136
|
|
|
$options |
137
|
|
|
)); |
138
|
|
|
$nextActionDropdownField->setEmptyString(_t('WorkflowTransition.SELECTONE', '(Select one)')); |
139
|
|
|
$fields->addFieldToTab('Root.Main', new DropdownField( |
140
|
|
|
'Type', |
141
|
|
|
_t('WorkflowTransition.TYPE', 'Type'), |
142
|
|
|
$typeOptions |
143
|
|
|
)); |
144
|
|
|
|
145
|
|
|
$members = Member::get(); |
146
|
|
|
$fields->findOrMakeTab( |
147
|
|
|
'Root.RestrictToUsers', |
148
|
|
|
_t('WorkflowTransition.TabTitle', 'Restrict to users') |
149
|
|
|
); |
150
|
|
|
$fields->addFieldToTab( |
151
|
|
|
'Root.RestrictToUsers', |
152
|
|
|
new CheckboxSetField('Users', _t('WorkflowDefinition.USERS', 'Restrict to Users'), $members) |
153
|
|
|
); |
154
|
|
|
$fields->addFieldToTab( |
155
|
|
|
'Root.RestrictToUsers', |
156
|
|
|
new TreeMultiselectField('Groups', _t('WorkflowDefinition.GROUPS', 'Restrict to Groups'), Group::class) |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
$this->extend('updateCMSFields', $fields); |
160
|
|
|
|
161
|
|
|
return $fields; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function fieldLabels($includerelations = true) |
165
|
|
|
{ |
166
|
|
|
$labels = parent::fieldLabels($includerelations); |
167
|
|
|
$labels['Title'] = _t('WorkflowAction.TITLE', 'Title'); |
168
|
|
|
$labels['ActionID'] = _t('WorkflowTransition.ACTION', 'Action'); |
169
|
|
|
$labels['NextActionID'] = _t('WorkflowTransition.NEXT_ACTION', 'Next Action'); |
170
|
|
|
|
171
|
|
|
return $labels; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function getValidator() |
175
|
|
|
{ |
176
|
|
|
$required = new AWRequiredFields('Title', 'ActionID', 'NextActionID'); |
177
|
|
|
$required->setCaller($this); |
178
|
|
|
return $required; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function numChildren() |
182
|
|
|
{ |
183
|
|
|
return 0; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function summaryFields() |
187
|
|
|
{ |
188
|
|
|
return array( |
189
|
|
|
'Title' => $this->fieldLabel('Title') |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Check if the current user can execute this transition |
196
|
|
|
* |
197
|
|
|
* @return bool |
198
|
|
|
**/ |
199
|
|
|
public function canExecute(WorkflowInstance $workflow) |
200
|
|
|
{ |
201
|
|
|
$return = true; |
202
|
|
|
$members = $this->getAssignedMembers(); |
203
|
|
|
|
204
|
|
|
// If not admin, check if the member is in the list of assigned members |
205
|
|
|
if (!Permission::check('ADMIN') && $members->exists()) { |
206
|
|
|
if (!$members->find('ID', Security::getCurrentUser()->ID)) { |
207
|
|
|
$return = false; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if ($return) { |
212
|
|
|
$extended = $this->extend('extendCanExecute', $workflow); |
213
|
|
|
if ($extended) { |
|
|
|
|
214
|
|
|
$return = min($extended); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $return !== false; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Allows users who have permission to create a WorkflowDefinition, to create actions on it too. |
223
|
|
|
* |
224
|
|
|
* @param Member $member |
225
|
|
|
* @param array $context |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
|
|
public function canCreate($member = null, $context = array()) |
229
|
|
|
{ |
230
|
|
|
return $this->Action()->WorkflowDef()->canCreate($member, $context); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param Member $member |
235
|
|
|
* @return bool |
236
|
|
|
*/ |
237
|
|
|
public function canEdit($member = null) |
238
|
|
|
{ |
239
|
|
|
return $this->canCreate($member); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param Member $member |
244
|
|
|
* @return bool |
245
|
|
|
*/ |
246
|
|
|
public function canDelete($member = null) |
247
|
|
|
{ |
248
|
|
|
return $this->canCreate($member); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Returns a set of all Members that are assigned to this transition, either directly or via a group. |
253
|
|
|
* |
254
|
|
|
* @return ArrayList |
255
|
|
|
*/ |
256
|
|
View Code Duplication |
public function getAssignedMembers() |
|
|
|
|
257
|
|
|
{ |
258
|
|
|
$members = ArrayList::create($this->Users()->toArray()); |
|
|
|
|
259
|
|
|
$groups = $this->Groups(); |
|
|
|
|
260
|
|
|
|
261
|
|
|
foreach ($groups as $group) { |
262
|
|
|
$members->merge($group->Members()); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
$members->removeDuplicates(); |
266
|
|
|
return $members; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/* |
270
|
|
|
* A simple field same-value checker. |
271
|
|
|
* |
272
|
|
|
* @param array $data |
273
|
|
|
* @return array |
274
|
|
|
* @see {@link AWRequiredFields} |
275
|
|
|
*/ |
276
|
|
|
public function extendedRequiredFieldsNotSame($data = null) |
277
|
|
|
{ |
278
|
|
|
$check = array('ActionID','NextActionID'); |
279
|
|
|
foreach ($check as $fieldName) { |
280
|
|
|
if (!isset($data[$fieldName])) { |
281
|
|
|
return self::$extendedMethodReturn; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
// Have we found some identical values? |
285
|
|
|
if ($data[$check[0]] == $data[$check[1]]) { |
286
|
|
|
// Used to display to the user, so the first of the array is fine |
287
|
|
|
self::$extendedMethodReturn['fieldName'] = $check[0]; |
288
|
|
|
self::$extendedMethodReturn['fieldValid'] = false; |
289
|
|
|
self::$extendedMethodReturn['fieldMsg'] = _t( |
290
|
|
|
'WorkflowTransition.TRANSITIONLOOP', |
291
|
|
|
'A transition cannot lead back to its parent action.' |
292
|
|
|
); |
293
|
|
|
} |
294
|
|
|
return self::$extendedMethodReturn; |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|