src/Controller/Blog/HomepageController.php line 85

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Blog;
  3. use App\Entity\Pages\Pages;
  4. use App\Entity\Pages\PagesHasBlocks;
  5. use App\Form\Articles\ContactForm;
  6. use App\Services\Core\Translations;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  16. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  17. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. use Doctrine\ORM\EntityManagerInterface;
  21. use Knp\Component\Pager\PaginatorInterface;
  22. use Symfony\Component\HttpFoundation\Cookie;
  23. use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
  24. use Symfony\Component\Routing\RouterInterface;
  25. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  26. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  27. use Symfony\Contracts\HttpClient\HttpClientInterface;
  28. use App\Security\LoginFormAuthenticator;
  29. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  30. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  31. use Twig\Environment;
  32. use Symfony\Component\Mailer\Transport;
  33. use Symfony\Component\Mailer\Mailer;
  34. use Symfony\Component\Mime\Address;
  35. use Symfony\Component\Mime\Email;
  36. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  37. use Symfony\Component\Mime\Part\DataPart;
  38. class HomepageController extends AbstractController
  39. {
  40. private $em;
  41. private $paginator;
  42. private $params;
  43. private $passwordEncoder;
  44. private $tokenGenerator;
  45. private $httpClient;
  46. private $us;
  47. private $ts;
  48. private $authenticator;
  49. private $guardHandler;
  50. private $mail;
  51. private $twig;
  52. public function __construct(EntityManagerInterface $em,
  53. PaginatorInterface $paginator,
  54. UserPasswordEncoderInterface $passwordEncoder,
  55. TokenGeneratorInterface $tokenGenerator,
  56. ParameterBagInterface $params,
  57. HttpClientInterface $httpClient,
  58. \App\Services\Core\Users $us,
  59. Translations $translationService,
  60. LoginFormAuthenticator $authenticator,
  61. GuardAuthenticatorHandler $guardHandler,
  62. \App\Services\Mailing\Mails $mail,
  63. Environment $twig
  64. ) {
  65. $this->em = $em;
  66. $this->paginator = $paginator;
  67. $this->passwordEncoder = $passwordEncoder;
  68. $this->tokenGenerator = $tokenGenerator;
  69. $this->params = $params;
  70. $this->httpClient = $httpClient;
  71. $this->us = $us;
  72. $this->ts = $translationService;
  73. $this->authenticator = $authenticator;
  74. $this->guardHandler = $guardHandler;
  75. $this->mail = $mail;
  76. $this->twig = $twig;
  77. }
  78. public function homepage(Request $request): Response
  79. {
  80. $locale = $request->getLocale();
  81. $page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'homepage','locale' => $locale]);
  82. if($page == null) {
  83. $page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'homepage','locale' => "en"]);
  84. }
  85. $blocks = $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page, 'type' => 'prod', 'startPage' => false],['sequence' => 'ASC']);
  86. $page->setViews((int)$page->getViews() + 1);
  87. $this->em->persist($page);
  88. $this->em->flush();
  89. return $this->render('vitrine/homepage.html.twig', [
  90. 'page' => $page,
  91. 'blocks' => $blocks
  92. ]);
  93. }
  94. public function contact(Request $request): Response
  95. {
  96. $session = $request->getSession();
  97. $user = $this->getUser();
  98. $locale = $request->getLocale();
  99. $page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'contact','locale' => $locale]);
  100. if($page == null) {
  101. $page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'contact','locale' => "en"]);
  102. }
  103. $blocks = $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page, 'type' => 'prod', 'startPage' => false],['sequence' => 'ASC']);
  104. $page->setViews((int)$page->getViews() + 1);
  105. $this->em->persist($page);
  106. $this->em->flush();
  107. $form = $this->createForm(ContactForm::class);
  108. if($locale == "en") {
  109. $form->add('question', ChoiceType::class, [
  110. 'label' => "",
  111. 'choices' => [
  112. 'I am an individual and I want to become a partner' => '1',
  113. 'I am a professional and I want to be listed' => '2',
  114. 'I want to be a family ambassador' => '3',
  115. 'Other' => '4'
  116. ],
  117. 'required' => false,
  118. 'multiple' => false,
  119. 'expanded' => false,
  120. 'attr' => ['class' => 'form-control']
  121. ]);
  122. } else {
  123. $form ->add('question',ChoiceType::class,[
  124. 'label' => "",
  125. 'choices' => array(
  126. 'Je suis un particulier et je veux être partenaire' => '1',
  127. 'Je suis un pro et je veux être référencé' => '2',
  128. 'Je veux être famille ambassadrice' => '3',
  129. 'Autre' => 4
  130. ),
  131. 'required' => false,
  132. 'multiple' => false,
  133. 'expanded' => false,
  134. 'attr' => ['class' => 'form-control']
  135. ]);
  136. }
  137. $form->handleRequest($request);
  138. if ($form->isSubmitted() && $form->isValid()) {
  139. $data = $request->request->all();
  140. $data = $data['contact_form'];
  141. $emailTo = "hello@letsgomino.com";
  142. $replaceTitle = "Demande de contact - Let's go mino";
  143. $dsn = $_ENV["MAILER_DSN"];
  144. $transport = Transport::fromDsn($dsn);
  145. $mailer = new Mailer($transport);
  146. $message = (new TemplatedEmail())
  147. ->from(new Address("hello@letsgomino.com","Let's go mino"))
  148. ->to($emailTo)
  149. ->subject($replaceTitle)
  150. ->html($this->twig->render("emails/contact.html.twig",[
  151. 'question' => $data['question'],
  152. 'name' => $data['name'],
  153. 'email' => $data['email'],
  154. 'message' => $data['message'],
  155. ]))
  156. ;
  157. $mailer->send($message);
  158. if($locale == "en") {
  159. $session->getFlashBag()->add('success', 'Mail sending');
  160. return $this->redirectToRoute('contact');
  161. }
  162. $session->getFlashBag()->add('success', 'Mail envoyé');
  163. return $this->redirectToRoute('locale_contact',['_locale' => $locale]);
  164. }
  165. return $this->render('vitrine/contact.html.twig', [
  166. 'page' => $page,
  167. 'blocks' => $blocks,
  168. 'form' => $form->createView()
  169. ]);
  170. }
  171. }