src/EventSubscriber/ArticleSubscriber.php line 207

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Article;
  4. use App\Entity\ArticleCategorie;
  5. use App\Entity\ArticleContenu;
  6. use App\Entity\ArticleCritere;
  7. use App\Entity\ArticleCritereValeur;
  8. use App\Entity\ArticleType;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  12. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  13. use Psr\Log\LoggerInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. class ArticleSubscriber implements EventSubscriberInterface
  17. {
  18.     private $entityManager;
  19.     private ManagerRegistry $manager;
  20.     public function __construct(
  21.         ManagerRegistry $manager,
  22.         EntityManagerInterface $entityManager,
  23.         LoggerInterface $logger,
  24.         TokenStorageInterface $tokenStorage
  25.     ) {
  26.         $this->manager $manager;
  27.         $this->entityManager $entityManager;
  28.         $this->logger $logger;
  29.         $this->tokenStorage $tokenStorage;
  30.     }
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             AfterEntityPersistedEvent::class => ['afterAdd'],
  35.             AfterEntityUpdatedEvent::class => ['afterUpdate']
  36.         ];
  37.     }
  38.     public function afterAdd(AfterEntityPersistedEvent $event)
  39.     {
  40.         $entity $event->getEntityInstance();
  41.         if ($entity instanceof ArticleType) {
  42.             $articles $entity->getArticles();
  43.             $nbr 0;
  44.             foreach ($articles as $article) {
  45.                 $nbr += count($article->getPage());
  46.             }
  47.             $entity->setNbrLiaison($nbr);
  48.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  49.                 $idSetImport date('YmdHis') . microtime(true) . 'articleType' $entity->getId() . "" $entity->getProjet()->getId();
  50.                 $entity->setIdImport(str_replace("."""$idSetImport));
  51.             }
  52.             if ($entity->getPermalink() == null || $entity->getPermalink() == '') {
  53.                 $time = new \DateTime();
  54.                 $year =  $time->format('y');
  55.                 $month =  $time->format('m');
  56.                 $day =  $time->format('d');
  57.                 $hour =  $time->format('H');
  58.                 $min =  $time->format('i');
  59.                 $sec =  $time->format('s');
  60.                 $idSetImport $year $month $day $entity->getId();
  61.                 $entity->setPermalink(str_replace("."""$idSetImport));
  62.             }
  63.             $articleTypes $entity->getProjet()->getArticleTypes();
  64.             $count 0;
  65.             foreach ($articleTypes as $key => $articleType) {
  66.                 if (strtolower($articleType->getType()) == strtolower($entity->getType())) {
  67.                     $count++;
  68.                 }
  69.             }
  70.             if ($count 1) {
  71.                 $this->entityManager->remove($entity);
  72.             }
  73.             $this->entityManager->flush();
  74.         } else if ($entity instanceof Article) {
  75.             $type $this->manager->getRepository(ArticleType::class)->findOneBy(['type' => $entity->getType()->getType()]);
  76.             $nbr 0;
  77.             $nbr += count($entity->getPage());
  78.             $type->setNbrLiaison($nbr);
  79.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  80.                 $idSetImport date('YmdHis') . microtime(true) . 'article' $entity->getId() . "" $entity->getType() . "" $entity->getType()->getProjet()->getId();
  81.                 $entity->setIdImport(str_replace("."""$idSetImport));
  82.             }
  83.             if ($entity->getPermalink() == null || $entity->getPermalink() == '') {
  84.                 $time = new \DateTime();
  85.                 $year =  $time->format('y');
  86.                 $month =  $time->format('m');
  87.                 $day =  $time->format('d');
  88.                 $hour =  $time->format('H');
  89.                 $min =  $time->format('i');
  90.                 $sec =  $time->format('s');
  91.                 
  92.                 $idSetImport $year $month $day $entity->getId();
  93.                 $entity->setPermalink(str_replace("."""$idSetImport));
  94.             }
  95.             $entity->setNomRedacteur($this->tokenStorage->getToken()->getUser());
  96.             $entity->setIdImportType($entity->getType()->getIdImport());
  97.             if ($entity->getCategorie() != null) {
  98.                 $entity->setIdImportCategorie($entity->getCategorie()->getIdImport());
  99.             }
  100.             $this->entityManager->flush();
  101.         } else if ($entity instanceof ArticleContenu) {
  102.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  103.                 $idSetImport date('YmdHis') . microtime(true) . 'articleContenu' $entity->getId() . "" $entity->getArticle()->getId() . $entity->getArticle()->getType()->getId() . $entity->getArticle()->getType()->getProjet()->getId();
  104.                 $entity->setIdImport(str_replace("."""$idSetImport));
  105.             }
  106.             $entity->setIdImportArticle($entity->getArticle()->getIdImport());
  107.             $this->entityManager->flush();
  108.         } else if ($entity instanceof ArticleCategorie) {
  109.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  110.                 $idSetImport  date('YmdHis') . microtime(true) . 'articleCategorie' $entity->getId() . "" $entity->getType() . "" $entity->getType()->getProjet()->getId();
  111.                 $entity->setIdImport(str_replace("."""$idSetImport));
  112.             }
  113.             $entity->setIdImportType($entity->getType()->getIdImport());
  114.             if ($entity->getPermalink() == null || $entity->getPermalink() == '') {
  115.                 $time = new \DateTime();
  116.                 $year =  $time->format('y');
  117.                 $month =  $time->format('m');
  118.                 $day =  $time->format('d');
  119.                 $hour =  $time->format('H');
  120.                 $min =  $time->format('i');
  121.                 $sec =  $time->format('s');
  122.                 $idSetImport $year $month $day $entity->getId();
  123.                 $entity->setPermalink(str_replace("."""$idSetImport));
  124.             }
  125.             $articleCategories $entity->getType()->getArticleCategories();
  126.             $count 0;
  127.             foreach ($articleCategories as $key => $articleCategorie) {
  128.                 if (strtolower($articleCategorie->getLabel()) == strtolower($entity->getLabel())) {
  129.                     $count++;
  130.                 }
  131.             }
  132.             if ($count 1) {
  133.                 $this->entityManager->remove($entity);
  134.             }
  135.             $this->entityManager->flush();
  136.         } else if ($entity instanceof ArticleCritere) {
  137.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  138.                 $idSetImport date('YmdHis') . microtime(true) . 'articleCritere' $entity->getId() . "" $entity->getType() . "" $entity->getType()->getProjet()->getId();
  139.                 $entity->setIdImport(str_replace("."""$idSetImport));
  140.             }
  141.             $entity->setIdImportType($entity->getType()->getIdImport());
  142.             $articleCriteres $entity->getType()->getArticleCriteres();
  143.             $count 0;
  144.             foreach ($articleCriteres as $key => $articleCritere) {
  145.                 if (strtolower($articleCritere->getLabel()) == strtolower($entity->getLabel())) {
  146.                     $count++;
  147.                 }
  148.             }
  149.             if ($count 1) {
  150.                 $this->entityManager->remove($entity);
  151.             }
  152.             $this->entityManager->flush();
  153.         } else if ($entity instanceof ArticleCritereValeur) {
  154.             if ($entity->getArticle() != null) {
  155.                 $entity->setIdImportArticle($entity->getArticle()->getIdImport());
  156.             }
  157.             $entity->setIdImportCritere($entity->getCritere()->getIdImport());
  158.             $this->entityManager->flush();
  159.         } else {
  160.             return;
  161.         }
  162.     }
  163.     public function afterUpdate(AfterEntityUpdatedEvent $event)
  164.     {
  165.         $entity $event->getEntityInstance();
  166.         if ($entity instanceof ArticleType) {
  167.             $articles $entity->getArticles();
  168.             $nbr 0;
  169.             foreach ($articles as $article) {
  170.                 $nbr += count($article->getPage());
  171.             }
  172.             $entity->setNbrLiaison($nbr);
  173.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  174.                 $idSetImport date('YmdHis') . microtime(true) . 'articleType' $entity->getId() . "" $entity->getProjet()->getId();
  175.                 $entity->setIdImport(str_replace("."""$idSetImport));
  176.             }
  177.             if ($entity->getPermalink() == null || $entity->getPermalink() == '') {
  178.                 $time = new \DateTime();
  179.                 $year =  $time->format('y');
  180.                 $month =  $time->format('m');
  181.                 $day =  $time->format('d');
  182.                 $hour =  $time->format('H');
  183.                 $min =  $time->format('i');
  184.                 $sec =  $time->format('s');
  185.                 $idSetImport $year $month $day $entity->getId();
  186.                 $entity->setPermalink(str_replace("."""$idSetImport));
  187.             }
  188.             $this->entityManager->flush();
  189.         } else if ($entity instanceof Article) {
  190.             $type $this->manager->getRepository(ArticleType::class)->findOneBy(['type' => $entity->getType()->getType()]);
  191.             $nbr 0;
  192.             $nbr += count($entity->getPage());
  193.             $type->setNbrLiaison($nbr);
  194.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  195.                 $idSetImport date('YmdHis') . microtime(true) . 'article' $entity->getId() . "" $entity->getType() . "" $entity->getType()->getProjet()->getId();
  196.                 $entity->setIdImport(str_replace("."""$idSetImport));
  197.             }
  198.             if ($entity->getPermalink() == null || $entity->getPermalink() == '') {
  199.                 $time = new \DateTime();
  200.                 $year =  $time->format('y');
  201.                 $month =  $time->format('m');
  202.                 $day =  $time->format('d');
  203.                 $hour =  $time->format('H');
  204.                 $min =  $time->format('i');
  205.                 $sec =  $time->format('s');
  206.                 $idSetImport $year $month $day $entity->getId();
  207.                 $entity->setPermalink(str_replace("."""$idSetImport));
  208.             }
  209.             $entity->setIdImportType($entity->getType()->getIdImport());
  210.             if ($entity->getCategorie() != null) {
  211.                 $entity->setIdImportCategorie($entity->getCategorie()->getIdImport());
  212.             }
  213.             $this->entityManager->flush();
  214.         } else if ($entity instanceof ArticleContenu) {
  215.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  216.                 $idSetImport date('YmdHis') . microtime(true) . 'articleContenu' $entity->getId() . "" $entity->getArticle()->getId() . $entity->getArticle()->getType()->getId() . $entity->getArticle()->getType()->getProjet()->getId();
  217.                 $entity->setIdImport(str_replace("."""$idSetImport));
  218.             }
  219.             $entity->setIdImportArticle($entity->getArticle()->getIdImport());
  220.             $this->entityManager->flush();
  221.         } else if ($entity instanceof ArticleCategorie) {
  222.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  223.                 $idSetImport date('YmdHis') . microtime(true) . 'articleCategorie' $entity->getId() . "" $entity->getType() . "" $entity->getType()->getProjet()->getId();
  224.                 $entity->setIdImport(str_replace("."""$idSetImport));
  225.             }
  226.             $entity->setIdImportType($entity->getType()->getIdImport());
  227.             if ($entity->getPermalink() == null || $entity->getPermalink() == '') {
  228.                 $time = new \DateTime();
  229.                 $year =  $time->format('y');
  230.                 $month =  $time->format('m');
  231.                 $day =  $time->format('d');
  232.                 $hour =  $time->format('H');
  233.                 $min =  $time->format('i');
  234.                 $sec =  $time->format('s');
  235.                 $idSetImport $year $month $day $entity->getId();
  236.                 $entity->setPermalink(str_replace("."""$idSetImport));
  237.             }
  238.             $this->entityManager->flush();
  239.         } else if ($entity instanceof ArticleCritere) {
  240.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  241.                 $idSetImport date('YmdHis') . microtime(true) . 'articleCritere' $entity->getId() . "" $entity->getType() . "" $entity->getType()->getProjet()->getId();
  242.                 $entity->setIdImport(str_replace("."""$idSetImport));
  243.             }
  244.             $entity->setIdImportType($entity->getType()->getIdImport());
  245.             $this->entityManager->flush();
  246.         } else if ($entity instanceof ArticleCritereValeur) {
  247.             if ($entity->getArticle() != null) {
  248.                 $entity->setIdImportArticle($entity->getArticle()->getIdImport());
  249.             }
  250.             $entity->setIdImportCritere($entity->getCritere()->getIdImport());
  251.             $this->entityManager->flush();
  252.         } else {
  253.             return;
  254.         }
  255.     }
  256. }