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