src/Controller/App/App/PublicController.php line 143

Open in your IDE?
  1. <?php
  2. namespace App\Controller\App\App;
  3. use App\Controller\ControllerTrait;
  4. use App\Form\App\ContactForm;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration as Ctrl;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Form\FormInterface;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. class PublicController extends AbstractController
  13. {
  14. const INDEX_ROUTE='public_index';
  15. use ControllerTrait;
  16. /**
  17. * @Route("/", name="public_index")
  18. *
  19. * @Ctrl\Cache(expires="+2 days", public=true)
  20. *
  21. * @param Request $request
  22. *
  23. * @return RedirectResponse
  24. */
  25. public function indexAction(Request $request): RedirectResponse
  26. {
  27. return $this->redirectToRoute('public_index_localized', array(
  28. '_locale' => $request->get('locale') ?? $request->getLocale(),
  29. ));
  30. }
  31. /**
  32. * @Route("/{_locale}", name="public_index_localized", requirements={
  33. * "_locale": "\w{2}"
  34. * })
  35. *
  36. * @Ctrl\Cache(expires="+2 days", public=true)
  37. *
  38. * @param Request $request
  39. *
  40. * @return Response
  41. */
  42. public function indexLocalizedAction(Request $request): Response
  43. {
  44. return $this->render('Public/Public/index.html.twig', [
  45. 'controller' => 'App\Controller\Content\App\PageController::indexAction',
  46. '_locale' => $request->getLocale(),
  47. ]);
  48. }
  49. /**
  50. * @Route("/contact/verify", name="public_contact", methods={"GET", "POST"})
  51. *
  52. * @param Request $request
  53. *
  54. * @return Response|array
  55. *
  56. * @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
  57. */
  58. public function contactAction(Request $request)
  59. {
  60. $form = $this->createForm(ContactForm::class)->handleRequest($request);
  61. $referer = $request->headers->get('referer');
  62. if (!$form->isSubmitted() || !$form->isValid()) {
  63. if ('/_fragment' === $request->getPathInfo()) {
  64. return $this->render('Public/Public/contact.ajax.twig', [
  65. 'form' => $form->createView(),
  66. ]);
  67. }
  68. if($referer) {
  69. $errors = $this->getErrorsFromForm($form);
  70. $errorMessage = '';
  71. foreach ($errors as $field => $error) {
  72. $errorMessage .= sprintf('%s: %s', $field, implode(', ', $error));
  73. }
  74. $this->addFlash('danger', $errorMessage);
  75. return $this->redirect($referer);
  76. }
  77. return $this->render('Public/Public/contact.html.twig', [
  78. 'form' => $form->createView(),
  79. ]);
  80. }
  81. $data = $form->getNormData();
  82. $message = (new \Swift_Message('Contact form Email'))
  83. ->setFrom($data['email'])
  84. ->setTo($this->getParameter('email_sender'))
  85. ->setBody($this->renderView('Content/Emails/contact.html.twig', $data), 'text/html');
  86. $this->get('mailer')->send($message);
  87. if ($referer) {
  88. return $this->redirect($referer);
  89. }
  90. return $this->redirectToIndex();
  91. }
  92. /**
  93. * @param FormInterface $form
  94. *
  95. * @return array
  96. */
  97. protected function getErrorsFromForm(FormInterface $form): array
  98. {
  99. $errors = array();
  100. foreach ($form->getErrors() as $error) {
  101. $errors[] = $error->getMessage();
  102. }
  103. foreach ($form->all() as $childForm) {
  104. if (!$childForm instanceof FormInterface) {
  105. continue;
  106. }
  107. if ($childErrors = $this->getErrorsFromForm($childForm)) {
  108. $errors[$childForm->getName()] = $childErrors;
  109. }
  110. }
  111. return $errors;
  112. }
  113. /**
  114. * @Ctrl\Cache(expires="+2 days", public=true)
  115. *
  116. * @param Request $request
  117. *
  118. * @return Response
  119. */
  120. public function contactFormAction(Request $request): Response
  121. {
  122. $form = $this->createForm(ContactForm::class, null, array(
  123. 'action' => $this->generateUrl('public_contact'),
  124. 'method' => 'POST',
  125. ));
  126. return $this->render('Public/Public/contact.ajax.twig', [
  127. 'form' => $form->createView(),
  128. ]);
  129. }
  130. /**
  131. * @Ctrl\Cache(expires="+2 days", public=true)
  132. */
  133. public function googleMapAction(Request $request)
  134. {
  135. return $this->render('Public/Public/map.ajax.twig', []);
  136. }
  137. }