src/EventSubscriber/ArticleSubscriber.php line 48

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