- Added a new automated Form save
This commit is contained in:
parent
899dd1a4d4
commit
2585bc3315
17
meta/i18n/fr/lean.api.json
Normal file
17
meta/i18n/fr/lean.api.json
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"form": {
|
||||||
|
"save": {
|
||||||
|
"error": {
|
||||||
|
"entity": "Une propriété $entity est nécessaire dans ce form",
|
||||||
|
"save": "Une erreur est survenue en tenant de sauvegarder les données",
|
||||||
|
"pdo": "Une erreur est survenue : '{$error}'"
|
||||||
|
},
|
||||||
|
"success": {
|
||||||
|
"save": "Sauvegarde effectué avec succès"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"delete": {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -12,5 +12,6 @@ class EntityField
|
|||||||
public ?int $maxLength = null,
|
public ?int $maxLength = null,
|
||||||
public ?string $regexFormat = null,
|
public ?string $regexFormat = null,
|
||||||
public mixed $example = null,
|
public mixed $example = null,
|
||||||
|
public ?string $field = null,
|
||||||
) {}
|
) {}
|
||||||
}
|
}
|
43
src/Form/Delete.php
Normal file
43
src/Form/Delete.php
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Lean\Api\Form;
|
||||||
|
|
||||||
|
use CSSLSJ\ExamenFga\Api\{Lib};
|
||||||
|
use Picea\Ui\Method\{FormContextInterface};
|
||||||
|
use Ulmus\Entity\EntityInterface;
|
||||||
|
|
||||||
|
class Delete implements \Picea\Ui\Method\FormInterface {
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
public EntityInterface $entity,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function getEntity() : EntityInterface
|
||||||
|
{
|
||||||
|
return $this->entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function initialize(FormContextInterface $context) : void {}
|
||||||
|
|
||||||
|
public function validate(FormContextInterface $context) : bool
|
||||||
|
{
|
||||||
|
return $context->valid();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function execute(FormContextInterface $context) : void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
if ( $this->getEntity()::repository()->destroy($this->getEntity()) ) {
|
||||||
|
$context->pushMessage(Lib\Message::generateSuccess(
|
||||||
|
"Suppression effectué avec succès"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new \InvalidArgumentException("Suppression impossible; une erreur semble survenue...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(\Throwable $ex) {
|
||||||
|
throw new \ErrorException(sprintf("Une erreur est survenue : '%s'", $ex->getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
97
src/Form/Save.php
Normal file
97
src/Form/Save.php
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Lean\Api\Form;
|
||||||
|
|
||||||
|
use CSSLSJ\ExamenFga\Api\{Form\Location\SessionContext, Lib, Entity};
|
||||||
|
use Picea\Ui\Method\{ FormContextInterface, Message\ErrorMessage };
|
||||||
|
|
||||||
|
use Ulmus\Attribute\Property\Field;
|
||||||
|
use Lean\Api\Attribute\EntityField;
|
||||||
|
use Lean\LanguageHandler;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
|
use Ulmus\Entity\EntityInterface;
|
||||||
|
use Ulmus\Entity\Field\Datetime;
|
||||||
|
use function CSSLSJ\ExamenFga\Api\View\{ lang };
|
||||||
|
|
||||||
|
abstract class Save implements \Picea\Ui\Method\FormInterface {
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
protected LanguageHandler $languageHandler,
|
||||||
|
# public EntityInterface $entity,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function getEntity() : EntityInterface
|
||||||
|
{
|
||||||
|
if (! isset($this->entity)) {
|
||||||
|
throw new \InvalidArgumentException($this->lang("lean.api.form.save.error.entity"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function validate(FormContextInterface $context) : bool
|
||||||
|
{
|
||||||
|
# Context is validating inputs
|
||||||
|
return $context->valid($this->getEntity()->isLoaded() ? $this->getEntity() : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function execute(FormContextInterface $context) : void
|
||||||
|
{
|
||||||
|
$entity = $this->getEntity();
|
||||||
|
|
||||||
|
if ($entity->isLoaded() ) {
|
||||||
|
$entity->updatedAt = new Datetime();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$entity->createdAt = new Datetime();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->assignContextToEntity($context);
|
||||||
|
|
||||||
|
if ( $entity::repository()->save($entity) ) {
|
||||||
|
$context->pushMessage(Lib\Message::generateSuccess(
|
||||||
|
$this->lang('lean.api.form.save.success.entity')
|
||||||
|
));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new \InvalidArgumentException($this->lang('lean.api.form.save.error.entity'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(\Throwable $ex) {
|
||||||
|
throw new \ErrorException($this->lang('lean.api.form.save.error.pdo', [ 'error' => $ex->getMessage() ]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function lang(string $key, array $variables = [])
|
||||||
|
{
|
||||||
|
return $this->languageHandler->languageFromKey($key, $variables);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function assignContextToEntity(FormContextInterface $context) : void
|
||||||
|
{
|
||||||
|
$entity = $this->getEntity();
|
||||||
|
|
||||||
|
foreach($entity::resolveEntity()->fieldList() as $key => $property) {
|
||||||
|
$field = $property->getAttribute(Field::class)->object;
|
||||||
|
|
||||||
|
if (! $field->readonly) {
|
||||||
|
$apiField = $property->getAttribute(EntityField::class)->object ?? null;
|
||||||
|
|
||||||
|
if ($apiField) {
|
||||||
|
if ($apiField->field) {
|
||||||
|
if ( isset($context->{$apiField->field}) ) {
|
||||||
|
$entity->$key = $context->{$apiField->field};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if ( isset($context->{$key}) ) {
|
||||||
|
$entity->$key = $context->{$key};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user