<?phpnamespace App\Controller\Blog;use App\Entity\Pages\Pages;use App\Entity\Pages\PagesHasBlocks;use App\Form\Articles\ContactForm;use App\Services\Core\Translations;use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\EventDispatcher\EventDispatcherInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpFoundation\RequestStack;use Doctrine\ORM\EntityManagerInterface;use Knp\Component\Pager\PaginatorInterface;use Symfony\Component\HttpFoundation\Cookie;use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;use Symfony\Component\Routing\RouterInterface;use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;use Symfony\Component\HttpFoundation\Session\SessionInterface;use Symfony\Contracts\HttpClient\HttpClientInterface;use App\Security\LoginFormAuthenticator;use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;use Symfony\Component\Form\Extension\Core\Type\ChoiceType;use Twig\Environment;use Symfony\Component\Mailer\Transport;use Symfony\Component\Mailer\Mailer;use Symfony\Component\Mime\Address;use Symfony\Component\Mime\Email;use Symfony\Bridge\Twig\Mime\TemplatedEmail;use Symfony\Component\Mime\Part\DataPart;class HomepageController extends AbstractController{ private $em; private $paginator; private $params; private $passwordEncoder; private $tokenGenerator; private $httpClient; private $us; private $ts; private $authenticator; private $guardHandler; private $mail; private $twig; public function __construct(EntityManagerInterface $em, PaginatorInterface $paginator, UserPasswordEncoderInterface $passwordEncoder, TokenGeneratorInterface $tokenGenerator, ParameterBagInterface $params, HttpClientInterface $httpClient, \App\Services\Core\Users $us, Translations $translationService, LoginFormAuthenticator $authenticator, GuardAuthenticatorHandler $guardHandler, \App\Services\Mailing\Mails $mail, Environment $twig ) { $this->em = $em; $this->paginator = $paginator; $this->passwordEncoder = $passwordEncoder; $this->tokenGenerator = $tokenGenerator; $this->params = $params; $this->httpClient = $httpClient; $this->us = $us; $this->ts = $translationService; $this->authenticator = $authenticator; $this->guardHandler = $guardHandler; $this->mail = $mail; $this->twig = $twig; } public function homepage(Request $request): Response { $locale = $request->getLocale(); $page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'homepage','locale' => $locale]); if($page == null) { $page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'homepage','locale' => "en"]); } $blocks = $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page, 'type' => 'prod', 'startPage' => false],['sequence' => 'ASC']); $page->setViews((int)$page->getViews() + 1); $this->em->persist($page); $this->em->flush(); return $this->render('vitrine/homepage.html.twig', [ 'page' => $page, 'blocks' => $blocks ]); } public function contact(Request $request): Response { $session = $request->getSession(); $user = $this->getUser(); $locale = $request->getLocale(); $page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'contact','locale' => $locale]); if($page == null) { $page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'contact','locale' => "en"]); } $blocks = $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page, 'type' => 'prod', 'startPage' => false],['sequence' => 'ASC']); $page->setViews((int)$page->getViews() + 1); $this->em->persist($page); $this->em->flush(); $form = $this->createForm(ContactForm::class); if($locale == "en") { $form->add('question', ChoiceType::class, [ 'label' => "", 'choices' => [ 'I am an individual and I want to become a partner' => '1', 'I am a professional and I want to be listed' => '2', 'I want to be a family ambassador' => '3', 'Other' => '4' ], 'required' => false, 'multiple' => false, 'expanded' => false, 'attr' => ['class' => 'form-control'] ]); } else { $form ->add('question',ChoiceType::class,[ 'label' => "", 'choices' => array( 'Je suis un particulier et je veux être partenaire' => '1', 'Je suis un pro et je veux être référencé' => '2', 'Je veux être famille ambassadrice' => '3', 'Autre' => 4 ), 'required' => false, 'multiple' => false, 'expanded' => false, 'attr' => ['class' => 'form-control'] ]); } $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $data = $request->request->all(); $data = $data['contact_form']; $emailTo = "hello@letsgomino.com"; $replaceTitle = "Demande de contact - Let's go mino"; $dsn = $_ENV["MAILER_DSN"]; $transport = Transport::fromDsn($dsn); $mailer = new Mailer($transport); $message = (new TemplatedEmail()) ->from(new Address("hello@letsgomino.com","Let's go mino")) ->to($emailTo) ->subject($replaceTitle) ->html($this->twig->render("emails/contact.html.twig",[ 'question' => $data['question'], 'name' => $data['name'], 'email' => $data['email'], 'message' => $data['message'], ])) ; $mailer->send($message); if($locale == "en") { $session->getFlashBag()->add('success', 'Mail sending'); return $this->redirectToRoute('contact'); } $session->getFlashBag()->add('success', 'Mail envoyé'); return $this->redirectToRoute('locale_contact',['_locale' => $locale]); } return $this->render('vitrine/contact.html.twig', [ 'page' => $page, 'blocks' => $blocks, 'form' => $form->createView() ]); }}