1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace Atk4\Ui\UserAction;
6:
7: use Atk4\Data\Model;
8: use Atk4\Ui\Form;
9:
10: class FormExecutor extends BasicExecutor
11: {
12: /** @var Form|null */
13: public $form;
14:
15: #[\Override]
16: public function initPreview(): void
17: {
18: $this->addHeader();
19:
20: if (!$this->form) {
21: $this->form = Form::addTo($this);
22: }
23:
24: // setup form model using action fields
25: if (!$this->form->model) {
26: if (!$this->action->fields) {
27: $this->action->fields = $this->getModelFields($this->action->getModel());
28: }
29: $this->form->setModel($this->action->getEntity(), $this->action->fields);
30: }
31:
32: $this->form->onSubmit(function (Form $form) {
33: return $this->executeModelAction();
34: });
35: }
36:
37: /**
38: * Returns array of names of fields.
39: * This includes all editable or visible fields of the model.
40: *
41: * @return array
42: */
43: protected function getModelFields(Model $model)
44: {
45: return array_keys($model->getFields(['editable', 'visible']));
46: }
47: }
48: