src/Controller/ContactController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\ContactType;
  4. use App\Manager\ContactManager;
  5. use App\Service\Internal\ContactService;
  6. use App\Service\Internal\HomePageService;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  11. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  12. use Symfony\Component\Serializer\Serializer;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. class ContactController extends AbstractController
  15. {
  16. public function getContact(
  17. Request $request,
  18. ContactManager $contactManager,
  19. ContactService $contactService,
  20. HomePageService $homePageService,
  21. TranslatorInterface $translator
  22. ) {
  23. $subject = $request->query->get('type', false);
  24. $categories = $contactManager->buildCategories(strtoupper($subject));
  25. $formSubject = $contactManager->getFormSubject($subject);
  26. $parameters = $homePageService->getRenderParameters();
  27. $parameters['subject'] = (null !== $formSubject) ? $subject : false;
  28. $formSubject = (null !== $formSubject) ? $formSubject : ContactManager::FORM_DEFAULT_SUBJECT;
  29. $parameters['formTemplate'] = 'form_' . $formSubject;
  30. $parameters['postResponse'] = false;
  31. $parameters['nbAttachments'] = ContactManager::NB_ATTACHMENTS;
  32. $parameters['rgpd_link'] = $this->getParameter('global.rgpd_link');
  33. $message = '';
  34. $journey = $request->query->get('journey');
  35. if (!empty($journey)) {
  36. $message = $contactManager->buildMessageForJourney($journey);
  37. }
  38. $schedule = $request->query->get('schedule');
  39. if (!empty($schedule)) {
  40. $message = $contactManager->buildMessageForSchedule($schedule);
  41. }
  42. $form = $this->createForm(
  43. ContactType::class,
  44. [],
  45. [
  46. 'categories' => $categories,
  47. 'message' => $message,
  48. 'subject' => $formSubject,
  49. 'manager' => $contactManager,
  50. 'translator' => $translator
  51. ]
  52. );
  53. if ($request->getMethod() == 'POST') {
  54. $form->handleRequest($request);
  55. $flashbag = $request->getSession()->getFlashBag();
  56. $captchaResponse = $request->request->get('g-recaptcha-response');
  57. if (empty($captchaResponse)) {
  58. $parameters['form'] = $form->createView();
  59. $flashbag->add('error', $translator->trans('captcha.label'));
  60. return $this->render('pages/sim/contact.html.twig', $parameters);
  61. }
  62. $isValidCaptcha = $contactService->postCaptchaToGoogle($captchaResponse);
  63. if (!$isValidCaptcha) {
  64. $parameters['form'] = $form->createView();
  65. $flashbag->add('error', $translator->trans('captcha.error'));
  66. return $this->render('pages/sim/contact.html.twig', $parameters);
  67. }
  68. if ($form->isSubmitted() && !$form->isValid()) {
  69. $parameters['form'] = $form->createView();
  70. $flashbag->add('error', $translator->trans('contact.invalid_fields'));
  71. return $this->render('pages/sim/contact.html.twig', $parameters);
  72. }
  73. $contact = $form->getData();
  74. if ($formSubject == ContactManager::FORM_ZOU_SUBJECT) {
  75. for ($i = 0; $i < ContactManager::NB_ATTACHMENTS; $i++) {
  76. $filename = "optionalFile_" . $i;
  77. if (isset($_FILES['contact_form']['error'][$filename]) && $_FILES['contact_form']['error'][$filename] == 0) {
  78. $contact['files'][] = $this->getUploadedFile($filename);
  79. }
  80. }
  81. } elseif (isset($_FILES['contact_form']['error']["optionalFile"]) && $_FILES['contact_form']['error']["optionalFile"] == 0) {
  82. $contact['file'] = $this->getUploadedFile();
  83. }
  84. $contact = $contactManager->normalizeContactData($subject, $contact);
  85. $encoders = [new JsonEncoder()];
  86. $normalizers = [new ObjectNormalizer()];
  87. $serializer = new Serializer($normalizers, $encoders);
  88. $postResponse = $contactService->postContact($serializer->serialize($contact, 'json'));
  89. if ($postResponse === true) {
  90. $flashbag->add('success', $translator->trans('contact.post_success'));
  91. } else {
  92. $flashbag->add('error', $translator->trans('contact.post_error'));
  93. }
  94. $parameters['postResponse'] = $postResponse;
  95. }
  96. $parameters['form'] = $form->createView();
  97. return $this->render('pages/sim/contact.html.twig', $parameters);
  98. }
  99. private function getUploadedFile(string $filename = 'optionalFile'): array
  100. {
  101. $name = '';
  102. $content = '';
  103. if (isset($_FILES['contact_form']["name"][$filename])) {
  104. $name = $_FILES['contact_form']["name"][$filename];
  105. }
  106. if (isset($_FILES['contact_form']["tmp_name"][$filename])) {
  107. $content = base64_encode(file_get_contents($_FILES['contact_form']["tmp_name"][$filename]));
  108. }
  109. return ['name' => $name, 'content' => $content];
  110. }
  111. public function getDirectionsAndStoppoints(?string $lineId, ContactManager $contactManager): JsonResponse
  112. {
  113. $directionsWithStoppoints = $contactManager->buildDirectionAndStoppoints($lineId);
  114. return new JsonResponse($directionsWithStoppoints);
  115. }
  116. }