src/EventSubscriber/PageSubscriber.php line 105

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