-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UserAction StepExecutor improvement #1687
base: develop
Are you sure you want to change the base?
Changes from all commits
185747f
a2d793b
4d70967
90d123f
51d5d0c
734602e
8bbbda8
8564190
9c8795c
5fec5da
dbd95de
0f4d39c
0fe881b
2e8ea00
327fb43
3c37d34
1e21dd4
569ad99
8722647
5f318b0
fcce80d
5a1a59f
fb8cb1a
9af3020
67fab83
d0ab1c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
use Atk4\Core\Factory; | ||
use Atk4\Data\Model; | ||
use Atk4\Data\Model\UserAction; | ||
use Atk4\Data\Persistence\Array_; | ||
use Atk4\Data\ValidationException; | ||
use Atk4\Ui\Button; | ||
use Atk4\Ui\Form; | ||
|
@@ -70,6 +71,9 @@ trait StepExecutorTrait | |
/** @var string */ | ||
public $finalMsg = 'Complete!'; | ||
|
||
/** @var array An extended copy of UserAction arguments. It contains original action arguments and arguments set by '__atk_model'. */ | ||
private $cloneArgs; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should add native Model args support to atk4/data model. It is also not completely clear to me, why there are args & fields. In atk4/data, the UserAction::$fields property is described to be used for dirty detection only. |
||
|
||
/** | ||
* Utility for setting Title for each step. | ||
*/ | ||
|
@@ -107,12 +111,47 @@ protected function setFormField(Form $form, array $fields, string $step): Form | |
return $form; | ||
} | ||
|
||
/** | ||
* Set model for userAction arguments. | ||
* Override existing argument with model definition. | ||
*/ | ||
protected function initActionArguments(): Model | ||
{ | ||
$args = $this->getAction()->args; | ||
if (array_key_exists('__atk_model', $args)) { | ||
/** @var Model $argsModel */ | ||
$argsModel = Factory::factory($args['__atk_model']); | ||
// if seed is supplied, we need to initialize | ||
if (!$argsModel->isInitialized()) { | ||
$argsModel->invokeInit(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Model::init is invoked when persistence is set when not initialized, I belive we should set side question, when custom model for args does bring any advantage? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If using seed then developper should supply proper property within the seed array, including Persistence. |
||
} | ||
|
||
unset($args['__atk_model']); | ||
} else { | ||
$argsModel = new Model(new Array_([])); | ||
} | ||
|
||
foreach ($args as $key => $val) { | ||
$argsModel->addField($key, $val); | ||
mvorisek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
$this->cloneArgs = []; | ||
// set userAction args using model field | ||
foreach ($argsModel->getFields('editable') as $k => $field) { | ||
mvorisek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->cloneArgs[$k] = $field->shortName; | ||
} | ||
|
||
return $argsModel; | ||
} | ||
|
||
protected function runSteps(): void | ||
{ | ||
$this->loader->set(function (Loader $p) { | ||
$argModel = $this->initActionArguments(); | ||
|
||
$this->loader->set(function (Loader $p) use ($argModel) { | ||
switch ($this->step) { | ||
case 'args': | ||
$this->doArgs($p); | ||
$this->doArgs($p, $argModel); | ||
|
||
break; | ||
case 'fields': | ||
|
@@ -131,23 +170,13 @@ protected function runSteps(): void | |
}); | ||
} | ||
|
||
protected function doArgs(View $page): void | ||
protected function doArgs(View $page, Model $model): 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); | ||
} | ||
} | ||
$form->setModel($model->createEntity()); | ||
|
||
// set args value if available | ||
$this->setFormField($form, $this->getActionData('args'), $this->step); | ||
|
@@ -157,8 +186,9 @@ protected function doArgs(View $page): void | |
$this->jsSetPrevHandler($page, $this->step); | ||
|
||
$form->onSubmit(function (Form $form) { | ||
$form->model->save(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is now save needed? |
||
// collect arguments | ||
$this->setActionDataFromModel('args', $form->model, array_keys($form->model->getFields())); | ||
$this->setActionDataFromModel('args', $form->model, array_keys($form->model->getFields('editable'))); | ||
|
||
return $this->jsStepSubmit($this->step); | ||
}); | ||
|
@@ -491,7 +521,7 @@ protected function getActionPreview() | |
{ | ||
$args = []; | ||
|
||
foreach ($this->action->args as $key => $val) { | ||
foreach ($this->cloneArgs as $key => $val) { | ||
$args[] = $this->getActionData('args')[$key]; | ||
} | ||
|
||
|
@@ -505,7 +535,7 @@ protected function getActionArgs(array $data): array | |
{ | ||
$args = []; | ||
|
||
foreach ($this->action->args as $key => $val) { | ||
foreach ($this->cloneArgs as $key => $val) { | ||
$args[] = $data[$key]; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
our testing/demos Model should be used here to test if field aliasing is used properly