src/Controller/FrontController.php line 47

Open in your IDE?
  1. <?php
  2. /*
  3.  * For the full copyright and license information, please view the LICENSE file
  4.  * that was distributed with this source code.
  5.  */
  6. namespace App\Controller;
  7. use App\Entity\Marque;
  8. use App\Entity\Recipe;
  9. use FOS\UserBundle\Mailer\Mailer;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  13. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  14. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  15. use Symfony\Component\Form\Extension\Core\Type\TextType;
  16. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use App\Entity\Slider;
  20. use App\Entity\News;
  21. use App\Entity\Contact;
  22. use App\Entity\Mail;
  23. use App\Entity\Farmer;
  24. use App\Entity\Pdv;
  25. use App\Entity\Demarche;
  26. use App\Entity\Img;
  27. use App\Entity\PdvHasDem;
  28. use App\Entity\ElvHasDem;
  29. use App\Entity\DemarcheBo;
  30. use Symfony\Component\HttpFoundation\JsonResponse;
  31. use JMS\Serializer\SerializerInterface;
  32. class FrontController extends AbstractController
  33. {
  34.     /**
  35.      * @Route(path="/", name="app_homepage")
  36.      *
  37.      * @param Request $request Request
  38.      *
  39.      * @return Response
  40.      */
  41.     public function homepageAction(Request $request)
  42.     {
  43.         $images $this->getDoctrine()->getRepository(Slider::class)->findAllWithoutDelete($page null);
  44.         $news $this->getDoctrine()->getRepository(News::class)->findLast();
  45.         return $this->render('front/pages/index.html.twig', [
  46.             'images' => $images,
  47.             'news' => $news
  48.         ]);
  49.     }
  50.     /**
  51.      * @Route(path="/404", name="app_404")
  52.      *
  53.      * @param Request $request Request
  54.      *
  55.      * @return Response
  56.      */
  57.     public function ErrorAction(Request $request)
  58.     {
  59.         return $this->render('TwigBundle/views/Exception/error404.html.twig', [
  60.         ]);
  61.     }
  62.     /**
  63.      * @Route(path="/eleveurs-bouchers", name="app_eleveurs")
  64.      *
  65.      * @param Request $request Request
  66.      *
  67.      * @return Response
  68.      */
  69.     public function eleveursAction(Request $request)
  70.     {
  71.         return $this->render('front/pages/eleveurs.html.twig', []);
  72.     }
  73.     /**
  74.      * @Route(path="/carte", name="app_map")
  75.      * @param Request $request Request
  76.      *
  77.      * @return Response
  78.      */
  79.     public function newMapAction(Request $request)
  80.     {
  81.         $boeuf $this->getDoctrine()
  82.             ->getRepository(Marque::class)
  83.             ->findByEspece('B');
  84.         $agneau $this->getDoctrine()
  85.             ->getRepository(Marque::class)
  86.             ->findByEspece('O');
  87.         $veau $this->getDoctrine()
  88.             ->getRepository(Marque::class)
  89.             ->findByEspece('V');
  90.         $root = (!empty($_SERVER['HTTPS']) ? 'https' 'http') . '://' $_SERVER['HTTP_HOST'] . '/';
  91.         if (!isset($_GET['dem1']) || !isset($_GET['dem2']) || !isset($_GET['dem3']) || !isset($_GET['dem4']) || !isset($_GET['cat']) || !isset($_GET['loc'])) {
  92.             return $this->redirect($root 'carte?cat=3&dem1=B&dem2=V&dem3=O&dem4=undefined&loc=null');
  93.         } else {
  94.             $dems = [$_GET['dem1'], $_GET['dem2'], $_GET['dem3'], $_GET['dem4']];
  95.             return $this->render('front/pages/map.html.twig', [
  96.                 'boeuf' => $boeuf,
  97.                 'agneau' => $agneau,
  98.                 'veau' => $veau,
  99.                 'typeselect' => $dems,
  100.                 'adresseselect' => $_GET['loc'],
  101.                 'catselect' => $_GET['cat']
  102.             ]);
  103.         }
  104.     }
  105.     /**
  106.      * @Route(path="/demarche/{type}", name="app_demarche")
  107.      *
  108.      * @param Request $request Request
  109.      *
  110.      * @return Response
  111.      */
  112.     public function boeufAction(Request $request$type)
  113.     {
  114.         $images $this->getDoctrine()->getRepository(Slider::class)->findAllWithoutDelete($type);
  115.         $demarches $this->getDoctrine()->getRepository(DemarcheBo::class)->findAllWithoutDelete($type);
  116.         $marques $this->getDoctrine()->getRepository(Marque::class)->findAll();
  117.         $title "Les boeufs Label Rouge - Viandes";
  118.         return $this->render('front/pages/demarche.html.twig', [
  119.             'images' => $images,
  120.             'demarches' => $demarches,
  121.             'type' => $type,
  122.             'marques' => $marques
  123.         ]);
  124.     }
  125.     /**
  126.      * @Route(path="/demarche/{type}/{demarche}", name="app_demarche_single")
  127.      *
  128.      * @param Request $request Request
  129.      *
  130.      * @return Response
  131.      */
  132.     public function demarcheSingleAction(Request $request$type$demarche)
  133.     {
  134.         $dem $this->getDoctrine()->getRepository(DemarcheBo::class)
  135.             ->findOneBy(array('title' => $demarche));
  136.         $imgsdem $this->getDoctrine()->getRepository(Img::class)
  137.             ->getLogo('LOGO_' $dem->getDemacode());
  138. //        dd($imgsdem);
  139.         return $this->render('front/pages/demarche_single.html.twig', [
  140.             'type' => $type,
  141.             'demarche' => $dem,
  142.             'logo' => $imgsdem
  143.         ]);
  144.     }
  145.     /**
  146.      * @Route(path="/pageEnConstuction", name="app_pageEnConstruction")
  147.      *
  148.      * @param Request $request Request
  149.      *
  150.      * @return Response
  151.      */
  152.     public function constructionAction(Request $request)
  153.     {
  154.         return $this->render('front/pages/construction.html.twig', [
  155.         ]);
  156.     }
  157.     /**
  158.      * @Route(path="/notre-mission", name="app_mission")
  159.      *
  160.      * @param Request $request Request
  161.      *
  162.      * @return Response
  163.      */
  164.     public function missionAction(Request $request)
  165.     {
  166.         return $this->render('front/pages/mission.html.twig', []);
  167.     }
  168.     /**
  169.      * @Route(path="/nos-actualites", name="app_news")
  170.      *
  171.      * @param Request $request Request
  172.      *
  173.      * @return Response
  174.      */
  175.     public function newsAction(Request $request)
  176.     {
  177.         $news $this->getDoctrine()->getRepository(News::class)->findAllWithoutDelete();
  178.         return $this->render('front/pages/news.html.twig', [
  179.             'news' => $news
  180.         ]);
  181.     }
  182.     /**
  183.      * @Route(path="/actualite/{slug}", name="app_news_single")
  184.      *
  185.      * @param Request $request Request
  186.      *
  187.      * @return Response
  188.      */
  189.     public function newsSingleAction(Request $request$slug)
  190.     {
  191.         $news $this->getDoctrine()->getRepository(News::class)->findOneBySlug($slug);
  192.         $other $this->getDoctrine()->getRepository(News::class)->findMoreNews($news->getId());
  193.         if ($news->getDeletedAt() != null)
  194.             return $this->redirectToRoute('app_homepage');
  195.         return $this->render('front/pages/news_single.html.twig', [
  196.             'news' => $news,
  197.             'other' => $other
  198.         ]);
  199.     }
  200.     /**
  201.      * @Route(path="/nos-recettes", name="app_recipes")
  202.      *
  203.      * @param Request $request Request
  204.      *
  205.      * @return Response
  206.      */
  207.     public function recipesAction(Request $request)
  208.     {
  209.         $recipes $this->getDoctrine()->getRepository(Recipe::class)->findAll();
  210.         return $this->render('front/pages/recipes.html.twig', [
  211.             'recipes' => $recipes
  212.         ]);
  213.     }
  214.     /**
  215.      * @Route(path="/nos-recettes/{slug}", name="app_recipes_single")
  216.      *
  217.      * @param Request $request Request
  218.      *
  219.      * @return Response
  220.      */
  221.     public function recipesSingleAction(Request $request$slug)
  222.     {
  223.         $recipes $this->getDoctrine()->getRepository(Recipe::class)->findOneBySlug($slug);
  224.         $other $this->getDoctrine()->getRepository(Recipe::class)->findMoreRecipes($recipes->getId());
  225.         if ($recipes == null)
  226.             return $this->redirectToRoute('app_homepage');
  227.         return $this->render('front/pages/recipes_single.html.twig', [
  228.             'recipes' => $recipes,
  229.             'other' => $other
  230.         ]);
  231.     }
  232.     /**
  233.      * @Route(path="/recettes/query", name="app_recipes_filter")
  234.      *
  235.      * @param Request $request Request
  236.      *
  237.      * @return Response
  238.      */
  239.     public function recipesfilterAction(Request $requestSerializerInterface $serializer)
  240.     {
  241.         if ($_GET['cat'] && $_GET['cat'] == 'all')
  242.             $recipes $this->getDoctrine()->getRepository(Recipe::class)->findAll();
  243.         else
  244.             $recipes $this->getDoctrine()->getRepository(Recipe::class)->findByCategorie($_GET['cat']);
  245.         return new JsonResponse(['content' =>$this->renderView('front/fiche/recipe.html.twig', [
  246.             'recipes' => $recipes
  247.         ])]);
  248.     }
  249.     /**
  250.      * @Route(path="/nous-contacter", name="app_contact")
  251.      * @Route(path="/nous-contacter/{farmer}", name="app_contact")
  252.      *
  253.      * @param Request $request Request
  254.      *
  255.      * @return Response
  256.      */
  257.     public function contactAction(Request $request, \Swift_Mailer $mailer$farmer null)
  258.     {
  259.         $contact = new Contact();
  260.         $form $this->createFormBuilder($contact)
  261.             ->add('name'TextType::class, array(
  262.                 'attr' => array(
  263.                     'placeholder' => 'Nom *',
  264.                 )
  265.             ))
  266.             ->add('fstname'TextType::class, array(
  267.                 'attr' => array(
  268.                     'placeholder' => 'Prénom *',
  269.                 )
  270.             ))
  271.             ->add('city'TextType::class, array(
  272.                 'attr' => array(
  273.                     'placeholder' => 'Ville *',
  274.                 )
  275.             ))
  276.             ->add('phone'TextType::class, array(
  277.                 'attr' => array(
  278.                     'placeholder' => 'Téléphone *',
  279.                 )
  280.             ))
  281.             ->add('email'EmailType::class, array(
  282.                 'attr' => array(
  283.                     'placeholder' => 'Email *',
  284.                 )
  285.             ))
  286.             ->add('subject'TextType::class, array(
  287.                 'attr' => array(
  288.                     'placeholder' => 'Objet *',
  289.                 )
  290.             ))
  291.             ->add('content'TextareaType::class, array(
  292.                 'attr' => array(
  293.                     'class' => 'tinymce',
  294.                     'placeholder' => "Votre message *"
  295.                 )))
  296.             ->add('checkrgpd'CheckboxType::class, array(
  297.                 'label' => 'En soumettant ce formulaire, j\'accepte que les informations saisies
  298.             dans ce formulaire soient utilisées, exploitées, traitées pour permettre
  299.              de me recontacter.',
  300.                 'required' => true
  301.             ))
  302.             ->add('submit'SubmitType::class)
  303.             ->getForm();
  304.         $form->handleRequest($request);
  305.         if ($form->isSubmitted() && $form->isValid() && $this->captchaverify($request->get('g-recaptcha-response'))) {
  306.             if ((!isset($phone)) || preg_match("^(0|\+33)[1-9]([-. ]?[0-9]{2}){4}$^"$form['phone']->getData())) {
  307.                 $mails $this->getDoctrine()->getRepository(Mail::class)->findAllWithoutDelete();
  308.                 $contact $form->getData();
  309.                 $em $this->getDoctrine()->getManager();
  310.                 $em->persist($contact);
  311.                 $em->flush();
  312.                 foreach ($mails as $mail) {
  313.                     $message = (new \Swift_Message($form['subject']->getData()))
  314.                         ->setFrom($form['email']->getData())
  315.                         ->setTo($mail->getEmail())
  316.                         ->setBody(
  317.                             $this->renderView(
  318.                                 'email/contact.html.twig',
  319.                                 array(
  320.                                     'fstname' => $form['fstname']->getData(),
  321.                                     'name' => $form['name']->getData(),
  322.                                     'email' => $form['email']->getData(),
  323.                                     'phone' => $form['phone']->getData(),
  324.                                     'subject' => $form['subject']->getData(),
  325.                                     'content' => $form['content']->getData(),
  326.                                     'ferme' => urldecode($farmer)
  327.                                 )
  328.                             ),
  329.                             'text/html'
  330.                         );
  331.                     $mailer->send($message);
  332.                 }
  333.                 $this->addFlash(
  334.                     'notice',
  335.                     "Email envoyé, nous vous répondrons dans les plus bref délais."
  336.                 );
  337.             } else {
  338.                 $this->addFlash(
  339.                     'error',
  340.                     "Les champs ne sont pas correct."
  341.                 );
  342.             }
  343.             return $this->redirectToRoute('app_homepage');
  344.         }
  345.         if ($form->isSubmitted() && !$this->captchaverify($request->get('g-recaptcha-response'))) {
  346.             $this->addFlash(
  347.                 'error',
  348.                 "Veuillez cocher le captcha."
  349.             );
  350.         }
  351.         return $this->render('front/pages/contact.html.twig', [
  352.             'form' => $form->createView()
  353.         ]);
  354.     }
  355.     /**
  356.      * @Route(path="/mentions-legales", name="app_mention")
  357.      *
  358.      * @param Request $request Request
  359.      *
  360.      * @return Response
  361.      */
  362.     public function mentionAction(Request $request)
  363.     {
  364.         return $this->render('front/pages/mention.html.twig', []);
  365.     }
  366.     /**
  367.      * @Route(path="/eleveurs/{id}/{slug}", name="app_card_elv")
  368.      *
  369.      * @param Request $request Request
  370.      *
  371.      * @return Response
  372.      */
  373.     public function singleElvAction($id)
  374.     {
  375.         $elv =
  376.             $this->getDoctrine()
  377.                 ->getRepository(Farmer::class)
  378.                 ->findOneById($id);
  379.         $demarches $this->getDoctrine()
  380.             ->getRepository(ElvHasDem::class)
  381.             ->getDem($elv->getId());
  382.         $imgs $this->getDoctrine()
  383.             ->getRepository(Img::class)
  384.             ->findByElv($elv);
  385.         if (count($imgs) == 0) {
  386.             $imgs = array();
  387.             foreach ($demarches as $dema) {
  388.                 $imgs array_merge($imgs$this->getDoctrine()->getRepository(Img::class)->getImagedefault('ELEVAGE_' $dema->getDemacode()));
  389.             }
  390.         }
  391.         $imgsdem = array();
  392.         foreach ($demarches as $dema) {
  393.             $imgsdem array_merge(
  394.                 $imgsdem,
  395.                 [$dema->getDemacode() => $this->getDoctrine()->getRepository(Img::class)
  396.                     ->getLogo('LOGO_' $dema->getDemacode())]
  397.             );
  398.         }
  399.         return $this->render('front/pages/cards/eleveurs.html.twig', [
  400.             'elv' => $elv,
  401.             'imgs' => $imgs,
  402.             'demarches' => $demarches,
  403.             'imgsdem' => $imgsdem
  404.         ]);
  405.     }
  406.     /**
  407.      * @Route(path="/boucheries/{id}/{slug}", name="app_card_pdv")
  408.      *
  409.      * @param Request $request Request
  410.      *
  411.      * @return Response
  412.      */
  413.     public function singlePdvAction($id)
  414.     {
  415.         $pdv =
  416.             $this->getDoctrine()
  417.                 ->getRepository(Pdv::class)
  418.                 ->findOneById($id);
  419.         $demarches $this->getDoctrine()
  420.             ->getRepository(Demarche::class)
  421.             ->getDemPdv($pdv->getId());
  422.         $imgs $this->getDoctrine()
  423.             ->getRepository(Img::class)
  424.             ->findByPdv($pdv);
  425.         $mrqs $this->getDoctrine()->getRepository(PdvHasDem::class)->getMrqPDV($pdv->getId());
  426.         if (count($imgs) == 0) {
  427.             $imgs = array();
  428.             foreach ($demarches as $dema) {
  429.                 $imgs array_merge(
  430.                     $imgs,
  431.                     $this->getDoctrine()->getRepository(Img::class)
  432.                         ->getImagedefault('PDV_' $dema->getDemacode())
  433.                 );
  434.             }
  435.         }
  436.         $imgsdem = array();
  437.         foreach ($demarches as $dema) {
  438.             $imgsdem array_merge(
  439.                 $imgsdem,
  440.                 [$dema->getDemacode() => $this->getDoctrine()->getRepository(Img::class)
  441.                     ->getLogo('LOGO_' $dema->getDemacode())]
  442.             );
  443.         }
  444.         return $this->render('front/pages/cards/pdv.html.twig', [
  445.             'pdv' => $pdv,
  446.             'imgs' => $imgs,
  447.             'demarches' => $demarches,
  448.             'imgsdem' => $imgsdem,
  449.             "mrqs" => $mrqs
  450.         ]);
  451.     }
  452.     function captchaverify($recaptcha)
  453.     {
  454.         $url "https://www.google.com/recaptcha/api/siteverify";
  455.         $ch curl_init();
  456.         curl_setopt($chCURLOPT_URL$url);
  457.         curl_setopt($chCURLOPT_HEADER0);
  458.         curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
  459.         curl_setopt($chCURLOPT_POSTtrue);
  460.         curl_setopt($chCURLOPT_POSTFIELDS, array(
  461.             "secret" => "6LdbcXwUAAAAALVuSe4Ij3e9H2Q1DZPSzlk_dZy6""response" => $recaptcha));
  462.         $response curl_exec($ch);
  463.         curl_close($ch);
  464.         $data json_decode($response);
  465.         return $data->success;
  466.     }
  467. }