src/Controller/SitemapController.php line 74

Open in your IDE?
  1. <?php
  2. namespace Acme\SudcmsBundle\Controller;
  3. use Acme\SudcmsBundle\Entity\Actualite;
  4. use Acme\SudcmsBundle\Entity\Agenda;
  5. use Acme\SudcmsBundle\Entity\Document;
  6. use Acme\SudcmsBundle\Entity\EcoMarks;
  7. use Acme\SudcmsBundle\Entity\EcoProducts;
  8. use Acme\SudcmsBundle\Entity\Pages;
  9. use Acme\SudcmsBundle\Service\OtideaUtils;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. class SitemapController extends AbstractController
  15. {
  16.     private $urls;
  17.     #[Route(path'/sitemap.xml'name'sitemap'defaults: ['_format' => 'xml'])]
  18.     public function index(Request $requestOtideaUtils $utils)
  19.     {
  20.         // Nous récupérons le nom d'hôte depuis l'URL
  21.         $hostname $request->getSchemeAndHttpHost();
  22.         $this->utils $utils;
  23.         // On initialise un tableau pour lister les URLs
  24.         $this->urls = [];
  25.         $this->addStaticsPages();
  26.         $this->addModPages();
  27.         $this->addModActualite();
  28.         //$this->addModAgenda();
  29.         //$this->addModDocument();
  30.         
  31.         if (CURRENT_SITE_ID == 1) {
  32.             $this->urls[] = ['loc' => "/cures"];
  33.         } else {
  34.             $this->addModBoutique();
  35.         }
  36.         // Fabrication de la réponse XML
  37.         $response = new Response(
  38.             $this->renderView('sitemap/index.html.twig', [
  39.                     'urls' => $this->urls,
  40.                     'hostname' => $hostname]
  41.             )
  42.         );
  43.         // Ajout des entêtes
  44.         $response->headers->set('Content-Type''text/xml');
  45.         // On envoie la réponse
  46.         return $response;
  47.     }
  48.     private function checkIfUrlAlreadyExist($newUrl null)
  49.     {
  50.         if (isset($newUrl) && is_string($newUrl) && trim($newUrl) != "" && sizeof($this->urls) > 0) {
  51.             foreach ($this->urls as $url) {
  52.                 if ($newUrl == $url['loc'])
  53.                     return true;
  54.             }
  55.         }
  56.         return false;
  57.     }
  58.     private function addStaticsPages()
  59.     {
  60.         $this->urls[] = ['loc' => $this->generateUrl('front_home'), "priority" => 1]; // la home est la plus prio
  61.     }
  62.     private function addModPages()
  63.     {
  64.         $repo $this->getDoctrine()->getRepository(Pages::class);
  65.         $items $repo->findBy(["penligne" => 1]);
  66.         foreach ($items as $item) {
  67.             $images = [];
  68.             if ($item->getPbandeau() != "" && $item->getPbandeau() != null) {
  69.                 $images = [
  70.                     'loc' => '/medias_front/pages/' $item->getPbandeau(), // URL to image
  71.                     'title' => $item->getPtitre()    // Optional, text describing the image
  72.                 ];
  73.             }
  74.             if ($item->getPType() == "standard" && !$this->checkIfUrlAlreadyExist($loc "/page/" $item->getPermalien())) {
  75.                 $this->urls[] = [
  76.                     'loc' => $loc,
  77. //                    'lastmod' => $item->getPdateLastupdate()->format('Y-m-d'),
  78.                     'image' => $images
  79.                 ];
  80.             }
  81.         }
  82.     }
  83.     private function addModActualite()
  84.     {
  85.         $this->urls[] = ['loc' => "/actualite"];
  86.         $repo $this->getDoctrine()->getRepository(Actualite::class);
  87.         $items $repo->findBy(["act_enligne" => 1]);
  88.         $categs = [];
  89.         foreach ($items as $item) {
  90.             $images = [];
  91.             if ($item->getActIllustrationFichier() != "" && $item->getActIllustrationFichier() != null) {
  92.                 $images = [
  93.                     'loc' => '/medias_front/actualite/' $item->getActIllustrationFichier(), // URL to image
  94.                     'title' => $item->getActTitre()    // Optional, text describing the image
  95.                 ];
  96.             }
  97.             if (!$this->checkIfUrlAlreadyExist($loc "/actualite/fiche/" $item->getId())) {
  98.                 $this->urls[] = [
  99.                     'loc' => $loc,
  100.                     'image' => $images
  101.                 ];
  102.                 if (!in_array($item->getActCategId(), $categs))
  103.                     $categs[] = $item->getActCategId();
  104.             }
  105.         }
  106.         // categs
  107.         if (sizeof($categs) > 0) {
  108.             foreach ($categs as $key => $categ) {
  109.                 $this->urls[] = [
  110.                     'loc' => "/actualite/" $categ,
  111.                 ];
  112.             }
  113.         }
  114.     }
  115.     private function addModAgenda()
  116.     {
  117.         $this->urls[] = ['loc' => "/agenda"]; // la home est la plus prio
  118.         $repo $this->getDoctrine()->getRepository(Agenda::class);
  119.         $items $repo->findBy(["agd_enligne" => 1]);
  120.         $categs = [];
  121.         foreach ($items as $item) {
  122.             $images = [];
  123.             if ($item->getAgdIllustrationFichier() != "" && $item->getAgdIllustrationFichier() != null) {
  124.                 $images = [
  125.                     'loc' => '/medias_front/agenda/' $item->getAgdIllustrationFichier(), // URL to image
  126.                     'title' => $item->getAgdTitre()    // Optional, text describing the image
  127.                 ];
  128.             }
  129.             if (!$this->checkIfUrlAlreadyExist($loc "/agenda/fiche/" $item->getId())) {
  130.                 $this->urls[] = [
  131.                     'loc' => $loc,
  132.                     'image' => $images
  133.                 ];
  134.                 if (!in_array($item->getAgdCategId(), $categs))
  135.                     $categs[] = $item->getAgdCategId();
  136.             }
  137.         }
  138.         // categs
  139.         if (sizeof($categs) > 0) {
  140.             foreach ($categs as $key => $categ) {
  141.                 $this->urls[] = [
  142.                     'loc' => "/agenda/" $categ,
  143.                 ];
  144.             }
  145.         }
  146.     }
  147.     private function addModDocument()
  148.     {
  149.         $this->urls[] = ['loc' => "/documents"];
  150.         $repo $this->getDoctrine()->getRepository(Document::class);
  151.         $items $repo->findBy(["doc_isonline" => 1]);
  152.         $categs = [];
  153.         foreach ($items as $item) {
  154.             if (!in_array($item->getDocUidCateg(), $categs))
  155.                 $categs[] = $item->getDocUidCateg();
  156.         }
  157.         // categs
  158.         if (sizeof($categs) > 0) {
  159.             foreach ($categs as $key => $categ) {
  160.                 $this->urls[] = [
  161.                     'loc' => "/documents/" $categ,
  162.                 ];
  163.             }
  164.         }
  165.     }
  166.     private function addModBoutique()
  167.     {
  168.         $this->urls[] = ['loc' => "/boutique"];
  169.         // marques
  170.         $repoMarks $this->getDoctrine()->getRepository(EcoMarks::class);
  171.         $marks $repoMarks->findAll();
  172.         if (sizeof($marks) > 0) {
  173.             foreach ($marks as $mark) {
  174.                 $loc "/boutique/categories/" $this->utils->urlrewrite($mark->getMark()) . "/0/" $mark->getId();
  175.                 if (!$this->checkIfUrlAlreadyExist($loc)) {
  176.                     $this->urls[] = ['loc' => $loc];
  177.                 }
  178.             }
  179.         }
  180.         // -------
  181.         // produits et categories
  182.         $repoProducts $this->getDoctrine()->getRepository(EcoProducts::class);
  183.         $repoProducts $this->getDoctrine()->getRepository(EcoProducts::class);
  184.         if (CURRENT_SITE_ID == 1) {
  185.             $products $repoProducts->findByProductsByCategorie(01'ASC'CURRENT_SITE_ID);
  186.         } else {
  187.             $products $repoProducts->findByProductsByCategorie(02'ASC'CURRENT_SITE_ID);
  188.         }
  189.         if (sizeof($products) > 0) {
  190.             foreach ($products as $item) {
  191.                 // produits
  192.                 $loc "/boutique/produit/" $this->utils->urlrewrite($item['prod_model']) . "/" $item['prod_id'];
  193.                 if (!$this->checkIfUrlAlreadyExist($loc)) {
  194.                     $this->urls[] = ['loc' => $loc];
  195.                 }
  196.                 // -------
  197.                 // categories
  198.                 $loc "/boutique/" $this->utils->urlrewrite($item['categ_entitled']) . "/marques/" $item['category_id'];
  199.                 if (!$this->checkIfUrlAlreadyExist($loc)) {
  200.                     $this->urls[] = ['loc' => $loc];
  201.                 }
  202.                 // -------
  203.                 // combo categories / marques dans l'url
  204.                 if (sizeof($marks) > 0) {
  205.                     foreach ($marks as $mark) {
  206.                         $loc "/boutique/" $this->utils->urlrewrite($item['categ_entitled']) . "/" $this->utils->urlrewrite($mark->getMark()) . "/" $item['category_id'] . "/" $mark->getId();
  207.                         if (!$this->checkIfUrlAlreadyExist($loc)) {
  208.                             $this->urls[] = ['loc' => $loc];
  209.                         }
  210.                     }
  211.                 }
  212.                 // -------
  213.             }
  214.         }
  215.         // -------
  216.     }
  217. }