src/EventSubscriber/ObjetSubscriber.php line 171

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.             $idSetImport date('YmdHis') . microtime(true) . 'objet' $entity->getId();
  41.             $entity->setIdImport(str_replace("."""$idSetImport));
  42.             $entity->setProjet($this->tokenStorage->getToken()->getUser()->getProjet());
  43.             $configSection $entity->getSection()->getConfigSections();
  44.             foreach ($configSection as $value) {
  45.                 $configObjet = new ConfigObjet();
  46.                 $configObjet->setConfigSection($value);
  47.                 $configObjet->setObjet($entity);
  48.                 $configObjet->setValeur($value->getValeur());
  49.                 $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  50.                 $configObjet->setIdImport(str_replace("."""$idSetImport));
  51.                 $this->entityManager->persist($configObjet);
  52.                 $this->entityManager->flush();
  53.             }
  54.             $this->entityManager->flush();
  55.         } else if ($entity instanceof Section) {
  56.             $idSetImport date('YmdHis') . microtime(true) . 'section' $entity->getId();
  57.             $entity->setIdImport(str_replace("."""$idSetImport));
  58.             $entity->addProjet($this->tokenStorage->getToken()->getUser()->getProjet());
  59.             $blockHtml $entity->getCodeHtml();
  60.             $htmlVars $this->codeVariables($blockHtml);
  61.             $blockCSS $entity->getCodeCss();
  62.             $cssVars $this->codeVariables($blockCSS);
  63.             $blockJs $entity->getCodeJs();
  64.             $jsVars $this->codeVariables($blockJs);
  65.             $blockBlock $entity->getCodeBlock();
  66.             $blockVars $this->codeVariables($blockBlock);
  67.             $innerBlock $entity->getCodeInnerBlock();
  68.             $innerBlockVars $this->codeVariables($innerBlock);
  69.             foreach ($htmlVars as $configLabel) {
  70.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  71.                     $config = new ConfigSection();
  72.                     $config->setSection($entity);
  73.                     $config->setLabel($configLabel);
  74.                     $config->setType('text');
  75.                     $config->setTypeCode('html');
  76.                     $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  77.                     $config->setIdImport(str_replace("."""$idSetImport));
  78.                     $this->entityManager->persist($config);
  79.                     $this->entityManager->flush();
  80.                 }
  81.             }
  82.             foreach ($cssVars as $configLabel) {
  83.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  84.                     $config = new ConfigSection();
  85.                     $config->setSection($entity);
  86.                     $config->setLabel($configLabel);
  87.                     $config->setType('text');
  88.                     $config->setTypeCode('css');
  89.                     $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  90.                     $config->setIdImport(str_replace("."""$idSetImport));
  91.                     $this->entityManager->persist($config);
  92.                     $this->entityManager->flush();
  93.                 }
  94.             }
  95.             foreach ($jsVars as $configLabel) {
  96.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  97.                     $config = new ConfigSection();
  98.                     $config->setSection($entity);
  99.                     $config->setLabel($configLabel);
  100.                     $config->setType('text');
  101.                     $config->setTypeCode('js');
  102.                     $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  103.                     $config->setIdImport(str_replace("."""$idSetImport));
  104.                     $this->entityManager->persist($config);
  105.                     $this->entityManager->flush();
  106.                 }
  107.             }
  108.             foreach ($blockVars as $configLabel) {
  109.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  110.                     $config = new ConfigSection();
  111.                     $config->setSection($entity);
  112.                     $config->setLabel($configLabel);
  113.                     $config->setType('text');
  114.                     $config->setTypeCode('block');
  115.                     $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  116.                     $config->setIdImport(str_replace("."""$idSetImport));
  117.                     $this->entityManager->persist($config);
  118.                     $this->entityManager->flush();
  119.                 }
  120.             }
  121.             foreach ($innerBlockVars as $configLabel) {
  122.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  123.                     $config = new ConfigSection();
  124.                     $config->setSection($entity);
  125.                     $config->setLabel($configLabel);
  126.                     $config->setType('text');
  127.                     $config->setTypeCode('innerBlock');
  128.                     $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  129.                     $config->setIdImport(str_replace("."""$idSetImport));
  130.                     $this->entityManager->persist($config);
  131.                     $this->entityManager->flush();
  132.                 }
  133.             }
  134.             $this->entityManager->flush();
  135.         } else {
  136.             return;
  137.         }
  138.     }
  139.     public function afterUpdate(AfterEntityUpdatedEvent $event)
  140.     {
  141.         $entity $event->getEntityInstance();
  142.         if ($entity instanceof Objet) {
  143.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  144.                 $idSetImport date('YmdHis') . microtime(true) . 'objet' $entity->getId();
  145.                 $entity->setIdImport(str_replace("."""$idSetImport));
  146.             }
  147.             $configSection $entity->getSection()->getConfigSections();
  148.             $configObjets $entity->getConfigObjets();
  149.             $create true;
  150.             foreach ($configObjets as $key => $value) {
  151.                 if ($value->getConfigSection()->getSection() == $entity->getSection()) {
  152.                     $create false;
  153.                     break;
  154.                 }
  155.             }
  156.             if ($create) {
  157.                 foreach ($configObjets as $key => $value) {
  158.                     $this->entityManager->remove($value);
  159.                     $this->entityManager->flush();
  160.                 }
  161.                 foreach ($configSection as $value) {
  162.                     $configObjet = new ConfigObjet();
  163.                     $configObjet->setConfigSection($value);
  164.                     $configObjet->setObjet($entity);
  165.                     $configObjet->setValeur($value->getValeur());
  166.                     $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  167.                     $configObjet->setIdImport(str_replace("."""$idSetImport));
  168.                     $this->entityManager->persist($configObjet);
  169.                     $this->entityManager->flush();
  170.                 }
  171.             }
  172.             $this->entityManager->flush();
  173.         } else if ($entity instanceof Section) {
  174.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  175.                 $idSetImport date('YmdHis') . microtime(true) . 'section' $entity->getId();
  176.                 $entity->setIdImport(str_replace("."""$idSetImport));
  177.             }
  178.             $entity->addProjet($this->tokenStorage->getToken()->getUser()->getProjet());
  179.             $blockHtml $entity->getCodeHtml();
  180.             $htmlVars $this->codeVariables($blockHtml);
  181.             $blockCSS $entity->getCodeCss();
  182.             $cssVars $this->codeVariables($blockCSS);
  183.             $blockJs $entity->getCodeJs();
  184.             $jsVars $this->codeVariables($blockJs);
  185.             $blockBlock $entity->getCodeBlock();
  186.             $blockVars $this->codeVariables($blockBlock);
  187.             $innerBlock $entity->getCodeInnerBlock();
  188.             $innerBlockVars $this->codeVariables($innerBlock);
  189.             foreach ($entity->getConfigSections() as $configSection) {
  190.                 
  191.                 if ($configSection->getTypeCode() == 'html') {
  192.                     if (!in_array($configSection->getLabel(), $htmlVars)) {
  193.                         $this->entityManager->remove($configSection);
  194.                     }
  195.                 } else if ($configSection->getTypeCode() == 'css') {
  196.                     if (!in_array($configSection->getLabel(), $cssVars)) {
  197.                         $this->entityManager->remove($configSection);
  198.                     }
  199.                 } else if ($configSection->getTypeCode() == 'js') {
  200.                     if (!in_array($configSection->getLabel(), $jsVars)) {
  201.                         $this->entityManager->remove($configSection);
  202.                     }
  203.                 } else if ($configSection->getTypeCode() == 'block') {
  204.                     if (!in_array($configSection->getLabel(), $blockVars)) {
  205.                         $this->entityManager->remove($configSection);
  206.                     }
  207.                 } else if ($configSection->getTypeCode() == 'innerBlock') {
  208.                     if (!in_array($configSection->getLabel(), $innerBlockVars)) {
  209.                         $this->entityManager->remove($configSection);
  210.                     }
  211.                 }
  212.             }
  213.             /* foreach ($entity->getConfigSections() as $configSection) {
  214.                 $inhtml = false;
  215.                 $incss = false;
  216.                 $injs = false;
  217.                 $inblock = false;
  218.                 $ininnerblock = false;
  219.                 if (in_array($configSection->getLabel(), $htmlVars)) {
  220.                     $inhtml = true;
  221.                 }
  222.                 if (in_array($configSection->getLabel(), $cssVars)) {
  223.                     $incss = true;
  224.                 }
  225.                 if (in_array($configSection->getLabel(), $jsVars)) {
  226.                     $injs = true;
  227.                 }
  228.                 if (in_array($configSection->getLabel(), $blockVars)) {
  229.                     $inblock = true;
  230.                 }
  231.                 if (in_array($configSection->getLabel(), $innerBlockVars)) {
  232.                     $ininnerblock = true;
  233.                 }
  234.                 if (!$inhtml && !$incss && !$injs && !$inblock && !$ininnerblock) {
  235.                     $this->entityManager->remove($configSection);
  236.                 }
  237.             } */
  238.             foreach ($htmlVars as $configLabel) {
  239.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  240.                     $config $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel'section' => $entity'typeCode' => 'html']);
  241.                     if ($config == null) {
  242.                         $config = new ConfigSection();
  243.                         $config->setType('text');
  244.                         $config->setSection($entity);
  245.                         $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  246.                         $config->setIdImport(str_replace("."""$idSetImport));
  247.                     }
  248.                     $config->setLabel($configLabel);
  249.                     $config->setTypeCode('html');
  250.                     $this->entityManager->persist($config);
  251.                     $this->entityManager->flush();
  252.                 }
  253.             }
  254.             foreach ($cssVars as $configLabel) {
  255.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  256.                     $config $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel'section' => $entity'typeCode' => 'css']);
  257.                     if ($config == null) {
  258.                         $config = new ConfigSection();
  259.                         $config->setSection($entity);
  260.                         $config->setType('text');
  261.                         $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  262.                         $config->setIdImport(str_replace("."""$idSetImport));
  263.                     }
  264.                     $config->setLabel($configLabel);
  265.                     $config->setTypeCode('css');
  266.                     $this->entityManager->persist($config);
  267.                     $this->entityManager->flush();
  268.                 }
  269.             }
  270.             foreach ($jsVars as $configLabel) {
  271.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  272.                     $config $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel'section' => $entity'typeCode' => 'js']);
  273.                     if ($config == null) {
  274.                         $config = new ConfigSection();
  275.                         $config->setSection($entity);
  276.                         $config->setType('text');
  277.                         $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  278.                         $config->setIdImport(str_replace("."""$idSetImport));
  279.                     }
  280.                     $config->setLabel($configLabel);
  281.                     $config->setTypeCode('js');
  282.                     $this->entityManager->persist($config);
  283.                     $this->entityManager->flush();
  284.                 }
  285.             }
  286.             foreach ($blockVars as $configLabel) {
  287.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  288.                     $config $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel'section' => $entity'typeCode' => 'block']);
  289.                     if ($config == null) {
  290.                         $config = new ConfigSection();
  291.                         $config->setSection($entity);
  292.                         $config->setType('text');
  293.                         $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  294.                         $config->setIdImport(str_replace("."""$idSetImport));
  295.                     }
  296.                     $config->setLabel($configLabel);
  297.                     $config->setTypeCode('block');
  298.                     $this->entityManager->persist($config);
  299.                     $this->entityManager->flush();
  300.                 }
  301.             }
  302.             foreach ($innerBlockVars as $configLabel) {
  303.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  304.                     $config $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel'section' => $entity'typeCode' => 'innerBlock']);
  305.                     if ($config == null) {
  306.                         $config = new ConfigSection();
  307.                         $config->setSection($entity);
  308.                         $config->setType('text');
  309.                         $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  310.                         $config->setIdImport(str_replace("."""$idSetImport));
  311.                     }
  312.                     $config->setLabel($configLabel);
  313.                     $config->setTypeCode('innerBlock');
  314.                     $this->entityManager->persist($config);
  315.                     $this->entityManager->flush();
  316.                 }
  317.             }
  318.             $this->entityManager->flush();
  319.         } else {
  320.             return;
  321.         }
  322.     }
  323.     public function codeVariables($block)
  324.     {
  325.         $pattern '/{{(.*?)}}/';
  326.         preg_match_all($pattern$block$matcheshtml);
  327.         return array_unique($matcheshtml[1]);
  328.     }
  329.     public function slugify($string)
  330.     {
  331.         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')), '-'));
  332.     }
  333. }