<?php
namespace App\Controller\Frontend;
use App\Entity\Content\Item;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use App\Helpers\Content\ContentHelper;
use App\Helpers\System\UtilHelper;
use App\Helpers\System\ConfigHelper;
class QuoteController extends AbstractController
{
public function __construct(EntityManagerInterface $entityManager, ContentHelper $contentHelper, UtilHelper $utilHelper, ConfigHelper $configHelper, MailerInterface $mailer)
{
$this->em = $entityManager;
$this->contentHelper = $contentHelper;
$this->utilHelper = $utilHelper;
$this->configHelper = $configHelper;
$this->mailer = $mailer;
}
/**
* @Route("/quick-quote", name="frontend_quote_quickquote")
*/
public function quickQuote(Request $request, \Swift_Mailer $mailer)
{
// No active language?
if(!$this->contentHelper->getActiveLanguage())
throw $this->createNotFoundException("No active language was found!");
// Url path is index?
if((strtolower(trim($request->getPathInfo(), "/")) == "index"))
{
return $this->redirect(str_replace("index", "/", $this->contentHelper->cleanPath($path)));
}
else
{
// get the url into regments
$urlParts = explode("/", trim($request->getPathInfo(), '/'));
// More than 2 url parts
if(count($urlParts) >= 2)
{
// Locale and index? We need to redirect...
if((strlen($urlParts[0]) == 2) && ($urlParts[1] == "index"))
return $this->redirect(str_replace("index", "", $request->getPathInfo()));
}
}
// Find the page
$page = $this->em->getRepository(Item::class)->findOneFiltered(array(
"path" => 'quick-quote',
"languageOrNull" => $this->contentHelper->getActiveLanguage(),
"status" => "published"
));
// Not found?
if(!$page)
throw $this->createNotFoundException("The page you're looking for was not found.");
// Check page access
$this->contentHelper->checkItemAccess($page, $this->getUser());
// Grab rendered content
$renderedContent = $this->contentHelper->getRenderedItemContent($page, $this->getUser());
// Get quote form
$quoteForm = $this->createForm(\App\Forms\Frontend\QuoteForm::class);
// Get quote data from url
$quoteUrlData = $request->query->get('quote', array());
// Got some url parameters?
if(array_key_exists('from', $quoteUrlData))
$quoteForm->get('collectionPostcode')->setData(strtoupper($quoteUrlData['from']));
// To postcode
if(array_key_exists('to', $quoteUrlData))
$quoteForm->get('deliveryAddress')->setData(strtoupper($quoteUrlData['to']));
// Weight
if(array_key_exists('weight', $quoteUrlData))
$quoteForm->get('weight')->setData(strtoupper($quoteUrlData['weight']));
// Handle the form
$quoteForm->handleRequest($request);
// Form submitted
if($quoteForm->isSubmitted())
{
// Recaptcha
$recaptcha = new \ReCaptcha\ReCaptcha($this->configHelper->getValue('recaptcha_secretkey'));
$recaptcha->setScoreThreshold(0.5);
$recaptchaResp = $recaptcha->verify($request->request->get('recaptcha_token'), $_SERVER['REMOTE_ADDR']);
// Captcha not valid?
if(!$recaptchaResp->isSuccess())
die("You must prove you're not a robot!");
// Valid?
if($quoteForm->isValid())
{
// Get form fields
$formFields = $quoteForm->all();
// Remove last field as it's a save button
array_pop($formFields);
// Build it
$email = (new Email())
->from($this->configHelper->getValue('email_from'))
->to($this->configHelper->getValue('contact_email'))
->subject('New Quote Request')
->html($this->render('Frontend/Quote/quoteEmail.html.twig', array(
'formFields' => $formFields,
'subject' => "New Quote Request"
))->getContent());
// Got a reply to?
if($formFields['email']->getData())
{
// Add it
$email->replyTo($formFields['email']->getData());
}
// Send it
$this->mailer->send($email);
// Redirect to thanks page
return $this->redirect($this->generateUrl('frontend_viewpage', array(
'path' => "quick-quote-thanks"
)));
}
}
// Render view
return $this->render('Frontend/Quote/quickQuote.html.twig', array(
'page' => $page,
'renderedContent' => $renderedContent,
'quoteForm' => $quoteForm->createView()
));
}
}