src/EventSubscriber/ObjetSubscriber.php line 202

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 App\Service\ObjetCSVService;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  11. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. class ObjetSubscriber implements EventSubscriberInterface
  16. {
  17.     private $entityManager;
  18.     private ManagerRegistry $manager;
  19.     private ObjetCSVService $objetCSVService;
  20.     public function __construct(
  21.         ManagerRegistry $manager,
  22.         EntityManagerInterface $entityManager,
  23.         LoggerInterface $logger,
  24.         TokenStorageInterface $tokenStorage,
  25.         ObjetCSVService $objetCSVService
  26.     ) {
  27.         $this->manager $manager;
  28.         $this->entityManager $entityManager;
  29.         $this->logger $logger;
  30.         $this->tokenStorage $tokenStorage;
  31.         $this->objetCSVService $objetCSVService;
  32.     }
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             AfterEntityPersistedEvent::class => ['afterAdd'],
  37.             AfterEntityUpdatedEvent::class => ['afterUpdate'],
  38.         ];
  39.     }
  40.     public function afterAdd(AfterEntityPersistedEvent $event)
  41.     {
  42.         $entity $event->getEntityInstance();
  43.         if ($entity instanceof Objet) {
  44.            
  45.             $idSetImport date('YmdHis') . microtime(true) . 'objet' $entity->getId();
  46.             $entity->setIdImport(str_replace("."""$idSetImport));
  47.             $entity->setProjet($this->tokenStorage->getToken()->getUser()->getProjet());
  48.             $configSection $entity->getSection()->getConfigSections();
  49.             $objets$entity->getProjet()->getObjets();
  50.             $count 0;
  51.             foreach ($objets as $objet) {
  52.                 if (strtolower($objet->getLabel()) == strtolower($entity->getLabel())) {
  53.                     $count++;
  54.                 }
  55.             }
  56.             
  57.             if ($count ) {
  58.                 $this->entityManager->remove($entity);
  59.                 $this->entityManager->flush();
  60.             }
  61.             else{
  62.             foreach ($configSection as $value) {
  63.                 $configObjet = new ConfigObjet();
  64.                 $configObjet->setConfigSection($value);
  65.                 $configObjet->setObjet($entity);
  66.                 $configObjet->setValeur($value->getValeur());
  67.                 $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  68.                 $configObjet->setIdImport(str_replace("."""$idSetImport));
  69.                 $this->entityManager->persist($configObjet);
  70.                 $this->entityManager->flush();
  71.             }
  72.         }
  73.             $this->entityManager->flush();
  74.         
  75.         } else if ($entity instanceof Section) {
  76.             $sections $this->tokenStorage->getToken()->getUser()->getProjet()->getSections();
  77.             $count 0;
  78.             foreach ($sections as $section) {
  79.                 if (strtolower($section->getLabel()) == strtolower($entity->getLabel())) {
  80.                     $count++;
  81.                 }
  82.             }
  83.             
  84.             if ($count ) {
  85.                 $this->entityManager->remove($entity);
  86.             
  87.             }
  88.             $this->entityManager->flush();
  89.             $idSetImport date('YmdHis') . microtime(true) . 'section' $entity->getId();
  90.             $entity->setIdImport(str_replace("."""$idSetImport));
  91.             //$entity->addProjet($this->tokenStorage->getToken()->getUser()->getProjet());
  92.             $entity->setProjet($this->tokenStorage->getToken()->getUser()->getProjet());
  93.             $blockHtml $entity->getCodeHtml();
  94.             $htmlVars $this->codeVariables($blockHtml);
  95.             $blockCSS $entity->getCodeCss();
  96.             $cssVars $this->codeVariables($blockCSS);
  97.             $blockJs $entity->getCodeJs();
  98.             $jsVars $this->codeVariables($blockJs);
  99.             $blockBlock $entity->getCodeBlock();
  100.             $blockVars $this->codeVariables($blockBlock);
  101.             $innerBlock $entity->getCodeInnerBlock();
  102.             $innerBlockVars $this->codeVariables($innerBlock);
  103.             foreach ($htmlVars as $configLabel) {
  104.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  105.                     $config = new ConfigSection();
  106.                     $config->setSection($entity);
  107.                     $config->setLabel($configLabel);
  108.                     $config->setType('text');
  109.                     $config->setTypeCode('html');
  110.                     $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  111.                     $config->setIdImport(str_replace("."""$idSetImport));
  112.                     $this->entityManager->persist($config);
  113.                     $this->entityManager->flush();
  114.                 }
  115.             }
  116.             foreach ($cssVars as $configLabel) {
  117.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  118.                     $config = new ConfigSection();
  119.                     $config->setSection($entity);
  120.                     $config->setLabel($configLabel);
  121.                     $config->setType('text');
  122.                     $config->setTypeCode('css');
  123.                     $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  124.                     $config->setIdImport(str_replace("."""$idSetImport));
  125.                     $this->entityManager->persist($config);
  126.                     $this->entityManager->flush();
  127.                 }
  128.             }
  129.             foreach ($jsVars as $configLabel) {
  130.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  131.                     $config = new ConfigSection();
  132.                     $config->setSection($entity);
  133.                     $config->setLabel($configLabel);
  134.                     $config->setType('text');
  135.                     $config->setTypeCode('js');
  136.                     $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  137.                     $config->setIdImport(str_replace("."""$idSetImport));
  138.                     $this->entityManager->persist($config);
  139.                     $this->entityManager->flush();
  140.                 }
  141.             }
  142.             foreach ($blockVars as $configLabel) {
  143.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  144.                     $config = new ConfigSection();
  145.                     $config->setSection($entity);
  146.                     $config->setLabel($configLabel);
  147.                     $config->setType('text');
  148.                     $config->setTypeCode('block');
  149.                     $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  150.                     $config->setIdImport(str_replace("."""$idSetImport));
  151.                     $this->entityManager->persist($config);
  152.                     $this->entityManager->flush();
  153.                 }
  154.             }
  155.             foreach ($innerBlockVars as $configLabel) {
  156.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  157.                     $config = new ConfigSection();
  158.                     $config->setSection($entity);
  159.                     $config->setLabel($configLabel);
  160.                     $config->setType('text');
  161.                     $config->setTypeCode('innerBlock');
  162.                     $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  163.                     $config->setIdImport(str_replace("."""$idSetImport));
  164.                     $this->entityManager->persist($config);
  165.                     $this->entityManager->flush();
  166.                 }
  167.             }
  168.             $this->entityManager->flush();
  169.         } else {
  170.             return;
  171.         }
  172.     }
  173.     public function afterUpdate(AfterEntityUpdatedEvent $event)
  174.     {
  175.         $entity $event->getEntityInstance();
  176.         $projet $this->tokenStorage->getToken()->getUser()->getProjet();
  177.         if ($entity instanceof Objet) {
  178.             $this->objetCSVService->exportObjetV2($projet);//export tous les objets
  179.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  180.                 $idSetImport date('YmdHis') . microtime(true) . 'objet' $entity->getId();
  181.                 $entity->setIdImport(str_replace("."""$idSetImport));
  182.             }
  183.             $configSection $entity->getSection()->getConfigSections();
  184.             $configObjets $entity->getConfigObjets();
  185.             $create true;
  186.             foreach ($configObjets as $key => $value) {
  187.                 if ($value->getConfigSection()->getSection() == $entity->getSection()) {
  188.                     $create false;
  189.                     break;
  190.                 }
  191.             }
  192.             if ($create) {
  193.                 foreach ($configObjets as $key => $value) {
  194.                     $this->entityManager->remove($value);
  195.                     $this->entityManager->flush();
  196.                 }
  197.                 foreach ($configSection as $value) {
  198.                     $configObjet = new ConfigObjet();
  199.                     $configObjet->setConfigSection($value);
  200.                     $configObjet->setObjet($entity);
  201.                     $configObjet->setValeur($value->getValeur());
  202.                     $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  203.                     $configObjet->setIdImport(str_replace("."""$idSetImport));
  204.                     $this->entityManager->persist($configObjet);
  205.                     $this->entityManager->flush();
  206.                 }
  207.             }
  208.             $this->entityManager->flush();
  209.         } else if ($entity instanceof Section) {
  210.             $this->objetCSVService->exportObjetV2($projet);
  211.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  212.                 $idSetImport date('YmdHis') . microtime(true) . 'section' $entity->getId();
  213.                 $entity->setIdImport(str_replace("."""$idSetImport));
  214.             }
  215.             //$entity->addProjet($this->tokenStorage->getToken()->getUser()->getProjet());
  216.             $entity->setProjet($this->tokenStorage->getToken()->getUser()->getProjet());
  217.             $blockHtml $entity->getCodeHtml();
  218.             $htmlVars $this->codeVariables($blockHtml);
  219.             $blockCSS $entity->getCodeCss();
  220.             $cssVars $this->codeVariables($blockCSS);
  221.             $blockJs $entity->getCodeJs();
  222.             $jsVars $this->codeVariables($blockJs);
  223.             $blockBlock $entity->getCodeBlock();
  224.             $blockVars $this->codeVariables($blockBlock);
  225.             $innerBlock $entity->getCodeInnerBlock();
  226.             $innerBlockVars $this->codeVariables($innerBlock);
  227.             foreach ($entity->getConfigSections() as $configSection) {
  228.                 
  229.                 if ($configSection->getTypeCode() == 'html') {
  230.                     if (!in_array($configSection->getLabel(), $htmlVars)) {
  231.                         $this->entityManager->remove($configSection);
  232.                     }
  233.                 } else if ($configSection->getTypeCode() == 'css') {
  234.                     if (!in_array($configSection->getLabel(), $cssVars)) {
  235.                         $this->entityManager->remove($configSection);
  236.                     }
  237.                 } else if ($configSection->getTypeCode() == 'js') {
  238.                     if (!in_array($configSection->getLabel(), $jsVars)) {
  239.                         $this->entityManager->remove($configSection);
  240.                     }
  241.                 } else if ($configSection->getTypeCode() == 'block') {
  242.                     if (!in_array($configSection->getLabel(), $blockVars)) {
  243.                         $this->entityManager->remove($configSection);
  244.                     }
  245.                 } else if ($configSection->getTypeCode() == 'innerBlock') {
  246.                     if (!in_array($configSection->getLabel(), $innerBlockVars)) {
  247.                         $this->entityManager->remove($configSection);
  248.                     }
  249.                 }
  250.             }
  251.             /* foreach ($entity->getConfigSections() as $configSection) {
  252.                 $inhtml = false;
  253.                 $incss = false;
  254.                 $injs = false;
  255.                 $inblock = false;
  256.                 $ininnerblock = false;
  257.                 if (in_array($configSection->getLabel(), $htmlVars)) {
  258.                     $inhtml = true;
  259.                 }
  260.                 if (in_array($configSection->getLabel(), $cssVars)) {
  261.                     $incss = true;
  262.                 }
  263.                 if (in_array($configSection->getLabel(), $jsVars)) {
  264.                     $injs = true;
  265.                 }
  266.                 if (in_array($configSection->getLabel(), $blockVars)) {
  267.                     $inblock = true;
  268.                 }
  269.                 if (in_array($configSection->getLabel(), $innerBlockVars)) {
  270.                     $ininnerblock = true;
  271.                 }
  272.                 if (!$inhtml && !$incss && !$injs && !$inblock && !$ininnerblock) {
  273.                     $this->entityManager->remove($configSection);
  274.                 }
  275.             } */
  276.             foreach ($htmlVars as $configLabel) {
  277.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  278.                     $config $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel'section' => $entity'typeCode' => 'html']);
  279.                     if ($config == null) {
  280.                         $config = new ConfigSection();
  281.                         $config->setType('text');
  282.                         $config->setSection($entity);
  283.                         $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  284.                         $config->setIdImport(str_replace("."""$idSetImport));
  285.                     }
  286.                     $config->setLabel($configLabel);
  287.                     $config->setTypeCode('html');
  288.                     $this->entityManager->persist($config);
  289.                     $this->entityManager->flush();
  290.                 }
  291.             }
  292.             foreach ($cssVars as $configLabel) {
  293.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  294.                     $config $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel'section' => $entity'typeCode' => 'css']);
  295.                     if ($config == null) {
  296.                         $config = new ConfigSection();
  297.                         $config->setSection($entity);
  298.                         $config->setType('text');
  299.                         $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  300.                         $config->setIdImport(str_replace("."""$idSetImport));
  301.                     }
  302.                     $config->setLabel($configLabel);
  303.                     $config->setTypeCode('css');
  304.                     $this->entityManager->persist($config);
  305.                     $this->entityManager->flush();
  306.                 }
  307.             }
  308.             foreach ($jsVars as $configLabel) {
  309.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  310.                     $config $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel'section' => $entity'typeCode' => 'js']);
  311.                     if ($config == null) {
  312.                         $config = new ConfigSection();
  313.                         $config->setSection($entity);
  314.                         $config->setType('text');
  315.                         $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  316.                         $config->setIdImport(str_replace("."""$idSetImport));
  317.                     }
  318.                     $config->setLabel($configLabel);
  319.                     $config->setTypeCode('js');
  320.                     $this->entityManager->persist($config);
  321.                     $this->entityManager->flush();
  322.                 }
  323.             }
  324.             foreach ($blockVars as $configLabel) {
  325.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  326.                     $config $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel'section' => $entity'typeCode' => 'block']);
  327.                     if ($config == null) {
  328.                         $config = new ConfigSection();
  329.                         $config->setSection($entity);
  330.                         $config->setType('text');
  331.                         $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  332.                         $config->setIdImport(str_replace("."""$idSetImport));
  333.                     }
  334.                     $config->setLabel($configLabel);
  335.                     $config->setTypeCode('block');
  336.                     $this->entityManager->persist($config);
  337.                     $this->entityManager->flush();
  338.                 }
  339.             }
  340.             foreach ($innerBlockVars as $configLabel) {
  341.                 if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
  342.                     $config $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel'section' => $entity'typeCode' => 'innerBlock']);
  343.                     if ($config == null) {
  344.                         $config = new ConfigSection();
  345.                         $config->setSection($entity);
  346.                         $config->setType('text');
  347.                         $idSetImport date('YmdHis') . microtime(true) . 'confobj' $entity->getId();
  348.                         $config->setIdImport(str_replace("."""$idSetImport));
  349.                     }
  350.                     $config->setLabel($configLabel);
  351.                     $config->setTypeCode('innerBlock');
  352.                     $this->entityManager->persist($config);
  353.                     $this->entityManager->flush();
  354.                 }
  355.             }
  356.             $this->entityManager->flush();
  357.         } else {
  358.             return;
  359.         }
  360.     }
  361.     public function codeVariables($block)
  362.     {
  363.         $pattern '/{{(.*?)}}/';
  364.         preg_match_all($pattern$block$matcheshtml);
  365.         return array_unique($matcheshtml[1]);
  366.     }
  367.     public function slugify($string)
  368.     {
  369.         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')), '-'));
  370.     }
  371. }