src/Service/EntityLog/EntityLogService.php line 197

Open in your IDE?
  1. <?php
  2. namespace App\Service\EntityLog;
  3. use App\Entity\EntityLog;
  4. use App\Entity\Person;
  5. use App\Enum\EntityLog\Type;
  6. use App\Library\Utils\Dev\ReflectionUtils\ReflectionUtils;
  7. use App\Library\Utils\Other\Other;
  8. use App\Library\Utils\StringUtils;
  9. use App\Service\BaseEntityService;
  10. use App\Service\ServiceRetriever;
  11. use App\Service\User\UserService;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. class EntityLogService extends BaseEntityService
  14. {
  15.     /**
  16.      * @var UserService
  17.      */
  18.     private $userService;
  19.     /**
  20.      * @var ReflectionUtils
  21.      */
  22.     private $reflectionUtils;
  23.     public function __construct(EntityManagerInterface $emUserService $userService,
  24.               ServiceRetriever $serviceRetrieverReflectionUtils $reflectionUtils)
  25.     {
  26.         parent::__construct($emnull$serviceRetriever);
  27.         $this->initialize(EntityLog::class);
  28.         $this->userService $userService;
  29.         $this->reflectionUtils $reflectionUtils;
  30.     }
  31.     private static $formTypeFieldsCache = [];
  32.     public function getEntityFormTypeFields(string $entityClass): array
  33.     {
  34.         $entityClass str_replace("Proxies\\__CG__\\"""$entityClass);
  35.         if (!isset(self::$formTypeFieldsCache[$entityClass])) {
  36.             self::$formTypeFieldsCache[$entityClass] =
  37.                 ReflectionUtils::getFormTypeFieldNames($entityClass$this->serviceRetriever);
  38.         }
  39.         return self::$formTypeFieldsCache[$entityClass];
  40.     }
  41.     public function getEntityFormFieldLabel(string $entityClass, ?string $fieldName): ?string
  42.     {
  43.         if (!$fieldName) {
  44.             return null;
  45.         }
  46.         $entityFormFields $this->getEntityFormTypeFields($entityClass);
  47.         $result $entityFormFields[$fieldName] ?? $fieldName;
  48.         return $result;
  49.     }
  50.     public function createByListenerChangesSet(array $changesstring $entityClass$entitystring $type)
  51.     {
  52.         try {
  53.             $shortClassName = (new \ReflectionClass($entityClass))->getShortName();
  54.             $user $this->userService->getLoggedInUser();
  55.             $newValues = [];
  56.             $oldValues = [];
  57.             foreach ($changes as $fieldName => $change) {
  58.                 $oldValues[$fieldName] = $change[0];
  59.                 $newValues[$fieldName] = $change[1];
  60.             }
  61.             $data $this->getEntityDataByFormFieldsData($entity$newValues);
  62.             if ($changes && ReflectionUtils::getClassShortName($entityClass) == ReflectionUtils::getClassShortName(Person::class)) {
  63. //                dd($changes);
  64.             }
  65.             $oldData $this->getEntityDataByFormFieldsData($entity$oldValues);
  66.             $item = (new EntityLog())
  67.                 ->setEntityClass($shortClassName)
  68.                 ->setEntityId($entity->getId())
  69.                 ->setType($type)
  70.                 ->setEditedBy($user)
  71. //            ->setEntity($entity)
  72.                 ->setData($data)
  73.                 ->setOldData($oldData);
  74.             $this->em->persist($item);
  75.             $this->em->flush();
  76.         } catch (\Throwable $exception) {
  77.             dd(__FILE__ ":" __LINE__ ": "Other::getBacktrace($exception->getTrace()),$exception);
  78.         }
  79. //        foreach ($changes as $fieldName => $change) {
  80. //            $entityFormFields = $this->getEntityFormTypeFields($entityClass);
  81. //            if (!ReflectionUtils::hasFormTypeField($entityClass, $fieldName,
  82. //                $entityFormFields, $this->serviceRetriever)) {
  83. //                continue;
  84. //            }
  85. //
  86. //            try {
  87. //                $oldValue = self::object2string($change[0]);
  88. //            } catch (\Exception $e) {
  89. //                $oldValue = "Error during old value conversion: " . $e->getMessage();
  90. //            }
  91. //
  92. //            try {
  93. //                $newValue = self::object2string($change[1]);
  94. //            } catch (\Exception $e) {
  95. //                $newValue = "Error during new value conversion: " . $e->getMessage();
  96. //            }
  97. //
  98. //            $item = (new EntityLog())
  99. //                ->setEntityClass($shortClassName)
  100. //                ->setEntityId($entity->getId())
  101. //                ->setType($type)
  102. //                ->setFieldName($fieldName)
  103. //                ->setOldValue($oldValue)
  104. //                ->setNewValue($newValue)
  105. //                ->setEditedBy($user)
  106. //                ->setEntity($entity)
  107. //            ;
  108. //
  109. //            $this->em->persist($item);
  110. //            $this->em->flush();
  111. //        }
  112.     }
  113.     public function createByListenerDelete(string $entityClass$entity)
  114.     {
  115.         $shortClassName = (new \ReflectionClass($entityClass))->getShortName();
  116.         $user $this->userService->getLoggedInUser();
  117.         $oldData $this->getEntityDataByFormFieldsData($entity);
  118.         $item = (new EntityLog())
  119.             ->setEntityClass($shortClassName)
  120.             ->setEntityId($entity->getId())
  121.             ->setType(Type::TYPE_DELETE)
  122.             ->setEditedBy($user)
  123. //            ->setEntity($entity)
  124.             ->setOldData($oldData)
  125.         ;
  126.         $this->em->persist($item);
  127.         $this->em->flush();
  128.     }
  129.     public function getEntityFieldClass(string $entityClassstring $fieldName): ?string
  130.     {
  131.         return $this->reflectionUtils->getEntityFieldClass($entityClass$fieldName);
  132.     }
  133.     public static function object2string($object): ?string
  134.     {
  135.         $string null;
  136.         if (is_object($object)) {
  137.             if (method_exists($object"getId")) {
  138.                 $string $object->getId();
  139.             } else {
  140.                 $string StringUtils::objectToString($object);
  141.             }
  142.         } else {
  143.             $string StringUtils::objectToString($object);
  144.         }
  145.         return $string;
  146.     }
  147.     public function getEntityText($entity): string
  148.     {
  149.         if (method_exists($entity"getName")) {
  150.             return $entity->getName();
  151.         } elseif (method_exists($entity"getTitle")) {
  152.             return $entity->getTitle();
  153.         } else {
  154.             return self::object2string($entity);
  155.         }
  156.     }
  157.     public function getEntityDataByFormFieldsData($entity, array $entityData null): array
  158.     {
  159.         $formFieldsData $this->getEntityFormTypeFields(get_class($entity));
  160.         $formFieldsData ReflectionUtils::getEntityDataByFormFieldsData($entity$formFieldsData,
  161.             $entityData);
  162.         foreach ($formFieldsData as $fieldName => &$fieldData) {
  163.             $fieldData['value'] = self::entityFieldValueText($entity$fieldNameself::object2string($fieldData['value']));
  164.         }
  165.         return $formFieldsData;
  166.     }
  167.     public static function entityFieldValueText($entity$fieldName$val)
  168.     {
  169.         switch ($val) {
  170.             case "null":
  171.                 return "Не задано";
  172.             case "true":
  173.                 return "☑️ Галочка установлена";
  174.             case "false":
  175.                 return "<span style='opacity: 50%'>☑️</span> Галочка не установлена";
  176.             default:
  177.                 $getter "get" ucfirst($fieldName);
  178.                 if ($entity && method_exists($entity$getter)) {
  179.                     $entityFieldVal $entity->$getter();
  180.                     if (is_object($entityFieldVal)) {
  181.                         if (method_exists($entityFieldVal"__toString")) {
  182.                             return $entityFieldVal->__toString();
  183.                         } elseif (method_exists($entityFieldVal"getTitle")) {
  184.                             return $entityFieldVal->getTitle();
  185.                         } elseif (method_exists($entityFieldVal"getName")) {
  186.                             return $entityFieldVal->getName();
  187.                         }
  188.                     } elseif ($enumFieldText ReflectionUtils::getEntityEnumFieldText($entity$fieldName$val)) {
  189.                         $css ReflectionUtils::getEntityFieldCss($entity$fieldName);
  190.                         $val $enumFieldText;
  191. //                        if ($css) {
  192. //                            $val = "<span style='$css'>$val</span>";
  193. //                        }
  194.                     }
  195.                 }
  196.                 return $val;
  197.         }
  198.     }
  199. }