<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use App\Controller\AbstractController;
use App\Entity\Departement;
use Symfony\Component\Routing\Router;
use App\Entity\Prestataire;
use App\Misc\StringUtils;
use App\Entity\Page;
use Symfony\Component\HttpFoundation\Request;
class SitemapController extends AbstractController
{
public function sitemap()
{
$categories = $this->getPageRepository()->getCategorieTree();
$locales = $this->getPageRepository()->getLocalPagesForSitemap();
foreach ($locales as $key => $locale) {
// Sanitized Url Ville
$locales[$key]['slug'] = StringUtils::sanitizeString($locale['title']);
}
$departements = $this->getDoctrine()->getRepository(Departement::class)->findBy([], ['nom' => 'ASC']);
foreach ($departements as $key => $departement) {
$dept = $departement->getCode();
$prestataires = $this->getPrestataireRepository()->getBySecteur(
$dept,
$this->getParameter('group_diagnostics')
);
if (count($prestataires) <= 0) {
unset($departements[$key]);
}
}
return $this->render('Public/sitemap.html.twig', array(
'categories' => $categories,
'locales' => $locales,
'departements' => $departements
));
}
public function index()
{
/** @var RouterInterface $router */
$router = $this->getRouter();
$sitemaps = array();
$sitemaps[] = array(
'loc' => $router->generate('sitemap_pages', array(), Router::ABSOLUTE_URL),
'modified' => $this->getPageRepository()->getLastModifiedDate()
);
// $sitemaps[] = array(
// 'loc' => $router->generate('sitemap_actus', array(), Router::ABSOLUTE_URL),
// 'modified' => $this->getPageRepository()->getLastModifiedDate(Page::TYPE_ACTU)
// );
$sitemaps[] = array(
'loc' => $router->generate('sitemap_locales', array(), Router::ABSOLUTE_URL),
'modified' => $this->getPageRepository()->getLastModifiedDate(Page::TYPE_LOCAL)
);
$sitemaps[] = array(
'loc' => $router->generate('sitemap_prestataires', array(), Router::ABSOLUTE_URL),
'modified' => $this->getPrestataireRepository()->getLastModifiedDate()
);
// $sitemaps[] = array(
// 'loc' => $router->generate('sitemap_demandes', array(), Router::ABSOLUTE_URL),
// 'modified' => $this->getDemandeRepository()->getLastModifiedDate()
// );
return $this->render('Sitemap/sitemap_index.xml.twig', array(
'sitemaps' => $sitemaps
), $this->getResponse());
}
private function getResponse()
{
$response = new Response();
$response->headers->set('Content-Type', 'text/xml');
return $response;
}
private function renderMap($urls)
{
return $this->render('Sitemap/sitemap.xml.twig', array(
'urls' => $urls,
), $this->getResponse());
}
public function pages()
{
$urls = array();
$urls[] = array(
'loc' => $this->getRouter()->generate('public_index', array(), Router::ABSOLUTE_URL),
'changefreq' => 'hourly',
'priority' => '1'
);
$urls[] = array(
'loc' => $this->getRouter()->generate('public_categories', array(), Router::ABSOLUTE_URL),
'changefreq' => 'hourly',
'priority' => '1'
);
$urls[] = array(
'loc' => $this->getRouter()->generate('public_prestataires', array(), Router::ABSOLUTE_URL),
'changefreq' => 'monthly',
'priority' => '0.7'
);
$urls[] = array(
'loc' => $this->getRouter()->generate('public_demandes', array(), Router::ABSOLUTE_URL),
'changefreq' => 'monthly',
'priority' => '0.7'
);
$urls[] = array(
'loc' => $this->getRouter()->generate('public_local_pages', array(), Router::ABSOLUTE_URL),
'changefreq' => 'monthly',
'priority' => '0.7'
);
$urls[] = array(
'loc' => $this->getRouter()->generate('public_sitemap', array(), Router::ABSOLUTE_URL),
'changefreq' => 'monthly',
'priority' => '0.5'
);
$urls[] = array(
'loc' => $this->getRouter()->generate('public_demande_form', array(), Router::ABSOLUTE_URL),
'changefreq' => 'monthly',
'priority' => '0.7'
);
$urls[] = array(
'loc' => $this->getRouter()->generate('presta_access', array(), Router::ABSOLUTE_URL),
'changefreq' => 'monthly',
'priority' => '0.7'
);
// $urls[] = array(
// 'loc' => $this->getRouter()->generate('public_actus', array(), Router::ABSOLUTE_URL),
// 'changefreq' => 'monthly',
// 'priority' => '0.7'
// );
$page = $this->getPageRepository()->getByType('prix');
$urls[] = array(
'loc' => $this->getRouter()->generate('public_page', array('url' => $page->getUrl()->getUrl()), Router::ABSOLUTE_URL),
'changefreq' => 'monthly',
'priority' => '0.8',
'lastmod' => $page->getModifiedDate()
);
/** @var Page[] $pages */
foreach ($this->getPageRepository()->getForXmlSiteMap() ?? [] as $page) {
if ($page->getType() != 'prix'):
$priority = '0.5';
switch ($page->getType()) {
case Page::TYPE_CATEGORIE:
$priority = '0.8';
break;
case Page::TYPE_AIDE:
case Page::TYPE_QUALITY:
$priority = '0.7';
break;
}
if ($page->getId() === 122 || ($page->getParent() && $page->getParent()->getId() === 122)):
$order = 10;
elseif ($page->getId() === 140 || ($page->getParent() && $page->getParent()->getId() == 140)):
$order = 100;
elseif ($page->getId() === 163 || ($page->getParent() && $page->getParent()->getId() == 163)):
$order = 1000;
elseif ($page->getId() === 178 || ($page->getParent() && $page->getParent()->getId() == 178)):
$order = 10000;
elseif ($page->getId() === 189 || ($page->getParent() && $page->getParent()->getId() == 189)):
$order = 100000;
endif;
$pages[] = array(
'loc' => $this->getRouter()->generate('public_page', array(
'url' => $page->getUrl()->getUrl()
), Router::ABSOLUTE_URL),
'priority' => $priority,
'changefreq' => 'monthly',
'lastmod' => $page->getModifiedDate(),
'order' => $order + $page->getOrder(),
);
endif;
}
usort($pages, function ($a, $b) {
return $a['order'] <=> $b['order'];
});
$urls = array_merge($urls, $pages);
$urls[] = array(
'loc' => $this->getRouter()->generate('public_contact', array(), Router::ABSOLUTE_URL),
'changefreq' => 'monthly',
'priority' => '0.5'
);
return $this->renderMap($urls);
}
// public function actus()
// {
// $urls = array();
// /** @var Page[] $actus */
// $actus = $this->getPageRepository()->findBy(array('type' => Page::TYPE_ACTU));
// foreach ($actus as $actu) {
// $urls[] = array(
// 'loc' => $this->getUrlGenerator()->getUrlFor($actu, true),
// 'priority' => '0.8',
// 'changefreq' => 'monthly',
// 'lastmod' => $actu->getModifiedDate()
// );
// }
// return $this->renderMap($urls);
// }
public function locales(Request $request)
{
$urls = array();
// Pages locales (diagnostic)
$locales = $this->getPageRepository()->getLocalPagesForSitemap();
foreach ($locales as $locale) {
$urls[] = array(
'loc' => $request->getScheme() . '://' . $request->getHttpHost() . '/diagnostic-immobilier-' . StringUtils::sanitizeString($locale['title']) . '-' . $locale['cp'] . '.html',
'priority' => '0.8',
'changefreq' => 'monthly',
'lastmod' => $locale['modified']
);
}
// Pages departement (diagnostiqueur immobilier)
$departements = $this->getEm()->getRepository(Departement::class)->findBy([], ['nom' => 'ASC']);
foreach ($departements as $departement) {
$dept = $departement->getCode();
$prestataires = $this->getPrestataireRepository()->getBySecteur(
$dept,
$this->getParameter('group_diagnostics')
);
if (count($prestataires) > 0) {
$urlsDepartements[] = array(
'loc' => $this->getRouter()->generate('public_local_page_departement', array(
'departement' => StringUtils::sanitizeString($departement->getSlug()),
'cp' => $departement->getCode()
), Router::ABSOLUTE_URL),
'priority' => '0.8',
'changefreq' => 'monthly',
'lastmod' => '2018-05-31 11:17'
);
}
}
$urls = array_merge($urls, $urlsDepartements);
return $this->renderMap($urls);
}
public function prestataires()
{
$urls = array();
/** @var Prestataire[] $prestataires */
$prestataires = $this->getPrestataireRepository()->findBy(array(
'status' => Prestataire::STATUS_VALIDATED
));
foreach ($prestataires as $prestataire) {
$url = array(
'loc' => $this->getUrlGenerator()->getUrlFor($prestataire, true),
'priority' => '0.7',
'changefreq' => 'weekly',
'lastmod' => $prestataire->getModifiedDate()
);
if ($prestataire->getLogoPath()) {
$url['image'] = array(
'loc' => 'data/logos/'. $prestataire->getLogoPath(),
'caption' => $prestataire->getEntreprise()
);
}
$urls[] = $url;
}
return $this->renderMap($urls);
}
// public function demandes()
// {
// $urls = array();
// /** @var Demande[] $demandes */
// $demandes = $this->getDemandeRepository()->getForXmlSiteMap();
// foreach ($demandes as $demande) {
// $urls[] = array(
// 'loc' => $this->getUrlGenerator()->getUrlFor($demande, true),
// 'priority' => '0.7',
// 'changefreq' => 'weekly',
// 'lastmod' => $demande->getModifiedDate()
// );
// }
// return $this->renderMap($urls);
// }
}