src/EventSubscriber/PageSubscriber.php line 92

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.                 $entity->setIdImport(date('Y-m-d-H-i-s') . '-page-contenu-' $entity->getId() . "-" $entity->getPage()->getId() . $entity->getPage()->getProjet()->getId());
  37.             }
  38.             $entity->setIdImportPage($entity->getPage()->getIdImport());
  39.             $this->entityManager->flush();
  40.         } else if ($entity instanceof Page) {
  41.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  42.                 $entity->setIdImport(date('Y-m-d-H-i-s') . '-page-' $entity->getId() . "-" $entity->getProjet()->getId());
  43.             }
  44.             if ($entity->getPermalink() == null || $entity->getPermalink() == '') {
  45.                 $time = new \DateTime();
  46.                 $year =  $time->format('y');
  47.                 $month =  $time->format('m');
  48.                 $day =  $time->format('d');
  49.                 $hour =  $time->format('H');
  50.                 $min =  $time->format('i');
  51.                 $sec =  $time->format('s');
  52.                 $entity->setPermalink($year $month $day $hour $min $sec $entity->getId());
  53.             }
  54.             if ($entity->getParent() != null) {
  55.                 $entity->setIdImportPageLiee($entity->getParent()->getIdImport());
  56.             }
  57.             $niveau 1;
  58.             $menu2 $entity->getParent();
  59.             if ($menu2 != null) {
  60.                 $menu3 $menu2->getParent();
  61.                 $niveau 2;
  62.                 if ($menu3 != null) {
  63.                     $niveau 3;
  64.                     $menu4 $menu3->getParent();
  65.                     if ($menu4 != null) {
  66.                         $niveau 4;
  67.                     }
  68.                 }
  69.             }
  70.             $entity->setNiveau($niveau);
  71.             $this->entityManager->flush();
  72.         } else {
  73.             return;
  74.         }
  75.     }
  76.     public function afterUpdate(AfterEntityUpdatedEvent $event)
  77.     {
  78.         $entity $event->getEntityInstance();
  79.         if ($entity instanceof Page) {
  80.             if ($entity->getParent() != null) {
  81.                 $entity->setIdImportPageLiee($entity->getParent()->getIdImport());
  82.             }
  83.             if ($entity->getPermalink() == null || $entity->getPermalink() == '') {
  84.                 $time = new \DateTime();
  85.                 $year =  $time->format('y');
  86.                 $month =  $time->format('m');
  87.                 $day =  $time->format('d');
  88.                 $hour =  $time->format('H');
  89.                 $min =  $time->format('i');
  90.                 $sec =  $time->format('s');
  91.                 $entity->setPermalink($year $month $day $hour $min $sec $entity->getId());
  92.             }
  93.             if ($entity->getParent() != null) {
  94.                 $menu2 $entity->getParent();
  95.                 if ($menu2 != null) {
  96.                     $menu3 $menu2->getParent();
  97.                     if ($menu3 != null) {
  98.                         if ($entity == $menu3) {
  99.                             $menu2->setParent(null);
  100.                             $nniv 1;
  101.                             if ($menu2->getNiveau() > 1) {
  102.                                 $nniv $menu2->getNiveau() - 1;
  103.                             }
  104.                             $menu2->setNiveau($nniv);
  105.                         }
  106.                     }
  107.                 }
  108.             }
  109.             $niveau 1;
  110.             $menu2 $entity->getParent();
  111.             if ($menu2 != null) {
  112.                 $menu3 $menu2->getParent();
  113.                 $niveau 2;
  114.                 if ($menu3 != null) {
  115.                     $niveau 3;
  116.                     $menu4 $menu3->getParent();
  117.                     if ($menu4 != null) {
  118.                         $niveau 4;
  119.                     }
  120.                 }
  121.             }
  122.             $entity->setNiveau($niveau);
  123.             $this->entityManager->flush();
  124.         } else if ($entity instanceof PageContenu) {
  125.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  126.                 $entity->setIdImport(date('Y-m-d-H-i-s') . '-page-contenu-' $entity->getId() . "-" $entity->getPage()->getId() . $entity->getPage()->getProjet()->getId());
  127.             }
  128.             $entity->setIdImportPage($entity->getPage()->getIdImport());
  129.             $this->entityManager->flush();
  130.         } else {
  131.             return;
  132.         }
  133.     }
  134. }