User Interface
Interactivity
Business Model
Persistence

One major benefit of Server Side Rendered applications is ability to directly interact with data. In other applications you may need to manually process data but in Agile Toolkit we use data mapping framework.

/* Showing Class definition.
class DemoInvoice extends Model
{
    public ?string $titleField = 'reference';

    protected function init(): void
    {
        parent::init();

        $this->addField('reference');
        $this->addField('date', ['type' => 'date']);
    }
}
*/
session_start();

$model = new DemoInvoice(new Persistence\Array_($_SESSION['atk4_ui_intro_demo'] ?? []));
$model->onHook(Model::HOOK_AFTER_SAVE, static function (Model $model) {
    $_SESSION['atk4_ui_intro_demo'][$model->getId()] = (clone $model->getModel())->addCondition($model->idField, $model->getId())->export(null, null, false)[$model->getId()];
});

Header::addTo($owner, ['Set invoice data:']);
$form = Form::addTo($owner);

$entity = $model->tryLoad(1);
if ($entity === null) {
    // set default data
    $entity = $model->createEntity();
    $entity->setMulti([
        'id' => 1,
        'reference' => 'Inv-' . random_int(1000, 9999),
        'date' => new \DateTime(),
    ]);
    $entity->save();
}
$form->setModel($entity);

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

    return new JsToast('Saved!');
});

View::addTo($owner, ['ui' => 'divider']);
Set invoice data:
Save

This code shows you a combination of 3 objects:

  • Form - a generic view that can display and handle any form
  • Model - defines fields for a business object
  • Persistence - creates a persistent storage location for the data

All three are combined by "setModel()" function and that is consistent throughout all the views.