-
Notifications
You must be signed in to change notification settings - Fork 70
/
AdvancedWorkflowAdmin.php
380 lines (340 loc) · 13.8 KB
/
AdvancedWorkflowAdmin.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
namespace Symbiote\AdvancedWorkflow\Admin;
use SilverStripe\Admin\ModelAdmin;
use SilverStripe\CMS\Controllers\CMSPageEditController;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_Base;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\Forms\GridField\GridFieldEditButton;
use SilverStripe\Forms\GridField\GridFieldExportButton;
use SilverStripe\Forms\GridField\GridFieldImportButton;
use SilverStripe\Forms\GridField\GridFieldPaginator;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Member;
use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
use SilverStripe\View\Requirements;
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowDefinition;
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowInstance;
use Symbiote\AdvancedWorkflow\Dev\WorkflowBulkLoader;
use Symbiote\AdvancedWorkflow\Forms\GridField\GridFieldExportAction;
use Symbiote\AdvancedWorkflow\Forms\GridField\GridFieldWorkflowRestrictedEditButton;
use Symbiote\AdvancedWorkflow\Services\WorkflowService;
/**
* @package advancedworkflow
* @todo UI/UX needs looking at for when current user has no pending and/or submitted items, (Current
* implementation is bog-standard <p> text)
*/
class AdvancedWorkflowAdmin extends ModelAdmin
{
private static $menu_title = 'Workflows';
private static $menu_priority = -1;
private static $url_segment = 'workflows';
private static $menu_icon_class = 'font-icon-flow-tree';
/**
*
* @var array Allowable actions on this controller.
*/
private static $allowed_actions = array(
'export',
'ImportForm'
);
private static $url_handlers = array(
'$ModelClass/export/$ID!' => 'export',
'$ModelClass/$Action' => 'handleAction',
'' => 'index'
);
private static $managed_models = WorkflowDefinition::class;
private static $model_importers = array(
'WorkflowDefinition' => WorkflowBulkLoader::class
);
private static $dependencies = array(
'workflowService' => '%$' . WorkflowService::class,
);
private static $fileEditActions = 'getCMSActions';
/**
* Defaults are set in {@link getEditForm()}.
*
* @var array
*/
private static $fieldOverrides = array();
/**
* @var WorkflowService
*/
public $workflowService;
/**
* Initialise javascript translation files
*
* @return void
*/
protected function init()
{
parent::init();
Requirements::add_i18n_javascript('symbiote/silverstripe-advancedworkflow:client/lang');
Requirements::javascript('symbiote/silverstripe-advancedworkflow:client/dist/js/advancedworkflow.js');
Requirements::css('symbiote/silverstripe-advancedworkflow:client/dist/styles/advancedworkflow.css');
}
/*
* Shows up to x2 GridFields for Pending and Submitted items, dependent upon the current CMS user and
* that user's permissions on the objects showing in each field.
*/
public function getEditForm($id = null, $fields = null)
{
$form = parent::getEditForm($id, $fields);
$definitionGridFieldName = $this->sanitiseClassName(WorkflowDefinition::class);
// Show items submitted into a workflow for current user to action
$fieldName = 'PendingObjects';
$pending = $this->userObjects(Security::getCurrentUser(), $fieldName);
if ($this->config()->fieldOverrides) {
$displayFields = $this->config()->fieldOverrides;
} else {
$displayFields = array(
'Title' => _t('AdvancedWorkflowAdmin.Title', 'Title'),
'LastEdited' => _t('AdvancedWorkflowAdmin.LastEdited', 'Changed'),
'WorkflowTitle' => _t('AdvancedWorkflowAdmin.WorkflowTitle', 'Effective workflow'),
'WorkflowStatus' => _t('AdvancedWorkflowAdmin.WorkflowStatus', 'Current action'),
);
}
// Pending/Submitted items GridField Config
$config = GridFieldConfig_Base::create();
$config->addComponent(GridFieldEditButton::create());
$config->addComponent(GridFieldDetailForm::create());
$config->getComponentByType(GridFieldPaginator::class)->setItemsPerPage(5);
$columns = $config->getComponentByType(GridFieldDataColumns::class);
$columns->setFieldFormatting($this->setFieldFormatting($config));
if ($pending->count()) {
$formFieldTop = GridField::create(
$fieldName,
$this->isAdminUser(Security::getCurrentUser()) ?
_t(
'AdvancedWorkflowAdmin.GridFieldTitleAssignedAll',
'All pending items'
):
_t(
'AdvancedWorkflowAdmin.GridFieldTitleAssignedYour',
'Your pending items'
),
$pending,
$config
);
$dataColumns = $formFieldTop->getConfig()->getComponentByType(GridFieldDataColumns::class);
$dataColumns->setDisplayFields($displayFields);
$formFieldTop->setForm($form);
$form->Fields()->insertBefore($definitionGridFieldName, $formFieldTop);
}
// Show items submitted into a workflow by current user
$fieldName = 'SubmittedObjects';
$submitted = $this->userObjects(Security::getCurrentUser(), $fieldName);
if ($submitted->count()) {
$formFieldBottom = GridField::create(
$fieldName,
$this->isAdminUser(Security::getCurrentUser()) ?
_t(
'AdvancedWorkflowAdmin.GridFieldTitleSubmittedAll',
'All submitted items'
):
_t(
'AdvancedWorkflowAdmin.GridFieldTitleSubmittedYour',
'Your submitted items'
),
$submitted,
$config
);
$dataColumns = $formFieldBottom->getConfig()->getComponentByType(GridFieldDataColumns::class);
$dataColumns->setDisplayFields($displayFields);
$formFieldBottom->setForm($form);
$formFieldBottom->getConfig()->removeComponentsByType(GridFieldEditButton::class);
$formFieldBottom->getConfig()->addComponent(GridFieldWorkflowRestrictedEditButton::create());
$form->Fields()->insertBefore($definitionGridFieldName, $formFieldBottom);
}
$grid = $form->Fields()->fieldByName($definitionGridFieldName);
if ($grid) {
$grid->getConfig()->getComponentByType(GridFieldDetailForm::class)
->setItemEditFormCallback(function ($form) {
$record = $form->getRecord();
if ($record) {
$record->updateAdminActions($form->Actions());
}
});
$grid->getConfig()->getComponentByType(GridFieldDetailForm::class)
->setItemRequestClass(WorkflowDefinitionItemRequestClass::class);
$grid->getConfig()->addComponent(GridFieldExportAction::create());
$grid->getConfig()->removeComponentsByType(GridFieldExportButton::class);
$grid->getConfig()->removeComponentsByType(GridFieldImportButton::class);
}
$this->extend('updateEditFormAfter', $form);
return $form;
}
/*
* @param Member $user
* @return boolean
*/
public function isAdminUser(Member $user)
{
if (Permission::checkMember($user, 'ADMIN')) {
return true;
}
return false;
}
/*
* By default, we implement GridField_ColumnProvider to allow users to click through to the PagesAdmin.
* We would also like a "Quick View", that allows users to quickly make a decision on a given workflow-bound
* content-object
*/
public function columns()
{
$fields = array(
'Title' => array(
'link' => function ($value, $item) {
$pageAdminLink = singleton(CMSPageEditController::class)->Link('show');
return sprintf('<a href="%s/%s">%s</a>', $pageAdminLink, $item->Link, $value);
}
),
'WorkflowStatus' => array(
'text' => function ($value, $item) {
return $item->WorkflowCurrentAction;
}
)
);
return $fields;
}
/*
* Discreet method used by both intro gridfields to format the target object's links and clickable text
*
* @param GridFieldConfig $config
* @return array $fieldFormatting
*/
public function setFieldFormatting(&$config)
{
$fieldFormatting = array();
// Parse the column information
foreach ($this->columns() as $source => $info) {
if (isset($info['link']) && $info['link']) {
$fieldFormatting[$source] = '<a href=\"$ObjectRecordLink\">$value</a>';
}
if (isset($info['text']) && $info['text']) {
$fieldFormatting[$source] = $info['text'];
}
}
return $fieldFormatting;
}
/**
* Get WorkflowInstance Target objects to show for users in initial gridfield(s)
*
* @param Member $member
* @param string $fieldName The name of the gridfield that determines which dataset to return
* @return ArrayList<WorkflowInstance>
* @todo Add the ability to see embargo/expiry dates in report-gridfields at-a-glance if QueuedJobs module installed
*/
public function userObjects(Member $user, $fieldName)
{
$list = new ArrayList();
$userWorkflowInstances = $this->getFieldDependentData($user, $fieldName);
foreach ($userWorkflowInstances as $instance) {
if (!$instance->TargetID || !$instance->DefinitionID) {
continue;
}
// @todo can we use $this->getDefinitionFor() to fetch the "Parent" definition of $instance? Maybe
// define $this->workflowParent()
$effectiveWorkflow = DataObject::get_by_id(WorkflowDefinition::class, $instance->DefinitionID);
$target = $instance->getTarget();
if (!is_object($effectiveWorkflow) || !$target) {
continue;
}
$instance->setField('WorkflowTitle', $effectiveWorkflow->getField('Title'));
$instance->setField('WorkflowCurrentAction', $instance->getCurrentAction());
// Note the order of property-setting here, somehow $instance->Title is overwritten by the Target
// Title property..
$instance->setField('Title', $target->getField('Title'));
$instance->setField('LastEdited', $target->getField('LastEdited'));
if (method_exists($target, 'CMSEditLink')) {
$instance->setField('ObjectRecordLink', $target->CMSEditLink());
}
$list->push($instance);
}
return $list;
}
/**
* Return content-object data depending on which gridfeld is calling for it
*
* @param Member $user
* @param string $fieldName
* @return DataList<WorkflowInstance>
*/
public function getFieldDependentData(Member $user, $fieldName)
{
if ($fieldName == 'PendingObjects') {
return $this->getWorkflowService()->userPendingItems($user);
}
if ($fieldName == 'SubmittedObjects') {
return $this->getWorkflowService()->userSubmittedItems($user);
}
}
/**
* Spits out an exported version of the selected WorkflowDefinition for download.
*
* @param HTTPRequest $request
* @return HTTPResponse
*/
public function export(HTTPRequest $request)
{
$url = explode('/', $request->getURL() ?? '');
$definitionID = end($url);
if ($definitionID && is_numeric($definitionID)) {
$exporter = new WorkflowDefinitionExporter($definitionID);
$exportFilename = WorkflowDefinitionExporter::config()
->get('export_filename_prefix') . '-' . $definitionID . '.yml';
$exportBody = $exporter->export();
$fileData = array(
'name' => $exportFilename,
'mime' => 'text/x-yaml',
'body' => $exportBody,
'size' => $exporter->getExportSize($exportBody)
);
return $exporter->sendFile($fileData);
}
}
/**
* Required so we can simply change the visible label of the "Import" button and lose some redundant form-fields.
*
* @return Form
*/
public function ImportForm()
{
$form = parent::ImportForm();
if (!$form) {
return;
}
$form->unsetAllActions();
$newActionList = new FieldList(array(
new FormAction('import', _t('AdvancedWorkflowAdmin.IMPORT', 'Import workflow'))
));
$form->Fields()->fieldByName('_CsvFile')->getValidator()->setAllowedExtensions(array('yml', 'yaml'));
$form->Fields()->removeByName('EmptyBeforeImport');
$form->setActions($newActionList);
return $form;
}
/**
* @param WorkflowService $workflowService
* @return $this
*/
public function setWorkflowService(WorkflowService $workflowService)
{
$this->workflowService = $workflowService;
return $this;
}
/**
* @return WorkflowService
*/
public function getWorkflowService()
{
return $this->workflowService;
}
}