src/EventSubscriber/IASqlSubscriber.php line 122

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\IASql;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  8. use Doctrine\DBAL\Exception as DBALException;
  9. use PDO;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  12. class IASqlSubscriber implements EventSubscriberInterface
  13. {
  14.     private $entityManager;
  15.     private ManagerRegistry $manager;
  16.     public function __construct(
  17.         ManagerRegistry $manager,
  18.         EntityManagerInterface $entityManager,
  19.         TokenStorageInterface $tokenStorage
  20.     ) {
  21.         $this->manager $manager;
  22.         $this->entityManager $entityManager;
  23.         $this->tokenStorage $tokenStorage;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             AfterEntityPersistedEvent::class => ['afterAdd'],
  29.             AfterEntityUpdatedEvent::class => ['afterUpdate'],
  30.         ];
  31.     }
  32.     public function afterAdd(AfterEntityPersistedEvent $event)
  33.     {
  34.         $entity $event->getEntityInstance();
  35.         if ($entity instanceof IASql) {
  36.             $keysSource = [];
  37.             $keysDestination = [];
  38.             $champsTable=[];
  39.             $champsTableD=[];
  40.             $sqlSource strtolower($entity->getSourceSql());
  41.             if ($sqlSource != "") {
  42.                 $sqlSource $this->cleanTextSql($sqlSource);
  43.                 $describe $this->getTableColumnsFromQuery($sqlSource);
  44.                 $tableNames $this->getTableNamesFromQuery($sqlSource);
  45.                 $resultats1 $this->popup($sqlSource);
  46.                 if ($resultats1 != null) {
  47.                     foreach (array_slice($resultats1010) as $resultat) {
  48.                         $keysSource array_keys($resultat);
  49.                     }
  50.                     foreach ($keysSource as $column) {
  51.                         foreach ($tableNames as $tableName) {
  52.                             foreach ($describe as $key => $desc) {
  53.                                 if ($key == $tableName) {
  54.                                     if (in_array($column$desc)) {
  55.                                         $champsTable[] = $column ' : ' $tableName;
  56.                                     }
  57.                                 }
  58.                             }
  59.                         }
  60.                     }
  61.                     $entity->setIaSqlChamps(implode("\n"$champsTable));
  62.                     $entity->setSourceChamps(implode("\n"$keysSource));
  63.                     $this->entityManager->persist($entity);
  64.                 }
  65.             } else {
  66.                 $entity->setSourceChamps("");
  67.                 $this->entityManager->persist($entity);
  68.             }
  69.             $sqlDestination strtolower($entity->getDestinationSql());
  70.             if ($sqlDestination != "") {
  71.                 $sqlDestination $this->cleanTextSql($sqlDestination);
  72.                 $describeD $this->getTableColumnsFromQuery($sqlDestination);
  73.                 $tableNamesD $this->getTableNamesFromQuery($sqlDestination);
  74.                 $resultats2 $this->popup($sqlDestination);
  75.                 if ($resultats2 != null) {
  76.                     foreach (array_slice($resultats2010) as $resultat) {
  77.                         $keysDestination array_keys($resultat);
  78.                     }
  79.                     foreach ($keysDestination as $column) {
  80.                         foreach ($tableNamesD as $tableName) {
  81.                             foreach ($describeD as $key => $desc) {
  82.                                 if ($key == $tableName) {
  83.                                     if (in_array($column$desc)) {
  84.                                         $champsTableD[] = $column ' : ' $tableName;
  85.                                     }
  86.                                 }
  87.                             }
  88.                         }
  89.                     }
  90.                     $entity->setIaSqlChampsD(implode("\n"$champsTableD));
  91.                     $entity->setDestinationChamps(implode("\n"$keysDestination));
  92.                     $this->entityManager->persist($entity);
  93.                 }
  94.             } else {
  95.                 $entity->setDestinationChamps("");
  96.                 $this->entityManager->persist($entity);
  97.             }
  98.             $this->entityManager->flush();
  99.             $count 0;
  100.             $jeuxSql $this->manager->getRepository(IASql::class)->findAll();
  101.             foreach ($jeuxSql as $jeuSql) {
  102.                 if (strtolower($jeuSql->getLabel()) == strtolower($entity->getLabel())) {
  103.                     $count++;
  104.                 }
  105.             }
  106.             if ($count 1) {
  107.                 $this->entityManager->remove($entity);
  108.                 $this->entityManager->flush();
  109.             }
  110.         }
  111.     }
  112.     public function afterUpdate(AfterEntityUpdatedEvent $event)
  113.     {
  114.         $entity $event->getEntityInstance();
  115.         if ($entity instanceof IASql) {
  116.             $keysSource = [];
  117.             $keysDestination = [];
  118.             $sqlSource strtolower($entity->getSourceSql());
  119.             $champsTable = [];
  120.             $champsTableD = [];
  121.             if ($sqlSource != "") {
  122.                 $sqlSource $this->cleanTextSql($sqlSource);
  123.                 $describe $this->getTableColumnsFromQuery($sqlSource);
  124.                 $tableNames $this->getTableNamesFromQuery($sqlSource);
  125.                 $resultats1 $this->popup($sqlSource);
  126.                 if ($resultats1 != null) {
  127.                     foreach (array_slice($resultats1010) as $resultat) {
  128.                         $keysSource array_keys($resultat);
  129.                     }
  130.                     foreach ($keysSource as $column) {
  131.                         foreach ($tableNames as $tableName) {
  132.                             foreach ($describe as $key => $desc) {
  133.                                 if ($key == $tableName) {
  134.                                     if (in_array($column$desc)) {
  135.                                         $champsTable[] = $column ' : ' $tableName;
  136.                                     }
  137.                                 }
  138.                             }
  139.                         }
  140.                     }
  141.                     $entity->setIaSqlChamps(implode("\n"$champsTable));
  142.                     $entity->setSourceChamps(implode("\n"$keysSource));
  143.                     $this->entityManager->persist($entity);
  144.                 } else {
  145.                     $entity->setSourceChamps("");
  146.                     $this->entityManager->persist($entity);
  147.                 }
  148.             } else {
  149.                 $entity->setSourceChamps("");
  150.                 $this->entityManager->persist($entity);
  151.             }
  152.             $sqlDestination strtolower($entity->getDestinationSql());
  153.             if ($sqlDestination != "") {
  154.                 $sqlDestination $this->cleanTextSql($sqlDestination);
  155.                 $describeD $this->getTableColumnsFromQuery($sqlDestination);
  156.                 $tableNamesD $this->getTableNamesFromQuery($sqlDestination);
  157.                 $resultats2 $this->popup($sqlDestination);
  158.                 if ($resultats2 != null) {
  159.                     foreach (array_slice($resultats2010) as $resultat) {
  160.                         $keysDestination array_keys($resultat);
  161.                     }
  162.                     foreach ($keysDestination as $column) {
  163.                         foreach ($tableNamesD as $tableName) {
  164.                             foreach ($describeD as $key => $desc) {
  165.                                 if ($key == $tableName) {
  166.                                     if (in_array($column$desc)) {
  167.                                         $champsTableD[] = $column ' : ' $tableName;
  168.                                     }
  169.                                 }
  170.                             }
  171.                         }
  172.                     }
  173.                     $entity->setIaSqlChampsD(implode("\n"$champsTableD));
  174.                     $entity->setDestinationChamps(implode("\n"$keysDestination));
  175.                     $this->entityManager->persist($entity);
  176.                 } else {
  177.                     $entity->setDestinationChamps("");
  178.                     $this->entityManager->persist($entity);
  179.                 }
  180.             } else {
  181.                 $entity->setDestinationChamps("");
  182.                 $this->entityManager->persist($entity);
  183.             }
  184.             $this->entityManager->flush();
  185.         }
  186.     }
  187.     public function popup($reqSource)
  188.     {
  189.         try {
  190.             $entityManager $this->manager->getManager();
  191.             if ($reqSource != "") {
  192.                 $conn $entityManager->getConnection();
  193.                 $sql $reqSource//."WHERE projet_id = ".$projet->getId();
  194.                 $stmt $conn->prepare($sql);
  195.                 $resultSet $stmt->executeQuery();
  196.                 return  $resultSet->fetchAllAssociative();
  197.             } else {
  198.                 return null;
  199.             }
  200.         } catch (DBALException $e) {
  201.             echo "<div class='alert alert-danger' role='alert'>Vous avez une erreur de syntaxe SQL !</p></div>";
  202.         }
  203.     }
  204.     function cleanTextSql($text)
  205.     {
  206.         $text htmlspecialchars_decode($text);
  207.         $text preg_replace('#<[^>]+>#'' '$text);
  208.         //$text = strip_tags($text);
  209.         $text str_replace("’"''$text);
  210.         $text str_replace("`"''$text);
  211.         $text str_replace('/''slach'$text);
  212.         $text str_replace('&nbsp;'''$text);
  213.         //$text = str_replace(';', ' limit 10', $text);
  214.         $text str_replace(';'''$text);
  215.         $text str_replace('< ''<'$text);
  216.         $text str_replace(' >''>'$text);
  217.         $text str_replace('»''mot'$text);
  218.         $text str_replace('«''mot'$text);
  219.         $text str_replace('#'''$text);
  220.         $text str_replace('œ''oe'$text);
  221.         $text preg_replace('/\:/''mot'$text);
  222.         $text str_replace('°'''$text);
  223.         $text str_replace(' – '''$text);
  224.         $utf8 = array(
  225.             '/[áàâãªä]/u'   =>   'a',
  226.             '/[ÁÀÂÃÄ]/u'    =>   'A',
  227.             '/[ÍÌÎÏ]/u'     =>   'I',
  228.             '/[íìîï]/u'     =>   'i',
  229.             '/[éèêë]/u'     =>   'e',
  230.             '/[ÉÈÊË]/u'     =>   'E',
  231.             '/[óòôõºö]/u'   =>   'o',
  232.             '/[ÓÒÔÕÖ]/u'    =>   'O',
  233.             '/[úùûü]/u'     =>   'u',
  234.             '/[ÚÙÛÜ]/u'     =>   'U',
  235.             '/ç/'           =>   'c',
  236.             '/Ç/'           =>   'C',
  237.             '/ñ/'           =>   'n',
  238.             '/Ñ/'           =>   'N',
  239.             //'/_/'           =>   '-', // UTF-8 hyphen to "normal" hyphen
  240.             '/[’‘‹›‚]/u'    =>   ' '// Literally a single quote
  241.             '/[“”«»„]/u'    =>   'mot'// Double quote
  242.             '/ /'           =>   ' '// nonbreaking space (equiv. to 0x160)
  243.         );
  244.         return preg_replace(array_keys($utf8), array_values($utf8), $text);
  245.     }
  246.     // SELECT `article`.*, `article_contenu`.*
  247.     //             FROM `article` 
  248.     //                 LEFT JOIN `article_contenu` ON `article_contenu`.`article_id` = `article`.`id`; 
  249.     //  SELECT 'article.*' , 
  250.     //                 'article_contenu.*' 
  251.     //                  FROM article
  252.     //                                 LEFT JOIN article_contenu ON 'article_contenu.article_id' = 'article.id';
  253.     function getTableNameFromQuery($query)
  254.     {
  255.         // Extract the table name from the FROM clause of the query
  256.         preg_match('/\bFROM\s+`?(\w+)`?\b/i'$query$matches);
  257.         if (count($matches) > 1) {
  258.             return $matches[1];
  259.         }
  260.         return false;
  261.     }
  262.     function getTableNamesFromQuery($query)
  263.     {
  264.         try {
  265.             // $conn =   $this->manager->getManager()->getConnection();
  266.             // $params = $conn->getParams();
  267.             // dd($params);
  268.             // Extract the table names from the FROM clause and the JOIN clause of the query
  269.             preg_match_all('/\bFROM\s+`?(\w+)`?\b/i'$query$matches1);
  270.             preg_match_all('/\bJOIN\s+`?(\w+)`?\b/i'$query$matches2);
  271.             $tableNames array_merge($matches1[1], $matches2[1]);
  272.             $tableNames array_unique($tableNames);
  273.             return $tableNames;
  274.         } catch (DBALException $e) {
  275.             echo "<div class='alert alert-danger' role='alert'>Vous avez une erreur de syntaxe SQL !</p></div>";
  276.         }
  277.     }
  278.     function getTableColumnsFromQuery($query)
  279.     {
  280.         try {
  281.             // Extract the table names from the FROM clause and the JOIN clause of the query
  282.             preg_match_all('/\bFROM\s+`?(\w+)`?\b/i'$query$matches1);
  283.             preg_match_all('/\bJOIN\s+`?(\w+)`?\b/i'$query$matches2);
  284.             $tableNames array_merge($matches1[1], $matches2[1]);
  285.             $tableNames array_unique($tableNames);
  286.             $conn $this->manager->getManager()->getConnection();
  287.             $tableColumns = array();
  288.             foreach ($tableNames as $tableName) {
  289.                 $stmt $conn->prepare("DESCRIBE $tableName");
  290.                 $stmt->execute();
  291.                 $tableColumns[$tableName] = $stmt->executeQuery()->fetchAll(PDO::FETCH_COLUMN);
  292.             }
  293.             return $tableColumns;
  294.         } catch (DBALException $e) {
  295.             echo "<div class='alert alert-danger' role='alert'>Vous avez une erreur de syntaxe SQL !</p></div>";
  296.         }
  297.     }
  298.     // public function afterUpdate(AfterEntityUpdatedEvent $event)
  299.     // {
  300.     //     $entity = $event->getEntityInstance();
  301.     //     if ($entity instanceof IASql) {
  302.     //         $champsSource = "";
  303.     //         $champsDestination = "";
  304.     //         $sqlSource = strtolower($entity->getSourceSql());
  305.     //         $champs = trim(substr($sqlSource, 6, strpos($sqlSource, "from") - strlen("from") - 2));
  306.     //         if($champs == "*"){
  307.     //             $table_name=trim($this->get_string_between(trim($sqlSource),'from','where'));
  308.     //             $columnNames=$this->getColumnName($table_name);
  309.     //             foreach($columnNames as $columnName){
  310.     //                 $champs=(implode(",",$columnName));
  311.     //             }
  312.     //         }
  313.     //         //dd($champs);
  314.     //         $champs = explode(",", $champs);
  315.     //         $resultats1 = $this->popupSource($sqlSource);
  316.     //         if ($resultats1 != null) {
  317.     //             foreach ($resultats1 as  $resultat) {
  318.     //                 foreach ($champs as $champ) {
  319.     //                     if (array_key_exists(trim($champ), $resultat)) {
  320.     //                         foreach ($resultat as $value) {
  321.     //                             $champsSource = $champsSource . $value . ",";
  322.     //                         }
  323.     //                     }
  324.     //                 }
  325.     //             }
  326.     //             $entity->setSourceChamps($champsSource);
  327.     //         } else {
  328.     //             $entity->setSourceChamps("");
  329.     //         }
  330.     //         /*********Destination */
  331.     //         $sqlDestination = strtolower($entity->getDestinationSql());
  332.     //         $champs2 = trim(substr($sqlDestination, 6, strpos($sqlDestination, "from") - strlen("from") - 2));
  333.     //         if($champs2 == "*"){
  334.     //             $table_name=trim($this->get_string_between(trim( $sqlDestination),'from','where'));
  335.     //             $columnNames=$this->getColumnName($table_name);
  336.     //             foreach($columnNames as $columnName){
  337.     //                 $champs2=(implode(",",$columnName));
  338.     //             }
  339.     //         }
  340.     //         $champs2 = explode(",", $champs2);
  341.     //         $resultats2 = $this->popupDestination($sqlDestination);
  342.     //         if ($resultats2 != null) {
  343.     //             foreach ($resultats2 as  $resultat) {
  344.     //                 foreach ($champs2 as $champ) {
  345.     //                     if (array_key_exists(trim($champ), $resultat)) {
  346.     //                         foreach ($resultat as $value) {
  347.     //                             $champsDestination = $champsDestination . $value . ",";
  348.     //                         }
  349.     //                     }
  350.     //                 }
  351.     //             }
  352.     //             $entity->setDestinationChamps($champsDestination);;
  353.     //         } else {
  354.     //             $entity->setDestinationChamps("");
  355.     //         }
  356.     //         $this->entityManager->persist($entity);
  357.     //         $this->entityManager->flush();
  358.     //     }
  359.     // }
  360. }