src/Controller/FrontController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Contact;
  4. use App\Entity\Page;
  5. use App\Entity\Paragraph;
  6. use App\Entity\Partner;
  7. use App\Entity\Post;
  8. use App\Form\ContactType;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Knp\Component\Pager\PaginatorInterface;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  12. use Symfony\Bridge\Twig\Mime\BodyRenderer;
  13. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\Asset\Packages;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Mailer\Mailer;
  19. use Symfony\Component\Mailer\MailerInterface;
  20. use Symfony\Component\Mailer\Transport;
  21. use Symfony\Component\Mime\Email;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Twig\Environment;
  24. use Twig\Loader\FilesystemLoader;
  25. class FrontController extends AbstractController
  26. {
  27.     /**
  28.      * @Route("/", name="front_landing")
  29.      */
  30.     public function landing(ManagerRegistry $managerRegistry): Response
  31.     {
  32.         $asbtemplate $this->getParameter('asbtemplate');
  33.         if ($asbtemplate["landing"]["partners"]) {
  34.             $repoPartner $managerRegistry->getRepository(Partner::class);
  35.             $partners $repoPartner->findAll();
  36.         }
  37.         if ($asbtemplate["landing"]["lastPosts"]) {
  38.             $repoPost $managerRegistry->getRepository(Post::class);
  39.             $data["between"]["notStrict"]["a.date"]["max"] = new \DateTime();
  40.             $data["orderBy"] = ["a.date""desc"];
  41.             $data["isPublished"] = true;
  42.             $data["limit"] = $asbtemplate["landing"]["nbLandingPosts"];
  43.             $posts $repoPost->search($data);
  44.         }
  45.         return $this->render('front/landing.html.twig', [
  46.             "posts" => (isset($posts) ? $posts : []),
  47.             "partners" => (isset($partners) ? $partners : [])
  48.         ]);
  49.     }
  50.     /**
  51.      * @Route("/sitemap.xml", name="sitemap", defaults={"_format"="xml"})
  52.      */
  53.     public function sitemap(Request $requestPackages $packages)
  54.     {
  55.         $repoPage $this->getDoctrine()->getRepository(Page::class);
  56.         $page $repoPage->search(["notNull" => ["a.sitemapFileName"], "limit" => 1]);
  57.         if ($page and $page->getSitemapFileName()) {
  58.             return $this->redirect($packages->getUrl('upload/sitemap/' $page->getSitemapFileName()));
  59.         } else {
  60.             return $this->redirectToRoute('front_landing');
  61.         }
  62.     }
  63.     /**
  64.      * @Route("/contact", name="front_contact")
  65.      */
  66.     public function contact(Request $requestManagerRegistry $managerRegistry)
  67.     {
  68.         $contact = new Contact();
  69.         $contactForm $this->createForm(ContactType::class, $contact);
  70.         $contactForm->handleRequest($request);
  71.         if ($contactForm->isSubmitted() && $contactForm->isValid()) {
  72.             $recaptchaResponse $request->request->get('g-recaptcha-response'null);
  73.             $isRecaptchaValid false;
  74.             if ($recaptchaResponse) {
  75.                 $paramsArr = array(
  76.                     "response" => $recaptchaResponse,
  77.                     "secret" => $this->getParameter('recaptchaSecret')
  78.                 );
  79.                 $ch curl_init();
  80.                 curl_setopt($chCURLOPT_URL"https://www.google.com/recaptcha/api/siteverify");
  81.                 curl_setopt($chCURLOPT_POST1);
  82.                 curl_setopt($chCURLOPT_POSTFIELDShttp_build_query($paramsArr));
  83.                 curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  84.                 $isRecaptchaValid json_decode(curl_exec($ch))->success;
  85.             }
  86.             if (!$isRecaptchaValid) {
  87.                 $this->addFlash("danger""Veuillez recommencer en validant le captcha.");
  88.             } else {
  89.                 $em $managerRegistry->getManager();
  90.                 $em->persist($contact);
  91.                 $em->flush();
  92.                 $this->addFlash("success""Message envoyé.");
  93.                 $transport Transport::fromDsn($this->getParameter('mailer_dsn'));
  94.                 $mailer = new Mailer($transport);
  95.                 $asbtemplate $this->getParameter('asbtemplate');
  96.                 $email = (new TemplatedEmail())
  97.                     ->from($this->getParameter('mailer_user'))
  98.                     ->to($this->getParameter('mailer_user'))
  99.                     ->subject("Nouveau message")
  100.                     // path of the Twig template to render
  101.                     ->htmlTemplate('mail/contact.html.twig')
  102.                     // pass variables (name => value) to the template
  103.                     ->context(["contact" => $contact'asbtemplate' => $asbtemplate]);
  104.                 if ($asbtemplate["contact"]["customFiles"]) {
  105.                     foreach ($contact->getCustomFiles() as $key => $customFile) {
  106.                         $email->attachFromPath(("upload/customFile/" $customFile->getCustomFileFileName()));
  107.                     }
  108.                 }
  109.                 $loader = new FilesystemLoader($this->getParameter('kernel.project_dir') . '/templates/');
  110.                 $twigEnv = new Environment($loader);
  111.                 $twigBodyRenderer = new BodyRenderer($twigEnv);
  112.                 $twigBodyRenderer->render($email);
  113.                 $mailer->send($email);
  114.                 return $this->redirectToRoute('front_contact');
  115.             }
  116.         }
  117.         return $this->render('front/contact.html.twig', array(
  118.             'contactForm' => $contactForm->createView(),
  119.         ));
  120.     }
  121.     /**
  122.      * @Route("/actualites", name="front_posts")
  123.      */
  124.     public function posts(Request $requestPaginatorInterface $paginatorManagerRegistry $managerRegistry)
  125.     {
  126.         $asbtemplate $this->getParameter('asbtemplate');
  127.         $repoPost $managerRegistry->getRepository(Post::class);
  128.         $data["between"]["notStrict"]["a.date"]["max"] = new \DateTime();
  129.         $data["orderBy"] = ["a.date""desc"];
  130.         $data["isPublished"] = true;
  131.         $posts $paginator->paginate(
  132.             $repoPost->search($data), $request->query->getInt('page'1)/* page number */$asbtemplate["post"]["postLimitPerPage"]/* limit per page */
  133.         );
  134.         return $this->render('front/posts.html.twig', array(
  135.             "posts" => $posts
  136.         ));
  137.     }
  138.     /**
  139.      * @Route("/actualite/{slug}", name="front_post")
  140.      */
  141.     public function post(Request $requestManagerRegistry $managerRegistryPost $post)
  142.     {
  143.         if ($post->getIsPublished() or $this->isGranted("ROLE_ADMIN")) {
  144.             $repoParagraph $managerRegistry->getRepository(Paragraph::class);
  145.             $paragraphs $repoParagraph->findBy(["post" => $post], ["position" => "asc"]);
  146.             return $this->render('front/post.html.twig', array(
  147.                 "post" => $post,
  148.                 "paragraphs" => $paragraphs,
  149.             ));
  150.         } else {
  151.             $this->addFlash("danger""Vous ne pouvez pas accéder à cet article.");
  152.             return $this->redirectToRoute('front_posts');
  153.         }
  154.     }
  155. }