<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Cookie;
use Twig\Environment;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
// use Symfony\Component\Mime\Converter\HtmlToTextConverter; // à partir de symfony 5.2
use League\HTMLToMarkdown\HtmlConverter;
use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3Validator;
use App\Services\SendinblueService;
use App\Entity\Page;
use App\Entity\Catalog;
use App\Entity\CatalogOrder;
use App\Entity\pays;
use App\Entity\Language;
use App\Entity\Residence;
use App\Repository\paysRepository;
use App\Form\ContactType;
use App\Form\CatalogOrderType;
use Exception;
/**
* @Route("/{_locale}", requirements={"_locale": "en|fr"})
*/
class PageController extends AbstractController
{
private $em;
private $session;
private $flashbag;
private $user;
private $sourceForm;
private $translator;
private $sendinblueService;
private $mailer;
private $twig;
private $recaptchaValidator;
function __construct(EntityManagerInterface $em, SessionInterface $session, TranslatorInterface $translator, SendinblueService $sendinblueService, MailerInterface $mailer, Environment $twig, Recaptcha3Validator $recaptchaValidator) {
$this->em = $em;
$this->session = $session;
$this->translator = $translator;
$this->sendinblueService = $sendinblueService;
$this->mailer = $mailer;
$this->flashbag = $this->session->getFlashBag();
$this->sourceForm = "Contact";
$this->twig = $twig;
$this->recaptchaValidator = $recaptchaValidator;
}
public function processForm(Request $request, $contact, $destination, $destinataire, $template_id) {
$session = $request->getSession();
$recaptcha = $this->recaptchaValidator->getLastResponse();
if($recaptcha->isSuccess()) {
$code = "sucess";
if($destination == "catalog" ) {
$contact["objet"] = "Demande de contact";
}
$retEmail = $this->sendMailContact($contact, $destination, $destinataire);
$this->sendinblueService->setRecipient($contact["email"], $contact["prenom"] . ' ' . $contact["nom"]);
$this->sendinblueService->setTemplateId($template_id);
$params = [
'FIRSTNAME' => $contact["prenom"],
'LASTNAME' => $contact["nom"],
];
$this->sendinblueService->setParams($params);
try {
$ret = $this->sendinblueService->sendEmail();
} catch (Exception $e) {
}
} else {
$code = "error";
}
$title = $this->translator->trans('contact.send.' . $code . ".title", array(), 'emails');
$message = $this->translator->trans('contact.send.' . $code . ".message", array(), 'emails');
$session->getFlashBag()->add($code, ['type' => $code, 'title' => $title, 'message' => $message]);
return true;
}
/**
*
* @Route("/contact/commande-catalogue", name="contact_catalogue")
*/
public function contactCatalogueAction(Request $request)
{
$catalog_id = $request->query->get('catalog_id');
$this->sourceForm = "commande-catalogue";
$locale = $request->getLocale();
$lang = $this->em->getRepository(Language::class)->findOneByCode($locale);
$catalogs = $this->em->getRepository(Catalog::class)->findByLang($lang);
$catalogOrder = new CatalogOrder();
$france = $this->em->getRepository(pays::class)->find(79);
$form = $this->createForm(CatalogOrderType::class, $catalogOrder, ['catalogs' => $catalogs, 'default_country' => [$france]]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$contact = $request->request->get('catalog_order');
$response = $this->processForm($request, $contact, "catalog", ["production@ateya.fr"], 812);
if($response) {
$this->em->persist($catalogOrder);
$this->em->flush();
}
}
return $this->render('/front/Contact/contact_catalogue.html.twig',[
'form' => $form->createView(),
'catalog_id' => $catalog_id
]);
}
/**
*
* @Route("/contact", name="contact")
*/
public function contact(Request $request)
{
$this->sourceForm = "contact";
$france = $this->em->getRepository(pays::class)->find(79);
$form = $this->createForm(ContactType::class, null, ['default_country' => [$france]]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$contact = $request->request->get('contact');
$this->processForm($request, $contact, "contact", ["commercial@ateya.fr", "production@ateya.fr"], 812);
}
return $this->render('/front/Contact/index.html.twig',[
'formcontact' => $form->createView(),
]);
}
/**
*
* @Route("/contact/ce-collectivites", name="contact_ce_collectivites")
*/
public function contactCeCollectives(Request $request)
{
$this->sourceForm = "ce-collectivites";
$france = $this->em->getRepository(pays::class)->find(79);
$form = $this->createForm(ContactType::class, null, ['default_country' => [$france], 'slug' => 'ce-collectivites']);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$contact = $request->request->get('contact');
$this->processForm($request, $contact, "ce-collectivites", ["commercial@ateya.fr", "production@ateya.fr"], 812);
}
return $this->render('/front/Contact/contact_ce_collectives.html.twig',[
'formcontact' => $form->createView(),
'slug' => 'ce-collectivites'
]);
}
/**
*
* @Route("/contact/groupe-seminaire", name="contact_groupe_seminaire")
*/
public function contactGroupeSeminaire(Request $request)
{
$this->sourceForm = "groupe-seminaire";
$france = $this->em->getRepository(pays::class)->find(79);
$form = $this->createForm(ContactType::class, null, ['default_country' => [$france], 'slug' => 'groupe-seminaire']);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$contact = $request->request->get('contact');
$this->processForm($request, $contact, "groupe-seminaire", ["commercial@ateya.fr", "production@ateya.fr"], 812);
}
return $this->render('/front/Contact/contact_groupe_seminaire.html.twig',[
'formcontact' => $form->createView(),
'slug' => 'groupe-seminaire'
]);
}
/**
*
* @Route("/contact/sejour-longue-duree", name="contact_sejour_longue_duree")
*/
public function contactSejourLongueDuree(Request $request)
{
$this->sourceForm = "sejour-longue-duree";
$france = $this->em->getRepository(pays::class)->find(79);
$form = $this->createForm(ContactType::class, null, ['default_country' => [$france], 'slug' => 'sejour-longue-duree']);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$contact = $request->request->get('contact');
$this->processForm($request, $contact, "sejour-longue-duree", ["contact@ateya.fr", "production@ateya.fr"], 812);
}
return $this->render('/front/Contact/contact_sejour_longue_duree.html.twig',[
'formcontact' => $form->createView(),
'slug' => 'sejour-longue-duree'
]);
}
/**
*
* @Route("/p/{code_traduction}", name="page_cms_redirect")
*/
public function pageRedirect($code_traduction, Request $request)
{
$locale = $request->getLocale();
$lang = $this->em->getRepository(Language::class)->findOneByCode($locale);
$params = [
"code_traduction" => $code_traduction,
"lang_id" => $lang->getId()
];
$page = $this->em->getRepository(Page::class)->findOneByLang($params);
if(!$page instanceof Page) {
throw $this->createNotFoundException("Page not found");
}
return $this->redirectToRoute('page_cms', ['code_traduction' => $page->getCodeTraduction(), 'slug' => $page->getSlug()]);
}
/**
*
* @Route("/l/{code_traduction}/{ancre}-{css}", name="page_cms_link")
*/
public function path($code_traduction, $ancre, $css, Request $request)
{
$locale = $request->getLocale();
$lang = $this->em->getRepository(Language::class)->findOneByCode($locale);
$params = [
"code_traduction" => $code_traduction,
"lang_id" => $lang->getId()
];
$page = $this->em->getRepository(Page::class)->findOneByLang($params);
if(!$page instanceof Page) {
throw $this->createNotFoundException("Page not found");
}
$path = $this->generateUrl('page_cms', [
"code_traduction" => $code_traduction,
"slug" => $page->getSlug()
]);
return $this->render('/front/_blocks/link.html.twig',['path' => $path, 'ancre' => $ancre, 'css' => $css]);
}
/**
*
* @Route("/p/{code_traduction}/{slug}", name="page_cms")
*/
public function page($code_traduction, $slug, Request $request)
{
$locale = $request->getLocale();
$lang = $this->em->getRepository(Language::class)->findOneByCode($locale);
$params = [
"slug" => $slug,
"code_traduction" => $code_traduction,
"lang_id" => $lang->getId()
];
$page = $this->em->getRepository(Page::class)->findOneByLang($params);
if(!$page instanceof Page) {
throw $this->createNotFoundException("Page not found");
}
$type = $slug == 'rgpd' ? 'rgpd' : $page->getType();
switch($type) {
default:
case "cms";
return $this->render('/front/Page/cms.html.twig',[
'page' => $page,
]);
break;
case "rgpd";
return $this->render('/front/Page/cms.html.twig',[
'page' => $page,
'script' => '<script id="CookieDeclaration" src="https://consent.cookiebot.com/c5d925a9-f659-4834-ae6f-4ab5d4e89f9b/cd.js" type="text/javascript" async></script>'
]);
break;
case "redirection":
return $this->redirectToRoute($page->getRedirection());
break;
case "catalog":
$catalogs = $this->em->getRepository(Catalog::class)->findByLang($lang);
return $this->render('/front/Page/catalog.html.twig',['page' => $page, "catalogs" => $catalogs]);
break;
}
return $this->render('/front/Page/page.html.twig',['page' => $page]);
}
/**
*
* @Route("/menu/{code}", name="menu")
*/
public function menu(Request $request, $code) {
$locale = $request->getLocale();
$lang = $this->em->getRepository(Language::class)->findOneByCode($locale);
$length = (int) $this->translator->trans($code . '.length', [], 'menu');
$template = $this->translator->trans($code . '.template', [], 'menu');
$menu = [];
for ($k = 1; $k <= $length; $k++) {
$type = $this->translator->trans($code . '.items.' . $k . ".type", [], 'menu');
$ancre = $this->translator->trans($code . '.items.' . $k . ".ancre", [], 'menu');
$class = $this->translator->trans($classKey = $code . '.items.' . $k . ".class", [], 'menu');
switch($type) {
case "text":
$text = $this->translator->trans($code . '.items.' . $k . ".text", [], 'menu');
$menu[] = ['type' => 'text', 'text' => $text];
break;
case "route":
$path = $this->translator->trans($code . '.items.' . $k . ".path", [], 'menu');
$url = $this->generateUrl($path);
$menu[] = ['type' => 'link', 'ancre' => $ancre, 'url' => $url, 'class' => $class, 'target' => ''];
break;
case "url":
$url = $this->translator->trans($code . '.items.' . $k . ".url", [], 'menu');
$menu[] = ['type' => 'link', 'ancre' => $ancre, 'url' => $url, 'class' => $class, 'target' => '_blank'];
break;
case "asset":
$url = $this->translator->trans($code . '.items.' . $k . ".url", [], 'menu');
$menu[] = ['type' => 'link', 'ancre' => $ancre, 'url' => $url, 'class' => $class, 'target' => '_blank'];
break;
case "cms":
$code_traduction = $this->translator->trans($code . '.items.' . $k . ".code", [], 'menu');
$params = [
"code_traduction" => $code_traduction,
"lang_id" => $lang->getId()
];
$page = $this->em->getRepository(Page::class)->findOneByLang($params);
if($page instanceof Page && $page->getEtat()) {
$url = $this->generateUrl(
'page_cms',
[
'code_traduction' => $page->getCodeTraduction(),
'slug' => $page->getSlug()
]
);
$menu[] = ['type' => 'link', 'ancre' => $ancre, 'url' => $url, 'class' => $class, 'target' => ''];
}
break;
}
}
return $this->render('/front/_blocks/menu_' . $template . '.html.twig',['menu' => $menu]);
}
/**
*
* @Route("/Conditions-d-annulation", name="page_conditions_d_annulation")
*/
public function ConditionsAnnulation(Request $request)
{
return $this->render('/front/Page/conditions-d-annulation.html.twig',['slug' => 'Conditions-d-annulation']);
}
public function sendMailContact($contact, $form, $destinataires)
{
$object = "";
if(array_key_exists("objet", $contact)) {
$object = $contact['objet'];
}
$sujet = "[Ateya-vacances][" . $this->sourceForm . "] " . $object;
$reply_email = $contact["email"];
$reply_name = $contact["prenom"] . ' ' . $contact["nom"];
$from_email = 'no-reply@ateya-vacances.fr';
$from_name = 'ateya-vacances';
if(array_key_exists("societe", $contact)) {
$from_name .= " [" . $contact["societe"] . "]";
}
$nomPays = "";
if(array_key_exists("pays", $contact)) {
$pays = $this->em->getRepository(Pays::class)->find($contact["pays"]);
$nomPays = $pays->getNom();
}
$catalogues = "";
if(array_key_exists("catalogues", $contact)) {
$catalogues = $this->em->getRepository(Catalog::class)->findBy(['id' =>$contact["catalogues"]]);
}
$body = $this->twig->render('/front/Email/' . $this->sourceForm . '.html.twig', [
'contact' => $contact,
'form' => $form,
'pays' => $nomPays,
'catalogues' => $catalogues
]);
$from = new Address($from_email, $from_name);
$reply_to = new Address($reply_email, $reply_name);
// Convertir le contenu HTML en texte brut
$converter = new HtmlConverter();
$texte = $converter->convert($body);
$message = (new Email())
->from($from)
->replyTo($reply_to)
->subject($sujet)
->text($texte)
->html($body)
;
// Ajouter les destinataires à l'objet Email
foreach ($destinataires as $destinataire) {
$message->addTo(new Address($destinataire, $destinataire));
}
try {
$this->mailer->send($message);
} catch (TransportExceptionInterface $e) {
throw new NotificationErrorSendingException(sprintf('error sending for %s', $notification->getRecipient()->getEmail()));
}
}
/**
*
* @Route("/fiches-descriptives", name="ac_platform_fichesdescriptives")
*/
public function fiches(Request $request)
{
$locale = $request->getLocale();
$lang = $this->em->getRepository(Language::class)->findOneByCode($locale);
$residences = $this->em->getRepository(Residence::class)->findAllWithMedias($lang);
$retour = array(
"residences" => $residences
);
return $this->render('/front/Page/fiche.html.twig', $retour);
}
}