<?php
declare(strict_types=1);
namespace Hitso\Bundle\ContentBundle\EventListener;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\Common\Util\Debug;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Doctrine\ORM\Query;
use Hitso\Bundle\AdminBundle\Exception\Exception;
use Hitso\Bundle\CatalogBundle\Entity\Section\ProductPageSection;
use Hitso\Bundle\CommonBundle\Doctrine\EventSubscriber\LoggableListener;
use Hitso\Bundle\CommonBundle\Doctrine\Manager\ManagerCollection;
use Hitso\Bundle\CommonBundle\Doctrine\Service\EntityAuthorizationChecker;
use Hitso\Bundle\CommonBundle\Doctrine\Service\LoggableReverter;
use Hitso\Bundle\CommonBundle\Entity\ChangeLog;
use Hitso\Bundle\ContentBundle\Entity\Article;
use Hitso\Bundle\ContentBundle\Entity\Content;
use Hitso\Bundle\ContentBundle\Entity\ContentInterface;
use Hitso\Bundle\ContentBundle\Entity\Elements\Element;
use Hitso\Bundle\ContentBundle\Entity\Elements\Image;
use Hitso\Bundle\ContentBundle\Entity\Page;
use Hitso\Bundle\FileManagerBundle\Entity\File;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class ChangeLogEventListener
*
* @package Hitso\Bundle\ContentBundle\EventListener
*/
class ChangeLogEventListener implements EventSubscriberInterface
{
/**
* @var ManagerCollection
*/
protected $managerCollection;
/**
* @var EntityManagerInterface
*/
protected $doctrine;
/**
* ProductChangeLogEventListener constructor.
*
* @param ManagerCollection $managerCollection
*/
public function __construct(
EntityManagerInterface $doctrine,
ManagerCollection $managerCollection
) {
$this->doctrine = $doctrine;
$this->managerCollection = $managerCollection;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'prePersist',
];
}
/**
* @param LifecycleEventArgs $eventArgs
*
* @throws \Exception
*/
public function prePersist(LifecycleEventArgs $eventArgs): void
{
/**
* @var ChangeLog
*/
$changeLogEntity = $eventArgs->getEntity();
{
if ($changeLogEntity instanceof ChangeLog && $changeLogEntity->getObjectParent() === Content::class) {
/**
* @var Content
*/
$content = $eventArgs->getEntityManager()
->getRepository($changeLogEntity->getObjectClass())
->find((int) $changeLogEntity->getObjectId());
if ($content) {
$data = $changeLogEntity->getData();
$this->prepareElements($content, $data);
$changeLogEntity->setData($data);
}
}
}
}
/**
* @param $content
* @param array|null $data
*/
private function prepareElements(
$content,
?array &$data
) {
$elements = $content->getElements();
$class = ClassUtils::getRealClass(get_class($content));
$elements = $this->managerCollection->get(Element::class)->getRepository()->findBy([
'content' => $content,
'deleted' => false,
]);
$data['elements'] = [];
foreach ($elements as $element) {
if ($element instanceof Image) {
$data['elements'][] = [
'class' => Image::class,
'id' => $element->getId(),
'file' => $element->getImage() ? $element->getImage()->getId() : null
];
// $element = $this->managerCollection->get(Image::class)->getRepository()->find($element->getId());
//// $element->setImage(
//// $this->managerCollection->get(File::class)->getRepository()
//// ->find($element->getImage()->getId())
//// );
//// $element = $this->managerCollection->get(Image::class)
//// ->getRepository()
//// ->createQueryBuilder('q')
//// ->select()
//// ->where('q.id = :id')
//// ->setParameter('id', $element->getId())
//// ->setMaxResults(1)
//// ->getQuery()
//// ->setHint(Query::HINT_REFRESH, true)
//// ->getOneOrNullResult();
// $element->setImage(
// $element->getImage()
// );
//
//
} else {
$data['elements'][] = $element;
}
}
$data['elements'] = array_reverse($data['elements']);
}
}