-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathStepExecutorTrait.php
514 lines (420 loc) · 14.8 KB
/
StepExecutorTrait.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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<?php
declare(strict_types=1);
namespace Atk4\Ui\UserAction;
use Atk4\Core\Factory;
use Atk4\Data\Model;
use Atk4\Data\Model\UserAction;
use Atk4\Data\ValidationException;
use Atk4\Ui\Button;
use Atk4\Ui\Form;
use Atk4\Ui\Js\JsBlock;
use Atk4\Ui\Js\JsExpressionable;
use Atk4\Ui\Js\JsFunction;
use Atk4\Ui\Loader;
use Atk4\Ui\Message;
use Atk4\Ui\View;
trait StepExecutorTrait
{
/** @var array<int, string> The steps need to complete the action. */
protected $steps;
/** @var string current step. */
protected $step;
/** @var Loader The Loader that will execute all action step. */
protected $loader;
/** @var string */
public $loaderUi = 'basic segment';
/** @var array */
public $loaderShim = [];
/** @var Button The action step prev button. */
protected $prevStepBtn;
/** @var Button The action next step button. */
protected $nextStepBtn;
/** @var Button The execute action button. */
protected $execActionBtn;
/** @var View View holding buttons. */
protected $btns;
/** @var UserAction The action to execute. */
public $action;
/** @var array will collect data while doing action step. */
private $actionData = [];
/** @var bool */
protected $actionInitialized = false;
/** @var JsExpressionable|\Closure JS expression to return if action was successful, e.g "new JsToast('Thank you')" */
public $jsSuccess;
/** @var array A seed for creating form in order to edit arguments/fields user entry. */
public $formSeed = [Form::class];
/** @var string can be "console", "text", or "html". Determine how preview step will display information. */
public $previewType = 'html';
/** @var array<string, array<mixed>> View seed for displaying title for each step. */
protected $stepTitle = ['args' => [], 'fields' => [], 'preview' => []];
/** @var string */
public $finalMsg = 'Complete!';
/**
* Utility for setting Title for each step.
*/
protected function addStepTitle(View $view, string $step): void
{
$seed = $this->stepTitle[$step] ?? null;
if ($seed) {
$view->add(Factory::factory($seed));
}
}
/**
* Utility for setting form in each step.
*/
protected function addFormTo(View $view): Form
{
/** @var Form $f */
$f = $view->add(Factory::factory($this->formSeed));
$f->buttonSave->destroy();
return $f;
}
/**
* Will add field into form based on $fields array.
*/
protected function setFormField(Form $form, array $fields, string $step): Form
{
foreach ($fields as $k => $val) {
$form->getControl($k)->set($val);
}
$this->hook(self::HOOK_STEP, [$step, $form]);
return $form;
}
protected function runSteps(): void
{
$this->loader->set(function (Loader $p) {
switch ($this->step) {
case 'args':
$this->doArgs($p);
break;
case 'fields':
$this->doFields($p);
break;
case 'preview':
$this->doPreview($p);
break;
case 'final':
$this->doFinal($p);
break;
}
});
}
protected function doArgs(View $page): void
{
$this->addStepTitle($page, $this->step);
$form = $this->addFormTo($page);
foreach ($this->action->args as $key => $val) {
if ($val instanceof Model) {
$val = ['model' => $val];
}
if (isset($val['model'])) {
$val['model'] = Factory::factory($val['model']);
$form->addControl($key, [Form\Control\Lookup::class])->setModel($val['model']);
} else {
$form->addControl($key, [], $val);
}
}
// set args value if available
$this->setFormField($form, $this->getActionData('args'), $this->step);
// setup exec, next and prev button handler for this step
$this->jsSetSubmitBtn($page, $form, $this->step);
$this->jsSetPrevHandler($page, $this->step);
$form->onSubmit(function (Form $form) {
// collect arguments
$this->setActionDataFromModel('args', $form->model, array_keys($form->model->getFields()));
return $this->jsStepSubmit($this->step);
});
}
protected function doFields(View $page): void
{
$this->addStepTitle($page, $this->step);
$form = $this->addFormTo($page);
$form->setModel($this->action->getEntity(), $this->action->fields);
// set Fields value if set from another step
$this->setFormField($form, $this->getActionData('fields'), $this->step);
// setup exec, next and prev button handler for this step
$this->jsSetSubmitBtn($page, $form, $this->step);
$this->jsSetPrevHandler($page, $this->step);
if (!$form->hookHasCallbacks(Form::HOOK_SUBMIT)) {
$form->onSubmit(function (Form $form) {
// collect fields defined in Model\UserAction
$this->setActionDataFromModel('fields', $form->model, $this->action->fields);
return $this->jsStepSubmit($this->step);
});
}
}
protected function doPreview(View $page): void
{
$this->addStepTitle($page, $this->step);
$fields = $this->getActionData('fields');
if ($fields) {
$this->action->getEntity()->setMulti($fields);
}
$prev = $this->getPreviousStep($this->step);
if ($prev) {
$chain = $this->loader->jsLoad([
'step' => $prev,
$this->name => $this->action->getEntity()->getId(),
], ['method' => 'post'], $this->loader->name);
$page->js(true, $this->prevStepBtn->js()->on('click', new JsFunction([], [$chain])));
}
// setup executor button to perform action
$page->js(
true,
$this->execActionBtn->js()->on('click', new JsFunction([], [
$this->loader->jsLoad(
[
'step' => 'final',
$this->name => $this->action->getEntity()->getId(),
],
['method' => 'post'],
$this->loader->name
),
]))
);
$text = $this->getActionPreview();
switch ($this->previewType) {
case 'console':
$preview = View::addTo($page, ['ui' => 'inverted black segment', 'element' => 'pre']);
$preview->set($text);
break;
case 'text':
$preview = View::addTo($page, ['ui' => 'basic segment']);
$preview->set($text);
break;
case 'html':
$preview = View::addTo($page, ['ui' => 'basic segment']);
$preview->template->dangerouslySetHtml('Content', $text);
break;
}
}
protected function doFinal(View $page): void
{
View::addTo($page, ['content' => $this->finalMsg]);
foreach ($this->getActionData('fields') as $field => $value) {
$this->action->getEntity()->set($field, $value);
}
$return = $this->action->execute(...$this->getActionArgs($this->getActionData('args')));
$page->js(true, $this->jsGetExecute($return, $this->action->getEntity()->getId()));
}
/**
* Get how many steps is required for this action.
*/
protected function getSteps(UserAction $action): array
{
$steps = [];
if ($action->args) {
$steps[] = 'args';
}
if ($action->fields) {
$steps[] = 'fields';
}
if ($action->preview) {
$steps[] = 'preview';
}
return $steps;
}
protected function getNextStep(string $step): ?string
{
$next = null;
if (!$this->isLastStep($step)) {
foreach ($this->steps as $k => $s) {
if ($step === $s) {
$next = $this->steps[$k + 1];
break;
}
}
}
return $next;
}
protected function getPreviousStep(string $step): ?string
{
$prev = null;
if (!$this->isFirstStep($step)) {
foreach ($this->steps as $k => $s) {
if ($s === $step) {
$prev = $this->steps[$k - 1];
break;
}
}
}
return $prev;
}
protected function isLastStep(string $step): bool
{
$isLast = false;
$step_count = count($this->steps);
foreach ($this->steps as $k => $s) {
if ($s === $step) {
$isLast = $k === $step_count - 1;
break;
}
}
return $isLast;
}
protected function isFirstStep(string $step): bool
{
return $step === $this->steps[0];
}
protected function getStep(): string
{
return $this->step;
}
protected function createButtonBar(Model\UserAction $action): View
{
$this->btns = (new View())->setStyle(['min-height' => '24px']);
$this->prevStepBtn = Button::addTo($this->btns, ['Prev'])->setStyle(['float' => 'left !important']);
$this->nextStepBtn = Button::addTo($this->btns, ['Next', 'class.blue' => true]);
$this->execActionBtn = $this->getExecutorFactory()->createTrigger($action, ExecutorFactory::MODAL_BUTTON);
$this->btns->add($this->execActionBtn);
return $this->btns;
}
/**
* Generate js for setting Buttons state based on current step.
*/
protected function jsSetBtnState(View $view, string $step): void
{
if (count($this->steps) === 1) {
$view->js(true, $this->prevStepBtn->js()->hide());
$view->js(true, $this->nextStepBtn->js()->hide());
} else {
$view->js(true, $this->jsSetPrevState($step));
$view->js(true, $this->jsSetNextState($step));
$view->js(true, $this->jsSetExecState($step));
}
// reset button handler
$view->js(true, $this->execActionBtn->js()->off());
$view->js(true, $this->nextStepBtn->js()->off());
$view->js(true, $this->prevStepBtn->js()->off());
$view->js(true, $this->nextStepBtn->js()->removeClass('disabled'));
$view->js(true, $this->execActionBtn->js()->removeClass('disabled'));
}
/**
* Generate js for Next btn state.
*/
protected function jsSetNextState(string $step): JsExpressionable
{
if ($this->isLastStep($step)) {
return $this->nextStepBtn->js()->hide();
}
return $this->nextStepBtn->js()->show();
}
/**
* Generated js for Prev btn state.
*/
protected function jsSetPrevState(string $step): JsExpressionable
{
if ($this->isFirstStep($step)) {
return $this->prevStepBtn->js()->hide();
}
return $this->prevStepBtn->js()->show();
}
/**
* Generate js for Exec button state.
*/
protected function jsSetExecState(string $step): JsExpressionable
{
if ($this->isLastStep($step)) {
return $this->execActionBtn->js()->show();
}
return $this->execActionBtn->js()->hide();
}
/**
* Generate js function for Previous button.
*/
protected function jsSetPrevHandler(View $view, string $step): void
{
$prev = $this->getPreviousStep($step);
if ($prev) {
$chain = $this->loader->jsLoad([
'step' => $prev,
$this->name => $this->action->getEntity()->getId(),
], ['method' => 'post'], $this->loader->name);
$view->js(true, $this->prevStepBtn->js()->on('click', new JsFunction([], [$chain])));
}
}
/**
* Determine which button is responsible for submitting form on a specific step.
*/
protected function jsSetSubmitBtn(View $view, Form $form, string $step): void
{
if ($this->isLastStep($step)) {
$view->js(true, $this->execActionBtn->js()->on('click', new JsFunction([], [$form->js(false, null, $form->formElement)->form('submit')])));
} else {
// submit on next
$view->js(true, $this->nextStepBtn->js()->on('click', new JsFunction([], [$form->js(false, null, $form->formElement)->form('submit')])));
}
}
/**
* Get proper js after submitting a form in a step.
*
* @return JsBlock|View
*/
protected function jsStepSubmit(string $step)
{
try {
if ($this->isLastStep($step)) {
// collect argument and execute action
$return = $this->action->execute(...$this->getActionArgs($this->getActionData('args')));
$js = $this->jsGetExecute($return, $this->action->getEntity()->getId());
} else {
// store data and setup reload
$js = new JsBlock([
$this->loader->jsAddStoreData($this->actionData, true),
$this->loader->jsLoad([
'step' => $this->getNextStep($step),
$this->name => $this->action->getEntity()->getId(),
], ['method' => 'post'], $this->loader->name),
]);
}
return $js;
} catch (ValidationException $e) {
throw $e;
} catch (\Throwable $e) {
$msg = new Message(['Error executing ' . $this->action->caption, 'type' => 'error', 'class.red' => true]);
$msg->setApp($this->getApp());
$msg->invokeInit();
$msg->text->content = $this->getApp()->renderExceptionHtml($e);
return $msg;
}
}
protected function getActionData(string $step): array
{
return $this->actionData[$step] ?? [];
}
/**
* @param array<string> $fields
*/
private function setActionDataFromModel(string $step, Model $model, array $fields): void
{
$data = [];
foreach ($fields as $k) {
$data[$k] = $model->get($k);
}
$this->actionData[$step] = $data;
}
/**
* Get action preview based on it's argument.
*
* @return mixed
*/
protected function getActionPreview()
{
$args = [];
foreach ($this->action->args as $key => $val) {
$args[] = $this->getActionData('args')[$key];
}
return $this->action->preview(...$args);
}
/**
* Utility for retrieving Argument.
*/
protected function getActionArgs(array $data): array
{
$args = [];
foreach ($this->action->args as $key => $val) {
$args[] = $data[$key];
}
return $args;
}
}