src/EventSubscriber/PageSubscriber.php line 98

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Page;
  4. use App\Entity\PageContenu;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  8. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  9. use Psr\Log\LoggerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class PageSubscriber implements EventSubscriberInterface
  12. {
  13.     private $entityManager;
  14.     private ManagerRegistry $manager;
  15.     public function __construct(
  16.         ManagerRegistry $manager,
  17.         EntityManagerInterface $entityManager,
  18.         LoggerInterface $logger
  19.     ) {
  20.         $this->manager $manager;
  21.         $this->entityManager $entityManager;
  22.         $this->logger $logger;
  23.     }
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [
  27.             AfterEntityPersistedEvent::class => ['afterAdd'],
  28.             AfterEntityUpdatedEvent::class => ['afterUpdate']
  29.         ];
  30.     }
  31.     public function afterAdd(AfterEntityPersistedEvent $event)
  32.     {
  33.         $entity $event->getEntityInstance();
  34.         if ($entity instanceof PageContenu) {
  35.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  36.                 $idSetImport date('YmdHis') . microtime(true) . 'pageContenu' $entity->getId() . "" $entity->getPage()->getId() . $entity->getPage()->getProjet()->getId();
  37.                 $entity->setIdImport(str_replace("."""$idSetImport));
  38.             }
  39.             $entity->setIdImportPage($entity->getPage()->getIdImport());
  40.             $this->entityManager->flush();
  41.         } else if ($entity instanceof Page) {
  42.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  43.                 $idSetImport date('YmdHis') . microtime(true) . 'page' $entity->getId() . "" $entity->getProjet()->getId();
  44.                 $entity->setIdImport(str_replace("."""$idSetImport));
  45.             }
  46.             if ($entity->getPermalink() == null || $entity->getPermalink() == '') {
  47.                 $time = new \DateTime();
  48.                 $year =  $time->format('y');
  49.                 $month =  $time->format('m');
  50.                 $day =  $time->format('d');
  51.                 $hour =  $time->format('H');
  52.                 $min =  $time->format('i');
  53.                 $sec =  $time->format('s');
  54.                 $idSetImport $year $month $day $entity->getId();
  55.                 $entity->setPermalink(str_replace("."""$idSetImport));
  56.             }
  57.             if ($entity->getParent() != null) {
  58.                 $entity->setIdImportPageLiee($entity->getParent()->getIdImport());
  59.             }
  60.             $niveau 1;
  61.             $menu2 $entity->getParent();
  62.             if ($menu2 != null) {
  63.                 $menu3 $menu2->getParent();
  64.                 $niveau 2;
  65.                 if ($menu3 != null) {
  66.                     $niveau 3;
  67.                     $menu4 $menu3->getParent();
  68.                     if ($menu4 != null) {
  69.                         $niveau 4;
  70.                     }
  71.                 }
  72.             }
  73.             $entity->setNiveau($niveau);
  74.             $this->entityManager->flush();
  75.         } else {
  76.             return;
  77.         }
  78.     }
  79.     public function afterUpdate(AfterEntityUpdatedEvent $event)
  80.     {
  81.         $entity $event->getEntityInstance();
  82.         if ($entity instanceof Page) {
  83.             if ($entity->getParent() != null) {
  84.                 $entity->setIdImportPageLiee($entity->getParent()->getIdImport());
  85.             }
  86.             if ($entity->getPermalink() == null || $entity->getPermalink() == '') {
  87.                 $time = new \DateTime();
  88.                 $year =  $time->format('y');
  89.                 $month =  $time->format('m');
  90.                 $day =  $time->format('d');
  91.                 $hour =  $time->format('H');
  92.                 $min =  $time->format('i');
  93.                 $sec =  $time->format('s');
  94.                 $idSetImport $year $month $day $entity->getId();
  95.                 $entity->setPermalink(str_replace("."""$idSetImport));
  96.             }
  97.             if ($entity->getParent() != null) {
  98.                 $menu2 $entity->getParent();
  99.                 if ($menu2 != null) {
  100.                     $menu3 $menu2->getParent();
  101.                     if ($menu3 != null) {
  102.                         if ($entity == $menu3) {
  103.                             $menu2->setParent(null);
  104.                             $nniv 1;
  105.                             if ($menu2->getNiveau() > 1) {
  106.                                 $nniv $menu2->getNiveau() - 1;
  107.                             }
  108.                             $menu2->setNiveau($nniv);
  109.                         }
  110.                     }
  111.                 }
  112.             }
  113.             $niveau 1;
  114.             $menu2 $entity->getParent();
  115.             if ($menu2 != null) {
  116.                 $menu3 $menu2->getParent();
  117.                 $niveau 2;
  118.                 if ($menu3 != null) {
  119.                     $niveau 3;
  120.                     $menu4 $menu3->getParent();
  121.                     if ($menu4 != null) {
  122.                         $niveau 4;
  123.                     }
  124.                 }
  125.             }
  126.             $entity->setNiveau($niveau);
  127.             $this->entityManager->flush();
  128.         } else if ($entity instanceof PageContenu) {
  129.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  130.                 $idSetImport date('YmdHis') . microtime(true) . 'pageContenu' $entity->getId() . "" $entity->getPage()->getId() . $entity->getPage()->getProjet()->getId();
  131.                 $entity->setIdImport(str_replace("."""$idSetImport));
  132.             }
  133.             $entity->setIdImportPage($entity->getPage()->getIdImport());
  134.             $this->entityManager->flush();
  135.         } else {
  136.             return;
  137.         }
  138.     }
  139. }