<?php
namespace App\Service\EntityLog;
use App\Entity\EntityLog;
use App\Entity\Person;
use App\Enum\EntityLog\Type;
use App\Library\Utils\Dev\ReflectionUtils\ReflectionUtils;
use App\Library\Utils\Other\Other;
use App\Library\Utils\StringUtils;
use App\Service\BaseEntityService;
use App\Service\ServiceRetriever;
use App\Service\User\UserService;
use Doctrine\ORM\EntityManagerInterface;
class EntityLogService extends BaseEntityService
{
/**
* @var UserService
*/
private $userService;
/**
* @var ReflectionUtils
*/
private $reflectionUtils;
public function __construct(EntityManagerInterface $em, UserService $userService,
ServiceRetriever $serviceRetriever, ReflectionUtils $reflectionUtils)
{
parent::__construct($em, null, $serviceRetriever);
$this->initialize(EntityLog::class);
$this->userService = $userService;
$this->reflectionUtils = $reflectionUtils;
}
private static $formTypeFieldsCache = [];
public function getEntityFormTypeFields(string $entityClass): array
{
$entityClass = str_replace("Proxies\\__CG__\\", "", $entityClass);
if (!isset(self::$formTypeFieldsCache[$entityClass])) {
self::$formTypeFieldsCache[$entityClass] =
ReflectionUtils::getFormTypeFieldNames($entityClass, $this->serviceRetriever);
}
return self::$formTypeFieldsCache[$entityClass];
}
public function getEntityFormFieldLabel(string $entityClass, ?string $fieldName): ?string
{
if (!$fieldName) {
return null;
}
$entityFormFields = $this->getEntityFormTypeFields($entityClass);
$result = $entityFormFields[$fieldName] ?? $fieldName;
return $result;
}
public function createByListenerChangesSet(array $changes, string $entityClass, $entity, string $type)
{
try {
$shortClassName = (new \ReflectionClass($entityClass))->getShortName();
$user = $this->userService->getLoggedInUser();
$newValues = [];
$oldValues = [];
foreach ($changes as $fieldName => $change) {
$oldValues[$fieldName] = $change[0];
$newValues[$fieldName] = $change[1];
}
$data = $this->getEntityDataByFormFieldsData($entity, $newValues);
if ($changes && ReflectionUtils::getClassShortName($entityClass) == ReflectionUtils::getClassShortName(Person::class)) {
// dd($changes);
}
$oldData = $this->getEntityDataByFormFieldsData($entity, $oldValues);
$item = (new EntityLog())
->setEntityClass($shortClassName)
->setEntityId($entity->getId())
->setType($type)
->setEditedBy($user)
// ->setEntity($entity)
->setData($data)
->setOldData($oldData);
$this->em->persist($item);
$this->em->flush();
} catch (\Throwable $exception) {
dd(__FILE__ . ":" . __LINE__ . ": ", Other::getBacktrace($exception->getTrace()),$exception);
}
// foreach ($changes as $fieldName => $change) {
// $entityFormFields = $this->getEntityFormTypeFields($entityClass);
// if (!ReflectionUtils::hasFormTypeField($entityClass, $fieldName,
// $entityFormFields, $this->serviceRetriever)) {
// continue;
// }
//
// try {
// $oldValue = self::object2string($change[0]);
// } catch (\Exception $e) {
// $oldValue = "Error during old value conversion: " . $e->getMessage();
// }
//
// try {
// $newValue = self::object2string($change[1]);
// } catch (\Exception $e) {
// $newValue = "Error during new value conversion: " . $e->getMessage();
// }
//
// $item = (new EntityLog())
// ->setEntityClass($shortClassName)
// ->setEntityId($entity->getId())
// ->setType($type)
// ->setFieldName($fieldName)
// ->setOldValue($oldValue)
// ->setNewValue($newValue)
// ->setEditedBy($user)
// ->setEntity($entity)
// ;
//
// $this->em->persist($item);
// $this->em->flush();
// }
}
public function createByListenerDelete(string $entityClass, $entity)
{
$shortClassName = (new \ReflectionClass($entityClass))->getShortName();
$user = $this->userService->getLoggedInUser();
$oldData = $this->getEntityDataByFormFieldsData($entity);
$item = (new EntityLog())
->setEntityClass($shortClassName)
->setEntityId($entity->getId())
->setType(Type::TYPE_DELETE)
->setEditedBy($user)
// ->setEntity($entity)
->setOldData($oldData)
;
$this->em->persist($item);
$this->em->flush();
}
public function getEntityFieldClass(string $entityClass, string $fieldName): ?string
{
return $this->reflectionUtils->getEntityFieldClass($entityClass, $fieldName);
}
public static function object2string($object): ?string
{
$string = null;
if (is_object($object)) {
if (method_exists($object, "getId")) {
$string = $object->getId();
} else {
$string = StringUtils::objectToString($object);
}
} else {
$string = StringUtils::objectToString($object);
}
return $string;
}
public function getEntityText($entity): string
{
if (method_exists($entity, "getName")) {
return $entity->getName();
} elseif (method_exists($entity, "getTitle")) {
return $entity->getTitle();
} else {
return self::object2string($entity);
}
}
public function getEntityDataByFormFieldsData($entity, array $entityData = null): array
{
$formFieldsData = $this->getEntityFormTypeFields(get_class($entity));
$formFieldsData = ReflectionUtils::getEntityDataByFormFieldsData($entity, $formFieldsData,
$entityData);
foreach ($formFieldsData as $fieldName => &$fieldData) {
$fieldData['value'] = self::entityFieldValueText($entity, $fieldName, self::object2string($fieldData['value']));
}
return $formFieldsData;
}
public static function entityFieldValueText($entity, $fieldName, $val)
{
switch ($val) {
case "null":
return "Не задано";
case "true":
return "☑️ Галочка установлена";
case "false":
return "<span style='opacity: 50%'>☑️</span> Галочка не установлена";
default:
$getter = "get" . ucfirst($fieldName);
if ($entity && method_exists($entity, $getter)) {
$entityFieldVal = $entity->$getter();
if (is_object($entityFieldVal)) {
if (method_exists($entityFieldVal, "__toString")) {
return $entityFieldVal->__toString();
} elseif (method_exists($entityFieldVal, "getTitle")) {
return $entityFieldVal->getTitle();
} elseif (method_exists($entityFieldVal, "getName")) {
return $entityFieldVal->getName();
}
} elseif ($enumFieldText = ReflectionUtils::getEntityEnumFieldText($entity, $fieldName, $val)) {
$css = ReflectionUtils::getEntityFieldCss($entity, $fieldName);
$val = $enumFieldText;
// if ($css) {
// $val = "<span style='$css'>$val</span>";
// }
}
}
return $val;
}
}
}