src/EventSubscriber/ObjetSubscriber.php line 158

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\ConfigObjet;
  4. use App\Entity\ConfigSection;
  5. use App\Entity\Objet;
  6. use App\Entity\Section;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  10. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. class ObjetSubscriber implements EventSubscriberInterface
  15. {
  16.     private $entityManager;
  17.     private ManagerRegistry $manager;
  18.     public function __construct(
  19.         ManagerRegistry $manager,
  20.         EntityManagerInterface $entityManager,
  21.         LoggerInterface $logger,
  22.         TokenStorageInterface $tokenStorage
  23.     ) {
  24.         $this->manager $manager;
  25.         $this->entityManager $entityManager;
  26.         $this->logger $logger;
  27.         $this->tokenStorage $tokenStorage;
  28.     }
  29.     public static function getSubscribedEvents()
  30.     {
  31.         return [
  32.             AfterEntityPersistedEvent::class => ['afterAdd'],
  33.             AfterEntityUpdatedEvent::class => ['afterUpdate'],
  34.         ];
  35.     }
  36.     public function afterAdd(AfterEntityPersistedEvent $event)
  37.     {
  38.         $entity $event->getEntityInstance();
  39.         if ($entity instanceof Objet) {
  40.             $entity->setIdImport(date('YmdHis') . 'objet' $entity->getId());
  41.             $entity->addProjet($this->tokenStorage->getToken()->getUser()->getProjet());
  42.             $configSection $entity->getSection()->getConfigSections();
  43.             foreach ($configSection as $value) {
  44.                 $configObjet = new ConfigObjet();
  45.                 $configObjet->setConfigSection($value);
  46.                 $configObjet->setObjet($entity);
  47.                 $configObjet->setValeur($value->getValeur());
  48.                 $configObjet->setIdImport(date('YmdHis') . 'confobj' $entity->getId());
  49.                 $this->entityManager->persist($configObjet);
  50.                 $this->entityManager->flush();
  51.             }
  52.             $this->entityManager->flush();
  53.         } else if ($entity instanceof Section) {
  54.             $entity->setIdImport(date('YmdHis') . 'section' $entity->getId());
  55.             $entity->setProjet($this->tokenStorage->getToken()->getUser()->getProjet());
  56.             $blockHtml $entity->getCodeHtml();
  57.             $htmlVars $this->codeVariables($blockHtml);
  58.             $blockCSS $entity->getCodeCss();
  59.             $cssVars $this->codeVariables($blockCSS);
  60.             $blockJs $entity->getCodeJs();
  61.             $jsVars $this->codeVariables($blockJs);
  62.             $blockBlock $entity->getCodeBlock();
  63.             $blockVars $this->codeVariables($blockBlock);
  64.             $innerBlock $entity->getCodeInnerBlock();
  65.             $innerBlockVars $this->codeVariables($innerBlock);
  66.             foreach ($htmlVars as $configLabel) {
  67.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  68.                     $config = new ConfigSection();
  69.                     $config->setSection($entity);
  70.                     $config->setLabel($configLabel);
  71.                     $config->setType('text');
  72.                     $config->setTypeCode('html');
  73.                     $config->setIdImport(date('YmdHis') . 'confobj' $entity->getId());
  74.                     $this->entityManager->persist($config);
  75.                     $this->entityManager->flush();
  76.                 }
  77.             }
  78.             foreach ($cssVars as $configLabel) {
  79.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  80.                     $config = new ConfigSection();
  81.                     $config->setSection($entity);
  82.                     $config->setLabel($configLabel);
  83.                     $config->setType('text');
  84.                     $config->setTypeCode('css');
  85.                     $config->setIdImport(date('YmdHis') . 'confobj' $entity->getId());
  86.                     $this->entityManager->persist($config);
  87.                     $this->entityManager->flush();
  88.                 }
  89.             }
  90.             foreach ($jsVars as $configLabel) {
  91.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  92.                     $config = new ConfigSection();
  93.                     $config->setSection($entity);
  94.                     $config->setLabel($configLabel);
  95.                     $config->setType('text');
  96.                     $config->setTypeCode('js');
  97.                     $config->setIdImport(date('YmdHis') . 'confobj' $entity->getId());
  98.                     $this->entityManager->persist($config);
  99.                     $this->entityManager->flush();
  100.                 }
  101.             }
  102.             foreach ($blockVars as $configLabel) {
  103.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  104.                     $config = new ConfigSection();
  105.                     $config->setSection($entity);
  106.                     $config->setLabel($configLabel);
  107.                     $config->setType('text');
  108.                     $config->setTypeCode('block');
  109.                     $config->setIdImport(date('YmdHis') . 'confobj' $entity->getId());
  110.                     $this->entityManager->persist($config);
  111.                     $this->entityManager->flush();
  112.                 }
  113.             }
  114.             foreach ($innerBlockVars as $configLabel) {
  115.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  116.                     $config = new ConfigSection();
  117.                     $config->setSection($entity);
  118.                     $config->setLabel($configLabel);
  119.                     $config->setType('text');
  120.                     $config->setTypeCode('innerBlock');
  121.                     $config->setIdImport(date('YmdHis') . 'confobj' $entity->getId());
  122.                     $this->entityManager->persist($config);
  123.                     $this->entityManager->flush();
  124.                 }
  125.             }
  126.             $this->entityManager->flush();
  127.         } else {
  128.             return;
  129.         }
  130.     }
  131.     public function afterUpdate(AfterEntityUpdatedEvent $event)
  132.     {
  133.         $entity $event->getEntityInstance();
  134.         if ($entity instanceof Objet) {
  135.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  136.                 $entity->setIdImport(date('YmdHis') . 'objet' $entity->getId());
  137.             }
  138.             $entity->addProjet($this->tokenStorage->getToken()->getUser()->getProjet());
  139.             $configSection $entity->getSection()->getConfigSections();
  140.             $configObjets $entity->getConfigObjets();
  141.             $create true;
  142.             foreach ($configObjets as $key => $value) {
  143.                 if ($value->getConfigSection()->getSection() == $entity->getSection()) {
  144.                     $create false;
  145.                     break;
  146.                 }
  147.             }
  148.             if ($create) {
  149.                 foreach ($configObjets as $key => $value) {
  150.                     $this->entityManager->remove($value);
  151.                     $this->entityManager->flush();
  152.                 }
  153.                 foreach ($configSection as $value) {
  154.                     $configObjet = new ConfigObjet();
  155.                     $configObjet->setConfigSection($value);
  156.                     $configObjet->setObjet($entity);
  157.                     $configObjet->setValeur($value->getValeur());
  158.                     $configObjet->setIdImport(date('YmdHis') . 'confobj' $entity->getId());
  159.                     $this->entityManager->persist($configObjet);
  160.                     $this->entityManager->flush();
  161.                 }
  162.             }
  163.             $this->entityManager->flush();
  164.         } else if ($entity instanceof Section) {
  165.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  166.                 $entity->setIdImport(date('YmdHis') . 'section' $entity->getId());
  167.             }
  168.             $entity->setProjet($this->tokenStorage->getToken()->getUser()->getProjet());
  169.             $blockHtml $entity->getCodeHtml();
  170.             $htmlVars $this->codeVariables($blockHtml);
  171.             $blockCSS $entity->getCodeCss();
  172.             $cssVars $this->codeVariables($blockCSS);
  173.             $blockJs $entity->getCodeJs();
  174.             $jsVars $this->codeVariables($blockJs);
  175.             $blockBlock $entity->getCodeBlock();
  176.             $blockVars $this->codeVariables($blockBlock);
  177.             $innerBlock $entity->getCodeInnerBlock();
  178.             $innerBlockVars $this->codeVariables($innerBlock);
  179.             foreach ($entity->getConfigSections() as $configSection) {
  180.                 $inhtml false;
  181.                 $incss false;
  182.                 $injs false;
  183.                 $inblock false;
  184.                 $ininnerblock false;
  185.                 if (in_array($configSection->getLabel(), $htmlVars)) {
  186.                     $inhtml true;
  187.                 }
  188.                 if (in_array($configSection->getLabel(), $cssVars)) {
  189.                     $incss true;
  190.                 }
  191.                 if (in_array($configSection->getLabel(), $jsVars)) {
  192.                     $injs true;
  193.                 }
  194.                 if (in_array($configSection->getLabel(), $blockVars)) {
  195.                     $inblock true;
  196.                 }
  197.                 if (in_array($configSection->getLabel(), $innerBlockVars)) {
  198.                     $ininnerblock true;
  199.                 }
  200.                 if (!$inhtml && !$incss && !$injs && !$inblock && !$ininnerblock) {
  201.                     $this->entityManager->remove($configSection);
  202.                 }
  203.             }
  204.             foreach ($htmlVars as $configLabel) {
  205.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  206.                     $config $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel'section' => $entity'typeCode' => 'html']);
  207.                     if ($config == null) {
  208.                         $config = new ConfigSection();
  209.                         $config->setType('text');
  210.                         $config->setSection($entity);
  211.                         $config->setIdImport(date('YmdHis') . 'confobj' $entity->getId());
  212.                     }
  213.                     $config->setLabel($configLabel);
  214.                     $config->setTypeCode('html');
  215.                     $this->entityManager->persist($config);
  216.                     $this->entityManager->flush();
  217.                 }
  218.             }
  219.             foreach ($cssVars as $configLabel) {
  220.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  221.                     $config $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel'section' => $entity'typeCode' => 'css']);
  222.                     if ($config == null) {
  223.                         $config = new ConfigSection();
  224.                         $config->setSection($entity);
  225.                         $config->setType('text');
  226.                         $config->setIdImport(date('YmdHis') . 'confobj' $entity->getId());
  227.                     }
  228.                     $config->setLabel($configLabel);
  229.                     $config->setTypeCode('css');
  230.                     $this->entityManager->persist($config);
  231.                     $this->entityManager->flush();
  232.                 }
  233.             }
  234.             foreach ($jsVars as $configLabel) {
  235.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  236.                     $config $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel'section' => $entity'typeCode' => 'js']);
  237.                     if ($config == null) {
  238.                         $config = new ConfigSection();
  239.                         $config->setSection($entity);
  240.                         $config->setType('text');
  241.                         $config->setIdImport(date('YmdHis') . 'confobj' $entity->getId());
  242.                     }
  243.                     $config->setLabel($configLabel);
  244.                     $config->setTypeCode('js');
  245.                     $this->entityManager->persist($config);
  246.                     $this->entityManager->flush();
  247.                 }
  248.             }
  249.             foreach ($blockVars as $configLabel) {
  250.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  251.                     $config $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel'section' => $entity'typeCode' => 'block']);
  252.                     if ($config == null) {
  253.                         $config = new ConfigSection();
  254.                         $config->setSection($entity);
  255.                         $config->setType('text');
  256.                         $config->setIdImport(date('YmdHis') . 'confobj' $entity->getId());
  257.                     }
  258.                     $config->setLabel($configLabel);
  259.                     $config->setTypeCode('block');
  260.                     $this->entityManager->persist($config);
  261.                     $this->entityManager->flush();
  262.                 }
  263.             }
  264.             foreach ($innerBlockVars as $configLabel) {
  265.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  266.                     $config $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel'section' => $entity'typeCode' => 'innerBlock']);
  267.                     if ($config == null) {
  268.                         $config = new ConfigSection();
  269.                         $config->setSection($entity);
  270.                         $config->setType('text');
  271.                         $config->setIdImport(date('YmdHis') . 'confobj' $entity->getId());
  272.                     }
  273.                     $config->setLabel($configLabel);
  274.                     $config->setTypeCode('innerBlock');
  275.                     $this->entityManager->persist($config);
  276.                     $this->entityManager->flush();
  277.                 }
  278.             }
  279.             $this->entityManager->flush();
  280.         } else {
  281.             return;
  282.         }
  283.     }
  284.     public function codeVariables($block)
  285.     {
  286.         $pattern '/{{(.*?)}}/';
  287.         preg_match_all($pattern$block$matcheshtml);
  288.         return array_unique($matcheshtml[1]);
  289.     }
  290.     public function slugify($string)
  291.     {
  292.         return strtolower(trim(preg_replace('~[^0-9a-z]+~i''-'html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i''$1'htmlentities($stringENT_QUOTES'UTF-8')), ENT_QUOTES'UTF-8')), '-'));
  293.     }
  294. }