<?php
namespace App\EventListener;
use App\Entity\Post;
use App\Entity\Product;
use App\Entity\UrlPost;
use App\Entity\Category;
use App\Entity\Language;
use App\Entity\TypePost;
use App\Entity\ProductOccassion;
use App\Entity\CategoriesProduct;
use Symfony\Component\Intl\Locale;
use App\Repository\UrlPostRepository;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\CategoriesProductOccassion;
use Presta\SitemapBundle\Sitemap\Url as Sitemap;
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
use Presta\SitemapBundle\Event\SitemapPopulateEvent;
use Presta\SitemapBundle\Service\UrlContainerInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SitemapSubscriber implements EventSubscriberInterface
{
/**
* @var UrlGeneratorInterface
*/
private $urlGenerator;
/**
* @var UrlPostRepository
*/
private $UrlPostRepository;
protected $em;
/**
* @param UrlGeneratorInterface $urlGenerator
* @param UrlPostRepository $UrlPostRepository
*/
public function __construct(UrlGeneratorInterface $urlGenerator,EntityManagerInterface $em, UrlPostRepository $UrlPostRepository)
{
$this->urlGenerator = $urlGenerator;
$this->em = $em;
$this->UrlPostRepository = $UrlPostRepository;
}
/**
* @inheritdoc
*/
public static function getSubscribedEvents()
{
return [
SitemapPopulateEvent::ON_SITEMAP_POPULATE => 'populate',
];
}
/**
* @param SitemapPopulateEvent $event
*/
public function populate(SitemapPopulateEvent $event): void
{
$this->registerPostPage($event->getUrlContainer());
$this->registerPostCategories($event->getUrlContainer());
}
/**
* @param UrlContainerInterface $urls
*/
public function registerPostPage(UrlContainerInterface $urls): void
{
//$languages = $this->em->getRepository(Language::class)->findActifLangNotEs();
$locale = Locale::getDefault();
$List_slug = $this->em->getRepository(Post::class)->findBy([],['createdAt'=>'ASC']);
if($List_slug){
foreach($List_slug as $cms){
if($cms->translate($locale)->getSlug() && $cms->translate($locale)->getSlug()!= 'home' && $cms->translate($locale)->getActif() == true){
$urls->addUrl(
new UrlConcrete(
$this->urlGenerator->generate(
'page',['slug' => $cms->translate($locale)->getSlug()],
UrlGeneratorInterface::ABSOLUTE_URL
)
),
'page'
);
}
}
}
}
/**
* @param UrlContainerInterface $urls
*/
public function registerPostCategories(UrlContainerInterface $urls): void
{
//$languages = $this->em->getRepository(Language::class)->findActifLangNotEs();
$locale = Locale::getDefault();
$List_slug = $this->em->getRepository(Category::class)->findBy([],['createdAt'=>'ASC']);
if($List_slug){
foreach($List_slug as $val){
$posts = $this->em->getRepository(Post::class)->findPostByCategory($val->getId(),false);
if ($val->getPagination()) {
$nombre_page = count($posts) / $val->getPageSize();
$urls->addUrl(
new UrlConcrete(
$this->urlGenerator->generate(
'page',['slug' => $val->translate($locale)->getSlugUrl()],
UrlGeneratorInterface::ABSOLUTE_URL
)
),
'page'
);
if ($nombre_page > 1) {
for($i=2; $i <= ceil($nombre_page); $i++) {
$urls->addUrl(
new UrlConcrete(
$this->urlGenerator->generate(
'page',['slug' => $val->translate($locale)->getSlugUrl(),'page'=> $i],
UrlGeneratorInterface::ABSOLUTE_URL
)
),
'page'
);
}
}
}else{
if($val->translate($locale)->getSlugUrl() && $val->translate($locale)->getSlugUrl()!= 'home' && $val->getActif() == true){
$urls->addUrl(
new UrlConcrete(
$this->urlGenerator->generate(
'page',['slug' => $val->translate($locale)->getSlugUrl()],
UrlGeneratorInterface::ABSOLUTE_URL
)
),
'page'
);
}
}
}
}
}
}