src/EventSubscriber/EasyAdminSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Commune;
  4. use App\Entity\Contact;
  5. use App\Entity\Document;
  6. use App\Entity\EventType;
  7. use App\Entity\Glossaire;
  8. use App\Entity\Pages;
  9. use App\Entity\PagesLegales;
  10. use App\Entity\PolitiqueDepartementale;
  11. use App\Entity\Post;
  12. use App\Entity\RssArticle;
  13. use App\Entity\RssEvent;
  14. use App\Entity\Service;
  15. use App\Entity\ServiceGrandProfil;
  16. use App\Entity\ServiceSousProfil;
  17. use App\Entity\ServiceTheme;
  18. use App\Service\ElasticsearchService;
  19. use App\Service\UrlFormatService;
  20. use DateInterval;
  21. use Doctrine\ORM\EntityManagerInterface;
  22. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityDeletedEvent;
  23. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  24. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. class EasyAdminSubscriber implements EventSubscriberInterface
  27. {
  28.     public function __construct(
  29.         private EntityManagerInterface $entityManager,
  30.         private UrlFormatService $url_service,
  31.         private ElasticsearchService $es_service,
  32.     ) {
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             BeforeEntityPersistedEvent::class => ['add'],
  38.             BeforeEntityUpdatedEvent::class => ['update'],
  39.             BeforeEntityDeletedEvent::class => ['remove']
  40.         ];
  41.     }
  42.     public function add(BeforeEntityPersistedEvent $event): void
  43.     {
  44.         $entity $event->getEntityInstance();
  45.         if ($entity instanceof ServiceGrandProfil) {
  46.             $this->updateGrandProfil($entity);
  47.         } else if ($entity instanceof ServiceTheme) {
  48.             $this->updateTheme($entity);
  49.         } else if ($entity instanceof RssArticle) {
  50.             $this->updateWidget($entity);
  51.         } else if ($entity instanceof RssEvent) {
  52.             $this->updateWidget($entity);
  53.         } else if ($entity instanceof Contact) {
  54.             $this->updateContact($entity);
  55.         } else if ($entity instanceof Post) {
  56.             $entity->setDefautFil(false);
  57.             $this->setPostDateDebut($entity);
  58.         }
  59.         $this->udpateEsDocument($entity"add");
  60.     }
  61.     public function update(BeforeEntityUpdatedEvent $event): void
  62.     {
  63.         $entity $event->getEntityInstance();
  64.         if ($entity instanceof ServiceGrandProfil) {
  65.             $this->updateGrandProfil($entity);
  66.         } else if ($entity instanceof ServiceSousProfil) {
  67.             $this->updateSousProfil($entity);
  68.         } else if ($entity instanceof ServiceTheme) {
  69.             $this->updateTheme($entity);
  70.         } else if ($entity instanceof RssArticle) {
  71.             $this->updateWidget($entity);
  72.         } else if ($entity instanceof RssEvent) {
  73.             $this->updateWidget($entity);
  74.         } else if ($entity instanceof Contact) {
  75.             $this->updateContact($entity);
  76.         } else if ($entity instanceof Post) {
  77.             $entity->setDefautFil(false);
  78.             $this->setPostDateDebut($entity);
  79.         }
  80.         $this->udpateEsDocument($entity"update");
  81.     }
  82.     
  83.     public function remove(BeforeEntityDeletedEvent $event): void
  84.     {
  85.         $entity $event->getEntityInstance();
  86.         $this->removeEsDocument($entity);
  87.     }
  88.     public function updateWidget(mixed $entity): void
  89.     {
  90.         $widget '<div data-src-rss="https://info.lenord.fr/rss/' $entity->getAlias() . '"></div><script src="https://info.lenord.fr/rss-grid-widget/rss-grid-widget.js"></script>';
  91.         $entity->setWidget($widget);
  92.     }
  93.     //Elastic
  94.     public function removeEsDocument(mixed $entity): void
  95.     {
  96.         if ($entity instanceof Document) {
  97.             $this->es_service->deleteDocument('lenord''lenord_document_' $entity->getId());
  98.         } else if ($entity instanceof PolitiqueDepartementale) {
  99.             $this->es_service->deleteDocument('lenord''lenord_politique_' $entity->getId());
  100.         } else if ($entity instanceof Pages) {
  101.             $this->es_service->deleteDocument('lenord''lenord_pages_' $entity->getId());
  102.         } else if ($entity instanceof Service) {
  103.             $this->es_service->deleteDocument('lenord''services_service_' $entity->getId());
  104.         } else if ($entity instanceof Post) {
  105.             $this->es_service->deleteDocument('lenord''nordinfo_post_' $entity->getId());
  106.         } else if ($entity instanceof Glossaire) {
  107.             $this->es_service->deleteDocument('lenord''lenord_glossaire_' $entity->getId());
  108.         } else if ($entity instanceof ServiceSousProfil) {
  109.             //chercher tous les services ayant ce sous profil
  110.             foreach ($entity->getServices() as $service) {
  111.                 $service->removeSousprofil($entity);
  112.                 $this->entityManager->persist($service);
  113.                 $this->entityManager->flush();
  114.                 $this->updateService($service);
  115.             }
  116.         } else if ($entity instanceof Contact) {
  117.             $this->es_service->deleteDocument('lenord''lenord_elu_' $entity->getId());
  118.         }
  119.     }
  120.     public function udpateEsDocument(mixed $entitystring $type "add"): void
  121.     {
  122.         $this->entityManager->persist($entity);
  123.         $this->entityManager->flush();
  124.         if ($entity instanceof Document) {
  125.             $entity->setAliasUrl($this->url_service->formatAlias($entity->getTitre()));
  126.             $this->setFileSize($entity);
  127.             if ($entity->getStatut() !== null && $entity->getStatut()->getId() == 5) {
  128.                 $types = [];
  129.                 foreach ($entity->getTypes() as $type) {
  130.                     array_push($types$type->getTitre());
  131.                 }
  132.                 $datas = ['website' => "lenord_document"'entity_id' => $entity->getId(), 'entity_type' => "document"'content' => $entity->getContent(), 'title' => $entity->getTitre(), 'subtitle' => $entity->getDescription(), 'slug' => $entity->getAliasUrl(), 'types' => $types'date_publication' => ($entity->getDatePublication() != null) ? $entity->getDatePublication()->format('d/m/Y') : null'format' => $entity->getFormatFile(), 'size' => $entity->getPoids()];
  133.                 $this->es_service->addDocument('lenord''lenord_document_' $entity->getId(), $datas);
  134.             } else {
  135.                 $this->es_service->deleteDocument('lenord''lenord_document_' $entity->getId());
  136.             }
  137.         } else if ($entity instanceof PolitiqueDepartementale) {
  138.             $entity->setAliasUrl($this->url_service->formatAlias($entity->getTitre()));
  139.             if ($entity->getStatut() != null && $entity->getStatut()->getId() == 5) {
  140.                 $datas = ['website' => "lenord"'entity_id' => $entity->getId(), 'entity_type' => "politique"'content' => $entity->getContent(), 'title' => $entity->getTitre(), 'pictogramme' => $entity->getPictogramme(), 'slug' => $entity->getAliasUrl()];
  141.                 $this->es_service->addDocument('lenord''lenord_politique_' $entity->getId(), $datas);
  142.             } else {
  143.                 $this->es_service->deleteDocument('lenord''lenord_politique_' $entity->getId());
  144.             }
  145.         } else if ($entity instanceof Pages) {
  146.             //$entity->setAliasUrl($this->url_service->formatAlias($entity->getTitre()));
  147.             if ($entity->getActif() == 1) {
  148.                 $datas = ['website' => "lenord"'entity_id' => $entity->getId(), 'entity_type' => "pages"'title' => $entity->getTitre(), 'content' => $entity->getContent(), 'slug' => $entity->getAliasUrl(), 'pictogramme' => $entity->getUrlIcone()];
  149.                 $this->es_service->addDocument('lenord''lenord_pages_' $entity->getId(), $datas);
  150.             } else {
  151.                 $this->es_service->deleteDocument('lenord''lenord_pages_' $entity->getId());
  152.             }
  153.         } else if ($entity instanceof PagesLegales) {
  154.             if ($entity->getActif() == 1) {
  155.                 $datas = ['website' => "lenord"'entity_id' => $entity->getId(), 'entity_type' => "pagelegale"'title' => $entity->getTitre(), 'content' => $entity->getContent(), 'slug' => $entity->getSlug()];
  156.                 $this->es_service->addDocument('lenord''lenord_pagelegale_' $entity->getId(), $datas);
  157.             } else {
  158.                 $this->es_service->deleteDocument('lenord''lenord_pagelegale_' $entity->getId());
  159.             }
  160.         } else if ($entity instanceof Service) {
  161.             $this->updateService($entity);
  162.         } else if ($entity instanceof Post) {
  163.             $entity->setDateUpdate(new \DateTime);
  164.             $this->entityManager->persist($entity);
  165.             //Gestion des dates
  166.             //$entity->getJson();
  167.             if (
  168.                 $entity->getStatus() != null && $entity->getStatus()->getId() == &&
  169.                 ($entity->getType()->getType() == 'article'
  170.                     || $entity->getType()->getType() == 'video' || ($entity->getType()->getType() == 'event') || $entity->getArchive() == false)
  171.             ) {
  172.                 $datas $this->setPostDatas($entity);
  173.                 $this->es_service->addDocument('lenord''nordinfo_post_' $entity->getId(), $datas);
  174.             } else {
  175.                 //Supprimer si changement de statut
  176.                 if (($entity->getStatus()->getId() !== 5
  177.                     && ($entity->getType()->getType() == 'article'
  178.                         || $entity->getType()->getType() == 'video' || $entity->getType()->getType() == 'event')) || $entity->getArchive() == true) {
  179.                     $this->es_service->deleteDocument('lenord''nordinfo_post_' $entity->getId());
  180.                 }
  181.             }
  182.             //NORDINFO SEARCH ONLY
  183.             if (
  184.                 $entity->getStatus() != null && $entity->getStatus()->getId() == && $entity->getArchive() == false
  185.                 && ($entity->getType()->getType() == 'article'
  186.                     || $entity->getType()->getType() == 'video'
  187.                     || $entity->getType()->getType() == 'event'
  188.                     || $entity->getType()->getType() == 'folder'
  189.                     || $entity->getType()->getType() == 'sondage'
  190.                     || $entity->getType()->getType() == 'intranet'
  191.                     || $entity->getType()->getType() == 'quiz'
  192.                 )
  193.             ) {
  194.                 $datas $this->setPostDatas($entity);
  195.                 $this->es_service->addDocument('nordinfo''nordinfo_only_' $entity->getId(), $datas);
  196.             } else {
  197.                 if (($entity->getStatus()->getId() !== || $entity->getArchive() == true
  198.                     && ($entity->getType()->getType() == 'article'
  199.                         || $entity->getType()->getType() == 'video'
  200.                         || $entity->getType()->getType() == 'event'
  201.                         || $entity->getType()->getType() == 'folder'
  202.                         || $entity->getType()->getType() == 'sondage'
  203.                         || $entity->getType()->getType() == 'intranet'
  204.                         || $entity->getType()->getType() == 'quiz'
  205.                     ))) {
  206.                     $this->es_service->deleteDocument('nordinfo''nordinfo_only_' $entity->getId());
  207.                 }
  208.             }
  209.         } else if ($entity instanceof Glossaire) {
  210.             //if($type == "add"){       
  211.             $datas = ['website' => "lenord"'entity_id' => $entity->getId(), 'entity_type' => "glossaire"'content' => $entity->getDefinition(), 'title' => $entity->getTerme(), 'slug' => $entity->getTerme()];
  212.             $this->es_service->addDocument('lenord''lenord_glossaire_' $entity->getId(), $datas);
  213.             //}else{
  214.             //}
  215.         } else if ($entity instanceof Contact) {
  216.             if ($entity->getActive() == && $entity->getType() == 'admin') {
  217.                 //$subtitle = $entity->getTypeContact();
  218.                 $subtitle null;
  219.                 if ($entity->getShortDescription() != null) {
  220.                     $subtitle .= ' - ' $entity->getShortDescription();
  221.                 }
  222.                 $content null;
  223.                 if ($entity->getCanton() != null) {
  224.                     $subtitle .= ' - ' $entity->getCanton()->getCanton();
  225.                     $communes_par_canton $this->entityManager->getRepository(Commune::class)->findBy(array('canton' => $entity->getCanton()->getCanton()));
  226.                     foreach ($communes_par_canton as $commune) {
  227.                         $content .= " " $commune->getCommune();
  228.                     }
  229.                 }
  230.                 $datas = ['website' => "lenord"'content' => $content'entity_id' => $entity->getId(), 'entity_type' => "elu"'title' => $entity->getLastName(), 'subtitle' => $subtitle'title_view' => $entity->getFirstName() . ' ' $entity->getLastName(), 'subtitle_view' => $subtitle'civilite' => $entity->getCivilite(), 'image' => $entity->getPicture(), 'slug' => $entity->getSlug()];
  231.                 $this->es_service->addDocument('lenord''lenord_elu_' $entity->getId(), $datas);
  232.             } else {
  233.                 $this->es_service->deleteDocument('lenord''lenord_elu_' $entity->getId());
  234.             }
  235.         }
  236.         //Ajouter glossaire
  237.     }
  238.     private function setPostDatas(mixed $entity): mixed
  239.     {
  240.         $topics = [];
  241.         $territories = [];
  242.         $publics = [];
  243.         foreach ($entity->getTopics() as $topic) {
  244.             array_push($topics$topic->getDisplayName());
  245.         }
  246.         foreach ($entity->getTerritories() as $territory) {
  247.             array_push($territories$territory->getDisplayName());
  248.         }
  249.         foreach ($entity->getTargetpublics() as $public) {
  250.             array_push($publics, ['name' => $public->getName()]);
  251.         }
  252.         if (empty($publics)) {
  253.             array_push($publics, ['name' => "nopublic"]);
  254.         }
  255.         $contact1 = [];
  256.         if ($entity->getContact1() != null) {
  257.             $contact1 = ["slug" => $entity->getContact1()->getSlug(), "picture" => $entity->getContact1()->getPicture(), "firstName" => $entity->getContact1()->getFirstName(), "lastName" => $entity->getContact1()->getLastName(), "shortDescription" => $entity->getContact1()->getShortDescription()];
  258.         }
  259.         $contact2 = [];
  260.         if ($entity->getContact2() != null) {
  261.             $contact2 = ["slug" => $entity->getContact2()->getSlug(), "picture" => $entity->getContact2()->getPicture(), "firstName" => $entity->getContact2()->getFirstName(), "lastName" => $entity->getContact2()->getLastName(), "shortDescription" => $entity->getContact2()->getShortDescription()];
  262.         }
  263.         $contact3 = [];
  264.         if ($entity->getContact3() != null) {
  265.             $contact3 = ["slug" => $entity->getContact3()->getSlug(), "picture" => $entity->getContact3()->getPicture(), "firstName" => $entity->getContact3()->getFirstName(), "lastName" => $entity->getContact3()->getLastName(), "shortDescription" => $entity->getContact3()->getShortDescription()];
  266.         }
  267.         $organisation1 = [];
  268.         if ($entity->getOrganisation1() != null) {
  269.             $organisation1 = ['id' => $entity->getOrganisation1()->getId(), 'affichageTypeMap' => $entity->getAffichageTypeMap(), 'longitude' => $entity->getOrganisation1()->getLongitude(), 'latitude' => $entity->getOrganisation1()->getLatitude(), 'rue' => $entity->getOrganisation1()->getRue(), 'codePostal' => $entity->getOrganisation1()->getCodePostal(), 'ville' => $entity->getOrganisation1()->getVille(), 'title' => $entity->getOrganisation1()->getTitle(), 'subtitle' => $entity->getOrganisation1()->getSubtitle()];
  270.         }
  271.         $organisation2 = [];
  272.         if ($entity->getOrganisation2() != null) {
  273.             $organisation2 = ['id' => $entity->getOrganisation2()->getId(), 'affichageTypeMap' => $entity->getAffichageTypeMap2(), 'longitude' => $entity->getOrganisation2()->getLongitude(), 'latitude' => $entity->getOrganisation2()->getLatitude(), 'rue' => $entity->getOrganisation2()->getRue(), 'codePostal' => $entity->getOrganisation2()->getCodePostal(), 'ville' => $entity->getOrganisation2()->getVille(), 'title' => $entity->getOrganisation2()->getTitle(), 'subtitle' => $entity->getOrganisation2()->getSubtitle()];
  274.         }
  275.         $organisation3 = [];
  276.         if ($entity->getOrganisation3() != null) {
  277.             $organisation3 = ['id' => $entity->getOrganisation3()->getId(), 'affichageTypeMap' => $entity->getAffichageTypeMap3(), 'longitude' => $entity->getOrganisation3()->getLongitude(), 'latitude' => $entity->getOrganisation3()->getLatitude(), 'rue' => $entity->getOrganisation3()->getRue(), 'codePostal' => $entity->getOrganisation3()->getCodePostal(), 'ville' => $entity->getOrganisation3()->getVille(), 'title' => $entity->getOrganisation3()->getTitle(), 'subtitle' => $entity->getOrganisation3()->getSubtitle()];
  278.         }
  279.         $related_posts $this->getRelatedPosts($entity);
  280.         if ($entity->getSlug() == null) {
  281.             $slug $this->url_service->formatAlias($entity->getTitle());
  282.             $entity->setSlug($slug);
  283.         }
  284.         $lieu null;
  285.         $ville null;
  286.         $event_lieu null;
  287.         $event_ville null;
  288.         if($entity->getLieuNew() !== null){
  289.             $lieu $entity->getLieuNew()->getAdresse();
  290.             $event_lieu $entity->getLieuNew()->getAdresse();
  291.             $ville $entity->getLieuNew()->getVille();
  292.             $event_ville $entity->getLieuNew()->getVille();
  293.         }
  294.         $event_date null;
  295.         if ($entity->getType()->getType() == 'event') {
  296.             $event_date $this->formatDateAgendaJson($entity->getJson());
  297.         }
  298.         $bool_actu $entity->getBoolActu();
  299.         $widthImageFocusPub 0;
  300.         $heightImageFocusPub 0;
  301.         $padding 0;
  302.         $responses = [];
  303.         foreach ($entity->getReponses() as $reponse) {
  304.             array_push($responses, [
  305.                 'id' => $reponse->getId(),
  306.                 'texte' => $reponse->getTexte(),
  307.                 'valide' => $reponse->getValide()
  308.             ]);
  309.         }
  310.         $defaut_fil false;
  311.         $image_fil $entity->getIllustrationfilName();
  312.         if ($entity->getIllustrationfilName() == null && $entity->getType()->getId() == 3) {
  313.             $event_type iterator_to_array($this->entityManager->getRepository(EventType::class)->getPostEventType($entity->getId()));
  314.             if (!empty($event_type)) {
  315.                 switch ($event_type[0]['id']) {
  316.                     case 1:
  317.                         $defaut_fil true;
  318.                         $image_fil "/img/images_defauts/spectacle_defaut.jpg";
  319.                         break;
  320.                     case 3:
  321.                         $defaut_fil true;
  322.                         $image_fil "/img/images_defauts/conference.jpg";
  323.                         break;
  324.                     case 4:
  325.                         $defaut_fil true;
  326.                         $image_fil "/img/images_defauts/salon.jpg";
  327.                         break;
  328.                     case 5:
  329.                         $defaut_fil true;
  330.                         $image_fil "/img/images_defauts/sport.jpg";
  331.                         break;
  332.                     case 6:
  333.                     case 12:
  334.                         $defaut_fil true;
  335.                         $image_fil "/img/images_defauts/emploi.jpg";
  336.                         break;
  337.                     case 7:
  338.                         $defaut_fil true;
  339.                         $image_fil "/img/images_defauts/braderie.jpg";
  340.                         break;
  341.                     case 8:
  342.                         $defaut_fil true;
  343.                         $image_fil "/img/images_defauts/nature.jpg";
  344.                         break;
  345.                     case 9:
  346.                     case 11:
  347.                         $defaut_fil true;
  348.                         $image_fil "/img/images_defauts/exposition.jpg";
  349.                         break;
  350.                     case 2:
  351.                     case 10:
  352.                         $defaut_fil true;
  353.                         $image_fil "/img/images_defauts/fete.jpg";
  354.                         break;
  355.                 }
  356.             }
  357.         }
  358.         $datas = [
  359.             'website' => "nordinfo",
  360.             'entity_id' => $entity->getId(),
  361.             'entity_type' => "post",
  362.             'content' => $entity->getContent(),
  363.             'title' => $entity->getTitle(),
  364.             'chapo' => $entity->getChapo(),
  365.             'subtitle' => $entity->getSubtitle(),
  366.             'type' => $entity->getType()->getType(),
  367.             'short_description' => $entity->getShortDescription(),
  368.             'statut' => $entity->getStatus()->getName(),
  369.             'image_fil' => $image_fil,
  370.             'image_detail' => $entity->getIllustrationdetail(),
  371.             'position_image' => ($entity->getPositionImage() != null) ? $entity->getPositionImage()->getId() : null,
  372.             'date_debut' => $entity->getDateDebut(),
  373.             'date_fin' => $entity->getDateFin(),
  374.             'event_date' => $event_date,
  375.             'territoires' => $territories,
  376.             'thematiques' => $topics,
  377.             'credits' => $entity->getCredits(),
  378.             'lieu' => $lieu,
  379.             'ville' => $ville,
  380.             'slug' => $entity->getSlug(),
  381.             'contact1' => $contact1,
  382.             'contact2' => $contact2,
  383.             'contact3' => $contact3,
  384.             'organisation1' => $organisation1,
  385.             'organisation2' => $organisation2,
  386.             'organisation3' => $organisation3,
  387.             'organisation4' => $entity->getOrganisation4(),
  388.             'documentName1' => $entity->getDocumentName1(),
  389.             'documentName2' => $entity->getDocumentName2(),
  390.             'documentName3' => $entity->getDocumentName3(),
  391.             'link1' => $entity->getLink1(),
  392.             'url1' => $entity->getUrl1(),
  393.             'link2' => $entity->getLink2(),
  394.             'url2' => $entity->getUrl2(),
  395.             'link3' => $entity->getLink3(),
  396.             'url3' => $entity->getUrl3(),
  397.             'related_posts' => $related_posts,
  398.             'publics' => $publics,
  399.             'bool_actu' => $bool_actu,
  400.             'defaut_fil' => $defaut_fil,
  401.             'duree' => $entity->getDuree(),
  402.             'musee_name' => $entity->getMuseeName(),
  403.             'thumb_url' => $entity->getThumbUrl(),
  404.             'event_lieu' => $event_lieu,
  405.             'event_ville' => $event_ville,
  406.             'palette_couleur' => ($entity->getPaletteCouleur() !== null) ? $entity->getPaletteCouleur()->getCodeHexa() : null,
  407.             'widthImageFocusPub' => $widthImageFocusPub,
  408.             'heightImageFocusPub' => $heightImageFocusPub,
  409.             'padding' => $padding,
  410.             'responses' => $responses,
  411.             'grand_format' => $entity->getGrandFormat()
  412.         ];
  413.         return $datas;
  414.     }
  415.     //Divers
  416.     public function updateService(mixed $entity): bool
  417.     {
  418.         if ($entity->getAliasUrl() !== null) {
  419.             if ($entity->getStatut() != null && $entity->getStatut()->getId() == 5) {
  420.                 $themes = [];
  421.                 $search_themes = [];
  422.                 foreach ($entity->getThemes() as $theme) {
  423.                     array_push($search_themes$theme->getAliasUrl());
  424.                     array_push(
  425.                         $themes,
  426.                         ['titre' => $theme->getTitre(), 'alias' => $theme->getAliasUrl(), 'pictogramme_bleu' => $theme->getPictogrammeBleu(), 'pictogramme_vert' => $theme->getPictogrammeVert()]
  427.                     );
  428.                 }
  429.                 $grandprofils = [];
  430.                 foreach ($entity->getGrandprofils() as $gp) {
  431.                     array_push($grandprofils, ['titre' => $gp->getTitre(), 'alias' => $gp->getAliasUrl()]);
  432.                 }
  433.                 $sousprofils = [];
  434.                 foreach ($entity->getSousprofils() as $sp) {
  435.                     array_push($sousprofils, ['titre' => $sp->getTitre(), 'alias' => $sp->getAliasUrl()]);
  436.                 }
  437.                 $datas = ['website' => "services"'entity_id' => $entity->getId(), 'entity_type' => "service"'content' => $entity->getContent(), 'title' => $entity->getTitre(), 'subtitle' => $entity->getChapo(), 'slug' => $entity->getAliasUrl(), 'themes' => $themes'search_themes' => $search_themes'grandprofils' => $grandprofils'sousprofils' => $sousprofils];
  438.                 $this->es_service->addDocument('lenord''services_service_' $entity->getId(), $datas);
  439.             } else if ($entity->getId() != null) {
  440.                 $this->es_service->deleteDocument('lenord''services_service_' $entity->getId());
  441.             }
  442.         }
  443.         return true;
  444.     }
  445.     public function updateContact(mixed $entity): void
  446.     {
  447.         $entity->setSlug($entity->getFirstName() . '-' $entity->getLastName());
  448.     }
  449.     public function updateTheme(mixed $entity): void
  450.     {
  451.         $entity->setAliasUrl($this->url_service->formatAlias($entity->getTitre()));
  452.     }
  453.     public function updateSousProfil(mixed $entity): void
  454.     {
  455.         $entity->setAliasUrl($this->url_service->formatAlias($entity->getTitre()));
  456.     }
  457.     public function updateGrandProfil(mixed $entity): void
  458.     {
  459.         //$entity->setAliasUrl($this->url_service->formatAlias($entity->getTitre()));
  460.     }
  461.     public function setFileSize(mixed $entity): void
  462.     {
  463.         if ($entity->getFichier() != null) {
  464.             $bytes filesize($entity->getFichier()->getPathname());
  465.             //if ($bytes >= 1048576){
  466.             $bytes number_format($bytes 1_048_5762) . ' Mo';
  467.             //}/*elseif ($bytes >= 1024){
  468.             //$bytes = number_format($bytes / 1024, 2) . ' Ko';
  469.             /*}elseif ($bytes > 1){
  470.                 $bytes = $bytes . ' o';
  471.             }elseif ($bytes == 1){
  472.                 $bytes = $bytes . ' o';
  473.             }*/
  474.             $entity->setPoids($bytes)
  475.                 ->setFormatFile($entity->getFichier()->guessExtension());
  476.         }
  477.         if ($entity->getFichier2() != null) {
  478.             $bytes filesize($entity->getFichier2()->getPathname());
  479.             //if ($bytes >= 1048576){
  480.             $bytes number_format($bytes 1_048_5762) . ' Mo';
  481.             //}/*elseif ($bytes >= 1024){
  482.             //$bytes = number_format($bytes / 1024, 2) . ' Ko';
  483.             /*}elseif ($bytes > 1){
  484.                 $bytes = $bytes . ' o';
  485.             }elseif ($bytes == 1){
  486.                 $bytes = $bytes . ' o';
  487.             }*/
  488.             $entity->setPoids2($bytes)
  489.                 ->setFormatFile2($entity->getFichier2()->guessExtension());
  490.         }
  491.         if ($entity->getFichier3() != null) {
  492.             $bytes filesize($entity->getFichier3()->getPathname());
  493.             //if ($bytes >= 1048576){
  494.             $bytes number_format($bytes 1_048_5762) . ' Mo';
  495.             //}/*elseif ($bytes >= 1024){
  496.             //$bytes = number_format($bytes / 1024, 2) . ' Ko';
  497.             /*}elseif ($bytes > 1){
  498.                 $bytes = $bytes . ' o';
  499.             }elseif ($bytes == 1){
  500.                 $bytes = $bytes . ' o';
  501.             }*/
  502.             $entity->setPoids3($bytes)
  503.                 ->setFormatFile3($entity->getFichier3()->guessExtension());
  504.         }
  505.         if ($entity->getFichier4() != null) {
  506.             $bytes filesize($entity->getFichier4()->getPathname());
  507.             //if ($bytes >= 1048576){
  508.             $bytes number_format($bytes 1_048_5762) . ' Mo';
  509.             //}/*elseif ($bytes >= 1024){
  510.             //$bytes = number_format($bytes / 1024, 2) . ' Ko';
  511.             /*}elseif ($bytes > 1){
  512.                 $bytes = $bytes . ' o';
  513.             }elseif ($bytes == 1){
  514.                 $bytes = $bytes . ' o';
  515.             }*/
  516.             $entity->setPoids4($bytes)
  517.                 ->setFormatFile4($entity->getFichier4()->guessExtension());
  518.         }
  519.         if ($entity->getFichier5() != null) {
  520.             $bytes filesize($entity->getFichier5()->getPathname());
  521.             //if ($bytes >= 1048576){
  522.             $bytes number_format($bytes 1_048_5762) . ' Mo';
  523.             //}/*elseif ($bytes >= 1024){
  524.             //$bytes = number_format($bytes / 1024, 2) . ' Ko';
  525.             /*}elseif ($bytes > 1){
  526.                 $bytes = $bytes . ' o';
  527.             }elseif ($bytes == 1){
  528.                 $bytes = $bytes . ' o';
  529.             }*/
  530.             $entity->setPoids5($bytes)
  531.                 ->setFormatFile5($entity->getFichier5()->guessExtension());
  532.         }
  533.         if ($entity->getFichier6() != null) {
  534.             $bytes filesize($entity->getFichier6()->getPathname());
  535.             //if ($bytes >= 1048576){
  536.             $bytes number_format($bytes 1_048_5762) . ' Mo';
  537.             //}/*elseif ($bytes >= 1024){
  538.             //$bytes = number_format($bytes / 1024, 2) . ' Ko';
  539.             /*}elseif ($bytes > 1){
  540.                 $bytes = $bytes . ' o';
  541.             }elseif ($bytes == 1){
  542.                 $bytes = $bytes . ' o';
  543.             }*/
  544.             $entity->setPoids6($bytes)
  545.                 ->setFormatFile6($entity->getFichier6()->guessExtension());
  546.         }
  547.         //Alias url
  548.         $entity->setAliasUrl($this->url_service->formatAlias($entity->getTitre()));
  549.     }
  550.     protected function getRelatedPosts(mixed $post): array
  551.     {
  552.         $number 0;
  553.         $relateds = [];
  554.         if ($post->getRelatedPost1() === null) {
  555.             $number 1;
  556.         } else {
  557.             $relateds[] = ['title' => $post->getRelatedPost1()->getTitle(), 'slug' => $post->getRelatedPost1()->getSlug()];
  558.         }
  559.         if ($post->getRelatedPost2() === null) {
  560.             $number 2;
  561.         } else {
  562.             $relateds[] = ['title' => $post->getRelatedPost2()->getTitle(), 'slug' => $post->getRelatedPost2()->getSlug()];
  563.         }
  564.         if ($post->getRelatedPost3() === null) {
  565.             $number 3;
  566.         } else {
  567.             $relateds[] = ['title' => $post->getRelatedPost3()->getTitle(), 'slug' => $post->getRelatedPost3()->getSlug()];
  568.         }
  569.         $type_id $post->getType()->getId();
  570.         $topics_id = [];
  571.         foreach ($post->getTopics() as $topic) {
  572.             $topics_id[] = $topic->getId();
  573.         }
  574.         $territories_id = [];
  575.         foreach ($post->getTerritories() as $territory) {
  576.             $territories_id[] = $territory->getId();
  577.         }
  578.         $posts $this->entityManager->getRepository(Post::class)->getRelatedPost($number$type_id$topics_id$territories_id$post->getId());
  579.         foreach ($posts as $p) {
  580.             if (count($relateds) === 4) {
  581.                 break;
  582.             }
  583.             if ($p->getSlug() !== null) {
  584.                 $relateds[] = ['title' => $p->getTitle(), 'slug' => $p->getSlug()];
  585.             }
  586.         }
  587.         return $relateds;
  588.     }
  589.     private function setPostDateDebut(mixed $post):void{
  590.         $current_date = new \DateTime();
  591.         $current_date->setTime(000);
  592.         $json_horaires json_decode($post->getJson(), true);
  593.         if ($json_horaires !== null) {
  594.             switch ($json_horaires['modeDates']) {
  595.                 case "range":
  596.                     //de date debut a date fin
  597.                     $post->setDateDebut($json_horaires['datesRange']['dateStart'])
  598.                         ->setDateFin($json_horaires['datesRange']['dateFinish'])
  599.                     ;
  600.                     break;
  601.                 case 'multiple':
  602.                     $dates_multiples = [];
  603.                     $dates $json_horaires['datesMultiple']['dates'];
  604.                     foreach ($dates as $d) {
  605.                         array_push($dates_multiples$d);
  606.                     }
  607.                     $post->setDateDebut($dates_multiples[0])
  608.                         ->setDateFin($dates_multiples[count($dates_multiples)-1]);
  609.                     break;
  610.                 case 'single':
  611.                     $post->setDateDebut($json_horaires['dateSingle']['dates'][0])
  612.                     ->setDateFin($json_horaires['dateSingle']['dates'][0]);
  613.                     break;
  614.             }
  615.         }
  616.         $this->entityManager->persist($post);
  617.         $this->entityManager->flush();
  618.     }
  619.     private function formatDateAgendaJson(mixed $json): mixed
  620.     {
  621.         $str_date null;
  622.         $current_date = new \DateTime();
  623.         $current_date->setTime(000);
  624.         $json_horaires json_decode($jsontrue);
  625.         if ($json_horaires !== null) {
  626.             switch ($json_horaires['modeDates']) {
  627.                 case "range":
  628.                     //de date debut a date fin
  629.                     $date_debut date_create_from_format('d/m/Y'$json_horaires['datesRange']['dateStart']);
  630.                     $date_fin date_create_from_format('d/m/Y'$json_horaires['datesRange']['dateFinish']);
  631.                     $ics_date_debut $json_horaires['datesRange']['dateStart'];
  632.                     $ics_date_fin $json_horaires['datesRange']['dateStart'];
  633.                     $evDateDebut $this->formatStringDate($date_debut);
  634.                     $evDateFin $this->formatStringDate($date_fin);
  635.                     $same_day false;
  636.                     if ($evDateDebut['day'] == $evDateFin['day']) {
  637.                         if ($evDateDebut['month'] == $evDateFin['month']) {
  638.                             $same_day true;
  639.                         }
  640.                     }
  641.                     if ($evDateDebut['day'] == || $evDateDebut['day'] == '01' || $evDateDebut['day'] == '1') {
  642.                         $day_debut $this->array_date_trad["fr"][1];
  643.                     } else {
  644.                         $day_debut $evDateDebut['day'];
  645.                     }
  646.                     if ($evDateDebut['month'] == $evDateFin['month'] && $evDateDebut['year'] == $evDateFin['year']) {
  647.                         if ($same_day) {
  648.                             //$this->translator->trans('du') . ' ' .
  649.                             $str_date =  $evDateFin['day'] . ' ' $evDateFin['month'];
  650.                         } else {
  651.                             $str_date 'Du ' $day_debut ' au ' $evDateFin['day'] . ' ' $evDateFin['month'];
  652.                         }
  653.                     } else {
  654.                         //check year de la date debut et fin
  655.                         if ($evDateDebut['year'] !== $evDateFin['year']) {
  656.                             $str_date 'Du ' $day_debut ' ' $evDateDebut['month'] . ' ' $evDateDebut['year'] . ' au ' $evDateFin['day'] . ' ' $evDateFin['month'] . ' ' $evDateFin['year'];
  657.                         } else {
  658.                             $str_date 'Du ' $day_debut ' ' $evDateDebut['month'] . ' au ' $evDateFin['day'] . ' ' $evDateFin['month'] . ' ' $evDateFin['year'];
  659.                         }
  660.                     }
  661.                     break;
  662.                 case 'multiple':
  663.                     $nombre_date 0;
  664.                     $dates_multiples = [];
  665.                     $dates $json_horaires['datesMultiple']['dates'];
  666.                     foreach ($dates as $d) {
  667.                         array_push($dates_multiples$d);
  668.                     }
  669.                     $date null;
  670.                     $array_dates $dates_multiples;
  671.                     $i 0;
  672.                     foreach ($array_dates as $d) {
  673.                         if ($i 0) {
  674.                             $str_date .= ", ";
  675.                         }
  676.                         $date date_create_from_format('d/m/Y'$dates_multiples[$i]);
  677.                         $str_date .= $date->format('d') . ' ' $this->months["fr"][$date->format('n')] . ' ' $date->format('Y');
  678.                         $i++;
  679.                     }
  680.                     break;
  681.                 case 'single':
  682.                     $start_date date_create_from_format('d/m/Y'$json_horaires['dateSingle']['dates'][0]);
  683.                     $day 'Le ' $start_date->format('d');
  684.                     $str_date .= $day;
  685.                     $str_month $this->months["fr"][$start_date->format('n')];
  686.                     $str_date .= ' ' $str_month;
  687.                     $str_date .= ' ' $start_date->format('Y');
  688.                     break;
  689.             }
  690.         }
  691.         return $str_date;
  692.     }
  693.     private function formatStringDate(mixed $date): mixed
  694.     {
  695.         if ($date instanceof \DateTime) {
  696.             return array(
  697.                 'day' => $date->format('j'),
  698.                 'month' => $this->months["fr"][$date->format('n')],
  699.                 'year' => $date->format('Y')
  700.             );
  701.         }
  702.         return array(
  703.             'day' => null,
  704.             'month' => null,
  705.             'year' => null
  706.         );
  707.     }
  708.     private mixed $array_date_trad = array(
  709.         'fr' => array(
  710.             => '1er'
  711.         ),
  712.         'en' => array(
  713.             => '1st'
  714.         ),
  715.         'nl' => array(
  716.             => '1st'
  717.         )
  718.     );
  719.     private mixed $months = array(
  720.         'fr' => array(
  721.             => 'janvier',
  722.             => 'février',
  723.             => 'mars',
  724.             => 'avril',
  725.             => 'mai',
  726.             => 'juin',
  727.             => 'juillet',
  728.             => 'août',
  729.             => 'septembre',
  730.             10 => 'octobre',
  731.             11 => 'novembre',
  732.             12 => 'décembre'
  733.         ),
  734.         'en' => array(
  735.             => 'January',
  736.             => 'February',
  737.             => 'March',
  738.             => 'April',
  739.             => 'May',
  740.             => 'June',
  741.             => 'July',
  742.             => 'August',
  743.             => 'September',
  744.             10 => 'October',
  745.             11 => 'November',
  746.             12 => 'December',
  747.         ),
  748.         'nl' => array(
  749.             => 'januari',
  750.             => 'februari',
  751.             => 'maart',
  752.             => 'april',
  753.             => 'mei',
  754.             => 'juni',
  755.             => 'juli',
  756.             => 'augustus',
  757.             => 'september',
  758.             10 => 'oktober',
  759.             11 => 'november',
  760.             12 => 'december'
  761.         )
  762.     );
  763. }