<?php
namespace App\EventSubscriber;
use App\Entity\Page;
use App\Entity\PageContenu;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PageSubscriber implements EventSubscriberInterface
{
private $entityManager;
private ManagerRegistry $manager;
public function __construct(
ManagerRegistry $manager,
EntityManagerInterface $entityManager,
LoggerInterface $logger
) {
$this->manager = $manager;
$this->entityManager = $entityManager;
$this->logger = $logger;
}
public static function getSubscribedEvents()
{
return [
AfterEntityPersistedEvent::class => ['afterAdd'],
AfterEntityUpdatedEvent::class => ['afterUpdate']
];
}
public function afterAdd(AfterEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if ($entity instanceof PageContenu) {
if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
$idSetImport = date('YmdHis') . microtime(true) . 'pageContenu' . $entity->getId() . "" . $entity->getPage()->getId() . $entity->getPage()->getProjet()->getId();
$entity->setIdImport(str_replace(".", "", $idSetImport));
}
$entity->setIdImportPage($entity->getPage()->getIdImport());
$this->entityManager->flush();
} else if ($entity instanceof Page) {
if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
$idSetImport = date('YmdHis') . microtime(true) . 'page' . $entity->getId() . "" . $entity->getProjet()->getId();
$entity->setIdImport(str_replace(".", "", $idSetImport));
}
if ($entity->getPermalink() == null || $entity->getPermalink() == '') {
$time = new \DateTime();
$year = $time->format('y');
$month = $time->format('m');
$day = $time->format('d');
$hour = $time->format('H');
$min = $time->format('i');
$sec = $time->format('s');
$idSetImport = $year . $month . $day . $entity->getId();
$entity->setPermalink(str_replace(".", "", $idSetImport));
}
if ($entity->getParent() != null) {
$entity->setIdImportPageLiee($entity->getParent()->getIdImport());
}
$niveau = 1;
$menu2 = $entity->getParent();
if ($menu2 != null) {
$menu3 = $menu2->getParent();
$niveau = 2;
if ($menu3 != null) {
$niveau = 3;
$menu4 = $menu3->getParent();
if ($menu4 != null) {
$niveau = 4;
}
}
}
$entity->setNiveau($niveau);
$this->entityManager->flush();
} else {
return;
}
}
public function afterUpdate(AfterEntityUpdatedEvent $event)
{
$entity = $event->getEntityInstance();
if ($entity instanceof Page) {
if ($entity->getParent() != null) {
$entity->setIdImportPageLiee($entity->getParent()->getIdImport());
}
if ($entity->getPermalink() == null || $entity->getPermalink() == '') {
$time = new \DateTime();
$year = $time->format('y');
$month = $time->format('m');
$day = $time->format('d');
$hour = $time->format('H');
$min = $time->format('i');
$sec = $time->format('s');
$idSetImport = $year . $month . $day . $entity->getId();
$entity->setPermalink(str_replace(".", "", $idSetImport));
}
if ($entity->getParent() != null) {
$menu2 = $entity->getParent();
if ($menu2 != null) {
$menu3 = $menu2->getParent();
if ($menu3 != null) {
if ($entity == $menu3) {
$menu2->setParent(null);
$nniv = 1;
if ($menu2->getNiveau() > 1) {
$nniv = $menu2->getNiveau() - 1;
}
$menu2->setNiveau($nniv);
}
}
}
}
$niveau = 1;
$menu2 = $entity->getParent();
if ($menu2 != null) {
$menu3 = $menu2->getParent();
$niveau = 2;
if ($menu3 != null) {
$niveau = 3;
$menu4 = $menu3->getParent();
if ($menu4 != null) {
$niveau = 4;
}
}
}
$entity->setNiveau($niveau);
$this->entityManager->flush();
} else if ($entity instanceof PageContenu) {
if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
$idSetImport = date('YmdHis') . microtime(true) . 'pageContenu' . $entity->getId() . "" . $entity->getPage()->getId() . $entity->getPage()->getProjet()->getId();
$entity->setIdImport(str_replace(".", "", $idSetImport));
}
$entity->setIdImportPage($entity->getPage()->getIdImport());
$this->entityManager->flush();
} else {
return;
}
}
}