src/Controller/PageController.php line 185

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.     public function processForm(Request $request$contact$destination$destinataire$template_id) {
  59.         $session $request->getSession();
  60.                
  61.         $recaptcha $this->recaptchaValidator->getLastResponse();
  62.         if($recaptcha->isSuccess()) {
  63.             $code "sucess";
  64.         
  65.             if($destination == "catalog" ) {
  66.                 $contact["objet"] = "Demande de contact";
  67.             }
  68.             
  69.             $retEmail $this->sendMailContact($contact$destination$destinataire);
  70.             
  71.             $this->sendinblueService->setRecipient($contact["email"], $contact["prenom"] . ' ' $contact["nom"]);
  72.             $this->sendinblueService->setTemplateId($template_id);
  73.             $params = [
  74.                 'FIRSTNAME' => $contact["prenom"],
  75.                 'LASTNAME' => $contact["nom"],
  76.             ];
  77.             $this->sendinblueService->setParams($params);
  78.             try {
  79.                 $ret $this->sendinblueService->sendEmail();
  80.             } catch (Exception $e) {
  81.             }
  82.         } else {
  83.             $code "error";
  84.         }
  85.         $title $this->translator->trans('contact.send.' $code ".title", array(), 'emails');
  86.         $message $this->translator->trans('contact.send.' $code ".message", array(), 'emails');
  87.         $session->getFlashBag()->add($code, ['type' => $code'title' => $title'message' => $message]);
  88.         
  89.         return true;
  90.     }
  91.     
  92.     /**
  93.      *
  94.      * @Route("/contact/commande-catalogue", name="contact_catalogue")
  95.      */
  96.     public function contactCatalogueAction(Request $request)
  97.     { 
  98.         $catalog_id $request->query->get('catalog_id');
  99.         $this->sourceForm "commande-catalogue";
  100.         $locale $request->getLocale();
  101.         $lang  $this->em->getRepository(Language::class)->findOneByCode($locale);
  102.         $catalogs $this->em->getRepository(Catalog::class)->findByLang($lang);
  103.         $catalogOrder = new CatalogOrder();
  104.         $france $this->em->getRepository(pays::class)->find(79);
  105.         $form $this->createForm(CatalogOrderType::class, $catalogOrder, ['catalogs' => $catalogs'default_country' => [$france]]);
  106.         
  107.         $form->handleRequest($request);
  108.         
  109.         if ($form->isSubmitted() && $form->isValid()) { 
  110.             $contact $request->request->get('catalog_order');
  111.             $response $this->processForm($request$contact"catalog", ["production@ateya.fr"], 812);
  112.             if($response) {
  113.                 $this->em->persist($catalogOrder);
  114.                 $this->em->flush();
  115.             }
  116.            
  117.         } 
  118.            
  119.         return $this->render('/front/Contact/contact_catalogue.html.twig',[
  120.             'form' => $form->createView(),
  121.             'catalog_id' => $catalog_id
  122.         ]);
  123.     }
  124.     /**
  125.      *
  126.      * @Route("/contact", name="contact")
  127.      */
  128.     public function contact(Request $request)
  129.     { 
  130.         $this->sourceForm "contact";
  131.         $france $this->em->getRepository(pays::class)->find(79);
  132.         $form $this->createForm(ContactType::class, null, ['default_country' => [$france]]);
  133.         $form->handleRequest($request);
  134.         if ($form->isSubmitted() && $form->isValid()) { 
  135.             $contact $request->request->get('contact');
  136.             $this->processForm($request$contact"contact", ["commercial@ateya.fr""production@ateya.fr"], 812);
  137.         }
  138.         return $this->render('/front/Contact/index.html.twig',[
  139.             'formcontact' => $form->createView(),
  140.         ]);
  141.     }
  142.     /**
  143.      *
  144.      * @Route("/contact/ce-collectivites", name="contact_ce_collectivites")
  145.      */
  146.     public function contactCeCollectives(Request $request)
  147.     { 
  148.         $this->sourceForm "ce-collectivites";
  149.         $france $this->em->getRepository(pays::class)->find(79);
  150.         $form $this->createForm(ContactType::class, null, ['default_country' => [$france], 'slug' => 'ce-collectivites']);
  151.         $form->handleRequest($request);
  152.         
  153.         if ($form->isSubmitted() && $form->isValid()) { 
  154.             $contact $request->request->get('contact');
  155.             $this->processForm($request$contact"ce-collectivites", ["commercial@ateya.fr""production@ateya.fr"], 812);
  156.         }
  157.         return $this->render('/front/Contact/contact_ce_collectives.html.twig',[
  158.             'formcontact' => $form->createView(),
  159.             'slug' => 'ce-collectivites'
  160.         ]);
  161.     }
  162.     /**
  163.      *
  164.      * @Route("/contact/groupe-seminaire", name="contact_groupe_seminaire")
  165.      */
  166.     public function contactGroupeSeminaire(Request $request)
  167.     { 
  168.         $this->sourceForm "groupe-seminaire";
  169.         $france $this->em->getRepository(pays::class)->find(79);
  170.         $form $this->createForm(ContactType::class, null, ['default_country' => [$france], 'slug' => 'groupe-seminaire']);
  171.         $form->handleRequest($request);
  172.         if ($form->isSubmitted() && $form->isValid()) { 
  173.             $contact $request->request->get('contact');
  174.             $this->processForm($request$contact"groupe-seminaire", ["commercial@ateya.fr""production@ateya.fr"], 812);
  175.         }
  176.         return $this->render('/front/Contact/contact_groupe_seminaire.html.twig',[
  177.             'formcontact' => $form->createView(),
  178.             'slug' => 'groupe-seminaire'
  179.         ]);
  180.     }
  181.     /**
  182.      *
  183.      * @Route("/contact/sejour-longue-duree", name="contact_sejour_longue_duree")
  184.      */
  185.     public function contactSejourLongueDuree(Request $request)
  186.     { 
  187.         $this->sourceForm "sejour-longue-duree";
  188.         $france $this->em->getRepository(pays::class)->find(79);
  189.         $form $this->createForm(ContactType::class, null, ['default_country' => [$france], 'slug' => 'sejour-longue-duree']);
  190.         $form->handleRequest($request);
  191.         
  192.         if ($form->isSubmitted() && $form->isValid()) { 
  193.             $contact $request->request->get('contact');
  194.             $this->processForm($request$contact"sejour-longue-duree", ["contact@ateya.fr""production@ateya.fr"], 812);
  195.         }
  196.         return $this->render('/front/Contact/contact_sejour_longue_duree.html.twig',[
  197.             'formcontact' => $form->createView(),
  198.             'slug' => 'sejour-longue-duree'
  199.         ]);
  200.     }
  201.     /**
  202.      *
  203.      * @Route("/p/{code_traduction}", name="page_cms_redirect")
  204.      */
  205.     public function pageRedirect($code_traductionRequest $request)
  206.     { 
  207.         $locale $request->getLocale();
  208.         $lang  $this->em->getRepository(Language::class)->findOneByCode($locale);
  209.         $params = [
  210.             "code_traduction" => $code_traduction,
  211.             "lang_id" => $lang->getId()
  212.         ];
  213.         $page $this->em->getRepository(Page::class)->findOneByLang($params);
  214.         if(!$page instanceof Page) {
  215.             throw $this->createNotFoundException("Page not found");
  216.         } 
  217.         return $this->redirectToRoute('page_cms', ['code_traduction' => $page->getCodeTraduction(), 'slug' => $page->getSlug()]);
  218.     }
  219.     /**
  220.      *
  221.      * @Route("/l/{code_traduction}/{ancre}-{css}", name="page_cms_link")
  222.      */
  223.     public function path($code_traduction$ancre$cssRequest $request)
  224.     { 
  225.         $locale $request->getLocale();
  226.         $lang  $this->em->getRepository(Language::class)->findOneByCode($locale);
  227.        
  228.         $params = [
  229.             "code_traduction" => $code_traduction,
  230.             "lang_id" => $lang->getId()
  231.         ];
  232.         $page $this->em->getRepository(Page::class)->findOneByLang($params);
  233.         if(!$page instanceof Page) {
  234.             throw $this->createNotFoundException("Page not found");
  235.         }
  236.         $path $this->generateUrl('page_cms', [
  237.             "code_traduction" => $code_traduction,
  238.             "slug" => $page->getSlug()
  239.         ]);
  240.         return $this->render('/front/_blocks/link.html.twig',['path' => $path'ancre' => $ancre'css' => $css]);
  241.     }
  242.     /**
  243.      *
  244.      * @Route("/p/{code_traduction}/{slug}", name="page_cms")
  245.      */
  246.     public function page($code_traduction$slugRequest $request)
  247.     { 
  248.         $locale $request->getLocale();
  249.         $lang  $this->em->getRepository(Language::class)->findOneByCode($locale);
  250.         $params = [
  251.             "slug" => $slug,
  252.             "code_traduction" => $code_traduction,
  253.             "lang_id" => $lang->getId()
  254.         ];
  255.         $page $this->em->getRepository(Page::class)->findOneByLang($params);
  256.         if(!$page instanceof Page) {
  257.             throw $this->createNotFoundException("Page not found");
  258.         } 
  259.         $type $slug == 'rgpd' 'rgpd' $page->getType();
  260.        
  261.         switch($type) {
  262.             default:
  263.             case "cms";
  264.                 return $this->render('/front/Page/cms.html.twig',[
  265.                     'page' => $page,
  266.                 ]);  
  267.             break;
  268.             case "rgpd";
  269.                 return $this->render('/front/Page/cms.html.twig',[
  270.                     'page' => $page,
  271.                     'script' => '<script id="CookieDeclaration" src="https://consent.cookiebot.com/c5d925a9-f659-4834-ae6f-4ab5d4e89f9b/cd.js" type="text/javascript" async></script>'
  272.                 ]);  
  273.                 break;
  274.                 
  275.              case "redirection":
  276.                 return $this->redirectToRoute($page->getRedirection());
  277.                 break;
  278.             case "catalog":
  279.                 $catalogs $this->em->getRepository(Catalog::class)->findByLang($lang);
  280.                 return $this->render('/front/Page/catalog.html.twig',['page' => $page"catalogs" => $catalogs]);  
  281.             break;
  282.             
  283.         }
  284.         
  285.         return $this->render('/front/Page/page.html.twig',['page' => $page]);
  286.     }
  287.     /**
  288.      *
  289.      * @Route("/menu/{code}", name="menu")
  290.      */
  291.     public function menu(Request $request$code) {
  292.        
  293.         $locale $request->getLocale();
  294.         $lang  $this->em->getRepository(Language::class)->findOneByCode($locale);
  295.         $length = (int) $this->translator->trans($code '.length', [], 'menu');
  296.         $template =  $this->translator->trans($code '.template', [], 'menu');
  297.         $menu = [];
  298.         for ($k 1$k <= $length$k++) {
  299.            
  300.             $type $this->translator->trans($code '.items.' $k ".type", [], 'menu');
  301.             $ancre $this->translator->trans($code '.items.' $k ".ancre", [], 'menu');
  302.             $class $this->translator->trans($classKey $code '.items.' $k ".class", [], 'menu');
  303.            
  304.             switch($type) {
  305.                 case "text":
  306.                     $text $this->translator->trans($code '.items.' $k ".text", [], 'menu');
  307.                     $menu[] = ['type' => 'text''text' => $text];
  308.                     break;
  309.                 case "route":
  310.                     $path $this->translator->trans($code '.items.' $k ".path", [], 'menu');
  311.                     $url $this->generateUrl($path);
  312.                     $menu[] = ['type' => 'link''ancre' => $ancre'url' => $url'class' => $class'target' => ''];
  313.                     break;
  314.                 
  315.                 case "url":
  316.                     $url $this->translator->trans($code '.items.' $k ".url", [], 'menu');
  317.                     $menu[] = ['type' => 'link''ancre' => $ancre'url' => $url'class' => $class'target' => '_blank'];
  318.                     break;
  319.                 case "asset":
  320.                     $url $this->translator->trans($code '.items.' $k ".url", [], 'menu');
  321.                     $menu[] = ['type' => 'link''ancre' => $ancre'url' => $url'class' => $class'target' => '_blank'];
  322.                     break;
  323.                     
  324.                 case "cms":
  325.                     $code_traduction $this->translator->trans($code '.items.' $k ".code", [], 'menu');
  326.                     
  327.                     $params = [
  328.                         "code_traduction" => $code_traduction,
  329.                         "lang_id" => $lang->getId()
  330.                     ];
  331.             
  332.                     $page $this->em->getRepository(Page::class)->findOneByLang($params);
  333.                     if($page instanceof Page && $page->getEtat()) {
  334.                         $url $this->generateUrl(
  335.                             'page_cms',
  336.                             [
  337.                                 'code_traduction' => $page->getCodeTraduction(), 
  338.                                 'slug' => $page->getSlug()
  339.                             ]
  340.                         );
  341.                         $menu[] = ['type' => 'link''ancre' => $ancre'url' => $url'class' => $class'target' => ''];
  342.                     }
  343.                     break;
  344.             }
  345.         }
  346.         return $this->render('/front/_blocks/menu_' $template '.html.twig',['menu' => $menu]);
  347.     }
  348.     /**
  349.      *
  350.      * @Route("/Conditions-d-annulation", name="page_conditions_d_annulation")
  351.      */
  352.     public function ConditionsAnnulation(Request $request)
  353.     { 
  354.         return $this->render('/front/Page/conditions-d-annulation.html.twig',['slug' => 'Conditions-d-annulation']);
  355.     }
  356.     public function sendMailContact($contact$form,  $destinataires)
  357.     {
  358.         $object "";
  359.         if(array_key_exists("objet"$contact)) {
  360.             $object =  $contact['objet'];
  361.         }
  362.         $sujet "[Ateya-vacances][" $this->sourceForm "] " .  $object;
  363.        
  364.         $reply_email $contact["email"];
  365.         $reply_name $contact["prenom"] . ' ' $contact["nom"];
  366.         
  367.         $from_email 'no-reply@ateya-vacances.fr';
  368.         $from_name 'ateya-vacances';
  369.         if(array_key_exists("societe"$contact)) {
  370.             $from_name .= " [" $contact["societe"] . "]";
  371.         }
  372.         $nomPays "";
  373.         if(array_key_exists("pays"$contact)) {
  374.             $pays $this->em->getRepository(Pays::class)->find($contact["pays"]);
  375.             $nomPays $pays->getNom();
  376.         }
  377.         $catalogues "";
  378.         if(array_key_exists("catalogues"$contact)) {
  379.             $catalogues $this->em->getRepository(Catalog::class)->findBy(['id' =>$contact["catalogues"]]);
  380.         }
  381.         $body $this->twig->render('/front/Email/' $this->sourceForm '.html.twig', [
  382.             'contact' => $contact
  383.             'form' => $form,
  384.             'pays' => $nomPays,
  385.             'catalogues' => $catalogues
  386.         ]);
  387.         
  388.         $from = new Address($from_email$from_name);
  389.         $reply_to = new Address($reply_email$reply_name);
  390.         
  391.         // Convertir le contenu HTML en texte brut
  392.         $converter = new HtmlConverter();
  393.         $texte $converter->convert($body);
  394.        
  395.         $message = (new Email())
  396.             ->from($from)
  397.             ->replyTo($reply_to)
  398.             ->subject($sujet)
  399.             ->text($texte)
  400.             ->html($body)
  401.             ;
  402.         // Ajouter les destinataires à l'objet Email
  403.         foreach ($destinataires as $destinataire) {
  404.             $message->addTo(new Address($destinataire$destinataire));
  405.         }
  406.         try {
  407.             $this->mailer->send($message);
  408.         } catch (TransportExceptionInterface $e) {
  409.             throw new NotificationErrorSendingException(sprintf('error sending for %s'$notification->getRecipient()->getEmail()));
  410.         }
  411.     }
  412.     /**
  413.      *
  414.      * @Route("/fiches-descriptives", name="ac_platform_fichesdescriptives")
  415.      */
  416.     public function fiches(Request $request
  417.     {
  418.         $locale $request->getLocale();
  419.         $lang  $this->em->getRepository(Language::class)->findOneByCode($locale);
  420.         
  421.         $residences  $this->em->getRepository(Residence::class)->findAllWithMedias($lang);
  422.         $retour = array(
  423.             "residences" => $residences
  424.         );
  425.         return $this->render('/front/Page/fiche.html.twig'$retour);
  426.     }
  427. }