<?php
namespace Aqarmap\Bundle\MainBundle\Controller;
use Aqarmap\Bundle\MainBundle\Constant\FeedbackSource;
use Aqarmap\Bundle\MainBundle\Entity\Feedback;
use Aqarmap\Bundle\MainBundle\Event\FeedbackEvent;
use Aqarmap\Bundle\MainBundle\Form\FeedbackType;
use Doctrine\ORM\EntityManagerInterface;
use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\RFCValidation;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class FeedbackController extends AbstractController
{
private TranslatorInterface $translator;
private SessionInterface $session;
private EventDispatcherInterface $eventDispatcher;
public function __construct(TranslatorInterface $translator, SessionInterface $session, EventDispatcherInterface $eventDispatcher)
{
$this->translator = $translator;
$this->session = $session;
$this->eventDispatcher = $eventDispatcher;
}
/**
* @Route("/feedback", name="aqarmap_feedback_post", methods={"POST"})
*/
public function post(Request $request, EntityManagerInterface $em)
{
$form = $this->createForm(FeedbackType::class, $entity = new Feedback(), [
'action' => $this->generateUrl('aqarmap_feedback_post'),
'method' => 'POST',
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$validator = new EmailValidator();
$isValidEmail = $validator->isValid($entity->getEmail(), new RFCValidation());
if (!$isValidEmail) {
$this->addFlash(
'danger',
$this->translator->trans('static.invalid_email')
);
return $this->redirectToRoute('aqarmap_feedback_get');
}
// Set collected user data
$entity->setCollectedData(array_merge([
'referer' => $form->get('referer')->getData() ?: $request->headers->get('referer'),
], $request->attributes->get('_browser')));
$entity->setIp($request->getClientIp());
$entity->setSource(FeedbackSource::WEB);
$em->persist($entity);
$this->eventDispatcher->dispatch(new FeedbackEvent($entity), 'aqarmap.feedback.send_message');
$userMail = $form->get('email')->getData();
} else {
// Set error flash message
$this->session->getFlashBag()->add(
'danger',
$this->translator->trans('static.problem_error_message')
);
return $this->redirect($request->headers->get('referer') ?: $this->generateUrl('homepage'));
}
return $this->render('@AqarmapMain/Feedback/post.html.twig', [
'userMail' => $userMail,
]);
}
/**
* @Route("/feedback", name="aqarmap_feedback_get", methods={"GET"})
*/
public function feedback(Request $request): Response
{
$form = $this->createForm(FeedbackType::class, new Feedback(), [
'action' => $this->generateUrl('aqarmap_feedback_post'),
'method' => 'POST',
]);
// Set the referrer URL
if (!$form->get('referer')->getData()) {
$form->get('referer')->setData($request->headers->get('referer'));
}
return $this->render('@AqarmapMain/Feedback/feedbackForm.html.twig', [
'form' => $form->createView(),
]);
}
}