Skip to content
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

Use View::$entity property for entity access #2192

Merged
merged 11 commits into from
Mar 27, 2024
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ $form->addField('email');
$form->onSubmit(function (Form $form) {
// implement subscribe here

return $form->jsSuccess('Subscribed ' . $form->model->get('email') . ' to newsletter.');
return $form->jsSuccess('Subscribed ' . $form->entity->get('email') . ' to newsletter.');
});

// decorate anything
Expand Down Expand Up @@ -129,8 +129,8 @@ It's easy to create your own application styling. Here are some example UI:
As of version 2.0 - Agile Toolkit offers support for User Actions. Those are easy to define in your Data Model declaration:

```php
$this->addUserAction('archive', function (Model $m) {
$m->set('is_archived', true);
$this->addUserAction('archive', function (Model $entity) {
$this->set('is_archived', true);
$this->saveAndUnload();
});
```
Expand Down
41 changes: 20 additions & 21 deletions demos/_includes/DemoActionsUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Atk4\Ui\Demos;

use Atk4\Data\Model;
use Atk4\Data\Model\UserAction;
use Atk4\Ui\Exception;
use Atk4\Ui\Form;
Expand All @@ -15,18 +14,18 @@ public static function setupDemoActions(Country $country): void
{
$country->addUserAction('callback', [
'description' => 'Callback',
'callback' => static function (Country $model) {
return 'callback execute using country ' . $model->getTitle();
'callback' => static function (Country $entity) {
return 'callback execute using country ' . $entity->getTitle();
},
]);

$country->addUserAction('preview', [
'description' => 'Display Preview prior to run the action',
'preview' => static function (Country $model) {
return 'Previewing country ' . $model->getTitle();
'preview' => static function (Country $entity) {
return 'Previewing country ' . $entity->getTitle();
},
'callback' => static function (Country $model) {
return 'Done previewing ' . $model->getTitle();
'callback' => static function (Country $entity) {
return 'Done previewing ' . $entity->getTitle();
},
]);

Expand All @@ -45,11 +44,11 @@ public static function setupDemoActions(Country $country): void
'args' => [
'age' => ['type' => 'integer', 'required' => true],
],
'callback' => static function (Country $model, int $age) {
'callback' => static function (Country $entity, int $age) {
if ($age < 18) {
$text = 'Sorry not old enough to visit ' . $model->getTitle();
$text = 'Sorry not old enough to visit ' . $entity->getTitle();
} else {
$text = $age . ' is old enough to visit ' . $model->getTitle();
$text = $age . ' is old enough to visit ' . $entity->getTitle();
}

return $text;
Expand All @@ -62,10 +61,10 @@ public static function setupDemoActions(Country $country): void
'args' => [
'age' => ['type' => 'integer', 'required' => true],
],
'preview' => static function (Country $model, int $age) {
'preview' => static function (Country $entity, int $age) {
return 'You age is: ' . $age;
},
'callback' => static function (Model $model, $age) {
'callback' => static function (Country $entity, $age) {
return 'age = ' . $age;
},
]);
Expand Down Expand Up @@ -98,13 +97,13 @@ public static function setupDemoActions(Country $country): void
$country->addUserAction('confirm', [
'caption' => 'User Confirmation',
'description' => 'Confirm the action using a ConfirmationExecutor',
'confirmation' => static function (UserAction $a) {
$iso3 = Country::assertInstanceOf($a->getEntity())->iso3;
'confirmation' => static function (UserAction $action) {
$iso3 = Country::assertInstanceOf($action->getEntity())->iso3;

return 'Are you sure you want to perform this action on: <b>' . $a->getEntity()->getTitle() . ' (' . $iso3 . ')</b>';
return 'Are you sure you want to perform this action on: <b>' . $action->getEntity()->getTitle() . ' (' . $iso3 . ')</b>';
},
'callback' => static function (Country $model) {
return 'Confirm country ' . $model->getTitle();
'callback' => static function (Country $entity) {
return 'Confirm country ' . $entity->getTitle();
},
]);

Expand All @@ -126,14 +125,14 @@ public static function setupDemoActions(Country $country): void
],
],
'fields' => [$country->fieldName()->iso3],
'callback' => static function (Country $model, int $age, string $city, string $gender) {
'preview' => static function (Country $entity, int $age, string $city, string $gender) {
return 'Gender = ' . $gender . ' / Age = ' . $age;
},
'callback' => static function (Country $entity, int $age, string $city, string $gender) {
$n = $gender === 'm' ? 'Mr.' : 'Mrs.';

return 'Thank you ' . $n . ' at age ' . $age;
},
'preview' => static function (Country $model, int $age, string $city, string $gender) {
return 'Gender = ' . $gender . ' / Age = ' . $age;
},
]);
}
}
5 changes: 2 additions & 3 deletions demos/_includes/FlyersForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Atk4\Ui\Demos;

use Atk4\Data\Model;
use Atk4\Data\Persistence;
use Atk4\Ui\Form;
use Atk4\Ui\Js\JsToast;
Expand Down Expand Up @@ -40,9 +39,9 @@ protected function init(): void
$this->addControl('country', [
Form\Control\Lookup::class,
'model' => new Country($this->getApp()->db),
'dependency' => static function (Model $model, $data) {
'dependency' => static function (Country $model, $data) {
if (isset($data['contains'])) {
$model->addCondition(Country::hinting()->fieldName()->name, 'like', '%' . $data['contains'] . '%');
$model->addCondition($model->fieldName()->name, 'like', '%' . $data['contains'] . '%');
}
},
'search' => [
Expand Down
2 changes: 1 addition & 1 deletion demos/_unit-test/callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
$button->on('click', new JsModal('Edit First Record', $vp));

$form->onSubmit(static function (Form $form) use ($table) {
$form->model->save();
$form->entity->save();

return new JsBlock([
$table->jsReload(),
Expand Down
2 changes: 1 addition & 1 deletion demos/_unit-test/lookup-virtual-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
$form = Form::addTo($vp);
$form->addControl('category', [Form\Control\Lookup::class, 'model' => new Category($vp->getApp()->db)]);
$form->onSubmit(static function (Form $form) {
$category = $form->getControl('category')->model->load($form->model->get('category'));
$category = $form->getControl('category')->model->load($form->entity->get('category'));

return new JsToast($category->getTitle());
});
Expand Down
4 changes: 2 additions & 2 deletions demos/_unit-test/lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
$app->getExecutorFactory()->useTriggerDefault(ExecutorFactory::TABLE_BUTTON);

$edit = $model->getUserAction('edit');
$edit->callback = static function (Product $model) {
return $model->product_category_id->getTitle() . ' - ' . $model->product_sub_category_id->getTitle();
$edit->callback = static function (Product $entity) {
return $entity->product_category_id->getTitle() . ' - ' . $entity->product_sub_category_id->getTitle();
};

$crud = Crud::addTo($app);
Expand Down
2 changes: 1 addition & 1 deletion demos/_unit-test/scope-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
$form->addControl('qb', [Form\Control\ScopeBuilder::class, 'model' => $model], ['type' => 'object']);

$form->onSubmit(static function (Form $form) use ($model) {
$message = $form->model->get('qb')->toWords($model);
$message = $form->entity->get('qb')->toWords($model);
$view = View::addTo($form)->addClass('atk-scope-builder-response');
$view->set($message);

Expand Down
2 changes: 1 addition & 1 deletion demos/_unit-test/virtual-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
$form = Form::addTo($thirdPage);
$form->addControl('category', [Form\Control\Lookup::class, 'model' => new Category($thirdPage->getApp()->db)]);
$form->onSubmit(static function (Form $form) {
$category = $form->getControl('category')->model->load($form->model->get('category'));
$category = $form->getControl('category')->model->load($form->entity->get('category'));

return new JsToast($category->getTitle());
});
Expand Down
6 changes: 3 additions & 3 deletions demos/collection/crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@
// condition on the model can be applied on a model
$model = new Country($app->db);
$model->addCondition($model->fieldName()->numcode, '<', 200);
$model->onHook(Model::HOOK_VALIDATE, static function (Country $model, ?string $intent) {
$model->onHook(Model::HOOK_VALIDATE, static function (Country $entity, ?string $intent) {
$err = [];
if ($model->numcode >= 200) {
$err[$model->fieldName()->numcode] = 'Should be less than 200';
if ($entity->numcode >= 200) {
$err[$entity->fieldName()->numcode] = 'Should be less than 200';
}

return $err;
Expand Down
4 changes: 2 additions & 2 deletions demos/collection/grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

$grid = Grid::addTo($app);
$model = new Country($app->db);
$model->addUserAction('test', static function (Model $model) {
return 'test from ' . $model->getTitle() . ' was successful!';
$model->addUserAction('test', static function (Model $entity) {
return 'test from ' . $entity->getTitle() . ' was successful!';
});

$grid->setModel($model);
Expand Down
4 changes: 2 additions & 2 deletions demos/collection/table2.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
$table->setModel($model, ['action']);

// copy of amount through a PHP callback
$model->addExpression('amount_copy', ['expr' => static function (Model $model) {
return $model->get('amount');
$model->addExpression('amount_copy', ['expr' => static function (Model $entity) {
return $entity->get('amount');
}, 'type' => 'atk4_money']);

// column with 2 decorators that stack. Money will use red ink and alignment, format will change text.
Expand Down
2 changes: 1 addition & 1 deletion demos/data-action/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
'description' => 'Import file in a specify path.',
// Display information prior to execute the action.
// ModalExecutor or PreviewExecutor will display preview.
'preview' => static function (Model $model, string $path) {
'preview' => static function (Model $entity, string $path) {
return 'Execute Import using path: "' . $path . '"';
},
// Argument needed to run the callback action method.
Expand Down
2 changes: 1 addition & 1 deletion demos/data-action/jsactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
'required' => true,
],
],
'callback' => static function (Country $model, string $name) {
'callback' => static function (Country $entity, string $name) {
return 'Hello ' . $name;
},
]);
Expand Down
4 changes: 2 additions & 2 deletions demos/data-action/jsactionscrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
'caption' => 'Import',
'callback' => 'importFromFilesystem',
'description' => 'Import file using path:',
'preview' => static function (Model $model, $path) {
'preview' => static function (Model $entity, $path) {
return 'Execute Import using path: "' . $path . '"';
},
'args' => [
Expand All @@ -32,7 +32,7 @@
'appliesTo' => Model\UserAction::APPLIES_TO_NO_RECORDS,
]);

$files->addUserAction('download', static function (Model $model) {
$files->addUserAction('download', static function (Model $entity) {
return 'File has been download!';
});

Expand Down
4 changes: 2 additions & 2 deletions demos/form-control/calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@

$form->onSubmit(static function (Form $form) use ($app) {
$data = [];
foreach ($form->model->get() as $k => $v) {
$data[$k] = $app->uiPersistence->typecastSaveField($form->model->getField($k), $v) ?? 'empty';
foreach ($form->entity->get() as $k => $v) {
$data[$k] = $app->uiPersistence->typecastSaveField($form->entity->getField($k), $v) ?? 'empty';
}

return new JsToast(implode(', ', $data));
Expand Down
2 changes: 1 addition & 1 deletion demos/form-control/checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@
$form->addControl('also_checked', ['caption' => 'Also checked by default'], ['type' => 'boolean'])->set(true);

$form->onSubmit(static function (Form $form) use ($app) {
return new JsToast($app->encodeJson($form->model->get()));
return new JsToast($app->encodeJson($form->entity->get()));
});
4 changes: 2 additions & 2 deletions demos/form-control/dropdown-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
$form->addControl('product_id', [Form\Control\DropdownCascade::class, 'cascadeFrom' => 'sub_category_id', 'reference' => SubCategory::hinting()->fieldName()->Products]);

$form->onSubmit(static function (Form $form) use ($app) {
$message = $app->encodeJson($app->uiPersistence->typecastSaveRow($form->model, $form->model->get()));
$message = $app->encodeJson($app->uiPersistence->typecastSaveRow($form->entity, $form->entity->get()));

$view = new Message('Values: ');
$view->setApp($form->getApp());
Expand Down Expand Up @@ -105,7 +105,7 @@
]);

$form->onSubmit(static function (Form $form) use ($app) {
$message = $app->encodeJson($form->model->get());
$message = $app->encodeJson($form->entity->get());

$view = new Message('Values:');
$view->setApp($form->getApp());
Expand Down
2 changes: 1 addition & 1 deletion demos/form-control/form6.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@
$form->addControl('string_r', [Form\Control\Radio::class], ['values' => ['F' => 'female', 'M' => 'male']])->set('M');

$form->onSubmit(static function (Form $form) use ($app) {
return new JsToast($app->encodeJson($form->getApp()->uiPersistence->typecastSaveRow($form->model, $form->model->get())));
return new JsToast($app->encodeJson($form->getApp()->uiPersistence->typecastSaveRow($form->entity, $form->entity->get())));
});
4 changes: 2 additions & 2 deletions demos/form-control/input2.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
]));

$form->onSubmit(static function (Form $form) {
return $form->model->get('name');
return $form->entity->get('name');
});

Header::addTo($app, ['Multiple Form Layouts']);
Expand All @@ -142,7 +142,7 @@
$formPage->addControl('age', new Form\Control\Line());

$form->onSubmit(static function (Form $form) {
return $form->model->get('name') . ' has age ' . $form->model->get('age');
return $form->entity->get('name') . ' has age ' . $form->entity->get('age');
});

Header::addTo($app, ['onChange event', 'subHeader' => 'see in browser console']);
Expand Down
4 changes: 2 additions & 2 deletions demos/form-control/lookup-dep.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
]);

$form->onSubmit(static function (Form $form) {
return 'Submitted: ' . print_r($form->model->get(), true);
return 'Submitted: ' . print_r($form->entity->get(), true);
});

Header::addTo($app, ['Lookup multiple values']);
Expand Down Expand Up @@ -92,5 +92,5 @@
]);

$form->onSubmit(static function (Form $form) {
return 'Submitted: ' . print_r($form->model->get(), true);
return 'Submitted: ' . print_r($form->entity->get(), true);
});
8 changes: 4 additions & 4 deletions demos/form-control/lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
$view = new Message('Select:');
$view->setApp($form->getApp());
$view->invokeInit();
$view->text->addParagraph(Country::assertInstanceOf($form->model->ref('country1'))->name ?? 'null');
$view->text->addParagraph(Country::assertInstanceOf($form->model->ref('country2'))->name ?? 'null');
$view->text->addParagraph($form->model->get('country3') !== '' // related with https://github.com/atk4/ui/pull/1805
? (new Country($form->getApp()->db))->load($form->model->get('country3'))->name
$view->text->addParagraph(Country::assertInstanceOf($form->entity->ref('country1'))->name ?? 'null');
$view->text->addParagraph(Country::assertInstanceOf($form->entity->ref('country2'))->name ?? 'null');
$view->text->addParagraph($form->entity->get('country3') !== '' // related with https://github.com/atk4/ui/pull/1805
? (new Country($form->getApp()->db))->load($form->entity->get('country3'))->name
: 'null');

return $view;
Expand Down
2 changes: 1 addition & 1 deletion demos/form-control/scope-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
$form->addControl('qb', [Form\Control\ScopeBuilder::class, 'model' => $model, 'options' => ['debug' => true]]);

$form->onSubmit(static function (Form $form) use ($model) {
return "Scope selected:\n\n" . $form->model->get('qb')->toWords($model);
return "Scope selected:\n\n" . $form->entity->get('qb')->toWords($model);
});
4 changes: 2 additions & 2 deletions demos/form-control/tree-item-selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@

$form->onSubmit(static function (Form $form) use ($app) {
$response = [
'multiple' => $form->model->get('tree'),
'single' => $form->model->get('tree1'),
'multiple' => $form->entity->get('tree'),
'single' => $form->entity->get('tree1'),
];

$view = new Message('Items:');
Expand Down
2 changes: 1 addition & 1 deletion demos/form-control/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,5 @@

$form->onSubmit(static function (Form $form) {
// implement submission here
return $form->jsSuccess('Thanks for submitting file: ' . $form->model->get('img') . ' / ' . $form->model->get('file'));
return $form->jsSuccess('Thanks for submitting file: ' . $form->entity->get('img') . ' / ' . $form->entity->get('file'));
});
4 changes: 2 additions & 2 deletions demos/form/form-section.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
$model = $model->loadAny();

$saveAndDumpValues = static function (Form $form) {
$form->model->save();
$form->entity->save();

return new JsToast([
'title' => 'POSTed field values',
'message' => '<pre>' . $form->getApp()->encodeJson($form->model->get()) . '</pre>',
'message' => '<pre>' . $form->getApp()->encodeJson($form->entity->get()) . '</pre>',
'class' => 'success',
'displayTime' => 5000,
]);
Expand Down
Loading
Loading