94 lines
2.7 KiB
PHP
94 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace Lean\Api\Form\Security;
|
|
|
|
use CSSLSJ\TransportScolaire\Api\{Entity};
|
|
use Lean\Api\Attribute\ContextField;
|
|
use Lean\Api\Lib\FormContext;
|
|
use Ulmus\Attribute\IndexTypeEnum;
|
|
use Ulmus\Entity\EntityInterface;
|
|
use Ulmus\Event\Entity\DatasetFill;
|
|
use Ulmus\Ulmus;
|
|
|
|
#[ContextField(description: "")]
|
|
class ActionLogContext extends FormContext
|
|
{
|
|
protected EntityInterface $entity;
|
|
|
|
#[ContextField(mandatory: true)]
|
|
public string $event;
|
|
|
|
#[ContextField(mandatory: true)]
|
|
public string $method;
|
|
|
|
#[ContextField(mandatory: true)]
|
|
public int|array $object_id;
|
|
|
|
#[ContextField(mandatory: true)]
|
|
public string $object_type;
|
|
|
|
#[ContextField]
|
|
public ? int $user_id;
|
|
|
|
#[ContextField]
|
|
public string $user_type;
|
|
|
|
#[ContextField]
|
|
public string $description;
|
|
|
|
#[ContextField]
|
|
public array $data = [];
|
|
|
|
private DatasetFill $datasetFillEvent;
|
|
|
|
public function attachEntity(EntityInterface $entity) : void
|
|
{
|
|
$this->entity = $entity;
|
|
|
|
$this->datasetFillEvent = new class() implements \Ulmus\Event\Entity\DatasetFill
|
|
{
|
|
public readonly array $data;
|
|
|
|
public function execute(\Generator $generator, \Ulmus\Entity\EntityInterface $entity): void
|
|
{
|
|
$this->data = $entity::repository()->generateDatasetDiff($entity);
|
|
}
|
|
};
|
|
|
|
$this->entity->eventRegister($this->datasetFillEvent);
|
|
}
|
|
|
|
public function initializeEntity(\Lean\Api\Entity\Security\ApiActionLog $actionLog) : void
|
|
{
|
|
if (isset($this->entity) && $this->entity->isLoaded()) {
|
|
$this->object_type = $this->entity::class;
|
|
|
|
if (isset($this->datasetFillEvent->data)) {
|
|
$this->data = $this->datasetFillEvent->data;
|
|
}
|
|
|
|
$pk = $this->entity::resolveEntity()->getPrimaryKeyField();
|
|
|
|
if ($pk) {
|
|
$pk = array_key_first($this->entity::resolveEntity()->getPrimaryKeyField());
|
|
|
|
if (isset($this->entity->$pk)) {
|
|
$this->object_id = $this->entity->$pk;
|
|
}
|
|
}
|
|
elseif ($index = $this->entity::resolveEntity()->getCompoundKeyFields()) {
|
|
if ($index->type === IndexTypeEnum::Primary) {
|
|
$this->object_id = array_combine($index->column, array_map(fn($e) => $this->entity->$e ?? null, $index->column));
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
throw new \InvalidArgumentException("An EntityInterface must be provided using the attachEntity() method.");
|
|
}
|
|
}
|
|
|
|
public function valid(? \Lean\Api\Entity\Security\ApiActionLog $actionLog = null): bool
|
|
{
|
|
return isset($this->object_id) && isset($this->object_type);
|
|
}
|
|
} |