Skip to content

Commit a17ed74

Browse files
committed
cherry pick minor improvements from 1687
1 parent 809c146 commit a17ed74

File tree

4 files changed

+56
-31
lines changed

4 files changed

+56
-31
lines changed

demos/_includes/DemoActionsUtil.php

+29-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Atk4\Data\Model;
88
use Atk4\Data\Model\UserAction;
9+
use Atk4\Ui\Form\Control\Dropdown;
910

1011
class DemoActionsUtil
1112
{
@@ -15,7 +16,7 @@ public static function setupDemoActions(Country $country): void
1516
'callback',
1617
[
1718
'description' => 'Callback',
18-
'callback' => function (Model $model) {
19+
'callback' => function (Country $model) {
1920
return 'callback execute using country ' . $model->getTitle();
2021
},
2122
]
@@ -25,10 +26,10 @@ public static function setupDemoActions(Country $country): void
2526
'preview',
2627
[
2728
'description' => 'Display Preview prior to run the action',
28-
'preview' => function (Model $model) {
29+
'preview' => function (Country $model) {
2930
return 'Previewing country ' . $model->getTitle();
3031
},
31-
'callback' => function (Model $model) {
32+
'callback' => function (Country $model) {
3233
return 'Done previewing ' . $model->getTitle();
3334
},
3435
]
@@ -54,7 +55,7 @@ public static function setupDemoActions(Country $country): void
5455
'args' => [
5556
'age' => ['type' => 'integer', 'required' => true],
5657
],
57-
'callback' => function (Model $model, $age) {
58+
'callback' => function (Country $model, int $age) {
5859
if ($age < 18) {
5960
$text = 'Sorry not old enough to visit ' . $model->getTitle();
6061
} else {
@@ -71,8 +72,10 @@ public static function setupDemoActions(Country $country): void
7172
[
7273
'caption' => 'Argument/Preview',
7374
'description' => 'Ask for argument "Age" and display preview prior to execute',
74-
'args' => ['age' => ['type' => 'integer', 'required' => true]],
75-
'preview' => function (Model $model, $age) {
75+
'args' => [
76+
'age' => ['type' => 'integer', 'required' => true],
77+
],
78+
'preview' => function (Country $model, int $age) {
7679
return 'You age is: ' . $age;
7780
},
7881
'callback' => function (Model $model, $age) {
@@ -100,7 +103,9 @@ public static function setupDemoActions(Country $country): void
100103
[
101104
'caption' => 'Exception',
102105
'description' => 'Throw an exception when executing an action',
103-
'args' => ['age' => ['type' => 'integer']],
106+
'args' => [
107+
'age' => ['type' => 'integer'],
108+
],
104109
'preview' => function () {
105110
return 'Be careful with this action.';
106111
},
@@ -115,10 +120,12 @@ public static function setupDemoActions(Country $country): void
115120
[
116121
'caption' => 'User Confirmation',
117122
'description' => 'Confirm the action using a ConfirmationExecutor',
118-
'confirmation' => function ($a) {
119-
return 'Are you sure you want to perform this action on: <b>' . $a->getEntity()->getTitle() . ' (' . $a->getEntity()->iso3 . ')</b>';
123+
'confirmation' => function (UserAction $a) {
124+
$iso3 = $a->getEntity()->get(Country::hinting()->fieldName()->iso3);
125+
126+
return 'Are you sure you want to perform this action on: <b>' . $a->getEntity()->getTitle() . ' (' . $iso3 . ')</b>';
120127
},
121-
'callback' => function (Model $model) {
128+
'callback' => function (Country $model) {
122129
return 'Confirm country ' . $model->getTitle();
123130
},
124131
]
@@ -132,15 +139,24 @@ public static function setupDemoActions(Country $country): void
132139
'args' => [
133140
'age' => ['type' => 'integer', 'required' => true],
134141
'city' => [],
135-
'gender' => ['type' => 'string', /* TODO 'values' => ['m' => 'Male', 'f' => 'Female'], */ 'required' => true, 'default' => 'm'],
142+
'gender' => [
143+
'type' => 'string',
144+
'required' => true,
145+
'default' => 'm',
146+
'ui' => [
147+
'form' => [
148+
Dropdown::class, 'values' => ['m' => 'Male', 'f' => 'Female'],
149+
],
150+
],
151+
],
136152
],
137153
'fields' => [$country->fieldName()->iso3],
138-
'callback' => function (Model $model, $age, $city, $gender) {
154+
'callback' => function (Country $model, int $age, string $city, string $gender) {
139155
$n = $gender === 'm' ? 'Mr.' : 'Mrs.';
140156

141157
return 'Thank you ' . $n . ' at age ' . $age;
142158
},
143-
'preview' => function (Model $model, $age, $city, $gender) {
159+
'preview' => function (Country $model, int $age, string $city, string $gender) {
144160
return 'Gender = ' . $gender . ' / Age = ' . $age;
145161
},
146162
]

src/CardDeck.php

-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ public function setModel(Model $model, array $fields = null, array $extra = null
155155
}
156156
if ($this->useAction) {
157157
if ($singleActions = $this->getModelActions(Model\UserAction::APPLIES_TO_SINGLE_RECORD)) {
158-
$args = $this->getReloadArgs();
159158
foreach ($singleActions as $action) {
160159
$c->addClickAction($action, null, $this->getReloadArgs());
161160
}

src/UserAction/StepExecutorTrait.php

+25-15
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ protected function setFormField(Form $form, array $fields, string $step): Form
102102
foreach ($fields as $k => $val) {
103103
$form->getControl($k)->set($val);
104104
}
105+
105106
$this->hook(self::HOOK_STEP, [$step, $form]);
106107

107108
return $form;
@@ -158,16 +159,16 @@ protected function doArgs(View $page): void
158159
}
159160
}
160161

161-
// set args value if available.
162+
// set args value if available
162163
$this->setFormField($form, $this->getActionData('args'), $this->step);
163164

164-
// setup exec, next and prev button handler for this step.
165+
// setup exec, next and prev button handler for this step
165166
$this->jsSetSubmitBtn($page, $form, $this->step);
166167
$this->jsSetPrevHandler($page, $this->step);
167168

168169
$form->onSubmit(function (Form $form) {
169-
// collect arguments.
170-
$this->actionData['args'] = $form->model->get();
170+
// collect arguments
171+
$this->setActionDataFromModel('args', $form->model, array_keys($form->model->getFields()));
171172

172173
return $this->jsStepSubmit($this->step);
173174
});
@@ -179,20 +180,17 @@ protected function doFields(View $page): void
179180
$form = $this->addFormTo($page);
180181

181182
$form->setModel($this->action->getEntity(), $this->action->fields);
182-
// set Fields value if set from another step.
183+
// set Fields value if set from another step
183184
$this->setFormField($form, $this->getActionData('fields'), $this->step);
184185

185-
// setup exec, next and prev button handler for this step.
186+
// setup exec, next and prev button handler for this step
186187
$this->jsSetSubmitBtn($page, $form, $this->step);
187188
$this->jsSetPrevHandler($page, $this->step);
188189

189190
if (!$form->hookHasCallbacks(Form::HOOK_SUBMIT)) {
190191
$form->onSubmit(function (Form $form) {
191-
// collect fields.
192-
$form_fields = $form->model->get();
193-
foreach ($this->action->fields as $field) {
194-
$this->actionData['fields'][$field] = $form_fields[$field];
195-
}
192+
// collect fields defined in Model\UserAction
193+
$this->setActionDataFromModel('fields', $form->model, $this->action->fields);
196194

197195
return $this->jsStepSubmit($this->step);
198196
});
@@ -216,7 +214,7 @@ protected function doPreview(View $page): void
216214
$page->js(true, $this->prevStepBtn->js()->on('click', new JsFunction([$chain])));
217215
}
218216

219-
// setup executor button to perform action.
217+
// setup executor button to perform action
220218
$page->js(
221219
true,
222220
$this->execActionBtn->js()->on(
@@ -371,7 +369,7 @@ protected function jsSetBtnState(View $view, string $step): void
371369
$view->js(true, $this->jsSetExecState($step));
372370
}
373371

374-
// reset button handler.
372+
// reset button handler
375373
$view->js(true, $this->execActionBtn->js(true)->off());
376374
$view->js(true, $this->nextStepBtn->js(true)->off());
377375
$view->js(true, $this->prevStepBtn->js(true)->off());
@@ -452,11 +450,11 @@ protected function jsStepSubmit(string $step)
452450
{
453451
try {
454452
if ($this->isLastStep($step)) {
455-
// collect argument and execute action.
453+
// collect argument and execute action
456454
$return = $this->action->execute(...$this->getActionArgs($this->getActionData('args')));
457455
$js = $this->jsGetExecute($return, $this->action->getEntity()->getId());
458456
} else {
459-
// store data and setup reload.
457+
// store data and setup reload
460458
$js = [
461459
$this->loader->jsAddStoreData($this->actionData, true),
462460
$this->loader->jsLoad([
@@ -483,6 +481,18 @@ protected function getActionData(string $step): array
483481
return $this->actionData[$step] ?? [];
484482
}
485483

484+
/**
485+
* @param array<string> $fields
486+
*/
487+
private function setActionDataFromModel(string $step, Model $model, array $fields): void
488+
{
489+
$data = [];
490+
foreach ($fields as $k) {
491+
$data[$k] = $model->get($k);
492+
}
493+
$this->actionData[$step] = $data;
494+
}
495+
486496
/**
487497
* Get action preview based on it's argument.
488498
*

src/View.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ public function setSource(array $data, $fields = null)
132132
{
133133
// ID with zero value is not supported (at least in MySQL replaces it with next AI value)
134134
if (isset($data[0])) {
135-
if ($data === array_values($data)) {
135+
if (array_is_list($data)) {
136136
$oldData = $data;
137137
$data = [];
138138
foreach ($oldData as $k => $row) {
139-
$data[$k + 1000_000_000] = $row; // large offset to prevent accessing wrong data by old key
139+
$data[$k + 1_000_000_000] = $row; // large offset to prevent accessing wrong data by old key
140140
}
141141
} else {
142142
throw new Exception('Source data contains unsupported zero key');

0 commit comments

Comments
 (0)