src/EventSubscriber/ArticleSubscriber.php line 194

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.                 $entity->setIdImport(date('Y-m-d-H-i-s') . '-article-type-' $entity->getId() . "-" $entity->getProjet()->getId());
  50.             }
  51.             if ($entity->getPermalink() == null || $entity->getPermalink() == '') {
  52.                 $time = new \DateTime();
  53.                 $year =  $time->format('y');
  54.                 $month =  $time->format('m');
  55.                 $day =  $time->format('d');
  56.                 $hour =  $time->format('H');
  57.                 $min =  $time->format('i');
  58.                 $sec =  $time->format('s');
  59.                 $entity->setPermalink($year $month $day $hour $min $sec $entity->getId());
  60.             }
  61.             $articleTypes $entity->getProjet()->getArticleTypes();
  62.             $count 0;
  63.             foreach ($articleTypes as $key => $articleType) {
  64.                 if (strtolower($articleType->getType()) == strtolower($entity->getType())) {
  65.                     $count++;
  66.                 }
  67.             }
  68.             if ($count 1) {
  69.                 $this->entityManager->remove($entity);
  70.             }
  71.             $this->entityManager->flush();
  72.         } else if ($entity instanceof Article) {
  73.             $type $this->manager->getRepository(ArticleType::class)->findOneBy(['type' => $entity->getType()->getType()]);
  74.             $nbr 0;
  75.             $nbr += count($entity->getPage());
  76.             $type->setNbrLiaison($nbr);
  77.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  78.                 $entity->setIdImport(date('Y-m-d-H-i-s') . '-article-' $entity->getId() . "-" $entity->getType() . "-" $entity->getType()->getProjet()->getId());
  79.             }
  80.             if ($entity->getPermalink() == null || $entity->getPermalink() == '') {
  81.                 $time = new \DateTime();
  82.                 $year =  $time->format('y');
  83.                 $month =  $time->format('m');
  84.                 $day =  $time->format('d');
  85.                 $hour =  $time->format('H');
  86.                 $min =  $time->format('i');
  87.                 $sec =  $time->format('s');
  88.                 $entity->setPermalink($year $month $day $hour $min $sec $entity->getId());
  89.             }
  90.             $entity->setNomRedacteur($this->tokenStorage->getToken()->getUser());
  91.             $entity->setIdImportType($entity->getType()->getIdImport());
  92.             if ($entity->getCategorie() != null) {
  93.                 $entity->setIdImportCategorie($entity->getCategorie()->getIdImport());
  94.             }
  95.             $this->entityManager->flush();
  96.         } else if ($entity instanceof ArticleContenu) {
  97.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  98.                 $entity->setIdImport(date('Y-m-d-H-i-s') . '-article-contenu-' $entity->getId() . "-" $entity->getArticle()->getId() . $entity->getArticle()->getType()->getId() . $entity->getArticle()->getType()->getProjet()->getId());
  99.             }
  100.             $entity->setIdImportArticle($entity->getArticle()->getIdImport());
  101.             $this->entityManager->flush();
  102.         } else if ($entity instanceof ArticleCategorie) {
  103.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  104.                 $entity->setIdImport(date('Y-m-d-H-i-s') . '-article-categorie-' $entity->getId() . "-" $entity->getType() . "-" $entity->getType()->getProjet()->getId());
  105.             }
  106.             $entity->setIdImportType($entity->getType()->getIdImport());
  107.             if ($entity->getPermalink() == null || $entity->getPermalink() == '') {
  108.                 $time = new \DateTime();
  109.                 $year =  $time->format('y');
  110.                 $month =  $time->format('m');
  111.                 $day =  $time->format('d');
  112.                 $hour =  $time->format('H');
  113.                 $min =  $time->format('i');
  114.                 $sec =  $time->format('s');
  115.                 $entity->setPermalink($year $month $day $hour $min $sec $entity->getId());
  116.             }
  117.             $articleCategories $entity->getType()->getArticleCategories();
  118.             $count 0;
  119.             foreach ($articleCategories as $key => $articleCategorie) {
  120.                 if (strtolower($articleCategorie->getLabel()) == strtolower($entity->getLabel())) {
  121.                     $count++;
  122.                 }
  123.             }
  124.             if ($count 1) {
  125.                 $this->entityManager->remove($entity);
  126.             }
  127.             $this->entityManager->flush();
  128.         } else if ($entity instanceof ArticleCritere) {
  129.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  130.                 $entity->setIdImport(date('Y-m-d-H-i-s') . '-article-critere-' $entity->getId() . "-" $entity->getType() . "-" $entity->getType()->getProjet()->getId());
  131.             }
  132.             $entity->setIdImportType($entity->getType()->getIdImport());
  133.             $articleCriteres $entity->getType()->getArticleCriteres();
  134.             $count 0;
  135.             foreach ($articleCriteres as $key => $articleCritere) {
  136.                 if (strtolower($articleCritere->getLabel()) == strtolower($entity->getLabel())) {
  137.                     $count++;
  138.                 }
  139.             }
  140.             if ($count 1) {
  141.                 $this->entityManager->remove($entity);
  142.             }
  143.             $this->entityManager->flush();
  144.         } else if ($entity instanceof ArticleCritereValeur) {
  145.             if ($entity->getArticle() != null) {
  146.                 $entity->setIdImportArticle($entity->getArticle()->getIdImport());
  147.             }
  148.             $entity->setIdImportCritere($entity->getCritere()->getIdImport());
  149.             $this->entityManager->flush();
  150.         } else {
  151.             return;
  152.         }
  153.     }
  154.     public function afterUpdate(AfterEntityUpdatedEvent $event)
  155.     {
  156.         $entity $event->getEntityInstance();
  157.         if ($entity instanceof ArticleType) {
  158.             $articles $entity->getArticles();
  159.             $nbr 0;
  160.             foreach ($articles as $article) {
  161.                 $nbr += count($article->getPage());
  162.             }
  163.             $entity->setNbrLiaison($nbr);
  164.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  165.                 $entity->setIdImport(date('Y-m-d-H-i-s') . '-article-type-' $entity->getId() . "-" $entity->getProjet()->getId());
  166.             }
  167.             if ($entity->getPermalink() == null || $entity->getPermalink() == '') {
  168.                 $time = new \DateTime();
  169.                 $year =  $time->format('y');
  170.                 $month =  $time->format('m');
  171.                 $day =  $time->format('d');
  172.                 $hour =  $time->format('H');
  173.                 $min =  $time->format('i');
  174.                 $sec =  $time->format('s');
  175.                 $entity->setPermalink($year $month $day $hour $min $sec $entity->getId());
  176.             }
  177.             $this->entityManager->flush();
  178.         } else if ($entity instanceof Article) {
  179.             $type $this->manager->getRepository(ArticleType::class)->findOneBy(['type' => $entity->getType()->getType()]);
  180.             $nbr 0;
  181.             $nbr += count($entity->getPage());
  182.             $type->setNbrLiaison($nbr);
  183.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  184.                 $entity->setIdImport(date('Y-m-d-H-i-s') . '-article-' $entity->getId() . "-" $entity->getType() . "-" $entity->getType()->getProjet()->getId());
  185.             }
  186.             if ($entity->getPermalink() == null || $entity->getPermalink() == '') {
  187.                 $time = new \DateTime();
  188.                 $year =  $time->format('y');
  189.                 $month =  $time->format('m');
  190.                 $day =  $time->format('d');
  191.                 $hour =  $time->format('H');
  192.                 $min =  $time->format('i');
  193.                 $sec =  $time->format('s');
  194.                 $entity->setPermalink($year $month $day $hour $min $sec $entity->getId());
  195.             }
  196.             $entity->setIdImportType($entity->getType()->getIdImport());
  197.             if ($entity->getCategorie() != null) {
  198.                 $entity->setIdImportCategorie($entity->getCategorie()->getIdImport());
  199.             }
  200.             $this->entityManager->flush();
  201.         } else if ($entity instanceof ArticleContenu) {
  202.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  203.                 $entity->setIdImport(date('Y-m-d-H-i-s') . '-article-contenu-' $entity->getId() . "-" $entity->getArticle()->getId() . $entity->getArticle()->getType()->getId() . $entity->getArticle()->getType()->getProjet()->getId());
  204.             }
  205.             $entity->setIdImportArticle($entity->getArticle()->getIdImport());
  206.             $this->entityManager->flush();
  207.         } else if ($entity instanceof ArticleCategorie) {
  208.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  209.                 $entity->setIdImport(date('Y-m-d-H-i-s') . '-article-categorie-' $entity->getId() . "-" $entity->getType() . "-" $entity->getType()->getProjet()->getId());
  210.             }
  211.             $entity->setIdImportType($entity->getType()->getIdImport());
  212.             if ($entity->getPermalink() == null || $entity->getPermalink() == '') {
  213.                 $time = new \DateTime();
  214.                 $year =  $time->format('y');
  215.                 $month =  $time->format('m');
  216.                 $day =  $time->format('d');
  217.                 $hour =  $time->format('H');
  218.                 $min =  $time->format('i');
  219.                 $sec =  $time->format('s');
  220.                 $entity->setPermalink($year $month $day $hour $min $sec $entity->getId());
  221.             }
  222.             $this->entityManager->flush();
  223.         } else if ($entity instanceof ArticleCritere) {
  224.             if ($entity->getIdImport() == null || $entity->getIdImport() == '') {
  225.                 $entity->setIdImport(date('Y-m-d-H-i-s') . '-article-critere-' $entity->getId() . "-" $entity->getType() . "-" $entity->getType()->getProjet()->getId());
  226.             }
  227.             $entity->setIdImportType($entity->getType()->getIdImport());
  228.             $this->entityManager->flush();
  229.         } else if ($entity instanceof ArticleCritereValeur) {
  230.             if ($entity->getArticle() != null) {
  231.                 $entity->setIdImportArticle($entity->getArticle()->getIdImport());
  232.             }
  233.             $entity->setIdImportCritere($entity->getCritere()->getIdImport());
  234.             $this->entityManager->flush();
  235.         } else {
  236.             return;
  237.         }
  238.     }
  239. }