1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Symbiote\AdvancedWorkflow\Admin; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Admin\ModelAdmin; |
6
|
|
|
use SilverStripe\CMS\Controllers\CMSPageEditController; |
7
|
|
|
use SilverStripe\Control\HTTPRequest; |
8
|
|
|
use SilverStripe\Control\HTTPResponse; |
9
|
|
|
use SilverStripe\Forms\FieldList; |
10
|
|
|
use SilverStripe\Forms\FormAction; |
11
|
|
|
use SilverStripe\Forms\GridField\GridField; |
12
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_Base; |
13
|
|
|
use SilverStripe\Forms\GridField\GridFieldDataColumns; |
14
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm; |
15
|
|
|
use SilverStripe\Forms\GridField\GridFieldEditButton; |
16
|
|
|
use SilverStripe\Forms\GridField\GridFieldExportButton; |
17
|
|
|
use SilverStripe\Forms\GridField\GridFieldImportButton; |
18
|
|
|
use SilverStripe\Forms\GridField\GridFieldPaginator; |
19
|
|
|
use SilverStripe\ORM\ArrayList; |
20
|
|
|
use SilverStripe\ORM\DataObject; |
21
|
|
|
use SilverStripe\Security\Member; |
22
|
|
|
use SilverStripe\Security\Permission; |
23
|
|
|
use SilverStripe\Security\Security; |
24
|
|
|
use SilverStripe\View\Requirements; |
25
|
|
|
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowDefinition; |
26
|
|
|
use Symbiote\AdvancedWorkflow\Dev\WorkflowBulkLoader; |
27
|
|
|
use Symbiote\AdvancedWorkflow\Forms\GridField\GridFieldExportAction; |
28
|
|
|
use Symbiote\AdvancedWorkflow\Forms\GridField\GridFieldWorkflowRestrictedEditButton; |
29
|
|
|
use Symbiote\AdvancedWorkflow\Services\WorkflowService; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @package advancedworkflow |
33
|
|
|
* @todo UI/UX needs looking at for when current user has no pending and/or submitted items, (Current |
34
|
|
|
* implementation is bog-standard <p> text) |
35
|
|
|
*/ |
36
|
|
|
class AdvancedWorkflowAdmin extends ModelAdmin |
37
|
|
|
{ |
38
|
|
|
private static $menu_title = 'Workflows'; |
|
|
|
|
39
|
|
|
private static $menu_priority = -1; |
|
|
|
|
40
|
|
|
private static $url_segment = 'workflows'; |
|
|
|
|
41
|
|
|
private static $menu_icon_class = 'font-icon-flow-tree'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* |
45
|
|
|
* @var array Allowable actions on this controller. |
46
|
|
|
*/ |
47
|
|
|
private static $allowed_actions = array( |
|
|
|
|
48
|
|
|
'export', |
49
|
|
|
'ImportForm' |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
private static $url_handlers = array( |
|
|
|
|
53
|
|
|
'$ModelClass/export/$ID!' => 'export', |
54
|
|
|
'$ModelClass/$Action' => 'handleAction', |
55
|
|
|
'' => 'index' |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
private static $managed_models = WorkflowDefinition::class; |
|
|
|
|
59
|
|
|
|
60
|
|
|
private static $model_importers = array( |
|
|
|
|
61
|
|
|
'WorkflowDefinition' => WorkflowBulkLoader::class |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
private static $dependencies = array( |
|
|
|
|
65
|
|
|
'workflowService' => '%$' . WorkflowService::class, |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
private static $fileEditActions = 'getCMSActions'; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Defaults are set in {@link getEditForm()}. |
72
|
|
|
* |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
private static $fieldOverrides = array(); |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var WorkflowService |
79
|
|
|
*/ |
80
|
|
|
public $workflowService; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Initialise javascript translation files |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
protected function init() |
88
|
|
|
{ |
89
|
|
|
parent::init(); |
90
|
|
|
|
91
|
|
|
Requirements::add_i18n_javascript('symbiote/silverstripe-advancedworkflow:client/lang'); |
92
|
|
|
Requirements::javascript('symbiote/silverstripe-advancedworkflow:client/dist/js/advancedworkflow.js'); |
93
|
|
|
Requirements::css('symbiote/silverstripe-advancedworkflow:client/dist/styles/advancedworkflow.css'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/* |
97
|
|
|
* Shows up to x2 GridFields for Pending and Submitted items, dependent upon the current CMS user and |
98
|
|
|
* that user's permissions on the objects showing in each field. |
99
|
|
|
*/ |
100
|
|
|
public function getEditForm($id = null, $fields = null) |
101
|
|
|
{ |
102
|
|
|
$form = parent::getEditForm($id, $fields); |
103
|
|
|
|
104
|
|
|
$definitionGridFieldName = $this->sanitiseClassName(WorkflowDefinition::class); |
105
|
|
|
|
106
|
|
|
// Show items submitted into a workflow for current user to action |
107
|
|
|
$fieldName = 'PendingObjects'; |
108
|
|
|
$pending = $this->userObjects(Security::getCurrentUser(), $fieldName); |
|
|
|
|
109
|
|
|
|
110
|
|
|
if ($this->config()->fieldOverrides) { |
111
|
|
|
$displayFields = $this->config()->fieldOverrides; |
112
|
|
|
} else { |
113
|
|
|
$displayFields = array( |
114
|
|
|
'Title' => _t('AdvancedWorkflowAdmin.Title', 'Title'), |
115
|
|
|
'LastEdited' => _t('AdvancedWorkflowAdmin.LastEdited', 'Changed'), |
116
|
|
|
'WorkflowTitle' => _t('AdvancedWorkflowAdmin.WorkflowTitle', 'Effective workflow'), |
117
|
|
|
'WorkflowStatus' => _t('AdvancedWorkflowAdmin.WorkflowStatus', 'Current action'), |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// Pending/Submitted items GridField Config |
122
|
|
|
$config = new GridFieldConfig_Base(); |
123
|
|
|
$config->addComponent(new GridFieldEditButton()); |
124
|
|
|
$config->addComponent(new GridFieldDetailForm()); |
125
|
|
|
$config->getComponentByType(GridFieldPaginator::class)->setItemsPerPage(5); |
|
|
|
|
126
|
|
|
$columns = $config->getComponentByType(GridFieldDataColumns::class); |
127
|
|
|
$columns->setFieldFormatting($this->setFieldFormatting($config)); |
|
|
|
|
128
|
|
|
|
129
|
|
|
if ($pending->count()) { |
130
|
|
|
$formFieldTop = GridField::create( |
131
|
|
|
$fieldName, |
132
|
|
|
$this->isAdminUser(Security::getCurrentUser()) ? |
|
|
|
|
133
|
|
|
_t( |
134
|
|
|
'AdvancedWorkflowAdmin.GridFieldTitleAssignedAll', |
135
|
|
|
'All pending items' |
136
|
|
|
): |
137
|
|
|
_t( |
138
|
|
|
'AdvancedWorkflowAdmin.GridFieldTitleAssignedYour', |
139
|
|
|
'Your pending items' |
140
|
|
|
), |
141
|
|
|
$pending, |
142
|
|
|
$config |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
$dataColumns = $formFieldTop->getConfig()->getComponentByType(GridFieldDataColumns::class); |
146
|
|
|
$dataColumns->setDisplayFields($displayFields); |
147
|
|
|
|
148
|
|
|
$formFieldTop->setForm($form); |
149
|
|
|
$form->Fields()->insertBefore($definitionGridFieldName, $formFieldTop); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// Show items submitted into a workflow by current user |
153
|
|
|
$fieldName = 'SubmittedObjects'; |
154
|
|
|
$submitted = $this->userObjects(Security::getCurrentUser(), $fieldName); |
|
|
|
|
155
|
|
|
if ($submitted->count()) { |
156
|
|
|
$formFieldBottom = GridField::create( |
157
|
|
|
$fieldName, |
158
|
|
|
$this->isAdminUser(Security::getCurrentUser()) ? |
|
|
|
|
159
|
|
|
_t( |
160
|
|
|
'AdvancedWorkflowAdmin.GridFieldTitleSubmittedAll', |
161
|
|
|
'All submitted items' |
162
|
|
|
): |
163
|
|
|
_t( |
164
|
|
|
'AdvancedWorkflowAdmin.GridFieldTitleSubmittedYour', |
165
|
|
|
'Your submitted items' |
166
|
|
|
), |
167
|
|
|
$submitted, |
168
|
|
|
$config |
169
|
|
|
); |
170
|
|
|
|
171
|
|
|
$dataColumns = $formFieldBottom->getConfig()->getComponentByType(GridFieldDataColumns::class); |
172
|
|
|
$dataColumns->setDisplayFields($displayFields); |
173
|
|
|
|
174
|
|
|
$formFieldBottom->setForm($form); |
175
|
|
|
$formFieldBottom->getConfig()->removeComponentsByType(GridFieldEditButton::class); |
176
|
|
|
$formFieldBottom->getConfig()->addComponent(new GridFieldWorkflowRestrictedEditButton()); |
177
|
|
|
$form->Fields()->insertBefore($definitionGridFieldName, $formFieldBottom); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$grid = $form->Fields()->fieldByName($definitionGridFieldName); |
181
|
|
|
if ($grid) { |
182
|
|
|
$grid->getConfig()->getComponentByType(GridFieldDetailForm::class) |
|
|
|
|
183
|
|
|
->setItemEditFormCallback(function ($form) { |
184
|
|
|
$record = $form->getRecord(); |
185
|
|
|
if ($record) { |
186
|
|
|
$record->updateAdminActions($form->Actions()); |
187
|
|
|
} |
188
|
|
|
}); |
189
|
|
|
|
190
|
|
|
$grid->getConfig()->getComponentByType(GridFieldDetailForm::class) |
|
|
|
|
191
|
|
|
->setItemRequestClass(WorkflowDefinitionItemRequestClass::class); |
192
|
|
|
$grid->getConfig()->addComponent(new GridFieldExportAction()); |
|
|
|
|
193
|
|
|
$grid->getConfig()->removeComponentsByType(GridFieldExportButton::class); |
|
|
|
|
194
|
|
|
$grid->getConfig()->removeComponentsByType(GridFieldImportButton::class); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $form; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/* |
201
|
|
|
* @param Member $user |
202
|
|
|
* @return boolean |
203
|
|
|
*/ |
204
|
|
|
public function isAdminUser(Member $user) |
205
|
|
|
{ |
206
|
|
|
if (Permission::checkMember($user, 'ADMIN')) { |
207
|
|
|
return true; |
208
|
|
|
} |
209
|
|
|
return false; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/* |
213
|
|
|
* By default, we implement GridField_ColumnProvider to allow users to click through to the PagesAdmin. |
214
|
|
|
* We would also like a "Quick View", that allows users to quickly make a decision on a given workflow-bound |
215
|
|
|
* content-object |
216
|
|
|
*/ |
217
|
|
|
public function columns() |
218
|
|
|
{ |
219
|
|
|
$fields = array( |
220
|
|
|
'Title' => array( |
221
|
|
|
'link' => function ($value, $item) { |
222
|
|
|
$pageAdminLink = singleton(CMSPageEditController::class)->Link('show'); |
223
|
|
|
return sprintf('<a href="%s/%s">%s</a>', $pageAdminLink, $item->Link, $value); |
224
|
|
|
} |
225
|
|
|
), |
226
|
|
|
'WorkflowStatus' => array( |
227
|
|
|
'text' => function ($value, $item) { |
228
|
|
|
return $item->WorkflowCurrentAction; |
229
|
|
|
} |
230
|
|
|
) |
231
|
|
|
); |
232
|
|
|
return $fields; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/* |
236
|
|
|
* Discreet method used by both intro gridfields to format the target object's links and clickable text |
237
|
|
|
* |
238
|
|
|
* @param GridFieldConfig $config |
239
|
|
|
* @return array $fieldFormatting |
240
|
|
|
*/ |
241
|
|
|
public function setFieldFormatting(&$config) |
|
|
|
|
242
|
|
|
{ |
243
|
|
|
$fieldFormatting = array(); |
244
|
|
|
// Parse the column information |
245
|
|
|
foreach ($this->columns() as $source => $info) { |
246
|
|
|
if (isset($info['link']) && $info['link']) { |
247
|
|
|
$fieldFormatting[$source] = '<a href=\"$ObjectRecordLink\">$value</a>'; |
248
|
|
|
} |
249
|
|
|
if (isset($info['text']) && $info['text']) { |
250
|
|
|
$fieldFormatting[$source] = $info['text']; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
return $fieldFormatting; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Get WorkflowInstance Target objects to show for users in initial gridfield(s) |
258
|
|
|
* |
259
|
|
|
* @param Member $member |
|
|
|
|
260
|
|
|
* @param string $fieldName The name of the gridfield that determines which dataset to return |
261
|
|
|
* @return DataList |
262
|
|
|
* @todo Add the ability to see embargo/expiry dates in report-gridfields at-a-glance if QueuedJobs module installed |
263
|
|
|
*/ |
264
|
|
|
public function userObjects(Member $user, $fieldName) |
265
|
|
|
{ |
266
|
|
|
$list = new ArrayList(); |
267
|
|
|
$userWorkflowInstances = $this->getFieldDependentData($user, $fieldName); |
268
|
|
|
foreach ($userWorkflowInstances as $instance) { |
|
|
|
|
269
|
|
|
if (!$instance->TargetID || !$instance->DefinitionID) { |
270
|
|
|
continue; |
271
|
|
|
} |
272
|
|
|
// @todo can we use $this->getDefinitionFor() to fetch the "Parent" definition of $instance? Maybe |
273
|
|
|
// define $this->workflowParent() |
274
|
|
|
$effectiveWorkflow = DataObject::get_by_id(WorkflowDefinition::class, $instance->DefinitionID); |
275
|
|
|
$target = $instance->getTarget(); |
276
|
|
|
if (!is_object($effectiveWorkflow) || !$target) { |
277
|
|
|
continue; |
278
|
|
|
} |
279
|
|
|
$instance->setField('WorkflowTitle', $effectiveWorkflow->getField('Title')); |
280
|
|
|
$instance->setField('WorkflowCurrentAction', $instance->getCurrentAction()); |
281
|
|
|
// Note the order of property-setting here, somehow $instance->Title is overwritten by the Target |
282
|
|
|
// Title property.. |
283
|
|
|
$instance->setField('Title', $target->getField('Title')); |
284
|
|
|
$instance->setField('LastEdited', $target->getField('LastEdited')); |
285
|
|
|
if (method_exists($target, 'CMSEditLink')) { |
286
|
|
|
$instance->setField('ObjectRecordLink', $target->CMSEditLink()); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
$list->push($instance); |
290
|
|
|
} |
291
|
|
|
return $list; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/* |
295
|
|
|
* Return content-object data depending on which gridfeld is calling for it |
296
|
|
|
* |
297
|
|
|
* @param Member $user |
298
|
|
|
* @param string $fieldName |
299
|
|
|
*/ |
300
|
|
|
public function getFieldDependentData(Member $user, $fieldName) |
301
|
|
|
{ |
302
|
|
|
if ($fieldName == 'PendingObjects') { |
303
|
|
|
return $this->getWorkflowService()->userPendingItems($user); |
304
|
|
|
} |
305
|
|
|
if ($fieldName == 'SubmittedObjects') { |
306
|
|
|
return $this->getWorkflowService()->userSubmittedItems($user); |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Spits out an exported version of the selected WorkflowDefinition for download. |
312
|
|
|
* |
313
|
|
|
* @param HTTPRequest $request |
314
|
|
|
* @return HTTPResponse |
315
|
|
|
*/ |
316
|
|
|
public function export(HTTPRequest $request) |
317
|
|
|
{ |
318
|
|
|
$url = explode('/', $request->getURL()); |
319
|
|
|
$definitionID = end($url); |
320
|
|
|
if ($definitionID && is_numeric($definitionID)) { |
321
|
|
|
$exporter = new WorkflowDefinitionExporter($definitionID); |
322
|
|
|
$exportFilename = WorkflowDefinitionExporter::config() |
323
|
|
|
->get('export_filename_prefix') . '-' . $definitionID . '.yml'; |
324
|
|
|
$exportBody = $exporter->export(); |
325
|
|
|
$fileData = array( |
326
|
|
|
'name' => $exportFilename, |
327
|
|
|
'mime' => 'text/x-yaml', |
328
|
|
|
'body' => $exportBody, |
329
|
|
|
'size' => $exporter->getExportSize($exportBody) |
|
|
|
|
330
|
|
|
); |
331
|
|
|
return $exporter->sendFile($fileData); |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Required so we can simply change the visible label of the "Import" button and lose some redundant form-fields. |
337
|
|
|
* |
338
|
|
|
* @return Form |
339
|
|
|
*/ |
340
|
|
|
public function ImportForm() |
341
|
|
|
{ |
342
|
|
|
$form = parent::ImportForm(); |
343
|
|
|
if (!$form) { |
344
|
|
|
return; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
$form->unsetAllActions(); |
348
|
|
|
$newActionList = new FieldList(array( |
349
|
|
|
new FormAction('import', _t('AdvancedWorkflowAdmin.IMPORT', 'Import workflow')) |
350
|
|
|
)); |
351
|
|
|
$form->Fields()->fieldByName('_CsvFile')->getValidator()->setAllowedExtensions(array('yml', 'yaml')); |
352
|
|
|
$form->Fields()->removeByName('EmptyBeforeImport'); |
353
|
|
|
$form->setActions($newActionList); |
354
|
|
|
|
355
|
|
|
return $form; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @param WorkflowService $workflowService |
360
|
|
|
* @return $this |
361
|
|
|
*/ |
362
|
|
|
public function setWorkflowService(WorkflowService $workflowService) |
363
|
|
|
{ |
364
|
|
|
$this->workflowService = $workflowService; |
365
|
|
|
return $this; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* @return WorkflowService |
370
|
|
|
*/ |
371
|
|
|
public function getWorkflowService() |
372
|
|
|
{ |
373
|
|
|
return $this->workflowService; |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|