src/Aqarmap/Bundle/MainBundle/Controller/FeedbackController.php line 87

  1. <?php
  2. namespace Aqarmap\Bundle\MainBundle\Controller;
  3. use Aqarmap\Bundle\MainBundle\Constant\FeedbackSource;
  4. use Aqarmap\Bundle\MainBundle\Entity\Feedback;
  5. use Aqarmap\Bundle\MainBundle\Event\FeedbackEvent;
  6. use Aqarmap\Bundle\MainBundle\Form\FeedbackType;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Egulias\EmailValidator\EmailValidator;
  9. use Egulias\EmailValidator\Validation\RFCValidation;
  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\Attribute\Route;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. class FeedbackController extends AbstractController
  17. {
  18. public function __construct(private readonly TranslatorInterface $translator, private readonly EventDispatcherInterface $eventDispatcher)
  19. {
  20. }
  21. #[Route(path: '/feedback', name: 'aqarmap_feedback_post', methods: ['POST'])]
  22. public function post(Request $request, EntityManagerInterface $em)
  23. {
  24. $form = $this->createForm(FeedbackType::class, $entity = new Feedback(), [
  25. 'action' => $this->generateUrl('aqarmap_feedback_post'),
  26. 'method' => 'POST',
  27. ]);
  28. $form->handleRequest($request);
  29. if ($form->isSubmitted() && $form->isValid()) {
  30. $validator = new EmailValidator();
  31. $isValidEmail = $validator->isValid($entity->getEmail(), new RFCValidation());
  32. if (!$isValidEmail) {
  33. $this->addFlash(
  34. 'danger',
  35. $this->translator->trans('static.invalid_email')
  36. );
  37. return $this->redirectToRoute('aqarmap_feedback_get');
  38. }
  39. // Set collected user data
  40. $entity->setCollectedData(array_merge([
  41. 'referer' => $form->get('referer')->getData() ?: $request->headers->get('referer'),
  42. ], $request->attributes->get('_browser')));
  43. $entity->setIp($request->getClientIp());
  44. $entity->setSource(FeedbackSource::WEB);
  45. $em->persist($entity);
  46. $this->eventDispatcher->dispatch(new FeedbackEvent($entity), 'aqarmap.feedback.send_message');
  47. $userMail = $form->get('email')->getData();
  48. } else {
  49. // Set error flash message
  50. $request->getSession()->getFlashBag()->add(
  51. 'danger',
  52. $this->translator->trans('static.problem_error_message')
  53. );
  54. return $this->redirect($request->headers->get('referer') ?: $this->generateUrl('homepage'));
  55. }
  56. return $this->render('@AqarmapMain/Feedback/post.html.twig', [
  57. 'userMail' => $userMail,
  58. ]);
  59. }
  60. #[Route(path: '/feedback', name: 'aqarmap_feedback_get', methods: ['GET'])]
  61. public function feedback(Request $request): Response
  62. {
  63. $form = $this->createForm(FeedbackType::class, new Feedback(), [
  64. 'action' => $this->generateUrl('aqarmap_feedback_post'),
  65. 'method' => 'POST',
  66. ]);
  67. // Set the referrer URL
  68. if (!$form->get('referer')->getData()) {
  69. $form->get('referer')->setData($request->headers->get('referer'));
  70. }
  71. return $this->render('@AqarmapMain/Feedback/feedbackForm.html.twig', [
  72. 'form' => $form,
  73. ]);
  74. }
  75. }