<?php
namespace App\Controller\Front;
use App\Form\Handler\SAVHandler;
use App\Form\Type\SAVType;
use App\Security\Voter\Front\PageVoter;
use Stimactiv\ClientBundle\Entity\Contact;
use Stimactiv\CmsBundle\Entity\Page;
use Stimactiv\CmsBundle\Repository\PageI18nRepository;
use Stimactiv\CoreBundle\Repository\LangRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* Controller is named ZPage to cheat Routing ordering system
* @Route("/", name="frontend_page_")
*/
class ZPageController extends AbstractController implements AgeVerificationControllerInterface
{
/**
* @Route("/page/{page_id}/lang/{lang_code}", name="redirect")
*/
public function redirectToPage(int $page_id, string $lang_code, LangRepository $langRepository, PageI18nRepository $pageI18nRepository): Response
{
$lang = $langRepository->findOneBy(['code' => $lang_code]);
if ($lang === null) {
$lang = $langRepository->findOneBy(['byDefault' => true]);
}
$pageI18n = $pageI18nRepository->findOneBy(['page' => $page_id, 'lang' => $lang]);
if ($pageI18n === null) {
throw $this->createNotFoundException();
}
return $this->redirectToRoute('frontend_page_show', ['slug' => $pageI18n->getSlug()]);
}
/**
* @Route("/{slug}", name="show", requirements={"slug": "^(?!uploads|tableaudebord|stimactiv|core|manager|elfinder|efconnect|_).+"})
*/
public function show(Request $request, string $slug, LangRepository $langRepository, PageI18nRepository $pageI18nRepository, SAVHandler $formHandler): Response
{
$params = null;
$lang = $langRepository->findOneBy(['code' => $request->getLocale()]);
// $translator = $this->get('translator');
if ($lang === null) {
$lang = $langRepository->findOneBy(['byDefault' => true]);
}
$pageI18n = $pageI18nRepository->findOneBy(['slug' => $slug, 'lang' => $lang->getId()]);
if ($pageI18n === null) {
throw $this->createNotFoundException();
}
$page = $pageI18n->getPage();
if ($page->getStatus() !== Page::__P_STATUS_1_) {
throw $this->createNotFoundException();
}
$this->denyAccessUnlessGranted(PageVoter::IS_READABLE, $page);
$template = $page->getTemplate();
$templateTwig = 'app/frontend/pages/templates/' . $template . '.html.twig';
switch ($template) {
case 'contact':
$user = $this->getUser() instanceof Contact ? $this->getUser() : null;
$form = $this->createForm(SAVType::class, null, ['user' => $user]);
if ($formHandler->create($form)) {
$this->addFlash('success', 'Votre demande vient d\'être envoyé.');
return $this->redirectToRoute('frontend_page_show', ['slug' => $slug]);
}
$params = [
'form' => $form->createView(),
];
break;
}
return $this->render($templateTwig, [
'page' => $page,
'page_i18n' => $pageI18n,
'params' => $params,
]);
}
}