src/Hitso/Bundle/ContentBundle/EventListener/ChangeLogEventListener.php line 74

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hitso\Bundle\ContentBundle\EventListener;
  4. use Doctrine\Common\Util\ClassUtils;
  5. use Doctrine\Common\Util\Debug;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Doctrine\ORM\Event\LifecycleEventArgs;
  8. use Doctrine\ORM\Event\PostFlushEventArgs;
  9. use Doctrine\ORM\Query;
  10. use Hitso\Bundle\AdminBundle\Exception\Exception;
  11. use Hitso\Bundle\CatalogBundle\Entity\Section\ProductPageSection;
  12. use Hitso\Bundle\CommonBundle\Doctrine\EventSubscriber\LoggableListener;
  13. use Hitso\Bundle\CommonBundle\Doctrine\Manager\ManagerCollection;
  14. use Hitso\Bundle\CommonBundle\Doctrine\Service\EntityAuthorizationChecker;
  15. use Hitso\Bundle\CommonBundle\Doctrine\Service\LoggableReverter;
  16. use Hitso\Bundle\CommonBundle\Entity\ChangeLog;
  17. use Hitso\Bundle\ContentBundle\Entity\Article;
  18. use Hitso\Bundle\ContentBundle\Entity\Content;
  19. use Hitso\Bundle\ContentBundle\Entity\ContentInterface;
  20. use Hitso\Bundle\ContentBundle\Entity\Elements\Element;
  21. use Hitso\Bundle\ContentBundle\Entity\Elements\Image;
  22. use Hitso\Bundle\ContentBundle\Entity\Page;
  23. use Hitso\Bundle\FileManagerBundle\Entity\File;
  24. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  25. /**
  26.  * Class ChangeLogEventListener
  27.  *
  28.  * @package Hitso\Bundle\ContentBundle\EventListener
  29.  */
  30. class ChangeLogEventListener implements EventSubscriberInterface
  31. {
  32.     /**
  33.      * @var ManagerCollection
  34.      */
  35.     protected $managerCollection;
  36.     /**
  37.      * @var EntityManagerInterface
  38.      */
  39.     protected $doctrine;
  40.     /**
  41.      * ProductChangeLogEventListener constructor.
  42.      *
  43.      * @param ManagerCollection $managerCollection
  44.      */
  45.     public function __construct(
  46.         EntityManagerInterface $doctrine,
  47.         ManagerCollection $managerCollection
  48.     ) {
  49.         $this->doctrine          $doctrine;
  50.         $this->managerCollection $managerCollection;
  51.     }
  52.     /**
  53.      * @return array
  54.      */
  55.     public static function getSubscribedEvents()
  56.     {
  57.         return [
  58.             'prePersist',
  59.         ];
  60.     }
  61.     /**
  62.      * @param LifecycleEventArgs $eventArgs
  63.      *
  64.      * @throws \Exception
  65.      */
  66.     public function prePersist(LifecycleEventArgs $eventArgs): void
  67.     {
  68.         /**
  69.          * @var ChangeLog
  70.          */
  71.         $changeLogEntity $eventArgs->getEntity();
  72.         {
  73.             if ($changeLogEntity instanceof ChangeLog && $changeLogEntity->getObjectParent() === Content::class) {
  74.                 /**
  75.                  * @var Content
  76.                  */
  77.                 $content $eventArgs->getEntityManager()
  78.                     ->getRepository($changeLogEntity->getObjectClass())
  79.                     ->find((int) $changeLogEntity->getObjectId());
  80.                 if ($content) {
  81.                     $data $changeLogEntity->getData();
  82.                     $this->prepareElements($content$data);
  83.                     $changeLogEntity->setData($data);
  84.                 }
  85.             }
  86.         }
  87.     }
  88.     /**
  89.      * @param            $content
  90.      * @param array|null $data
  91.      */
  92.     private function prepareElements(
  93.         $content,
  94.         ?array &$data
  95.     ) {
  96.         $elements         $content->getElements();
  97.         $class            ClassUtils::getRealClass(get_class($content));
  98.         $elements         $this->managerCollection->get(Element::class)->getRepository()->findBy([
  99.             'content' => $content,
  100.             'deleted' => false,
  101.         ]);
  102.         $data['elements'] = [];
  103.         foreach ($elements as $element) {
  104.             if ($element instanceof Image) {
  105.                 $data['elements'][] = [
  106.                     'class' => Image::class,
  107.                     'id' => $element->getId(),
  108.                     'file' => $element->getImage() ? $element->getImage()->getId() : null
  109.                 ];
  110. //                $element = $this->managerCollection->get(Image::class)->getRepository()->find($element->getId());
  111. ////                $element->setImage(
  112. ////                    $this->managerCollection->get(File::class)->getRepository()
  113. ////                        ->find($element->getImage()->getId())
  114. ////                );
  115. ////                $element = $this->managerCollection->get(Image::class)
  116. ////                    ->getRepository()
  117. ////                    ->createQueryBuilder('q')
  118. ////                    ->select()
  119. ////                    ->where('q.id = :id')
  120. ////                    ->setParameter('id', $element->getId())
  121. ////                    ->setMaxResults(1)
  122. ////                    ->getQuery()
  123. ////                    ->setHint(Query::HINT_REFRESH, true)
  124. ////                    ->getOneOrNullResult();
  125. //                $element->setImage(
  126. //                    $element->getImage()
  127. //                );
  128. //
  129. //
  130.             } else {
  131.                 $data['elements'][] = $element;
  132.             }
  133.         }
  134.         $data['elements'] = array_reverse($data['elements']);
  135.     }
  136. }