Skip to content

Commit

Permalink
Replace locking with ModelPreventModificationTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jan 14, 2022
1 parent 52e186d commit eba9431
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 45 deletions.
15 changes: 6 additions & 9 deletions demos/_includes/DemoLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,17 @@ protected function initQuickNewRecord()
$form->setModel($entity, $this->plus['fields'] ?? null);

$form->onSubmit(function (\Atk4\Ui\Form $form) {
// Prevent from saving
// $form->model->save();
$form->model->save();

$ret = [
new \Atk4\Ui\JsToast('Form submit!. Demo cannot saved data.'),
new \Atk4\Ui\JsToast('Form submit!. Data are not save in demo mode.'),
(new \Atk4\Ui\Jquery('.atk-modal'))->modal('hide'),
];

if ($row = $this->renderRow($form->model)) {
$chain = new \Atk4\Ui\Jquery('#' . $this->name . '-ac');
$chain->dropdown('set value', $row['value'])->dropdown('set text', $row['title']);

$ret[] = $chain;
}
$row = $this->renderRow($form->model);
$chain = new \Atk4\Ui\Jquery('#' . $this->name . '-ac');
$chain->dropdown('set value', $row['value'])->dropdown('set text', $row['title']);
$ret[] = $chain;

return $ret;
});
Expand Down
7 changes: 0 additions & 7 deletions demos/collection/crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@
$form->js(true, $form->getControl($model->fieldName()->name)->jsInput()->attr('readonly', true));
});

// callback for both model action edit and add.
$crud->onFormAddEdit(function (Form $form, $ex) {
$form->onSubmit(function (Form $form) use ($ex) {
return [$ex->hide(), new \Atk4\Ui\JsToast('Submit all right! This demo does not saved data.')];
});
});

$crud->setModel($model);

$crud->addDecorator($model->title_field, [\Atk4\Ui\Table\Column\Link::class, ['test' => false, 'path' => 'interfaces/page'], ['_id' => 'id']]);
Expand Down
13 changes: 7 additions & 6 deletions demos/form/form-section.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
$model = new Country($app->db);
$model = $model->loadAny();

// Prevent form from saving
$noSave = function (Form $form) {
$saveAndDumpValues = function (Form $form) {
$form->model->save();

return new \Atk4\Ui\JsToast([
'title' => 'POSTed field values',
'message' => '<pre>' . $form->getApp()->encodeJson($form->model->get()) . '</pre>',
Expand Down Expand Up @@ -46,7 +47,7 @@

$form->addControl($model->fieldName()->phonecode);

$form->onSubmit($noSave);
$form->onSubmit($saveAndDumpValues);

\Atk4\Ui\View::addTo($app, ['ui' => 'divider']);

Expand All @@ -68,7 +69,7 @@
$a2 = $accordionLayout->addSection('Section 2');
$a2->setModel($model, [$model->fieldName()->numcode, $model->fieldName()->phonecode]);

$form->onSubmit($noSave);
$form->onSubmit($saveAndDumpValues);

\Atk4\Ui\View::addTo($app, ['ui' => 'divider']);

Expand All @@ -90,7 +91,7 @@
$tab2 = $tabsLayout->addTab('Tab 2');
$tab2->setModel($model, [$model->fieldName()->numcode, $model->fieldName()->phonecode]);

$form->onSubmit($noSave);
$form->onSubmit($saveAndDumpValues);

\Atk4\Ui\View::addTo($app, ['ui' => 'divider']);

Expand All @@ -115,6 +116,6 @@
$c2 = $colsLayout->addColumn();
$c2->setModel($model, [$model->fieldName()->numcode, $model->fieldName()->phonecode]);

$form->onSubmit($noSave);
$form->onSubmit($saveAndDumpValues);

\Atk4\Ui\View::addTo($app, ['ui' => 'divider']);
65 changes: 42 additions & 23 deletions demos/init-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,48 @@

// a very basic file that sets up Agile Data to be used in some demonstrations

trait ModelPreventModificationTrait
{
public function atomic(\Closure $fx)
{
$eRollback = new \Exception('Prevent modification');
$res = null;
try {
parent::atomic(function () use ($fx, $eRollback, &$res) {
$res = $fx();

throw $eRollback;
});
} catch (\Exception $e) {
if ($e !== $eRollback) {
throw $e;
}
}

return $res;
}

protected function initPreventModification(): void
{
$this->getUserAction('add')->callback = function (Model $model) {
return 'Form Submit! Data are not save in demo mode.';
};

$this->getUserAction('edit')->callback = function (Model $model) {
return 'Form Submit! Data are not save in demo mode.';
};

$this->getUserAction('delete')->confirmation = 'Please go ahead. Demo mode does not really delete data.';
$this->getUserAction('delete')->callback = function (Model $model) {
return 'Only simulating delete when in demo mode.';
};
}
}

class ModelWithPrefixedFields extends Model
{
use ModelPreventModificationTrait;

/** @var array<string, string> */
private static $prefixedFieldNames = [];

Expand Down Expand Up @@ -89,6 +129,8 @@ protected function init(): void
}

parent::init();

$this->initPreventModification();
}

public function addField($name, $seed = []): \Atk4\Data\Field
Expand All @@ -102,26 +144,6 @@ public function addField($name, $seed = []): \Atk4\Data\Field
}
}

trait ModelLockTrait
{
public function lock(): void
{
$this->getUserAction('add')->callback = function (Model $model) {
return 'Form Submit! Data are not save in demo mode.';
};
$this->getUserAction('edit')->callback = function (Model $model) {
return 'Form Submit! Data are not save in demo mode.';
};

$delete = $this->getUserAction('delete');
$delete->confirmation = 'Please go ahead. Demo mode does not really delete data.';

$delete->callback = function (Model $model) {
return 'Only simulating delete when in demo mode.';
};
}
}

/**
* @property string $name @Atk4\Field()
* @property string $sys_name @Atk4\Field()
Expand Down Expand Up @@ -335,14 +357,11 @@ public function importFromFilesystem(string $path, bool $isSub = null): void
if (in_array($fileinfo->getFilename(), ['demos', 'src', 'tests'], true) || $isSub) {
$entity = $this->createEntity();

/*
// Disabling saving file in db
$entity->save([
$this->fieldName()->name => $fileinfo->getFilename(),
$this->fieldName()->is_folder => $fileinfo->isDir(),
$this->fieldName()->type => pathinfo($fileinfo->getFilename(), \PATHINFO_EXTENSION),
]);
*/

if ($fileinfo->isDir()) {
$entity->SubFolder->importFromFilesystem($fileinfo->getPath() . '/' . $fileinfo->getFilename(), true);
Expand Down

0 comments on commit eba9431

Please sign in to comment.