src/EventListener/SitemapSubscriber.php line 65

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Post;
  4. use App\Entity\Product;
  5. use App\Entity\UrlPost;
  6. use App\Entity\Category;
  7. use App\Entity\Language;
  8. use App\Entity\Secteurs;
  9. use App\Entity\TypePost;
  10. use App\Entity\Activites;
  11. use App\Entity\ProductOccassion;
  12. use App\Entity\CategoriesProduct;
  13. use Symfony\Component\Intl\Locale;
  14. use App\Repository\UrlPostRepository;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use App\Entity\CategoriesProductOccassion;
  17. use Presta\SitemapBundle\Sitemap\Url as Sitemap;
  18. use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
  19. use Presta\SitemapBundle\Event\SitemapPopulateEvent;
  20. use Presta\SitemapBundle\Service\UrlContainerInterface;
  21. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. class SitemapSubscriber implements EventSubscriberInterface
  24. {
  25.     /**
  26.      * @var UrlGeneratorInterface
  27.      */
  28.     private $urlGenerator;
  29.     /**
  30.      * @var UrlPostRepository
  31.      */
  32.     private $UrlPostRepository;
  33.     protected $em;
  34.     /**
  35.      * @param UrlGeneratorInterface $urlGenerator
  36.      * @param UrlPostRepository    $UrlPostRepository
  37.      */
  38.     public function __construct(UrlGeneratorInterface $urlGenerator,EntityManagerInterface $emUrlPostRepository $UrlPostRepository)
  39.     {
  40.         $this->urlGenerator $urlGenerator;
  41.         $this->em $em;
  42.         $this->UrlPostRepository $UrlPostRepository;
  43.     }
  44.     /**
  45.      * @inheritdoc
  46.      */
  47.     public static function getSubscribedEvents()
  48.     {
  49.         return [
  50.             SitemapPopulateEvent::ON_SITEMAP_POPULATE => 'populate',
  51.         ];
  52.     }
  53.     /**
  54.      * @param SitemapPopulateEvent $event
  55.      */
  56.     public function populate(SitemapPopulateEvent $event): void
  57.     {
  58.         $this->registerPostPage($event->getUrlContainer());
  59.         $this->registerPageActivite($event->getUrlContainer());
  60.         $this->registerPostCategories($event->getUrlContainer());
  61.         $this->registerSecteur($event->getUrlContainer());
  62.         $this->registerProductPage($event->getUrlContainer());
  63.     }
  64.     
  65.     /**
  66.      * @param UrlContainerInterface $urls
  67.      */
  68.     public function registerPageActivite(UrlContainerInterface $urls): void
  69.     {
  70.         $locale Locale::getDefault();
  71.         $activites $this->em->getRepository(Activites::class)->findBy(['actif'=> true],['createdAt'=>'ASC']);
  72.         if($activites){
  73.             foreach($activites as $activite){
  74.                 $urls->addUrl(
  75.                     new UrlConcrete(
  76.                         $this->urlGenerator->generate(
  77.                             'page',['slug' => $activite->translate($locale)->getLibelleUrl()],
  78.                             UrlGeneratorInterface::ABSOLUTE_URL
  79.                         )
  80.                     ),
  81.                     'page'
  82.                 );
  83.             }
  84.         }
  85.     }
  86.     /**
  87.      * @param UrlContainerInterface $urls
  88.      */
  89.     public function registerPostPage(UrlContainerInterface $urls): void
  90.     {
  91.         //$languages = $this->em->getRepository(Language::class)->findActifLangNotEs();
  92.         $locale Locale::getDefault();
  93.         $List_slug $this->em->getRepository(Post::class)->findBy([],['createdAt'=>'ASC']);
  94.         if($List_slug){
  95.             foreach($List_slug as $cms){
  96.                 if($cms->translate($locale)->getSlug() && $cms->translate($locale)->getSlug()!= 'home' && $cms->translate($locale)->getActif() == true && $cms->getTypePost()->translate($locale)->getSystemName() != 'produits'){
  97.                     $urls->addUrl(
  98.                         new UrlConcrete(
  99.                             $this->urlGenerator->generate(
  100.                                 'page',['slug' => $cms->translate($locale)->getSlug()],
  101.                                 UrlGeneratorInterface::ABSOLUTE_URL
  102.                             )
  103.                         ),
  104.                         'page'
  105.                     );
  106.                 }
  107.             }
  108.         }
  109.     }
  110.     
  111.     /**
  112.      * @param UrlContainerInterface $urls
  113.      */
  114.     public function registerSecteur(UrlContainerInterface $urls): void
  115.     {
  116.         $locale Locale::getDefault();
  117.         $secteurs $this->em->getRepository(Secteurs::class)->findBy(['actif'=> true]);
  118.         if($secteurs){
  119.             foreach($secteurs as $secteur){
  120.                 if($secteur->translate($locale)->getLibelleUrl() && $secteur->getActif() == true){
  121.                     $urls->addUrl(
  122.                         new UrlConcrete(
  123.                             $this->urlGenerator->generate(
  124.                                 'page_details_localite',['slug' => $secteur->translate($locale)->getLibelleUrl()],
  125.                                 UrlGeneratorInterface::ABSOLUTE_URL
  126.                             )
  127.                         ),
  128.                         'page_localites'
  129.                     );
  130.                 }
  131.             }
  132.         }
  133.     }
  134.    
  135.     /**
  136.      * @param UrlContainerInterface $urls
  137.      */
  138.     public function registerPostCategories(UrlContainerInterface $urls): void
  139.     {
  140.         $locale Locale::getDefault();
  141.         $List_slug $this->em->getRepository(Category::class)->findBy(['actif'=> true,'parent'=> 0,'sousParent'=> 0],['createdAt'=>'ASC']);
  142.         if($List_slug){
  143.             foreach($List_slug as $val){
  144.                 if ($val->getTypePost()->translate($locale)->getSystemName() == 'produits') {
  145.                     $urls->addUrl(
  146.                         new UrlConcrete(
  147.                             $this->urlGenerator->generate(
  148.                                 'page',['slug' => $val->translate($locale)->getSlugUrl()],
  149.                                 UrlGeneratorInterface::ABSOLUTE_URL
  150.                             )
  151.                         ),
  152.                         'page'
  153.                     );
  154.                     $sous_categories $this->em->getRepository(Category::class)->findBy(['actif'=> true,'parent'=> $val->getId()]);
  155.                     if ($sous_categories) {
  156.                         foreach ($sous_categories as $item_sous_categorie) {
  157.                             $urls->addUrl(
  158.                                 new UrlConcrete(
  159.                                     $this->urlGenerator->generate(
  160.                                         'page_sous_categorie',[
  161.                                         'categorie1' => $val->translate($locale)->getSlugUrl(),
  162.                                         'categorie2' => $item_sous_categorie->translate($locale)->getSlugUrl(),
  163.                                         ],
  164.                                         UrlGeneratorInterface::ABSOLUTE_URL
  165.                                     )
  166.                                 ),
  167.                                 'page'
  168.                             );
  169.                             $sous_sous_categorie $this->em->getRepository(Category::class)->findBy(['actif'=> true,'parent'=> 0,'sousParent'=> $item_sous_categorie->getId()]);
  170.                             if ($sous_sous_categorie) {
  171.                                 foreach ($sous_sous_categorie as $item_sous_sous_categorie) {
  172.                                     $urls->addUrl(
  173.                                         new UrlConcrete(
  174.                                             $this->urlGenerator->generate(
  175.                                                 'page_sous_sous_categorie',[
  176.                                                 'categorie1' => $val->translate($locale)->getSlugUrl(),
  177.                                                 'categorie2' => $item_sous_categorie->translate($locale)->getSlugUrl(),
  178.                                                 'categorie3' => $item_sous_sous_categorie->translate($locale)->getSlugUrl(),
  179.                                                 ],
  180.                                                 UrlGeneratorInterface::ABSOLUTE_URL
  181.                                             )
  182.                                         ),
  183.                                         'page'
  184.                                     );
  185.                                 }
  186.                             }
  187.                         }
  188.                     }
  189.                 }else{
  190.                     if($val->translate($locale)->getSlugUrl() && $val->translate($locale)->getSlugUrl()!= 'home' && $val->getActif() == true){
  191.                         $urls->addUrl(
  192.                             new UrlConcrete(
  193.                                 $this->urlGenerator->generate(
  194.                                     'page',['slug' => $val->translate($locale)->getSlugUrl()],
  195.                                     UrlGeneratorInterface::ABSOLUTE_URL
  196.                                 )
  197.                             ),
  198.                             'page'
  199.                         );
  200.                     } 
  201.                 }
  202.             }
  203.         }
  204.     }
  205.     /**
  206.      * @param UrlContainerInterface $urls
  207.      */
  208.     public function registerProductPage(UrlContainerInterface $urls): void
  209.     {
  210.         $locale Locale::getDefault();
  211.         $products $this->em->getRepository(Post::class)->findPostBySystemName('produits');
  212.         if ($products) {
  213.             foreach ($products as $product) {
  214.                 if ($product->getDefaultCategorie()) {
  215.                     $categorie_produit $this->em->getRepository(Category::class)->find($product->getDefaultCategorie());
  216.                     if ($categorie_produit) {
  217.                         $urls->addUrl(
  218.                             new UrlConcrete(
  219.                                 $this->urlGenerator->generate(
  220.                                     'page_sous_categorie',[
  221.                                     'categorie1'=> $categorie_produit->translate($locale)->getSlugUrl(),
  222.                                     'categorie2' => $product->translate($locale)->getSlug(),
  223.                                     ],
  224.                                     UrlGeneratorInterface::ABSOLUTE_URL
  225.                                 )
  226.                             ),
  227.                             'page'
  228.                         );
  229.                     }
  230.          
  231.                 }
  232.                 // if ($product->getPostCategory()[0]) {
  233.                 //     $urls->addUrl(
  234.                 //         new UrlConcrete(
  235.                 //             $this->urlGenerator->generate(
  236.                 //                 'page_sous_categorie',[
  237.                 //                 'categorie1' => $product->getPostCategory()[0]->getCategory()->translate($locale)->getSlugUrl(),
  238.                 //                 'categorie2' => $product->translate($locale)->getSlug(),
  239.                 //                 ],
  240.                 //                 UrlGeneratorInterface::ABSOLUTE_URL
  241.                 //             )
  242.                 //         ),
  243.                 //         'page'
  244.                 //     );
  245.                 // }
  246.             }
  247.         }
  248.     }
  249. }