<?php
namespace App\Controller;
use App\Entity\Enquiry;
use App\Entity\EnquiryCV;
use App\Form\EnquiryType;
use App\Form\EnquiryCVType;
use App\Annotation\CmsComponent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class EnquiryDefaultController extends AbstractController
{
public function __construct(
private EntityManagerInterface $em,
private \Swift_Mailer $mailer,
private string $email_noreply,
private string $email_primary
) { }
/**
* @CmsComponent("Embed Enquiry Form", active=true, routeName="embed_enquiry")
*/
#[Route(path: '/cms-enquiry', name: 'embed_enquiry')]
public function embedEnquiry(Request $request, ): Response
{
$enquiry = new Enquiry();
$enquiry->setSubject('New Website Enquiry');
$form = $this->createForm(EnquiryType::class, $enquiry);
$form->handleRequest($request);
$error = false;
$success = false;
$errorMessage = '';
if ($form->isSubmitted()) {
if ($this->captchaIsValid($request)) {
if ($form->isValid()) {
$this->em->persist($enquiry);
$this->em->flush();
$success = true;
$this->sendEmail($enquiry);
} else {
$error = true;
$errorMessage = 'Error - Check the form for errors';
}
} else {
$error = true;
$errorMessage = 'Error - Captcha Invalid';
}
}
return $this->render('@theme/enquiry/enquiry.html.twig', [
'enquiry' => $enquiry,
'error' => $error,
'success' => $success,
'errorMessage' => $errorMessage,
'form' => $form->createView(),
]);
}
/**
* @CmsComponent("Embed Enquiry Form & Map", active=true, routeName="embed_enquiry_map")
*/
#[Route(path: '/cms-enquiry-map', name: 'embed_enquiry_map')]
public function embedEnquiryMap(Request $request, ): Response
{
$enquiry = new Enquiry();
$enquiry->setSubject('New Website Enquiry');
$form = $this->createForm(EnquiryType::class, $enquiry);
$form->handleRequest($request);
$error = false;
$success = false;
$errorMessage = '';
if ($form->isSubmitted()) {
if ($this->captchaIsValid($request)) {
if ($form->isValid()) {
$this->em->persist($enquiry);
$this->em->flush();
$success = true;
$this->sendEmail($enquiry);
} else {
$error = true;
$errorMessage = 'Error - Check the form for errors';
}
} else {
$error = true;
$errorMessage = 'Error - Captcha Invalid';
}
}
return $this->render('@theme/enquiry/enquiry_map.html.twig', [
'enquiry' => $enquiry,
'error' => $error,
'success' => $success,
'errorMessage' => $errorMessage,
'form' => $form->createView(),
]);
}
/**
* @CmsComponent("Embed CV form", active=true, routeName="embed_enquiry_cv")
*/
#[Route(path: '/cms-enquiry-cv', name: 'embed_enquiry_cv')]
public function embedEnquiryCv(Request $request): Response
{
$enquiry = new EnquiryCV();
$form = $this->createForm(EnquiryCVType::class, $enquiry);
$form->handleRequest($request);
$error = false;
$success = false;
$errorMessage = '';
if ($form->isSubmitted()) {
if ($this->captchaIsValid($request)) {
if ($form->isValid()) {
$enquiry->uploadFile();
$this->em->persist($enquiry);
$this->em->flush();
$success = true;
$this->sendCVEmail($enquiry);
} else {
$error = true;
$errorMessage = 'Error - Check the form for errors';
}
} else {
$error = true;
$errorMessage = 'Error - Captcha Invalid';
}
}
return $this->render('@theme/enquiry/enquiry_cv.html.twig', [
'enquiry' => $enquiry,
'error' => $error,
'success' => $success,
'errorMessage' => $errorMessage,
'form' => $form->createView(),
]);
}
#[Route(path: '/email-test', name: 'email_test')]
public function emailTest(): Response
{
return $this->render('@theme/emails/enquiry-confirmed.html.twig');
}
private function captchaIsValid(Request $request): bool
{
$posted = $request->request->All();
$secretKey = $this->getParameter('recaptcha_secret');
$recaptcha = $posted['g-recaptcha-response'];
$verify = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secretKey.'&response='.$recaptcha.'&remoteip='.$_SERVER['REMOTE_ADDR']);
$verify_json = json_decode($verify, false, 512, JSON_THROW_ON_ERROR);
return $verify_json->success;
}
private function sendEmail(Enquiry $enquiry): void
{
$message_to_client = (new \Swift_Message())
->setSubject('Enquiry Received via '.$this->getParameter('sitename').' website')
->setFrom($this->email_noreply)
->setTo($this->email_primary)
->setBody(
$this->renderView('@theme/emails/enquiry-to-client.html.twig', ['enquiry' => $enquiry]),
'text/html'
)
;
try {
$this->mailer->send($message_to_client);
} catch (\Throwable $th) {
// ignore
}
$this->sendConfirmationEmail($enquiry);
}
private function sendCVEmail(EnquiryCV $enquiry): void
{
$message_to_client = (new \Swift_Message())
->setSubject('Enquiry Received via '.$this->getParameter('sitename').' website')
->setFrom($this->email_noreply)
->setTo($this->email_primary)
->setBody(
$this->renderView('@theme/emails/enquiry-to-client-cv.html.twig', ['enquiry' => $enquiry]),
'text/html'
)
;
try {
$this->mailer->send($message_to_client);
} catch (\Throwable $th) {
// ignore
}
$this->sendConfirmationEmail($enquiry);
}
private function sendConfirmationEmail($enquiry): void
{
dump($enquiry);
$message_to_user = (new \Swift_Message())
->setSubject('Enquiry sent to '.$this->getParameter('sitename').' confirmed')
->setFrom($this->email_noreply)
->setTo($enquiry->getEmail())
->setBody(
$this->renderView(
'@theme/emails/enquiry-confirmed.html.twig',
['enquiry' => $enquiry]
),
'text/html'
)
;
// try {
$this->mailer->send($message_to_user);
// } catch (\Throwable $th) {
// ignore
// }
}
}