src/EventListener/SitemapSubscriber.php line 63

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\TypePost;
  9. use App\Entity\ProductOccassion;
  10. use App\Entity\CategoriesProduct;
  11. use Symfony\Component\Intl\Locale;
  12. use App\Repository\UrlPostRepository;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use App\Entity\CategoriesProductOccassion;
  15. use Presta\SitemapBundle\Sitemap\Url as Sitemap;
  16. use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
  17. use Presta\SitemapBundle\Event\SitemapPopulateEvent;
  18. use Presta\SitemapBundle\Service\UrlContainerInterface;
  19. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. class SitemapSubscriber implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var UrlGeneratorInterface
  25.      */
  26.     private $urlGenerator;
  27.     /**
  28.      * @var UrlPostRepository
  29.      */
  30.     private $UrlPostRepository;
  31.     protected $em;
  32.     /**
  33.      * @param UrlGeneratorInterface $urlGenerator
  34.      * @param UrlPostRepository    $UrlPostRepository
  35.      */
  36.     public function __construct(UrlGeneratorInterface $urlGenerator,EntityManagerInterface $emUrlPostRepository $UrlPostRepository)
  37.     {
  38.         $this->urlGenerator $urlGenerator;
  39.         $this->em $em;
  40.         $this->UrlPostRepository $UrlPostRepository;
  41.     }
  42.     /**
  43.      * @inheritdoc
  44.      */
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             SitemapPopulateEvent::ON_SITEMAP_POPULATE => 'populate',
  49.         ];
  50.     }
  51.     /**
  52.      * @param SitemapPopulateEvent $event
  53.      */
  54.     public function populate(SitemapPopulateEvent $event): void
  55.     {
  56.         $this->registerPostPage($event->getUrlContainer());
  57.         $this->registerPostCategories($event->getUrlContainer());
  58.     }
  59.         /**
  60.      * @param UrlContainerInterface $urls
  61.      */
  62.     public function registerPostPage(UrlContainerInterface $urls): void
  63.     {
  64.         //$languages = $this->em->getRepository(Language::class)->findActifLangNotEs();
  65.         $locale Locale::getDefault();
  66.         $List_slug $this->em->getRepository(Post::class)->findBy([],['createdAt'=>'ASC']);
  67.         if($List_slug){
  68.             foreach($List_slug as $cms){
  69.                 if($cms->translate($locale)->getSlug() && $cms->translate($locale)->getSlug()!= 'home' && $cms->translate($locale)->getActif() == true){
  70.                     $urls->addUrl(
  71.                         new UrlConcrete(
  72.                             $this->urlGenerator->generate(
  73.                                 'page',['slug' => $cms->translate($locale)->getSlug()],
  74.                                 UrlGeneratorInterface::ABSOLUTE_URL
  75.                             )
  76.                         ),
  77.                         'page'
  78.                     );
  79.                 }
  80.             }
  81.         }
  82.     }
  83.     
  84.     /**
  85.      * @param UrlContainerInterface $urls
  86.      */
  87.     public function registerPostCategories(UrlContainerInterface $urls): void
  88.     {
  89.         //$languages = $this->em->getRepository(Language::class)->findActifLangNotEs();
  90.         $locale Locale::getDefault();
  91.         $List_slug $this->em->getRepository(Category::class)->findBy([],['createdAt'=>'ASC']);
  92.         if($List_slug){
  93.             foreach($List_slug as $val){
  94.                 $posts $this->em->getRepository(Post::class)->findPostByCategory($val->getId(),false);
  95.  
  96.                 if ($val->getPagination()) {
  97.                     $nombre_page count($posts) / $val->getPageSize();
  98.                     $urls->addUrl(
  99.                         new UrlConcrete(
  100.                             $this->urlGenerator->generate(
  101.                                 'page',['slug' => $val->translate($locale)->getSlugUrl()],
  102.                                 UrlGeneratorInterface::ABSOLUTE_URL
  103.                             )
  104.                         ),
  105.                         'page'
  106.                     );
  107.                     if ($nombre_page 1) {
  108.                         for($i=2$i <= ceil($nombre_page); $i++) { 
  109.                             $urls->addUrl(
  110.                                 new UrlConcrete(
  111.                                     $this->urlGenerator->generate(
  112.                                         'page',['slug' => $val->translate($locale)->getSlugUrl(),'page'=> $i],
  113.                                         UrlGeneratorInterface::ABSOLUTE_URL
  114.                                     )
  115.                                 ),
  116.                                 'page'
  117.                             );
  118.                         }   
  119.                     }
  120.                 }else{
  121.                     if($val->translate($locale)->getSlugUrl() && $val->translate($locale)->getSlugUrl()!= 'home' && $val->getActif() == true){
  122.                         $urls->addUrl(
  123.                             new UrlConcrete(
  124.                                 $this->urlGenerator->generate(
  125.                                     'page',['slug' => $val->translate($locale)->getSlugUrl()],
  126.                                     UrlGeneratorInterface::ABSOLUTE_URL
  127.                                 )
  128.                             ),
  129.                             'page'
  130.                         );
  131.                     }
  132.                 }
  133.             }
  134.         }
  135.     }
  136.    
  137. }