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

Fix CardDeck action /w entity #1845

Merged
merged 15 commits into from
Aug 31, 2022
2 changes: 1 addition & 1 deletion demos/collection/card-deck.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@
'country' => ['required' => true, 'ui' => ['form' => [Form\Control\Lookup::class, 'model' => new Country($app->db), 'placeholder' => 'Please select a country.']]],
];

$deck = CardDeck::addTo($app, ['noRecordScopeActions' => ['request_info'], 'singleScopeActions' => ['book']]);
$deck = CardDeck::addTo($app);
mvorisek marked this conversation as resolved.
Show resolved Hide resolved

$deck->setModel($countries, ['Cost'], [$countries->fieldName()->iso, $countries->fieldName()->iso3]);
28 changes: 22 additions & 6 deletions demos/init-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,36 @@ public function atomic(\Closure $fx)
return $res;
}

protected function wrapUserActionCallbackPreventModification(Model\UserAction $action, \Closure $outputCallback): void
{
$originalCallback = $action->callback;
$action->callback = function (Model $model, ...$args) use ($action, $originalCallback, $outputCallback) {
$callbackBackup = $action->callback;
try {
$action->callback = $originalCallback;
$action->execute($model, ...$args);
} finally {
$action->callback = $callbackBackup;
}

return $outputCallback($model, ...$args);
};
}

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

$this->getUserAction('edit')->callback = function (Model $model) {
$this->wrapUserActionCallbackPreventModification($this->getUserAction('edit'), 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) {
$this->wrapUserActionCallbackPreventModification($this->getUserAction('delete'), function (Model $model) {
return 'Only simulating delete when in demo mode.';
};
});
}
}

Expand Down
15 changes: 6 additions & 9 deletions src/CardDeck.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ protected function jsExecute($return, Model\UserAction $action = null)
} elseif ($return instanceof Model) {
$msg = $return->isLoaded() ? $this->saveMsg : ($action->appliesTo === Model\UserAction::APPLIES_TO_SINGLE_RECORD ? $this->deleteMsg : $this->defaultMsg);

return $this->jsModelReturn($action, $msg);
return $this->jsModelReturn($action, $return, $msg);
}

return $this->getNotifier($this->defaultMsg, $action);
Expand All @@ -255,11 +255,11 @@ protected function getNotifier(string $msg = null, Model\UserAction $action = nu
/**
* Js expression return when action afterHook executor return a Model.
*/
protected function jsModelReturn(Model\UserAction $action = null, string $msg = 'Done!'): array
protected function jsModelReturn(Model\UserAction $action = null, Model $entity, string $msg = 'Done!'): array
{
$js = [];
$js[] = $this->getNotifier($msg, $action);
if ($action->getModel()->isLoaded() && $card = $this->findCard($action->getModel())) {
if ($entity->isLoaded() && $card = $this->findCard($entity)) {
$js[] = $card->jsReload($this->getReloadArgs());
} else {
$js[] = $this->container->jsReload($this->getReloadArgs());
Expand All @@ -281,21 +281,18 @@ protected function jsModelReturn(Model\UserAction $action = null, string $msg =
*
* @return mixed
*/
protected function findCard(Model $model)
protected function findCard(Model $entity)
{
$mapResults = function ($a) use ($model) {
return $a[$model->idField];
};
$deck = [];
foreach ($this->cardHolder->elements as $element) {
if ($element instanceof $this->card) {
$deck[$element->model->getId()] = $element;
}
}

if (in_array($model->getId(), array_map($mapResults, $model->export([$model->idField])), true)) {
if ($entity->getModel()->tryLoad($entity->getId()) !== null) {
// might be in result set but not in deck, for example when adding a card.
return $deck[$model->getId()] ?? null;
return $deck[$entity->getId()] ?? null;
}

return null;
Expand Down