/
WorkflowApplicable.php
355 lines (302 loc) · 10.1 KB
/
WorkflowApplicable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/**
* DataObjects that have the WorkflowApplicable extension can have a
* workflow definition applied to them. At some point, the workflow definition is then
* triggered.
*
* @author marcus@silverstripe.com.au
* @license BSD License (http://silverstripe.org/bsd-license/)
* @package advancedworkflow
*/
class WorkflowApplicable extends DataExtension {
private static $has_one = array(
'WorkflowDefinition' => 'WorkflowDefinition',
);
private static $many_many = array(
'AdditionalWorkflowDefinitions' => 'WorkflowDefinition'
);
private static $dependencies = array(
'workflowService' => '%$WorkflowService',
);
/**
*
* Used to flag to this extension if there's a WorkflowPublishTargetJob running.
* @var boolean
*/
public $isPublishJobRunning = false;
/**
*
* @param boolean $truth
*/
public function setIsPublishJobRunning($truth) {
$this->isPublishJobRunning = $truth;
}
/**
*
* @return boolean
*/
public function getIsPublishJobRunning() {
return $this->isPublishJobRunning;
}
/**
*
* @see {@link $this->isPublishJobRunning}
* @return boolean
*/
public function isPublishJobRunning() {
$propIsSet = $this->getIsPublishJobRunning() ? true : false;
return class_exists('AbstractQueuedJob') && $propIsSet;
}
/**
* @var WorkflowService
*/
public $workflowService;
/**
*
* A cache var for the current workflow instance
*
* @var WorkflowInstance
*/
protected $currentInstance;
public function updateSettingsFields(FieldList $fields) {
$this->updateFields($fields);
}
public function updateCMSFields(FieldList $fields) {
if(!$this->owner->hasMethod('getSettingsFields')) $this->updateFields($fields);
// Instantiate a hidden form field to pass the triggered workflow definition through, allowing a dynamic form action.
$fields->push(HiddenField::create(
'TriggeredWorkflowID'
));
}
public function updateFields(FieldList $fields) {
if (!$this->owner->ID) {
return $fields;
}
$tab = $fields->fieldByName('Root') ? $fields->findOrMakeTab('Root.Workflow') : $fields;
if(Permission::check('APPLY_WORKFLOW')) {
$definition = new DropdownField('WorkflowDefinitionID', _t('WorkflowApplicable.DEFINITION', 'Applied Workflow'));
$definitions = $this->workflowService->getDefinitions()->map()->toArray();
$definition->setSource($definitions);
$definition->setEmptyString(_t('WorkflowApplicable.INHERIT', 'Inherit from parent'));
$tab->push($definition);
// Allow an optional selection of additional workflow definitions.
if($this->owner->WorkflowDefinitionID) {
$fields->removeByName('AdditionalWorkflowDefinitions');
unset($definitions[$this->owner->WorkflowDefinitionID]);
$tab->push($additional = ListboxField::create(
'AdditionalWorkflowDefinitions',
_t('WorkflowApplicable.ADDITIONAL_WORKFLOW_DEFINITIONS', 'Additional Workflows')
));
$additional->setSource($definitions);
$additional->setMultiple(true);
}
}
// Display the effective workflow definition.
if($effective = $this->getWorkflowInstance()) {
$title = $effective->Definition()->Title;
$tab->push(ReadonlyField::create(
'EffectiveWorkflow',
_t('WorkflowApplicable.EFFECTIVE_WORKFLOW', 'Effective Workflow'),
$title
));
}
if($this->owner->ID) {
$config = new GridFieldConfig_Base();
$config->addComponent(new GridFieldEditButton());
$config->addComponent(new GridFieldDetailForm());
$insts = $this->owner->WorkflowInstances();
$log = new GridField('WorkflowLog', _t('WorkflowApplicable.WORKFLOWLOG', 'Workflow Log'), $insts, $config);
$tab->push($log);
}
}
public function updateCMSActions(FieldList $actions) {
$active = $this->workflowService->getWorkflowFor($this->owner);
$c = Controller::curr();
if ($c && $c->hasExtension('AdvancedWorkflowExtension')) {
if ($active) {
if ($this->canEditWorkflow()) {
$workflowOptions = new Tab(
'WorkflowOptions',
_t('SiteTree.WorkflowOptions', 'Workflow options', 'Expands a view for workflow specific buttons')
);
$menu = $actions->fieldByName('ActionMenus');
if (!$menu) {
// create the menu for adding to any arbitrary non-sitetree object
$menu = $this->createActionMenu();
$actions->push($menu);
}
$menu->push($workflowOptions);
$transitions = $active->CurrentAction()->getValidTransitions();
foreach ($transitions as $transition) {
if ($transition->canExecute($active)) {
$action = FormAction::create('updateworkflow-' . $transition->ID, $transition->Title)
->setAttribute('data-transitionid', $transition->ID)->setUseButtonTag(true);
$workflowOptions->push($action);
}
}
// $action = FormAction::create('updateworkflow', $active->CurrentAction() ? $active->CurrentAction()->Title : _t('WorkflowApplicable.UPDATE_WORKFLOW', 'Update Workflow'))
// ->setAttribute('data-icon', 'navigation');
// $actions->fieldByName('MajorActions') ? $actions->fieldByName('MajorActions')->push($action) : $actions->push($action);
}
} else {
// Instantiate the workflow definition initial actions.
$definitions = $this->workflowService->getDefinitionsFor($this->owner);
if($definitions) {
$menu = $actions->fieldByName('ActionMenus');
if(is_null($menu)) {
// Instantiate a new action menu for any data objects.
$menu = $this->createActionMenu();
$actions->push($menu);
}
$tab = Tab::create(
'AdditionalWorkflows'
);
$menu->insertBefore($tab, 'MoreOptions');
$addedFirst = false;
foreach($definitions as $definition) {
if($definition->getInitialAction()) {
$action = FormAction::create(
"startworkflow-{$definition->ID}",
$definition->InitialActionButtonText ? $definition->InitialActionButtonText : $definition->getInitialAction()->Title
)->addExtraClass('start-workflow')->setAttribute('data-workflow', $definition->ID)->setUseButtonTag(true);
// The first element is the main workflow definition, and will be displayed as a major action.
if(!$addedFirst) {
$addedFirst = true;
$action->setAttribute('data-icon', 'navigation');
$majorActions = $actions->fieldByName('MajorActions');
$majorActions ? $majorActions->push($action) : $actions->push($action);
} else {
$tab->push($action);
}
}
}
}
}
}
}
protected function createActionMenu() {
$rootTabSet = new TabSet('ActionMenus');
$rootTabSet->addExtraClass('ss-ui-action-tabset action-menus');
return $rootTabSet;
}
/**
* Included in CMS-generated email templates for a NotifyUsersWorkflowAction.
* Returns an absolute link to the CMS UI for a Page object
*
* @return string | null
*/
public function AbsoluteEditLink() {
$CMSEditLink = null;
if($this->owner instanceof CMSPreviewable) {
$CMSEditLink = $this->owner->CMSEditLink();
} else if ($this->owner->hasMethod('WorkflowLink')) {
$CMSEditLink = $this->owner->WorkflowLink();
}
if ($CMSEditLink === null) {
return null;
}
return Controller::join_links(Director::absoluteBaseURL(), $CMSEditLink);
}
/**
* Included in CMS-generated email templates for a NotifyUsersWorkflowAction.
* Allows users to select a link in an email for direct access to the transition-selection dropdown in the CMS UI.
*
* @return string
*/
public function LinkToPendingItems() {
$urlBase = Director::absoluteBaseURL();
$urlFrag = 'admin/workflows/WorkflowDefinition/EditForm/field';
$urlInst = $this->getWorkflowInstance();
return Controller::join_links($urlBase, $urlFrag, 'PendingObjects', 'item', $urlInst->ID, 'edit');
}
/**
* After a workflow item is written, we notify the
* workflow so that it can take action if needbe
*/
public function onAfterWrite() {
$instance = $this->getWorkflowInstance();
if ($instance && $instance->CurrentActionID) {
$action = $instance->CurrentAction()->BaseAction()->targetUpdated($instance);
}
}
public function WorkflowInstances() {
return WorkflowInstance::get()->filter(array(
'TargetClass' => $this->ownerBaseClass,
'TargetID' => $this->owner->ID
));
}
/**
* Gets the current instance of workflow
*
* @return WorkflowInstance
*/
public function getWorkflowInstance() {
if (!$this->currentInstance) {
$this->currentInstance = $this->workflowService->getWorkflowFor($this->owner);
}
return $this->currentInstance;
}
/**
* Gets the history of a workflow instance
*
* @return DataObjectSet
*/
public function getWorkflowHistory($limit = null) {
return $this->workflowService->getWorkflowHistoryFor($this->owner, $limit);
}
/**
* Check all recent WorkflowActionIntances and return the most recent one with a Comment
*
* @return WorkflowActionInstance
*/
public function RecentWorkflowComment($limit = 10){
if($actions = $this->getWorkflowHistory($limit)){
foreach ($actions as $action) {
if ($action->Comment != '') {
return $action;
}
}
}
}
/**
* Content can never be directly publishable if there's a workflow applied.
*
* If there's an active instance, then it 'might' be publishable
*/
public function canPublish() {
// Override any default behaviour, to allow queuedjobs to complete
if($this->isPublishJobRunning()) {
return true;
}
if ($active = $this->getWorkflowInstance()) {
return $active->canPublishTarget($this->owner);
}
// otherwise, see if there's any workflows applied. If there are, then we shouldn't be able
// to directly publish
if ($effective = $this->workflowService->getDefinitionFor($this->owner)) {
return false;
}
}
/**
* Can only edit content that's NOT in another person's content changeset
*/
public function canEdit($member) {
// Override any default behaviour, to allow queuedjobs to complete
if($this->isPublishJobRunning()) {
return true;
}
if ($active = $this->getWorkflowInstance()) {
return $active->canEditTarget($this->owner);
}
}
/**
* Can a user edit the current workflow attached to this item?
*/
public function canEditWorkflow() {
$active = $this->getWorkflowInstance();
if ($active) {
return $active->canEdit();
}
return false;
}
}