-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathWorkflowBehaviorTrait.php
More file actions
423 lines (345 loc) · 11.2 KB
/
WorkflowBehaviorTrait.php
File metadata and controls
423 lines (345 loc) · 11.2 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2020 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\MVC\Model;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Workflow\Workflow;
use Joomla\Database\DatabaseDriver;
use Joomla\Filesystem\Path;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Trait which supports state behavior
*
* @since 4.0.0
*/
trait WorkflowBehaviorTrait
{
/**
* The name of the component.
*
* @var string
* @since 4.0.0
*/
protected $extension = null;
/**
* The section of the component.
*
* @var string
* @since 4.0.0
*/
protected $section = '';
/**
* Is workflow for this component enabled?
*
* @var boolean
* @since 4.0.0
*/
protected $workflowEnabled = false;
/**
* The workflow object
*
* @var Workflow
* @since 4.0.0
*/
protected $workflow;
/**
* Set Up the workflow
*
* @param string $extension The option and section separated by.
*
* @return void
*
* @since 4.0.0
*/
public function setUpWorkflow($extension)
{
$parts = explode('.', $extension);
$this->extension = array_shift($parts);
if (\count($parts)) {
$this->section = array_shift($parts);
}
if (method_exists($this, 'getDatabase')) {
$db = $this->getDatabase();
} else {
@trigger_error('From 6.0 implementing the getDatabase method will be mandatory.', E_USER_DEPRECATED);
$db = Factory::getContainer()->get(DatabaseDriver::class);
}
$this->workflow = new Workflow($extension, Factory::getApplication(), $db);
$params = ComponentHelper::getParams($this->extension);
$this->workflowEnabled = $params->get('workflow_enabled');
$this->enableWorkflowBatch();
}
/**
* Add the workflow batch to the command list. Can be overwritten by the child class
*
* @return void
*
* @since 4.0.0
*/
protected function enableWorkflowBatch()
{
// Enable batch
if ($this->workflowEnabled && property_exists($this, 'batch_commands')) {
$this->batch_commands['workflowstage_id'] = 'batchWorkflowStage';
}
}
/**
* Method to allow derived classes to preprocess the form.
*
* @param Form $form A Form object.
* @param mixed $data The data expected for the form.
*
* @return void
*
* @since 4.0.0
* @see FormField
*/
public function workflowPreprocessForm(Form $form, $data)
{
$this->addTransitionField($form, $data);
if (!$this->workflowEnabled) {
return;
}
// Import the workflow plugin group to allow form manipulation.
$this->importWorkflowPlugins();
}
/**
* Let plugins access stage change events
*
* @return void
*
* @since 4.0.0
*/
public function workflowBeforeStageChange()
{
if (!$this->workflowEnabled) {
return;
}
$this->importWorkflowPlugins();
}
/**
* Preparation of workflow data/plugins
*
* @return void
*
* @since 4.0.0
*/
public function workflowBeforeSave()
{
if (!$this->workflowEnabled) {
return;
}
$this->importWorkflowPlugins();
}
/**
* Executing of relevant workflow methods
*
* @return void
*
* @since 4.0.0
*/
public function workflowAfterSave($data)
{
// Regardless if workflow is active or not, we have to set the default stage
// So we can work with the workflow, when the user activates it later
$id = $this->getState($this->getName() . '.id');
$isNew = $this->getState($this->getName() . '.new');
// We save the first stage
if ($isNew) {
// We have to add the paths, because it could be called outside of the extension context
$path = JPATH_BASE . '/components/' . $this->extension;
$path = Path::check($path);
Form::addFormPath($path . '/forms');
Form::addFormPath($path . '/models/forms');
Form::addFieldPath($path . '/models/fields');
Form::addFormPath($path . '/model/form');
Form::addFieldPath($path . '/model/field');
$form = $this->getForm();
$stage_id = $this->getStageForNewItem($form, $data);
$this->workflow->createAssociation($id, $stage_id);
}
if (!$this->workflowEnabled) {
return;
}
// Execute transition
if (!empty($data['transition'])) {
$this->executeTransition([$id], $data['transition']);
}
}
/**
* Batch change workflow stage or current.
*
* @param integer $value The workflow stage ID.
* @param array $pks An array of row IDs.
* @param array $contexts An array of item contexts.
*
* @return mixed An array of new IDs on success, boolean false on failure.
*
* @since 4.0.0
*/
public function batchWorkflowStage(int $value, array $pks, array $contexts)
{
$user = Factory::getApplication()->getIdentity();
$workflow = Factory::getApplication()->bootComponent('com_workflow');
if (!$user->authorise('core.admin', $this->option)) {
$this->setError(Text::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EXECUTE_TRANSITION'));
}
// Get workflow stage information
$stage = $workflow->getMVCFactory()->createTable('Stage', 'Administrator');
if (empty($value) || !$stage->load($value)) {
Factory::getApplication()->enqueueMessage(Text::sprintf('JGLOBAL_BATCH_WORKFLOW_STAGE_ROW_NOT_FOUND'), 'error');
return false;
}
if (empty($pks)) {
Factory::getApplication()->enqueueMessage(Text::sprintf('JGLOBAL_BATCH_WORKFLOW_STAGE_ROW_NOT_FOUND'), 'error');
return false;
}
// Update workflow associations
return $this->workflow->updateAssociations($pks, $value);
}
/**
* Batch change workflow stage or current.
*
* @param integer $oldId The ID of the item copied from
* @param integer $newId The ID of the new item
*
* @return null
*
* @since 4.0.0
*/
public function workflowCleanupBatchMove($oldId, $newId)
{
// Trigger workflow plugins only if enable (will be triggered from parent class)
if ($this->workflowEnabled) {
$this->importWorkflowPlugins();
}
// We always need an association, so create one
$table = $this->getTable();
$table->load($newId);
$catKey = $table->getColumnAlias('catid');
$stage_id = $this->workflow->getDefaultStageByCategory($table->$catKey);
if (empty($stage_id)) {
return;
}
$this->workflow->createAssociation((int) $newId, (int) $stage_id);
}
/**
* Runs transition for item.
*
* @param array $pks Id of items to execute the transition
* @param integer $transitionId Id of transition
*
* @return boolean
*
* @since 4.0.0
*/
public function executeTransition(array $pks, int $transitionId)
{
$result = $this->workflow->executeTransition($pks, $transitionId);
if (!$result) {
$app = Factory::getApplication();
$app->enqueueMessage(Text::_('COM_CONTENT_ERROR_UPDATE_STAGE', $app::MSG_WARNING));
return false;
}
return true;
}
/**
* Import the Workflow plugins.
*
* @param Form $form A Form object.
* @param mixed $data The data expected for the form.
*
* @return void
*/
protected function importWorkflowPlugins()
{
PluginHelper::importPlugin('workflow');
}
/**
* Adds a transition field to the form. Can be overwritten by the child class if not needed
*
* @param Form $form A Form object.
* @param mixed $data The data expected for the form.
*
* @return void
* @since 4.0.0
*/
protected function addTransitionField(Form $form, $data)
{
$extension = $this->extension . ($this->section ? '.' . $this->section : '');
$field = new \SimpleXMLElement('<field></field>');
$field->addAttribute('name', 'transition');
$field->addAttribute('type', $this->workflowEnabled ? 'transition' : 'hidden');
$field->addAttribute('label', 'COM_CONTENT_WORKFLOW_STAGE');
$field->addAttribute('extension', $extension);
if ($this->workflowEnabled) {
$field->addAttribute('layout', 'joomla.form.field.groupedlist-transition');
}
$form->setField($field);
$table = $this->getTable();
$key = $table->getKeyName();
$id = $data->$key ?? $form->getValue($key);
if ($id) {
// Transition field
$assoc = $this->workflow->getAssociation($id);
if (!empty($assoc->stage_id)) {
$form->setFieldAttribute('transition', 'workflow_stage', (int) $assoc->stage_id);
}
} else {
$stage_id = $this->getStageForNewItem($form, $data);
if (!empty($stage_id)) {
$form->setFieldAttribute('transition', 'workflow_stage', (int) $stage_id);
}
}
}
/**
* Try to load a workflow stage for newly created items
* which does not have a workflow assigned yet. If the category is not the
* carrier, overwrite it on your model and deliver your own carrier.
*
* @param Form $form A Form object.
* @param mixed $data The data expected for the form.
*
* @return boolean|integer An integer, holding the stage ID or false
* @since 4.0.0
*/
protected function getStageForNewItem(Form $form, $data)
{
$table = $this->getTable();
$hasKey = $table->hasField('catid');
if (!$hasKey) {
return false;
}
$catKey = $table->getColumnAlias('catid');
$field = $form->getField($catKey);
if (!$field) {
return false;
}
$catId = ((object) $data)->$catKey ?? $form->getValue($catKey);
// Try to get the category from the html code of the field
if (empty($catId)) {
$catId = $field->getAttribute('default', null);
if (!$catId) {
// Choose the first category available
$catOptions = $field->options;
if ($catOptions && !empty($catOptions[0]->value)) {
$catId = (int) $catOptions[0]->value;
}
}
}
if (empty($catId)) {
return false;
}
return $this->workflow->getDefaultStageByCategory($catId);
}
}