src/Controller/PageController.php line 93

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\HttpFoundation\Cookie;
  12. use Twig\Environment;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use Symfony\Component\Mime\Address;
  16. use Symfony\Component\Mime\Email;
  17. // use Symfony\Component\Mime\Converter\HtmlToTextConverter; // à partir de symfony 5.2
  18. use League\HTMLToMarkdown\HtmlConverter;
  19. use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3Validator;
  20. use App\Services\SendinblueService;
  21. use App\Entity\Page;
  22. use App\Entity\Catalog;
  23. use App\Entity\CatalogOrder;
  24. use App\Entity\pays;
  25. use App\Entity\Language;
  26. use App\Entity\Residence;
  27. use App\Repository\paysRepository;
  28. use App\Form\ContactType;
  29. use App\Form\CatalogOrderType;
  30. use Exception;
  31. /**
  32.  * @Route("/{_locale}", requirements={"_locale": "en|fr"})
  33.  */
  34. class PageController extends AbstractController
  35. {
  36.     private $em;
  37.     private $session;
  38.     private $flashbag;
  39.     private $user;
  40.     private $sourceForm;
  41.     private $translator;
  42.     private $sendinblueService;
  43.     private $mailer;
  44.     private $twig;
  45.     private $recaptchaValidator;
  46.     
  47.     function __construct(EntityManagerInterface $em,  SessionInterface $sessionTranslatorInterface $translatorSendinblueService $sendinblueServiceMailerInterface $mailerEnvironment $twigRecaptcha3Validator $recaptchaValidator) {
  48.         $this->em $em;
  49.         $this->session $session;
  50.         $this->translator $translator;
  51.         $this->sendinblueService $sendinblueService;
  52.         $this->mailer $mailer;
  53.         $this->flashbag $this->session->getFlashBag();
  54.         $this->sourceForm "Contact";
  55.         $this->twig $twig;
  56.         $this->recaptchaValidator $recaptchaValidator;
  57.     }
  58.     /**
  59.      *
  60.      * @Route("/contact", name="contact")
  61.      */
  62.     public function contact(Request $request)
  63.     { 
  64.         $this->sourceForm "contact";
  65.         $france $this->em->getRepository(pays::class)->find(79);
  66.         $form $this->createForm(ContactType::class, null, ['default_country' => [$france]]);
  67.         $form->handleRequest($request);
  68.         if ($form->isSubmitted() && $form->isValid()) { 
  69.             $contact $request->request->get('contact');
  70.             $this->processForm($request$contact"contact", ["commercial@ateya.fr""production@ateya.fr"], 812);
  71.         }
  72.         return $this->render('/front/Contact/index.html.twig',[
  73.             'formcontact' => $form->createView(),
  74.         ]);
  75.     }
  76.     
  77.     
  78.     /**
  79.      *
  80.      * @Route("/contact/commande-catalogue", name="contact_catalogue")
  81.      */
  82.     public function contactCatalogueAction(Request $request)
  83.     { 
  84.         $catalog_id $request->query->get('catalog_id');
  85.         $this->sourceForm "commande-catalogue";
  86.         $locale $request->getLocale();
  87.         $lang  $this->em->getRepository(Language::class)->findOneByCode($locale);
  88.         $catalogs $this->em->getRepository(Catalog::class)->findByLang($lang);
  89.         $catalogOrder = new CatalogOrder();
  90.         $france $this->em->getRepository(pays::class)->find(79);
  91.         $form $this->createForm(CatalogOrderType::class, $catalogOrder, ['catalogs' => $catalogs'default_country' => [$france]]);
  92.         
  93.         $form->handleRequest($request);
  94.         
  95.         if ($form->isSubmitted() && $form->isValid()) { 
  96.             $contact $request->request->get('catalog_order');
  97.             $response $this->processForm($request$contact"catalog", ["production@ateya.fr"], 812);
  98.             if($response) {
  99.                 $this->em->persist($catalogOrder);
  100.                 $this->em->flush();
  101.             }
  102.            
  103.         } 
  104.            
  105.         return $this->render('/front/Contact/contact_catalogue.html.twig',[
  106.             'form' => $form->createView(),
  107.             'catalog_id' => $catalog_id
  108.         ]);
  109.     }
  110.     /**
  111.      *
  112.      * @Route("/contact/ce-collectivites", name="contact_ce_collectivites")
  113.      */
  114.     public function contactCeCollectives(Request $request)
  115.     { 
  116.         $this->sourceForm "ce-collectivites";
  117.         $france $this->em->getRepository(pays::class)->find(79);
  118.         $form $this->createForm(ContactType::class, null, ['default_country' => [$france], 'slug' => 'ce-collectivites']);
  119.         $form->handleRequest($request);
  120.         
  121.         if ($form->isSubmitted() && $form->isValid()) { 
  122.             $contact $request->request->get('contact');
  123.             $this->processForm($request$contact"ce-collectivites", ["commercial@ateya.fr""production@ateya.fr"], 812);
  124.         }
  125.         return $this->render('/front/Contact/contact_ce_collectives.html.twig',[
  126.             'formcontact' => $form->createView(),
  127.             'slug' => 'ce-collectivites'
  128.         ]);
  129.     }
  130.     /**
  131.      *
  132.      * @Route("/contact/groupe-seminaire", name="contact_groupe_seminaire")
  133.      */
  134.     public function contactGroupeSeminaire(Request $request)
  135.     { 
  136.         $this->sourceForm "groupe-seminaire";
  137.         $france $this->em->getRepository(pays::class)->find(79);
  138.         $form $this->createForm(ContactType::class, null, ['default_country' => [$france], 'slug' => 'groupe-seminaire']);
  139.         $form->handleRequest($request);
  140.         if ($form->isSubmitted() && $form->isValid()) { 
  141.             $contact $request->request->get('contact');
  142.             $this->processForm($request$contact"groupe-seminaire", ["commercial@ateya.fr""production@ateya.fr"], 812);
  143.         }
  144.         return $this->render('/front/Contact/contact_groupe_seminaire.html.twig',[
  145.             'formcontact' => $form->createView(),
  146.             'slug' => 'groupe-seminaire'
  147.         ]);
  148.     }
  149.     /**
  150.      *
  151.      * @Route("/contact/sejour-longue-duree", name="contact_sejour_longue_duree")
  152.      */
  153.     public function contactSejourLongueDuree(Request $request)
  154.     { 
  155.         $this->sourceForm "sejour-longue-duree";
  156.         $france $this->em->getRepository(pays::class)->find(79);
  157.         $form $this->createForm(ContactType::class, null, ['default_country' => [$france], 'slug' => 'sejour-longue-duree']);
  158.         $form->handleRequest($request);
  159.         
  160.         if ($form->isSubmitted() && $form->isValid()) { 
  161.             $contact $request->request->get('contact');
  162.             $this->processForm($request$contact"sejour-longue-duree", ["contact@ateya.fr""production@ateya.fr"], 812);
  163.         }
  164.         return $this->render('/front/Contact/contact_sejour_longue_duree.html.twig',[
  165.             'formcontact' => $form->createView(),
  166.             'slug' => 'sejour-longue-duree'
  167.         ]);
  168.     }
  169.     /**
  170.      *
  171.      * @Route("/p/{code_traduction}", name="page_cms_redirect")
  172.      */
  173.     public function pageRedirect($code_traductionRequest $request)
  174.     { 
  175.         $locale $request->getLocale();
  176.         $lang  $this->em->getRepository(Language::class)->findOneByCode($locale);
  177.         $params = [
  178.             "code_traduction" => $code_traduction,
  179.             "lang_id" => $lang->getId()
  180.         ];
  181.         $page $this->em->getRepository(Page::class)->findOneByLang($params);
  182.         if(!$page instanceof Page) {
  183.             throw $this->createNotFoundException("Page not found");
  184.         } 
  185.         return $this->redirectToRoute('page_cms', ['code_traduction' => $page->getCodeTraduction(), 'slug' => $page->getSlug()]);
  186.     }
  187.     /**
  188.      *
  189.      * @Route("/l/{code_traduction}/{ancre}-{css}", name="page_cms_link")
  190.      */
  191.     public function path($code_traduction$ancre$cssRequest $request)
  192.     { 
  193.         $locale $request->getLocale();
  194.         $lang  $this->em->getRepository(Language::class)->findOneByCode($locale);
  195.        
  196.         $params = [
  197.             "code_traduction" => $code_traduction,
  198.             "lang_id" => $lang->getId()
  199.         ];
  200.         $page $this->em->getRepository(Page::class)->findOneByLang($params);
  201.         if(!$page instanceof Page) {
  202.             throw $this->createNotFoundException("Page not found");
  203.         }
  204.         $path $this->generateUrl('page_cms', [
  205.             "code_traduction" => $code_traduction,
  206.             "slug" => $page->getSlug()
  207.         ]);
  208.         return $this->render('/front/_blocks/link.html.twig',['path' => $path'ancre' => $ancre'css' => $css]);
  209.     }
  210.     /**
  211.      *
  212.      * @Route("/p/{code_traduction}/{slug}", name="page_cms")
  213.      */
  214.     public function page($code_traduction$slugRequest $request)
  215.     { 
  216.         $locale $request->getLocale();
  217.         $lang  $this->em->getRepository(Language::class)->findOneByCode($locale);
  218.         $params = [
  219.             "slug" => $slug,
  220.             "code_traduction" => $code_traduction,
  221.             "lang_id" => $lang->getId()
  222.         ];
  223.         $page $this->em->getRepository(Page::class)->findOneByLang($params);
  224.         if(!$page instanceof Page) {
  225.             throw $this->createNotFoundException("Page not found");
  226.         } 
  227.         $type $slug == 'rgpd' 'rgpd' $page->getType();
  228.        
  229.         $routeParams = [
  230.             'code_traduction' => $code_traduction,
  231.             'slug' => $slug
  232.         ];
  233.         switch($type) {
  234.             default:
  235.             case "cms";
  236.                 return $this->render('/front/Page/cms.html.twig',[
  237.                     'page' => $page,
  238.                     'route_name' => 'page_cms',
  239.                     'params' => $routeParams
  240.                 ]);  
  241.             break;
  242.             case "rgpd";
  243.                 return $this->render('/front/Page/cms.html.twig',[
  244.                     'page' => $page,
  245.                     'script' => '<script id="CookieDeclaration" src="https://consent.cookiebot.com/c5d925a9-f659-4834-ae6f-4ab5d4e89f9b/cd.js" type="text/javascript" async></script>',
  246.                     'route_name' => 'page_cms',
  247.                     'params' => $routeParams
  248.                 ]);  
  249.                 break;
  250.                 
  251.              case "redirection":
  252.                 return $this->redirectToRoute($page->getRedirection());
  253.                 break;
  254.             case "catalog":
  255.                 $catalogs $this->em->getRepository(Catalog::class)->findByLang($lang);
  256.                 return $this->render('/front/Page/catalog.html.twig',[
  257.                     'page' => $page,
  258.                     'catalogs' => $catalogs,
  259.                     'route_name' => 'page_cms',
  260.                     'params' => $routeParams
  261.                 ]);  
  262.             break;
  263.             
  264.         }
  265.         
  266.         return $this->render('/front/Page/page.html.twig',[
  267.             'page' => $page,
  268.             'route_name' => 'page_cms',
  269.             'params' => $routeParams
  270.         ]);
  271.     }
  272.     /**
  273.      *
  274.      * @Route("/menu/{code}", name="menu")
  275.      */
  276.     public function menu(Request $request$code) {
  277.         $locale $request->getLocale();
  278.         $lang  $this->em->getRepository(Language::class)->findOneByCode($locale);
  279.         $length = (int) $this->translator->trans($code '.length', [], 'menu');
  280.         $template =  $this->translator->trans($code '.template', [], 'menu');
  281.         $menu = [];
  282.         for ($k 1$k <= $length$k++) {
  283.            
  284.             $type $this->translator->trans($code '.items.' $k ".type", [], 'menu');
  285.             $ancre $this->translator->trans($code '.items.' $k ".ancre", [], 'menu');
  286.             $class $this->translator->trans($classKey $code '.items.' $k ".class", [], 'menu');
  287.            
  288.             switch($type) {
  289.                 case "text":
  290.                     $text $this->translator->trans($code '.items.' $k ".text", [], 'menu');
  291.                     $menu[] = ['type' => 'text''text' => $text];
  292.                     break;
  293.                 case "route":
  294.                     $path $this->translator->trans($code '.items.' $k ".path", [], 'menu');
  295.                     $params = [];
  296.                     $paramsRaw $this->translator->trans($code '.items.' $k '.params', [], 'menu');
  297.                    
  298.                     // On vérifie si la traduction retournée est différente de la clé recherchée ET qu'elle n'est pas vide.
  299.                     if ( $paramsRaw !== $code '.items.' $k '.params' && !empty($paramsRaw) ) {
  300.                         $params json_decode($paramsRawtrue);
  301.                     }
  302.                     $url $this->generateUrl($path$params);
  303.                     $menu[] = ['type' => 'link''ancre' => $ancre'url' => $url'class' => $class'target' => ''];
  304.                     break;
  305.                 
  306.                 case "url":
  307.                     $url $this->translator->trans($code '.items.' $k ".url", [], 'menu');
  308.                     $menu[] = ['type' => 'link''ancre' => $ancre'url' => $url'class' => $class'target' => '_blank'];
  309.                     break;
  310.                 case "asset":
  311.                     $url $this->translator->trans($code '.items.' $k ".url", [], 'menu');
  312.                     $menu[] = ['type' => 'link''ancre' => $ancre'url' => $url'class' => $class'target' => '_blank'];
  313.                     break;
  314.                     
  315.                 case "cms":
  316.                     $code_traduction $this->translator->trans($code '.items.' $k ".code", [], 'menu');
  317.                     
  318.                     $params = [
  319.                         "code_traduction" => $code_traduction,
  320.                         "lang_id" => $lang->getId()
  321.                     ];
  322.             
  323.                     $page $this->em->getRepository(Page::class)->findOneByLang($params);
  324.                     if($page instanceof Page && $page->getEtat()) {
  325.                         $url $this->generateUrl(
  326.                             'page_cms',
  327.                             [
  328.                                 'code_traduction' => $page->getCodeTraduction(), 
  329.                                 'slug' => $page->getSlug()
  330.                             ]
  331.                         );
  332.                         $menu[] = ['type' => 'link''ancre' => $ancre'url' => $url'class' => $class'target' => ''];
  333.                     }
  334.                     break;
  335.             }
  336.         }
  337.         return $this->render('/front/_blocks/menu_' $template '.html.twig',['menu' => $menu]);
  338.     }
  339.     /**
  340.      *
  341.      * @Route("/Conditions-d-annulation", name="page_conditions_d_annulation")
  342.      */
  343.     public function ConditionsAnnulation(Request $request)
  344.     { 
  345.         return $this->render('/front/Page/conditions-d-annulation.html.twig',['slug' => 'Conditions-d-annulation']);
  346.     }
  347.     public function sendMailContact($contact$form,  $destinataires)
  348.     {
  349.         $object "";
  350.         if(array_key_exists("objet"$contact)) {
  351.             $object =  $contact['objet'];
  352.         }
  353.         $sujet "[Ateya-vacances][" $this->sourceForm "] " .  $object;
  354.        
  355.         $reply_email $contact["email"];
  356.         $reply_name $contact["prenom"] . ' ' $contact["nom"];
  357.         
  358.         $from_email 'no-reply@ateya-vacances.fr';
  359.         $from_name 'ateya-vacances';
  360.         if(array_key_exists("societe"$contact)) {
  361.             $from_name .= " [" $contact["societe"] . "]";
  362.         }
  363.         $nomPays "";
  364.         if(array_key_exists("pays"$contact)) {
  365.             $pays $this->em->getRepository(Pays::class)->find($contact["pays"]);
  366.             $nomPays $pays->getNom();
  367.         }
  368.         $catalogues "";
  369.         if(array_key_exists("catalogues"$contact)) {
  370.             $catalogues $this->em->getRepository(Catalog::class)->findBy(['id' =>$contact["catalogues"]]);
  371.         }
  372.         $body $this->twig->render('/front/Email/' $this->sourceForm '.html.twig', [
  373.             'contact' => $contact
  374.             'form' => $form,
  375.             'pays' => $nomPays,
  376.             'catalogues' => $catalogues
  377.         ]);
  378.         
  379.         $from = new Address($from_email$from_name);
  380.         $reply_to = new Address($reply_email$reply_name);
  381.         
  382.         // Convertir le contenu HTML en texte brut
  383.         $converter = new HtmlConverter();
  384.         $texte $converter->convert($body);
  385.        
  386.         $message = (new Email())
  387.             ->from($from)
  388.             ->replyTo($reply_to)
  389.             ->subject($sujet)
  390.             ->text($texte)
  391.             ->html($body)
  392.             ;
  393.         // Ajouter les destinataires à l'objet Email
  394.         foreach ($destinataires as $destinataire) {
  395.             $message->addTo(new Address($destinataire$destinataire));
  396.         }
  397.         try {
  398.             $this->mailer->send($message);
  399.         } catch (TransportExceptionInterface $e) {
  400.             throw new NotificationErrorSendingException(sprintf('error sending for %s'$notification->getRecipient()->getEmail()));
  401.         }
  402.     }
  403.     public function processForm(Request $request$contact$destination$destinataire$template_id) {
  404.         $session $request->getSession();
  405.                
  406.         $recaptcha $this->recaptchaValidator->getLastResponse();
  407.         if($recaptcha->isSuccess()) {
  408.             $code "success";
  409.         
  410.             if($destination == "catalog" ) {
  411.                 $contact["objet"] = "Demande de contact";
  412.             }
  413.             
  414.             $retEmail $this->sendMailContact($contact$destination$destinataire);
  415.             
  416.             $this->sendinblueService->setRecipient($contact["email"], $contact["prenom"] . ' ' $contact["nom"]);
  417.             $this->sendinblueService->setTemplateId($template_id);
  418.             $params = [
  419.                 'FIRSTNAME' => $contact["prenom"],
  420.                 'LASTNAME' => $contact["nom"],
  421.             ];
  422.             $this->sendinblueService->setParams($params);
  423.             try {
  424.                 $ret $this->sendinblueService->sendEmail();
  425.             } catch (Exception $e) {
  426.             }
  427.         } else {
  428.             $code "error";
  429.         }
  430.         $title $this->translator->trans('contact.send.' $code ".title", array(), 'emails');
  431.         $message $this->translator->trans('contact.send.' $code ".message", array(), 'emails');
  432.         $session->getFlashBag()->add($code, ['type' => $code'title' => $title'message' => $message]);
  433.         
  434.         return true;
  435.     }
  436. }