src/Entity/EntityLog.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Enum\EntityLog\Type;
  4. use App\Repository\EntityLogRepository;
  5. use App\Service\EntityService;
  6. use Doctrine\ORM\Mapping as ORM;
  7. /**
  8.  * @ORM\Entity(repositoryClass=EntityLogRepository::class)
  9.  */
  10. class EntityLog
  11. {
  12.     /**
  13.      * @ORM\Id
  14.      * @ORM\GeneratedValue
  15.      * @ORM\Column(type="integer")
  16.      */
  17.     private $id;
  18.     /**
  19.      * @ORM\Column(type="string", length=255)
  20.      */
  21.     private $entityClass;
  22.     /**
  23.      * @ORM\Column(type="enum", options={"values"="entity_log_enum", "default"="update"})
  24.      */
  25.     private $type Type::TYPE_UPDATE;
  26.     /**
  27.      * @ORM\Column(type="datetime")
  28.      */
  29.     private $createdAt;
  30.     /**
  31.      * @ORM\Column(type="string", length=255, nullable=true)
  32.      */
  33.     private $fieldName;
  34.     /**
  35.      * @ORM\Column(type="text", nullable=true)
  36.      */
  37.     private $oldValue;
  38.     /**
  39.      * @ORM\Column(type="text", nullable=true)
  40.      */
  41.     private $newValue;
  42.     /**
  43.      * @ORM\Column(type="integer", nullable=true)
  44.      */
  45.     private $entityId;
  46.     /**
  47.      * @ORM\ManyToOne(targetEntity=User::class)
  48.      * @ORM\JoinColumn(nullable=true)
  49.      */
  50.     private $editedBy;
  51.     /**
  52.      * @ORM\ManyToOne(targetEntity=Student::class)
  53.      * @ORM\JoinColumn(nullable=true)
  54.      */
  55.     private $student;
  56.     /**
  57.      * @ORM\ManyToOne(targetEntity=Person::class)
  58.      * @ORM\JoinColumn(nullable=true)
  59.      */
  60.     private $person;
  61.     /**
  62.      * @ORM\ManyToOne(targetEntity=UserSettings::class)
  63.      * @ORM\JoinColumn(nullable=true)
  64.      */
  65.     private $userSettings;
  66.     /**
  67.      * @ORM\ManyToOne(targetEntity=Candidate::class)
  68.      * @ORM\JoinColumn(nullable=true)
  69.      */
  70.     private $candidate;
  71.     /**
  72.      * @ORM\ManyToOne(targetEntity=Payment::class)
  73.      * @ORM\JoinColumn(nullable=true)
  74.      */
  75.     private $payment;
  76.     /**
  77.      * @ORM\Column(type="text", nullable=true)
  78.      */
  79.     private $data;
  80.     /**
  81.      * @ORM\Column(type="text", nullable=true)
  82.      */
  83.     private $oldData;
  84.     /**
  85.      * @ORM\Column(type="string", length=255, nullable=true)
  86.      */
  87.     private $entityText;
  88.     /**
  89.      * @ORM\Column(type="text", nullable=true)
  90.      */
  91.     private $fields;
  92.     /**
  93.      * @ORM\Column(type="datetime", nullable=true)
  94.      */
  95.     private $updatedAt;
  96.     /**
  97.      * Has higher priority than editedBy.
  98.      * @ORM\Column(type="string", length=255, nullable=true)
  99.      */
  100.     private $editedByText;
  101.     public function __construct()
  102.     {
  103.         $this->createdAt = new \DateTime();
  104.         $this->updatedAt = new \DateTime();
  105.     }
  106.     public function getId(): ?int
  107.     {
  108.         return $this->id;
  109.     }
  110.     public function getEntityClass($full false): ?string
  111.     {
  112.         return ($full "App\\Entity\\" "") . $this->entityClass;
  113.     }
  114.     public function getEntityServiceClass(): ?string
  115.     {
  116.         $result "App\\Service\\" $this->getEntityClass() . "Service";
  117.         if (!class_exists($result)) {
  118.             $result "App\\Service\\" $this->getEntityClass()
  119.                 . "\\" $this->getEntityClass() . "Service";
  120.             if (!class_exists($result)) {
  121.                 $result null;
  122.             }
  123.         }
  124.         return $result;
  125.     }
  126.     public function setEntityClass(string $entityClass): self
  127.     {
  128.         $this->entityClass $entityClass;
  129.         return $this;
  130.     }
  131.     public function getType(): ?string
  132.     {
  133.         return $this->type;
  134.     }
  135.     public function isCreateType(): bool
  136.     {
  137.         return $this->type === Type::TYPE_CREATE;
  138.     }
  139.     public function isUpdateType(): bool
  140.     {
  141.         return $this->type === Type::TYPE_UPDATE;
  142.     }
  143.     public function isDeleteType(): bool
  144.     {
  145.         return $this->type === Type::TYPE_DELETE;
  146.     }
  147.     public function isRecoverType(): bool
  148.     {
  149.         return $this->type === Type::TYPE_RECOVER;
  150.     }
  151.     public function setType(string $type): self
  152.     {
  153.         $this->type $type;
  154.         return $this;
  155.     }
  156.     public function getCreatedAt(): ?\DateTime
  157.     {
  158.         return $this->createdAt;
  159.     }
  160.     public function setCreatedAt(\DateTime $createdAt): self
  161.     {
  162.         $this->createdAt $createdAt;
  163.         return $this;
  164.     }
  165.     public function getFieldName(): ?string
  166.     {
  167.         return $this->fieldName;
  168.     }
  169.     public function setFieldName(?string $fieldName): self
  170.     {
  171.         $this->fieldName $fieldName;
  172.         return $this;
  173.     }
  174.     public function getOldValue(): ?string
  175.     {
  176.         return $this->oldValue;
  177.     }
  178.     public function setOldValue(?string $oldValue): self
  179.     {
  180.         $this->oldValue $oldValue;
  181.         return $this;
  182.     }
  183.     public function getNewValue(): ?string
  184.     {
  185.         return $this->newValue;
  186.     }
  187.     public function setNewValue(?string $newValue): self
  188.     {
  189.         $this->newValue $newValue;
  190.         return $this;
  191.     }
  192.     public function getEntityId(): ?int
  193.     {
  194.         return $this->entityId;
  195.     }
  196.     public function setEntityId(?int $entityId): self
  197.     {
  198.         $this->entityId $entityId;
  199.         return $this;
  200.     }
  201.     private function getEditedBy($ignorePriority false): ?User
  202.     {
  203.         if ($this->editedByText && !$ignorePriority) {
  204.             throw new \Exception('editedByText has higher priority');
  205.         }
  206.         return $this->editedBy;
  207.     }
  208.     public function setEditedBy(?User $editedBy): self
  209.     {
  210.         $this->editedBy $editedBy;
  211.         return $this;
  212.     }
  213. //    public function getStudent(): ?Student
  214. //    {
  215. //        return $this->student;
  216. //    }
  217. //
  218. //    public function setStudent(?Student $student): self
  219. //    {
  220. //        $this->student = $student;
  221. //        return $this;
  222. //    }
  223. //    public function getPerson(): ?Person
  224. //    {
  225. //        return $this->person;
  226. //    }
  227. //
  228. //    public function setPerson(?Person $person): self
  229. //    {
  230. //        $this->person = $person;
  231. //        return $this;
  232. //    }
  233. //    public function getUserSettings(): ?UserSettings
  234. //    {
  235. //        return $this->userSettings;
  236. //    }
  237. //
  238. //    public function setUserSettings(?UserSettings $userSettings): self
  239. //    {
  240. //        $this->userSettings = $userSettings;
  241. //        return $this;
  242. //    }
  243. //    public function getCandidate(): ?Candidate
  244. //    {
  245. //        return $this->candidate;
  246. //    }
  247. //    public function setCandidate(?Candidate $candidate): self
  248. //    {
  249. //        $this->candidate = $candidate;
  250. //        return $this;
  251. //    }
  252. //    public function getPayment(): ?Payment
  253. //    {
  254. //        return $this->payment;
  255. //    }
  256. //
  257. //    public function setPayment(?Payment $payment): self
  258. //    {
  259. //        $this->payment = $payment;
  260. //        return $this;
  261. //    }
  262.     public function getTypeText(): string
  263.     {
  264.         return Type::getText($this->type);
  265.     }
  266.     public function getTypeCss(): string
  267.     {
  268.         return Type::getCssClass($this->type);
  269.     }
  270.     public function getEntityClassCss(): string
  271.     {
  272.         return EntityService::getEntityClassCssClass("App\\Entity\\{$this->entityClass}");
  273.     }
  274.     public function getEntityClassText(): string
  275.     {
  276.         return EntityService::getEntityClassText($this->entityClass);
  277.     }
  278. //    public function getEntity()
  279. //    {
  280. //        return $this->getPerson() ?? $this->getStudent() ?? $this->getUserSettings() ?? $this->getCandidate() ?? $this->getPayment();
  281. //    }
  282. //    public function setEntity($entity): self
  283. //    {
  284. //        if ($entity instanceof Person) {
  285. //            return $this->setPerson($entity);
  286. //        } elseif ($entity instanceof Student) {
  287. //            return $this->setStudent($entity);
  288. //        } elseif ($entity instanceof UserSettings) {
  289. //            return $this->setUserSettings($entity);
  290. //        } elseif ($entity instanceof Candidate) {
  291. //            return $this->setCandidate($entity);
  292. //        } elseif ($entity instanceof Payment) {
  293. //            return $this->setPayment($entity);
  294. //        }
  295. //        return $this;
  296. //    }
  297. //    public function getEntityFieldText(): ?string
  298. //    {
  299. //        $entityClass = "App\\Entity\\{$this->entityClass}";
  300. //        $text = null;
  301. //        $entity = $this->getEntity();
  302. //        if ($entity && method_exists($entityClass, 'getFieldText')) {
  303. //            $text = $entityClass::getFieldText($this->fieldName);
  304. //        }
  305. //        return $text ?: $this->fieldName;
  306. //    }
  307. //    public function getOldFieldValueText(): ?string
  308. //    {
  309. //        $oldValue = $this->getOldValue();
  310. //        return $this->getFieldValueText($oldValue);
  311. //    }
  312. //    public function getNewFieldValueText(): ?string
  313. //    {
  314. //        $newValue = $this->getNewValue();
  315. //        $result = $this->getFieldValueText($newValue);
  316. //        return $result;
  317. //    }
  318. //    private function getFieldValueText($val)
  319. //    {
  320. //        return EntityLogService::entityFieldValueText(
  321. //            $this->getEntity(), $this->fieldName, $val
  322. //        );
  323. //    }
  324.     public function getData(): ?array
  325.     {
  326.         return json_decode($this->datatrue);
  327.     }
  328.     public function setData(?array $data): self
  329.     {
  330.         $this->data json_encode($data,
  331.             JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES);
  332.         return $this;
  333.     }
  334.     public function addData(array $data$addOnlyNew false): self
  335.     {
  336.         $currentData $this->getData() ?: [];
  337.         if ($addOnlyNew) {
  338.            foreach ($data as $key => $value) {
  339.                if (!array_key_exists($key$currentData)) {
  340.                    $currentData[$key] = $value;
  341.                }
  342.            }
  343.         } else {
  344.             $data $this->getDataWithKeyPostfix($datacount($currentData));
  345.             $newData array_merge($currentData$data);
  346.         }
  347.         return $this->setData($newData);
  348.     }
  349.     private function getDataWithKeyPostfix(array $dataint $currentDataCount): array
  350.     {
  351.         $dataWithKeyPostfix = [];
  352.         $postfix $currentDataCount;
  353.         foreach ($data as $key => $value) {
  354.             $dataWithKeyPostfix["{$key}_$postfix"] = $value;
  355.         }
  356.         return $dataWithKeyPostfix;
  357.     }
  358.     public function getOldData(): ?array
  359.     {
  360.         return json_decode($this->oldDatatrue);
  361.     }
  362.     public function setOldData(?array $oldData): self
  363.     {
  364.         $this->oldData json_encode($oldData,
  365.             JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES);
  366.         return $this;
  367.     }
  368.     public function addOldData(array $oldData$addOnlyNew false): self
  369.     {
  370.         $currentOldData $this->getOldData() ?: [];
  371.         if ($addOnlyNew) {
  372.             foreach ($oldData as $key => $value) {
  373.                 if (!array_key_exists($key$currentOldData)) {
  374.                     $currentOldData[$key] = $value;
  375.                 }
  376.             }
  377.                 return $this->setOldData($currentOldData);
  378.         } else {
  379.             $oldData $this->getDataWithKeyPostfix($oldDatacount($currentOldData));
  380.             $newOldData array_merge($currentOldData$oldData);
  381.             return $this->setOldData($newOldData);
  382.         }
  383.     }
  384.     public function setEntityText(?string $entityText): self
  385.     {
  386.         $this->entityText $entityText;
  387.         return $this;
  388.     }
  389.     public function getEntityText(): ?string
  390.     {
  391.         return $this->entityText;
  392.     }
  393.     public function getFields(): ?string
  394.     {
  395.         return $this->fields;
  396.     }
  397.     public function setFields(?string $fields): self
  398.     {
  399.         $this->fields $fields;
  400.         return $this;
  401.     }
  402.     public function addFieldsIfNotExists(array $fields): self
  403.     {
  404.         $currentFields $this->getFields() ? explode(","$this->getFields()) : [];
  405.         $newFields array_unique(array_merge($currentFields$fields));
  406.         return $this->setFields(implode(","$newFields));
  407.     }
  408.     public function getUpdatedAt($real false): ?\DateTimeInterface
  409.     {
  410.         if (!$real && !$this->updatedAt) {
  411.             return $this->createdAt;
  412.         }
  413.         return $this->updatedAt;
  414.     }
  415.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  416.     {
  417.         $this->updatedAt $updatedAt;
  418.         return $this;
  419.     }
  420.     public function setDataByKey(string $key$value): self
  421.     {
  422.         $data $this->getData() ?: [];
  423.         $data[$key] = $value;
  424.         return $this->setData($data);
  425.     }
  426.     public function setOldDataByKey(string $key$value): self
  427.     {
  428.         $oldData $this->getOldData() ?: [];
  429.         $oldData[$key] = $value;
  430.         return $this->setOldData($oldData);
  431.     }
  432.     public function updateData(array $data): self
  433.     {
  434.         foreach ($data as $key => $value) {
  435.             $this->setDataByKey($key$value);
  436.         }
  437.         return $this;
  438.     }
  439.     public function updateOldData(array $oldData): self
  440.     {
  441.         foreach ($oldData as $key => $value) {
  442.             $this->setOldDataByKey($key$value);
  443.         }
  444.         return $this;
  445.     }
  446.     public function getEditedByText(): ?string
  447.     {
  448.         return $this->editedByText
  449.             ?: ($this->getEditedBy(true) ? $this->getEditedBy(true)->getName() : null);
  450.     }
  451.     public function setEditedByText(?string $editedByText): self
  452.     {
  453.         $this->editedByText $editedByText;
  454.         return $this;
  455.     }
  456. }