<?php
namespace App\Entity;
use App\Enum\EntityLog\Type;
use App\Library\Utils\Dev\ReflectionUtils\ReflectionUtils;
use App\Repository\EntityLogRepository;
use App\Service\EntityLog\EntityLogService;
use App\Service\EntityService;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=EntityLogRepository::class)
*/
class EntityLog
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $entityClass;
/**
* @ORM\Column(type="enum", options={"values"="entity_log_enum", "default"="update"})
*/
private $type = Type::TYPE_UPDATE;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $fieldName;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $oldValue;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $newValue;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $entityId;
/**
* @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(nullable=true)
*/
private $editedBy;
/**
* @ORM\ManyToOne(targetEntity=Student::class)
* @ORM\JoinColumn(nullable=true)
*/
private $student;
/**
* @ORM\ManyToOne(targetEntity=Person::class)
* @ORM\JoinColumn(nullable=true)
*/
private $person;
/**
* @ORM\ManyToOne(targetEntity=UserSettings::class)
* @ORM\JoinColumn(nullable=true)
*/
private $userSettings;
/**
* @ORM\ManyToOne(targetEntity=Candidate::class)
* @ORM\JoinColumn(nullable=true)
*/
private $candidate;
/**
* @ORM\ManyToOne(targetEntity=Payment::class)
* @ORM\JoinColumn(nullable=true)
*/
private $payment;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $data;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $oldData;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $entityText;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $fields;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $updatedAt;
public function __construct()
{
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function getEntityClass($full = false): ?string
{
return ($full ? "App\\Entity\\" : "") . $this->entityClass;
}
public function getEntityServiceClass(): ?string
{
$result = "App\\Service\\" . $this->getEntityClass() . "Service";
if (!class_exists($result)) {
$result = "App\\Service\\" . $this->getEntityClass()
. "\\" . $this->getEntityClass() . "Service";
if (!class_exists($result)) {
$result = null;
}
}
return $result;
}
public function setEntityClass(string $entityClass): self
{
$this->entityClass = $entityClass;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function isCreateType(): bool
{
return $this->type === Type::TYPE_CREATE;
}
public function isUpdateType(): bool
{
return $this->type === Type::TYPE_UPDATE;
}
public function isDeleteType(): bool
{
return $this->type === Type::TYPE_DELETE;
}
public function isRecoverType(): bool
{
return $this->type === Type::TYPE_RECOVER;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
public function getCreatedAt(): ?\DateTime
{
return $this->createdAt;
}
public function setCreatedAt(\DateTime $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getFieldName(): ?string
{
return $this->fieldName;
}
public function setFieldName(?string $fieldName): self
{
$this->fieldName = $fieldName;
return $this;
}
public function getOldValue(): ?string
{
return $this->oldValue;
}
public function setOldValue(?string $oldValue): self
{
$this->oldValue = $oldValue;
return $this;
}
public function getNewValue(): ?string
{
return $this->newValue;
}
public function setNewValue(?string $newValue): self
{
$this->newValue = $newValue;
return $this;
}
public function getEntityId(): ?int
{
return $this->entityId;
}
public function setEntityId(?int $entityId): self
{
$this->entityId = $entityId;
return $this;
}
public function getEditedBy(): ?User
{
return $this->editedBy;
}
public function setEditedBy(?User $editedBy): self
{
$this->editedBy = $editedBy;
return $this;
}
public function getStudent(): ?Student
{
return $this->student;
}
public function setStudent(?Student $student): self
{
$this->student = $student;
return $this;
}
public function getPerson(): ?Person
{
return $this->person;
}
public function setPerson(?Person $person): self
{
$this->person = $person;
return $this;
}
public function getUserSettings(): ?UserSettings
{
return $this->userSettings;
}
public function setUserSettings(?UserSettings $userSettings): self
{
$this->userSettings = $userSettings;
return $this;
}
public function getCandidate(): ?Candidate
{
return $this->candidate;
}
public function setCandidate(?Candidate $candidate): self
{
$this->candidate = $candidate;
return $this;
}
public function getPayment(): ?Payment
{
return $this->payment;
}
public function setPayment(?Payment $payment): self
{
$this->payment = $payment;
return $this;
}
public function getTypeText(): string
{
return Type::getText($this->type);
}
public function getTypeCss(): string
{
return Type::getCssClass($this->type);
}
public function getEntityClassCss(): string
{
return EntityService::getEntityClassCssClass("App\\Entity\\{$this->entityClass}");
}
public function getEntityClassText(): string
{
return EntityService::getEntityClassText($this->entityClass);
}
public function getEntity()
{
return $this->getPerson() ?? $this->getStudent() ?? $this->getUserSettings() ?? $this->getCandidate() ?? $this->getPayment();
}
public function setEntity($entity): self
{
if ($entity instanceof Person) {
return $this->setPerson($entity);
} elseif ($entity instanceof Student) {
return $this->setStudent($entity);
} elseif ($entity instanceof UserSettings) {
return $this->setUserSettings($entity);
} elseif ($entity instanceof Candidate) {
return $this->setCandidate($entity);
} elseif ($entity instanceof Payment) {
return $this->setPayment($entity);
}
return $this;
}
public function getEntityFieldText(): ?string
{
$entityClass = "App\\Entity\\{$this->entityClass}";
$text = null;
$entity = $this->getEntity();
if ($entity && method_exists($entityClass, 'getFieldText')) {
$text = $entityClass::getFieldText($this->fieldName);
}
return $text ?: $this->fieldName;
}
public function getOldFieldValueText(): ?string
{
$oldValue = $this->getOldValue();
return $this->getFieldValueText($oldValue);
}
public function getNewFieldValueText(): ?string
{
$newValue = $this->getNewValue();
$result = $this->getFieldValueText($newValue);
return $result;
}
private function getFieldValueText($val)
{
return EntityLogService::entityFieldValueText(
$this->getEntity(), $this->fieldName, $val
);
}
public function getData(): ?array
{
return json_decode($this->data, true);
}
public function setData(?array $data): self
{
$this->data = json_encode($data,
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
return $this;
}
public function addData(array $data, $addOnlyNew = false): self
{
$currentData = $this->getData() ?: [];
if ($addOnlyNew) {
foreach ($data as $key => $value) {
if (!array_key_exists($key, $currentData)) {
$currentData[$key] = $value;
}
}
} else {
$data = $this->getDataWithKeyPostfix($data, count($currentData));
$newData = array_merge($currentData, $data);
}
return $this->setData($newData);
}
private function getDataWithKeyPostfix(array $data, int $currentDataCount): array
{
$dataWithKeyPostfix = [];
$postfix = $currentDataCount;
foreach ($data as $key => $value) {
$dataWithKeyPostfix["{$key}_$postfix"] = $value;
}
return $dataWithKeyPostfix;
}
public function getOldData(): ?array
{
return json_decode($this->oldData, true);
}
public function setOldData(?array $oldData): self
{
$this->oldData = json_encode($oldData,
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
return $this;
}
public function addOldData(array $oldData, $addOnlyNew = false): self
{
$currentOldData = $this->getOldData() ?: [];
if ($addOnlyNew) {
foreach ($oldData as $key => $value) {
if (!array_key_exists($key, $currentOldData)) {
$currentOldData[$key] = $value;
}
}
return $this->setOldData($currentOldData);
} else {
$oldData = $this->getDataWithKeyPostfix($oldData, count($currentOldData));
$newOldData = array_merge($currentOldData, $oldData);
return $this->setOldData($newOldData);
}
}
public function setEntityText(?string $entityText): self
{
$this->entityText = $entityText;
return $this;
}
public function getEntityText(): ?string
{
return $this->entityText;
}
public function getFields(): ?string
{
return $this->fields;
}
public function setFields(?string $fields): self
{
$this->fields = $fields;
return $this;
}
public function addFieldsIfNotExists(array $fields): self
{
$currentFields = $this->getFields() ? explode(",", $this->getFields()) : [];
$newFields = array_unique(array_merge($currentFields, $fields));
return $this->setFields(implode(",", $newFields));
}
public function getUpdatedAt($real = false): ?\DateTimeInterface
{
if (!$real && !$this->updatedAt) {
return $this->createdAt;
}
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function setDataByKey(string $key, $value): self
{
$data = $this->getData() ?: [];
$data[$key] = $value;
return $this->setData($data);
}
public function setOldDataByKey(string $key, $value): self
{
$oldData = $this->getOldData() ?: [];
$oldData[$key] = $value;
return $this->setOldData($oldData);
}
public function updateData(array $data): self
{
foreach ($data as $key => $value) {
$this->setDataByKey($key, $value);
}
return $this;
}
public function updateOldData(array $oldData): self
{
foreach ($oldData as $key => $value) {
$this->setOldDataByKey($key, $value);
}
return $this;
}
}