These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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'; |
||
0 ignored issues
–
show
|
|||
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('Root.RestrictToUsers', new CheckboxSetField('Users', _t('WorkflowDefinition.USERS', 'Restrict to Users'), $members)); |
||
151 | $fields->addFieldToTab('Root.RestrictToUsers', new TreeMultiselectField('Groups', _t('WorkflowDefinition.GROUPS', 'Restrict to Groups'), Group::class)); |
||
152 | |||
153 | $this->extend('updateCMSFields', $fields); |
||
154 | |||
155 | return $fields; |
||
156 | } |
||
157 | |||
158 | public function fieldLabels($includerelations = true) |
||
159 | { |
||
160 | $labels = parent::fieldLabels($includerelations); |
||
161 | $labels['Title'] = _t('WorkflowAction.TITLE', 'Title'); |
||
162 | $labels['ActionID'] = _t('WorkflowTransition.ACTION', 'Action'); |
||
163 | $labels['NextActionID'] = _t('WorkflowTransition.NEXT_ACTION', 'Next Action'); |
||
164 | |||
165 | return $labels; |
||
166 | } |
||
167 | |||
168 | public function getValidator() |
||
169 | { |
||
170 | $required = new AWRequiredFields('Title', 'ActionID', 'NextActionID'); |
||
171 | $required->setCaller($this); |
||
172 | return $required; |
||
173 | } |
||
174 | |||
175 | public function numChildren() |
||
176 | { |
||
177 | return 0; |
||
178 | } |
||
179 | |||
180 | public function summaryFields() |
||
181 | { |
||
182 | return array( |
||
183 | 'Title' => $this->fieldLabel('Title') |
||
184 | ); |
||
185 | } |
||
186 | |||
187 | |||
188 | /** |
||
189 | * Check if the current user can execute this transition |
||
190 | * |
||
191 | * @return bool |
||
192 | **/ |
||
193 | public function canExecute(WorkflowInstance $workflow) |
||
194 | { |
||
195 | $return = true; |
||
196 | $members = $this->getAssignedMembers(); |
||
197 | |||
198 | // If not admin, check if the member is in the list of assigned members |
||
199 | if (!Permission::check('ADMIN') && $members->exists()) { |
||
200 | if (!$members->find('ID', Security::getCurrentUser()->ID)) { |
||
201 | $return = false; |
||
202 | } |
||
203 | } |
||
204 | |||
205 | if ($return) { |
||
206 | $extended = $this->extend('extendCanExecute', $workflow); |
||
207 | if ($extended) { |
||
208 | $return = min($extended); |
||
209 | } |
||
210 | } |
||
211 | |||
212 | return $return !== false; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Allows users who have permission to create a WorkflowDefinition, to create actions on it too. |
||
217 | * |
||
218 | * @param Member $member |
||
219 | * @param array $context |
||
220 | * @return bool |
||
221 | */ |
||
222 | public function canCreate($member = null, $context = array()) |
||
223 | { |
||
224 | return $this->Action()->WorkflowDef()->canCreate($member, $context); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @param Member $member |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function canEdit($member = null) |
||
232 | { |
||
233 | return $this->canCreate($member); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param Member $member |
||
238 | * @return bool |
||
239 | */ |
||
240 | public function canDelete($member = null) |
||
241 | { |
||
242 | return $this->canCreate($member); |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Returns a set of all Members that are assigned to this transition, either directly or via a group. |
||
247 | * |
||
248 | * @return ArrayList |
||
249 | */ |
||
250 | View Code Duplication | public function getAssignedMembers() |
|
251 | { |
||
252 | $members = ArrayList::create($this->Users()->toArray()); |
||
253 | $groups = $this->Groups(); |
||
254 | |||
255 | foreach ($groups as $group) { |
||
256 | $members->merge($group->Members()); |
||
257 | } |
||
258 | |||
259 | $members->removeDuplicates(); |
||
260 | return $members; |
||
261 | } |
||
262 | |||
263 | /* |
||
264 | * A simple field same-value checker. |
||
265 | * |
||
266 | * @param array $data |
||
267 | * @return array |
||
268 | * @see {@link AWRequiredFields} |
||
269 | */ |
||
270 | public function extendedRequiredFieldsNotSame($data = null) |
||
271 | { |
||
272 | $check = array('ActionID','NextActionID'); |
||
273 | foreach ($check as $fieldName) { |
||
274 | if (!isset($data[$fieldName])) { |
||
275 | return self::$extendedMethodReturn; |
||
276 | } |
||
277 | } |
||
278 | // Have we found some identical values? |
||
279 | if ($data[$check[0]] == $data[$check[1]]) { |
||
280 | self::$extendedMethodReturn['fieldName'] = $check[0]; // Used to display to the user, so the first of the array is fine |
||
281 | self::$extendedMethodReturn['fieldValid'] = false; |
||
282 | self::$extendedMethodReturn['fieldMsg'] = _t( |
||
283 | 'WorkflowTransition.TRANSITIONLOOP', |
||
284 | 'A transition cannot lead back to its parent action.' |
||
285 | ); |
||
286 | } |
||
287 | return self::$extendedMethodReturn; |
||
288 | } |
||
289 | } |
||
290 |
This check marks private properties in classes that are never used. Those properties can be removed.