src/Controller/Front/ZPageController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Form\Handler\SAVHandler;
  4. use App\Form\Type\SAVType;
  5. use App\Security\Voter\Front\PageVoter;
  6. use Stimactiv\ClientBundle\Entity\Contact;
  7. use Stimactiv\CmsBundle\Entity\Page;
  8. use Stimactiv\CmsBundle\Repository\PageI18nRepository;
  9. use Stimactiv\CoreBundle\Repository\LangRepository;
  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. /**
  15.  * Controller is named ZPage to cheat Routing ordering system
  16.  * @Route("/", name="frontend_page_")
  17.  */
  18. class ZPageController extends AbstractController implements AgeVerificationControllerInterface
  19. {
  20.     /**
  21.      * @Route("/page/{page_id}/lang/{lang_code}", name="redirect")
  22.      */
  23.     public function redirectToPage(int $page_idstring $lang_codeLangRepository $langRepositoryPageI18nRepository $pageI18nRepository): Response
  24.     {
  25.         $lang $langRepository->findOneBy(['code' => $lang_code]);
  26.         if ($lang === null) {
  27.             $lang $langRepository->findOneBy(['byDefault' => true]);
  28.         }
  29.         $pageI18n $pageI18nRepository->findOneBy(['page' => $page_id'lang' => $lang]);
  30.         if ($pageI18n === null) {
  31.             throw $this->createNotFoundException();
  32.         }
  33.         return $this->redirectToRoute('frontend_page_show', ['slug' => $pageI18n->getSlug()]);
  34.     }
  35.     /**
  36.      * @Route("/{slug}", name="show", requirements={"slug": "^(?!uploads|tableaudebord|stimactiv|core|manager|elfinder|efconnect|_).+"})
  37.      */
  38.     public function show(Request $requeststring $slugLangRepository $langRepositoryPageI18nRepository $pageI18nRepositorySAVHandler $formHandler): Response
  39.     {
  40.         $params null;
  41.         $lang $langRepository->findOneBy(['code' => $request->getLocale()]);
  42. //        $translator = $this->get('translator');
  43.         if ($lang === null) {
  44.             $lang $langRepository->findOneBy(['byDefault' => true]);
  45.         }
  46.         $pageI18n $pageI18nRepository->findOneBy(['slug' => $slug'lang' => $lang->getId()]);
  47.         if ($pageI18n === null) {
  48.             throw $this->createNotFoundException();
  49.         }
  50.         $page $pageI18n->getPage();
  51.         if ($page->getStatus() !== Page::__P_STATUS_1_) {
  52.             throw $this->createNotFoundException();
  53.         }
  54.         $this->denyAccessUnlessGranted(PageVoter::IS_READABLE$page);
  55.         $template $page->getTemplate();
  56.         $templateTwig 'app/frontend/pages/templates/' $template '.html.twig';
  57.         switch ($template) {
  58.             case 'contact':
  59.                 $user $this->getUser() instanceof Contact $this->getUser() : null;
  60.                 $form $this->createForm(SAVType::class, null, ['user' => $user]);
  61.                 if ($formHandler->create($form)) {
  62.                     $this->addFlash('success''Votre demande vient d\'être envoyé.');
  63.                     return $this->redirectToRoute('frontend_page_show', ['slug' => $slug]);
  64.                 }
  65.                 $params = [
  66.                     'form' => $form->createView(),
  67.                 ];
  68.                 break;
  69.         }
  70.         return $this->render($templateTwig, [
  71.             'page' => $page,
  72.             'page_i18n' => $pageI18n,
  73.             'params' => $params,
  74.         ]);
  75.     }
  76. }