<?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\Secteurs;
use App\Entity\TypePost;
use App\Entity\Activites;
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->registerPageActivite($event->getUrlContainer());
$this->registerPostCategories($event->getUrlContainer());
$this->registerSecteur($event->getUrlContainer());
$this->registerProductPage($event->getUrlContainer());
}
/**
* @param UrlContainerInterface $urls
*/
public function registerPageActivite(UrlContainerInterface $urls): void
{
$locale = Locale::getDefault();
$activites = $this->em->getRepository(Activites::class)->findBy(['actif'=> true],['createdAt'=>'ASC']);
if($activites){
foreach($activites as $activite){
$urls->addUrl(
new UrlConcrete(
$this->urlGenerator->generate(
'page',['slug' => $activite->translate($locale)->getLibelleUrl()],
UrlGeneratorInterface::ABSOLUTE_URL
)
),
'page'
);
}
}
}
/**
* @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 && $cms->getTypePost()->translate($locale)->getSystemName() != 'produits'){
$urls->addUrl(
new UrlConcrete(
$this->urlGenerator->generate(
'page',['slug' => $cms->translate($locale)->getSlug()],
UrlGeneratorInterface::ABSOLUTE_URL
)
),
'page'
);
}
}
}
}
/**
* @param UrlContainerInterface $urls
*/
public function registerSecteur(UrlContainerInterface $urls): void
{
$locale = Locale::getDefault();
$secteurs = $this->em->getRepository(Secteurs::class)->findBy(['actif'=> true]);
if($secteurs){
foreach($secteurs as $secteur){
if($secteur->translate($locale)->getLibelleUrl() && $secteur->getActif() == true){
$urls->addUrl(
new UrlConcrete(
$this->urlGenerator->generate(
'page_details_localite',['slug' => $secteur->translate($locale)->getLibelleUrl()],
UrlGeneratorInterface::ABSOLUTE_URL
)
),
'page_localites'
);
}
}
}
}
/**
* @param UrlContainerInterface $urls
*/
public function registerPostCategories(UrlContainerInterface $urls): void
{
$locale = Locale::getDefault();
$List_slug = $this->em->getRepository(Category::class)->findBy(['actif'=> true,'parent'=> 0,'sousParent'=> 0],['createdAt'=>'ASC']);
if($List_slug){
foreach($List_slug as $val){
if ($val->getTypePost()->translate($locale)->getSystemName() == 'produits') {
$urls->addUrl(
new UrlConcrete(
$this->urlGenerator->generate(
'page',['slug' => $val->translate($locale)->getSlugUrl()],
UrlGeneratorInterface::ABSOLUTE_URL
)
),
'page'
);
$sous_categories = $this->em->getRepository(Category::class)->findBy(['actif'=> true,'parent'=> $val->getId()]);
if ($sous_categories) {
foreach ($sous_categories as $item_sous_categorie) {
$urls->addUrl(
new UrlConcrete(
$this->urlGenerator->generate(
'page_sous_categorie',[
'categorie1' => $val->translate($locale)->getSlugUrl(),
'categorie2' => $item_sous_categorie->translate($locale)->getSlugUrl(),
],
UrlGeneratorInterface::ABSOLUTE_URL
)
),
'page'
);
$sous_sous_categorie = $this->em->getRepository(Category::class)->findBy(['actif'=> true,'parent'=> 0,'sousParent'=> $item_sous_categorie->getId()]);
if ($sous_sous_categorie) {
foreach ($sous_sous_categorie as $item_sous_sous_categorie) {
$urls->addUrl(
new UrlConcrete(
$this->urlGenerator->generate(
'page_sous_sous_categorie',[
'categorie1' => $val->translate($locale)->getSlugUrl(),
'categorie2' => $item_sous_categorie->translate($locale)->getSlugUrl(),
'categorie3' => $item_sous_sous_categorie->translate($locale)->getSlugUrl(),
],
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'
);
}
}
}
}
}
/**
* @param UrlContainerInterface $urls
*/
public function registerProductPage(UrlContainerInterface $urls): void
{
$locale = Locale::getDefault();
$products = $this->em->getRepository(Post::class)->findPostBySystemName('produits');
if ($products) {
foreach ($products as $product) {
if ($product->getDefaultCategorie()) {
$categorie_produit = $this->em->getRepository(Category::class)->find($product->getDefaultCategorie());
if ($categorie_produit) {
$urls->addUrl(
new UrlConcrete(
$this->urlGenerator->generate(
'page_sous_categorie',[
'categorie1'=> $categorie_produit->translate($locale)->getSlugUrl(),
'categorie2' => $product->translate($locale)->getSlug(),
],
UrlGeneratorInterface::ABSOLUTE_URL
)
),
'page'
);
}
}
// if ($product->getPostCategory()[0]) {
// $urls->addUrl(
// new UrlConcrete(
// $this->urlGenerator->generate(
// 'page_sous_categorie',[
// 'categorie1' => $product->getPostCategory()[0]->getCategory()->translate($locale)->getSlugUrl(),
// 'categorie2' => $product->translate($locale)->getSlug(),
// ],
// UrlGeneratorInterface::ABSOLUTE_URL
// )
// ),
// 'page'
// );
// }
}
}
}
}