<?php
namespace App\EventSubscriber;
use App\Entity\ConfigObjet;
use App\Entity\ConfigSection;
use App\Entity\Objet;
use App\Entity\Section;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class ObjetSubscriber implements EventSubscriberInterface
{
private $entityManager;
private ManagerRegistry $manager;
public function __construct(
ManagerRegistry $manager,
EntityManagerInterface $entityManager,
LoggerInterface $logger,
TokenStorageInterface $tokenStorage
) {
$this->manager = $manager;
$this->entityManager = $entityManager;
$this->logger = $logger;
$this->tokenStorage = $tokenStorage;
}
public static function getSubscribedEvents()
{
return [
AfterEntityPersistedEvent::class => ['afterAdd'],
AfterEntityUpdatedEvent::class => ['afterUpdate'],
];
}
public function afterAdd(AfterEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if ($entity instanceof Objet) {
$idSetImport = date('YmdHis') . microtime(true) . 'objet' . $entity->getId();
$entity->setIdImport(str_replace(".", "", $idSetImport));
$entity->setProjet($this->tokenStorage->getToken()->getUser()->getProjet());
$configSection = $entity->getSection()->getConfigSections();
foreach ($configSection as $value) {
$configObjet = new ConfigObjet();
$configObjet->setConfigSection($value);
$configObjet->setObjet($entity);
$configObjet->setValeur($value->getValeur());
$idSetImport = date('YmdHis') . microtime(true) . 'confobj' . $entity->getId();
$configObjet->setIdImport(str_replace(".", "", $idSetImport));
$this->entityManager->persist($configObjet);
$this->entityManager->flush();
}
$this->entityManager->flush();
} else if ($entity instanceof Section) {
$idSetImport = date('YmdHis') . microtime(true) . 'section' . $entity->getId();
$entity->setIdImport(str_replace(".", "", $idSetImport));
$entity->addProjet($this->tokenStorage->getToken()->getUser()->getProjet());
$blockHtml = $entity->getCodeHtml();
$htmlVars = $this->codeVariables($blockHtml);
$blockCSS = $entity->getCodeCss();
$cssVars = $this->codeVariables($blockCSS);
$blockJs = $entity->getCodeJs();
$jsVars = $this->codeVariables($blockJs);
$blockBlock = $entity->getCodeBlock();
$blockVars = $this->codeVariables($blockBlock);
$innerBlock = $entity->getCodeInnerBlock();
$innerBlockVars = $this->codeVariables($innerBlock);
foreach ($htmlVars as $configLabel) {
if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
$config = new ConfigSection();
$config->setSection($entity);
$config->setLabel($configLabel);
$config->setType('text');
$config->setTypeCode('html');
$idSetImport = date('YmdHis') . microtime(true) . 'confobj' . $entity->getId();
$config->setIdImport(str_replace(".", "", $idSetImport));
$this->entityManager->persist($config);
$this->entityManager->flush();
}
}
foreach ($cssVars as $configLabel) {
if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
$config = new ConfigSection();
$config->setSection($entity);
$config->setLabel($configLabel);
$config->setType('text');
$config->setTypeCode('css');
$idSetImport = date('YmdHis') . microtime(true) . 'confobj' . $entity->getId();
$config->setIdImport(str_replace(".", "", $idSetImport));
$this->entityManager->persist($config);
$this->entityManager->flush();
}
}
foreach ($jsVars as $configLabel) {
if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
$config = new ConfigSection();
$config->setSection($entity);
$config->setLabel($configLabel);
$config->setType('text');
$config->setTypeCode('js');
$idSetImport = date('YmdHis') . microtime(true) . 'confobj' . $entity->getId();
$config->setIdImport(str_replace(".", "", $idSetImport));
$this->entityManager->persist($config);
$this->entityManager->flush();
}
}
foreach ($blockVars as $configLabel) {
if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
$config = new ConfigSection();
$config->setSection($entity);
$config->setLabel($configLabel);
$config->setType('text');
$config->setTypeCode('block');
$idSetImport = date('YmdHis') . microtime(true) . 'confobj' . $entity->getId();
$config->setIdImport(str_replace(".", "", $idSetImport));
$this->entityManager->persist($config);
$this->entityManager->flush();
}
}
foreach ($innerBlockVars as $configLabel) {
if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
$config = new ConfigSection();
$config->setSection($entity);
$config->setLabel($configLabel);
$config->setType('text');
$config->setTypeCode('innerBlock');
$idSetImport = date('YmdHis') . microtime(true) . 'confobj' . $entity->getId();
$config->setIdImport(str_replace(".", "", $idSetImport));
$this->entityManager->persist($config);
$this->entityManager->flush();
}
}
$this->entityManager->flush();
} else {
return;
}
}
public function afterUpdate(AfterEntityUpdatedEvent $event)
{
$entity = $event->getEntityInstance();
if ($entity instanceof Objet) {
if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
$idSetImport = date('YmdHis') . microtime(true) . 'objet' . $entity->getId();
$entity->setIdImport(str_replace(".", "", $idSetImport));
}
$configSection = $entity->getSection()->getConfigSections();
$configObjets = $entity->getConfigObjets();
$create = true;
foreach ($configObjets as $key => $value) {
if ($value->getConfigSection()->getSection() == $entity->getSection()) {
$create = false;
break;
}
}
if ($create) {
foreach ($configObjets as $key => $value) {
$this->entityManager->remove($value);
$this->entityManager->flush();
}
foreach ($configSection as $value) {
$configObjet = new ConfigObjet();
$configObjet->setConfigSection($value);
$configObjet->setObjet($entity);
$configObjet->setValeur($value->getValeur());
$idSetImport = date('YmdHis') . microtime(true) . 'confobj' . $entity->getId();
$configObjet->setIdImport(str_replace(".", "", $idSetImport));
$this->entityManager->persist($configObjet);
$this->entityManager->flush();
}
}
$this->entityManager->flush();
} else if ($entity instanceof Section) {
if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
$idSetImport = date('YmdHis') . microtime(true) . 'section' . $entity->getId();
$entity->setIdImport(str_replace(".", "", $idSetImport));
}
$entity->addProjet($this->tokenStorage->getToken()->getUser()->getProjet());
$blockHtml = $entity->getCodeHtml();
$htmlVars = $this->codeVariables($blockHtml);
$blockCSS = $entity->getCodeCss();
$cssVars = $this->codeVariables($blockCSS);
$blockJs = $entity->getCodeJs();
$jsVars = $this->codeVariables($blockJs);
$blockBlock = $entity->getCodeBlock();
$blockVars = $this->codeVariables($blockBlock);
$innerBlock = $entity->getCodeInnerBlock();
$innerBlockVars = $this->codeVariables($innerBlock);
foreach ($entity->getConfigSections() as $configSection) {
if ($configSection->getTypeCode() == 'html') {
if (!in_array($configSection->getLabel(), $htmlVars)) {
$this->entityManager->remove($configSection);
}
} else if ($configSection->getTypeCode() == 'css') {
if (!in_array($configSection->getLabel(), $cssVars)) {
$this->entityManager->remove($configSection);
}
} else if ($configSection->getTypeCode() == 'js') {
if (!in_array($configSection->getLabel(), $jsVars)) {
$this->entityManager->remove($configSection);
}
} else if ($configSection->getTypeCode() == 'block') {
if (!in_array($configSection->getLabel(), $blockVars)) {
$this->entityManager->remove($configSection);
}
} else if ($configSection->getTypeCode() == 'innerBlock') {
if (!in_array($configSection->getLabel(), $innerBlockVars)) {
$this->entityManager->remove($configSection);
}
}
}
/* foreach ($entity->getConfigSections() as $configSection) {
$inhtml = false;
$incss = false;
$injs = false;
$inblock = false;
$ininnerblock = false;
if (in_array($configSection->getLabel(), $htmlVars)) {
$inhtml = true;
}
if (in_array($configSection->getLabel(), $cssVars)) {
$incss = true;
}
if (in_array($configSection->getLabel(), $jsVars)) {
$injs = true;
}
if (in_array($configSection->getLabel(), $blockVars)) {
$inblock = true;
}
if (in_array($configSection->getLabel(), $innerBlockVars)) {
$ininnerblock = true;
}
if (!$inhtml && !$incss && !$injs && !$inblock && !$ininnerblock) {
$this->entityManager->remove($configSection);
}
} */
foreach ($htmlVars as $configLabel) {
if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
$config = $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel, 'section' => $entity, 'typeCode' => 'html']);
if ($config == null) {
$config = new ConfigSection();
$config->setType('text');
$config->setSection($entity);
$idSetImport = date('YmdHis') . microtime(true) . 'confobj' . $entity->getId();
$config->setIdImport(str_replace(".", "", $idSetImport));
}
$config->setLabel($configLabel);
$config->setTypeCode('html');
$this->entityManager->persist($config);
$this->entityManager->flush();
}
}
foreach ($cssVars as $configLabel) {
if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
$config = $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel, 'section' => $entity, 'typeCode' => 'css']);
if ($config == null) {
$config = new ConfigSection();
$config->setSection($entity);
$config->setType('text');
$idSetImport = date('YmdHis') . microtime(true) . 'confobj' . $entity->getId();
$config->setIdImport(str_replace(".", "", $idSetImport));
}
$config->setLabel($configLabel);
$config->setTypeCode('css');
$this->entityManager->persist($config);
$this->entityManager->flush();
}
}
foreach ($jsVars as $configLabel) {
if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
$config = $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel, 'section' => $entity, 'typeCode' => 'js']);
if ($config == null) {
$config = new ConfigSection();
$config->setSection($entity);
$config->setType('text');
$idSetImport = date('YmdHis') . microtime(true) . 'confobj' . $entity->getId();
$config->setIdImport(str_replace(".", "", $idSetImport));
}
$config->setLabel($configLabel);
$config->setTypeCode('js');
$this->entityManager->persist($config);
$this->entityManager->flush();
}
}
foreach ($blockVars as $configLabel) {
if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
$config = $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel, 'section' => $entity, 'typeCode' => 'block']);
if ($config == null) {
$config = new ConfigSection();
$config->setSection($entity);
$config->setType('text');
$idSetImport = date('YmdHis') . microtime(true) . 'confobj' . $entity->getId();
$config->setIdImport(str_replace(".", "", $idSetImport));
}
$config->setLabel($configLabel);
$config->setTypeCode('block');
$this->entityManager->persist($config);
$this->entityManager->flush();
}
}
foreach ($innerBlockVars as $configLabel) {
if ($configLabel != 'Block_Html' && $configLabel != 'auto' && $configLabel != 'Inner_Block') {
$config = $this->manager->getRepository(ConfigSection::class)->findOneBy(['label' => $configLabel, 'section' => $entity, 'typeCode' => 'innerBlock']);
if ($config == null) {
$config = new ConfigSection();
$config->setSection($entity);
$config->setType('text');
$idSetImport = date('YmdHis') . microtime(true) . 'confobj' . $entity->getId();
$config->setIdImport(str_replace(".", "", $idSetImport));
}
$config->setLabel($configLabel);
$config->setTypeCode('innerBlock');
$this->entityManager->persist($config);
$this->entityManager->flush();
}
}
$this->entityManager->flush();
} else {
return;
}
}
public function codeVariables($block)
{
$pattern = '/{{(.*?)}}/';
preg_match_all($pattern, $block, $matcheshtml);
return array_unique($matcheshtml[1]);
}
public function slugify($string)
{
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($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
}
}