<?php
namespace App\Controller;
use App\Controller\AbstractController;
use App\Entity\Page;
use App\Misc\EntityUrlGenerator;
use App\Misc\StringUtils;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;
class PageController extends AbstractController
{
public function __construct(
private Environment $twig,
protected MailerInterface $mailer,
private EntityUrlGenerator $entityUrlGenerator,
private RouterInterface $router,
private RequestStack $requestStack,
private EntityManagerInterface $entityManager,
) {
parent::__construct($mailer, $entityUrlGenerator, $router, $requestStack, $entityManager);
}
public function localPage($ville, $cp)
{
// Debug url routing for city with "-"
while (str_contains($cp, '-')):
$ville .= '-' . $error = strtok($cp, '-');
$cp = str_replace($error . '-', '', $cp);
endwhile;
$page = $this->getPageRepository()->getByMeta('cp', $cp);
if (!$page) {
return $this->throw404();
}
$sanitized = StringUtils::sanitizeString($page->getTitle());
if ($ville != $sanitized) {
return $this->redirectToRoute('public_local_page', array(
'ville' => $sanitized,
'cp' => $page->getMeta('cp')
));
}
$deptRepo = $this->getDepartementRepository();
$departement = $deptRepo->getByCode($page->getMeta('departement'));
$departements = $deptRepo->findBy(['region' => $departement->getRegion()]);
$prestataires = [];
foreach ($departements as $dept) {
$presta = (array) $this->getPrestataireRepository()->getByCodePostal(
$dept->getCode(),
$this->getParameter('group_diagnostics')
);
$prestataires = array_merge($prestataires, $presta);
}
return $this->render('Public/local_page.html.twig', array(
'page' => $page,
'departement' => $departement,
'prestataires' => $prestataires
));
}
public function localPages()
{
$repo = $this->getPageRepository();
$page = $repo->getByType(Page::TYPE_LOCAL_INDEX);
$pages = $repo->getLocalPagesList();
// Get Prestataires by page
foreach ($pages as $key => $item) {
$prestataires = $this->getPrestataireRepository()->getBySecteur(
$item['code'],
$this->getParameter('group_diagnostics')
);
$pages[$key]['prestataires'] = count($prestataires);
}
return $this->render('Public/local_pages.html.twig', array(
'page' => $page,
'pages' => $pages
));
}
public function localPageDepartement($departement, $cp)
{
$deptRepo = $this->getDepartementRepository();
$departement = $deptRepo->getByCode($cp);
$repo = $this->getPrestataireRepository();
$prestataires = $repo->getBySecteur(
$departement->getCode(),
$this->getParameter('group_diagnostics')
);
$categories = $repo->getCategoriesForAll($prestataires);
return $this->render('Public/local_page_departement.html.twig', array(
'departement' => $departement,
'prestataires' => $prestataires,
'categories' => $categories
));
}
public function page($url)
{
/** @var Url $url */
$url = $this->getUrlRepository()->findOneBy(array('url' => $url));
if ($url && $url->getRedirectOn()) {
return $this->redirectToRoute('public_page', array(
'url' => $url->getRedirectOn()->getUrl()
), Response::HTTP_MOVED_PERMANENTLY);
}
$page = $url ? $url->getPage() : null;
if (!$page) {
return $this->throw404();
}
$categorieTree = $this->getPageRepository()->getCategorieTree();
$groups = array_flip([
'diagnostics' => 72,
'expertises' => 73,
'etudes' => 74,
'solutions' => 75,
'energies' => 76
]);
// echo '<pre>';
// print_r($groups);
// print_r($categorieTree);
// echo '</pre>';
$data = array(
'page' => $page,
// 'groups' => array_flip($this->container->getParameter('category_groups')),
'cid' => $this->getCategorieRepository()->getByPageId($page->getId())
);
$template = 'Public/page_'. $page->getType() .'.html.twig';
if ($this->twig->getLoader()->exists($template)) {
return $this->render($template, $data);
} else {
// echo '<pre>';
// print_r($page->getRobots());
// echo '</pre>';
return $this->render('Public/page.html.twig', $data);
}
}
}