src/Controller/FrontController.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Page;
  4. use App\Entity\TPage;
  5. use App\Entity\TPageTranslation;
  6. use App\Form\ContactType;
  7. use App\Form\ContactTypeData;
  8. use App\Repository\TPageRepository;
  9. use App\Services\PageBuilder;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\Form\FormError;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. use Symfony\Component\HttpKernel\KernelInterface;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. class FrontController extends AbstractController
  18. {
  19.     /**
  20.      * @Route("/", name="homepage", options={"i18n"=false})
  21.      */
  22.     public function index()
  23.     {
  24.         return $this->render('front/index.html.twig', [
  25.             'controller_name' => 'FrontController',
  26.         ]);
  27.     }
  28.     /**
  29.      * @Route("/search/{term}", name="search")
  30.      */
  31.     public function search($term=null)
  32.     {
  33.         return $this->render('front/index.html.twig', [
  34.             'controller_name' => 'FrontController',
  35.         ]);
  36.     }
  37.     /**
  38.      * @Route("/multilingue", name="multilingue")
  39.      */
  40.     public function multilingue(Request $request)
  41.     {
  42.         return $this->render('front/'.$request->getLocale().'/multilingue.html.twig', [
  43.             'controller_name' => 'FrontController',
  44.         ]);
  45.     }
  46.     /**
  47.      * @Route("/{slug}", name="tpage", priority=-2)
  48.      */
  49.     public function tpage(Request $requestKernelInterface $kernelSessionInterface $sessionEntityManagerInterface $emPageBuilder $pbstring $slug)
  50.     {
  51.         $locale $request->attributes->get('_locale') ? $request->attributes->get('_locale') : $request->getSession()->get('_locale');
  52.         /** @var TPageRepository $repo */
  53.         $repo $em->getRepository(TPage::class);
  54.         /** @var TPage $page */
  55.         $page $repo->findBySlug($slug$locale);
  56.         if(!$page)
  57.             throw $this->createNotFoundException();
  58.         /** @var TPageTranslation $pageTrans */
  59.         $pageTrans $page->translate();
  60.         /* exceptions */
  61.         if($pageTrans->getSlug() == "contact")
  62.             return $this->contact($kernel$session$request$em$page);
  63.         if($pageTrans->getSlug() != $slug && $pageTrans->getSlug() != "")
  64.             return $this->redirectToRoute("tpage", ["slug" => $pageTrans->getSlug()], 301);
  65.         /* pagebuilder editmode */
  66.         $isEditMode = (($this->isGranted('ROLE_ADMIN')|| true) && $request->get('editor'));
  67.         $pbtest $pb->compile($pageTrans->getPbcontent());
  68.         return $this->render('front/pages/templates/'.$page->getTemplate().'.html.twig', [
  69.             'isEditMode' => $isEditMode,
  70.             'page' => $page,
  71.             'pbtest' => $pbtest
  72.         ]);
  73.     }
  74.     /**
  75.      * @Route("/{slug}", name="page", options={"i18n"=false}, priority=-2)
  76.      */
  77.     /**
  78.      * @Route("/{slug}", name="page", options={"i18n"=false}, priority=-2)
  79.      */
  80.     public function page(EntityManagerInterface $emPage $page)
  81.     {
  82.         return $this->render('front/pages/templates/'.$page->getTemplate().'.html.twig', [
  83.             'page' => $page
  84.         ]);
  85.     }
  86.     private function contact(KernelInterface $kernelSessionInterface $sessionRequest $requestEntityManagerInterface $emTPage $page)
  87.     {
  88.         $contactData = new ContactTypeData();
  89.         $contactForm $this->createForm(ContactType::class, $contactData);
  90.         $contactForm->handleRequest($request);
  91.         $captcha $session->get('bt-cptch');
  92.         if ($contactForm->isSubmitted()) {
  93.             if($contactData->getCptcha() != $session->get('bt-cptch'))
  94.                 $contactForm->get('cptcha')->addError(new FormError("Captcha incorrect"));
  95.             if($contactForm->isValid()) {
  96.                 if($contactData->getAdresse() == "") {
  97.                     $dest $contactData->getDestinataire() != "" $contactData->getDestinataire() : 'autre';
  98.                     $destMail "info@medipremagroup.com";
  99.                     $message = (new \Swift('Contact Site Mediprema - ' $dest))
  100.                         ->setFrom($contactData->getEmail())
  101.                         ->setTo($kernel->isDebug() ? "nicolas@factory02.com" $destMail)
  102.                         ->setBcc("nicolas@factory02.com")
  103.                         ->setBody($this->renderView('email/contact.html.twig', ["data" => $contactData"destMail" => $destMail"isDebug" => $kernel->isDebug()]), 'text/html');
  104.                     $mailer->send($message);
  105.                     $this->addFlash('success'$translator->trans('message.sent', [], 'contact'));
  106.                 }
  107.                 $data = ["form" => isset($contactForm) ? $contactForm->createView() : null"preset" => $preset $preset false];
  108.                 return $this->render("front/page/contact.html.twig", array(
  109.                     "page" => $page,
  110.                     "data" => $data
  111.                 ));
  112.                 //return $this->redirectToRoute('page', ['slug' => $page->translate()->getSlug()]);
  113.             }
  114.         }
  115.         else {
  116.             $captcha "";
  117.             for($i 0$i 5$i++)
  118.                 $captcha .= rand(19);
  119.             $session->set("bt-cptch"$captcha);
  120.         }
  121.         return $this->render('front/pages/templates/'.$page->getTemplate().'.html.twig', [
  122.             'page' => $page,
  123.             "data" => ["form" => $contactForm->createView()],
  124.             "captcha" => $captcha
  125.         ]);
  126.     }
  127. }