<?php
namespace Acme\SudcmsBundle\Controller;
use Acme\SudcmsBundle\Entity\Actualite;
use Acme\SudcmsBundle\Entity\Agenda;
use Acme\SudcmsBundle\Entity\Document;
use Acme\SudcmsBundle\Entity\EcoMarks;
use Acme\SudcmsBundle\Entity\EcoProducts;
use Acme\SudcmsBundle\Entity\Pages;
use Acme\SudcmsBundle\Service\OtideaUtils;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class SitemapController extends AbstractController
{
private $urls;
#[Route(path: '/sitemap.xml', name: 'sitemap', defaults: ['_format' => 'xml'])]
public function index(Request $request, OtideaUtils $utils)
{
// Nous récupérons le nom d'hôte depuis l'URL
$hostname = $request->getSchemeAndHttpHost();
$this->utils = $utils;
// On initialise un tableau pour lister les URLs
$this->urls = [];
$this->addStaticsPages();
$this->addModPages();
$this->addModActualite();
//$this->addModAgenda();
//$this->addModDocument();
if (CURRENT_SITE_ID == 1) {
$this->urls[] = ['loc' => "/cures"];
} else {
$this->addModBoutique();
}
// Fabrication de la réponse XML
$response = new Response(
$this->renderView('sitemap/index.html.twig', [
'urls' => $this->urls,
'hostname' => $hostname]
)
);
// Ajout des entêtes
$response->headers->set('Content-Type', 'text/xml');
// On envoie la réponse
return $response;
}
private function checkIfUrlAlreadyExist($newUrl = null)
{
if (isset($newUrl) && is_string($newUrl) && trim($newUrl) != "" && sizeof($this->urls) > 0) {
foreach ($this->urls as $url) {
if ($newUrl == $url['loc'])
return true;
}
}
return false;
}
private function addStaticsPages()
{
$this->urls[] = ['loc' => $this->generateUrl('front_home'), "priority" => 1]; // la home est la plus prio
}
private function addModPages()
{
$repo = $this->getDoctrine()->getRepository(Pages::class);
$items = $repo->findBy(["penligne" => 1]);
foreach ($items as $item) {
$images = [];
if ($item->getPbandeau() != "" && $item->getPbandeau() != null) {
$images = [
'loc' => '/medias_front/pages/' . $item->getPbandeau(), // URL to image
'title' => $item->getPtitre() // Optional, text describing the image
];
}
if ($item->getPType() == "standard" && !$this->checkIfUrlAlreadyExist($loc = "/page/" . $item->getPermalien())) {
$this->urls[] = [
'loc' => $loc,
// 'lastmod' => $item->getPdateLastupdate()->format('Y-m-d'),
'image' => $images
];
}
}
}
private function addModActualite()
{
$this->urls[] = ['loc' => "/actualite"];
$repo = $this->getDoctrine()->getRepository(Actualite::class);
$items = $repo->findBy(["act_enligne" => 1]);
$categs = [];
foreach ($items as $item) {
$images = [];
if ($item->getActIllustrationFichier() != "" && $item->getActIllustrationFichier() != null) {
$images = [
'loc' => '/medias_front/actualite/' . $item->getActIllustrationFichier(), // URL to image
'title' => $item->getActTitre() // Optional, text describing the image
];
}
if (!$this->checkIfUrlAlreadyExist($loc = "/actualite/fiche/" . $item->getId())) {
$this->urls[] = [
'loc' => $loc,
'image' => $images
];
if (!in_array($item->getActCategId(), $categs))
$categs[] = $item->getActCategId();
}
}
// categs
if (sizeof($categs) > 0) {
foreach ($categs as $key => $categ) {
$this->urls[] = [
'loc' => "/actualite/" . $categ,
];
}
}
}
private function addModAgenda()
{
$this->urls[] = ['loc' => "/agenda"]; // la home est la plus prio
$repo = $this->getDoctrine()->getRepository(Agenda::class);
$items = $repo->findBy(["agd_enligne" => 1]);
$categs = [];
foreach ($items as $item) {
$images = [];
if ($item->getAgdIllustrationFichier() != "" && $item->getAgdIllustrationFichier() != null) {
$images = [
'loc' => '/medias_front/agenda/' . $item->getAgdIllustrationFichier(), // URL to image
'title' => $item->getAgdTitre() // Optional, text describing the image
];
}
if (!$this->checkIfUrlAlreadyExist($loc = "/agenda/fiche/" . $item->getId())) {
$this->urls[] = [
'loc' => $loc,
'image' => $images
];
if (!in_array($item->getAgdCategId(), $categs))
$categs[] = $item->getAgdCategId();
}
}
// categs
if (sizeof($categs) > 0) {
foreach ($categs as $key => $categ) {
$this->urls[] = [
'loc' => "/agenda/" . $categ,
];
}
}
}
private function addModDocument()
{
$this->urls[] = ['loc' => "/documents"];
$repo = $this->getDoctrine()->getRepository(Document::class);
$items = $repo->findBy(["doc_isonline" => 1]);
$categs = [];
foreach ($items as $item) {
if (!in_array($item->getDocUidCateg(), $categs))
$categs[] = $item->getDocUidCateg();
}
// categs
if (sizeof($categs) > 0) {
foreach ($categs as $key => $categ) {
$this->urls[] = [
'loc' => "/documents/" . $categ,
];
}
}
}
private function addModBoutique()
{
$this->urls[] = ['loc' => "/boutique"];
// marques
$repoMarks = $this->getDoctrine()->getRepository(EcoMarks::class);
$marks = $repoMarks->findAll();
if (sizeof($marks) > 0) {
foreach ($marks as $mark) {
$loc = "/boutique/categories/" . $this->utils->urlrewrite($mark->getMark()) . "/0/" . $mark->getId();
if (!$this->checkIfUrlAlreadyExist($loc)) {
$this->urls[] = ['loc' => $loc];
}
}
}
// -------
// produits et categories
$repoProducts = $this->getDoctrine()->getRepository(EcoProducts::class);
$repoProducts = $this->getDoctrine()->getRepository(EcoProducts::class);
if (CURRENT_SITE_ID == 1) {
$products = $repoProducts->findByProductsByCategorie(0, 1, 'ASC', CURRENT_SITE_ID);
} else {
$products = $repoProducts->findByProductsByCategorie(0, 2, 'ASC', CURRENT_SITE_ID);
}
if (sizeof($products) > 0) {
foreach ($products as $item) {
// produits
$loc = "/boutique/produit/" . $this->utils->urlrewrite($item['prod_model']) . "/" . $item['prod_id'];
if (!$this->checkIfUrlAlreadyExist($loc)) {
$this->urls[] = ['loc' => $loc];
}
// -------
// categories
$loc = "/boutique/" . $this->utils->urlrewrite($item['categ_entitled']) . "/marques/" . $item['category_id'];
if (!$this->checkIfUrlAlreadyExist($loc)) {
$this->urls[] = ['loc' => $loc];
}
// -------
// combo categories / marques dans l'url
if (sizeof($marks) > 0) {
foreach ($marks as $mark) {
$loc = "/boutique/" . $this->utils->urlrewrite($item['categ_entitled']) . "/" . $this->utils->urlrewrite($mark->getMark()) . "/" . $item['category_id'] . "/" . $mark->getId();
if (!$this->checkIfUrlAlreadyExist($loc)) {
$this->urls[] = ['loc' => $loc];
}
}
}
// -------
}
}
// -------
}
}