src/Controller/Frontend/QuoteController.php line 77

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Entity\Content\Item;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\Mailer\MailerInterface;
  10. use Symfony\Component\Mime\Email;
  11. use App\Helpers\Content\ContentHelper;
  12. use App\Helpers\System\UtilHelper;
  13. use App\Helpers\System\ConfigHelper;
  14. class QuoteController extends AbstractController
  15. {
  16.     public function __construct(EntityManagerInterface $entityManagerContentHelper $contentHelperUtilHelper $utilHelperConfigHelper $configHelperMailerInterface $mailer)
  17.     {
  18.         $this->em $entityManager;
  19.         $this->contentHelper $contentHelper;
  20.         $this->utilHelper $utilHelper;
  21.         $this->configHelper $configHelper;
  22.         $this->mailer $mailer;
  23.     }
  24.     /**
  25.     * @Route("/quick-quote", name="frontend_quote_quickquote")
  26.     */
  27.     public function quickQuote(Request $request\Swift_Mailer $mailer)
  28.     {
  29.         // No active language?
  30.         if(!$this->contentHelper->getActiveLanguage())
  31.             throw $this->createNotFoundException("No active language was found!");
  32.         // Url path is index?
  33.         if((strtolower(trim($request->getPathInfo(), "/")) == "index"))
  34.         {
  35.             return $this->redirect(str_replace("index""/"$this->contentHelper->cleanPath($path)));
  36.         }
  37.         else
  38.         {
  39.             // get the url into regments
  40.             $urlParts explode("/"trim($request->getPathInfo(), '/'));
  41.             // More than 2 url parts
  42.             if(count($urlParts) >= 2)
  43.             {
  44.                 // Locale and index? We need to redirect...
  45.                 if((strlen($urlParts[0]) == 2) && ($urlParts[1] == "index"))
  46.                     return $this->redirect(str_replace("index"""$request->getPathInfo()));
  47.             }
  48.         }
  49.         // Find the page
  50.         $page $this->em->getRepository(Item::class)->findOneFiltered(array(
  51.             "path" => 'quick-quote',
  52.             "languageOrNull" => $this->contentHelper->getActiveLanguage(),
  53.             "status" => "published"
  54.         ));
  55.         // Not found?
  56.         if(!$page)
  57.             throw $this->createNotFoundException("The page you're looking for was not found.");
  58.         // Check page access
  59.         $this->contentHelper->checkItemAccess($page$this->getUser());
  60.         // Grab rendered content
  61.         $renderedContent $this->contentHelper->getRenderedItemContent($page$this->getUser());
  62.         // Get quote form
  63.         $quoteForm $this->createForm(\App\Forms\Frontend\QuoteForm::class);
  64.         // Get quote data from url
  65.         $quoteUrlData $request->query->get('quote', array());
  66.         // Got some url parameters?
  67.         if(array_key_exists('from'$quoteUrlData))
  68.             $quoteForm->get('collectionPostcode')->setData(strtoupper($quoteUrlData['from']));
  69.         // To postcode
  70.         if(array_key_exists('to'$quoteUrlData))
  71.             $quoteForm->get('deliveryAddress')->setData(strtoupper($quoteUrlData['to']));
  72.         // Weight
  73.         if(array_key_exists('weight'$quoteUrlData))
  74.             $quoteForm->get('weight')->setData(strtoupper($quoteUrlData['weight']));
  75.         // Handle the form
  76.         $quoteForm->handleRequest($request);
  77.         // Form submitted
  78.         if($quoteForm->isSubmitted())
  79.         {
  80.             // Recaptcha
  81.             $recaptcha = new \ReCaptcha\ReCaptcha($this->configHelper->getValue('recaptcha_secretkey'));
  82.             $recaptcha->setScoreThreshold(0.5);
  83.             $recaptchaResp $recaptcha->verify($request->request->get('recaptcha_token'), $_SERVER['REMOTE_ADDR']);
  84.             // Captcha not valid?
  85.             if(!$recaptchaResp->isSuccess())
  86.                 die("You must prove you're not a robot!");
  87.             // Valid?
  88.             if($quoteForm->isValid())
  89.             {
  90.                 // Get form fields
  91.                 $formFields $quoteForm->all();
  92.                 // Remove last field as it's a save button
  93.                 array_pop($formFields);
  94.                 // Build it
  95.                 $email = (new Email())
  96.                     ->from($this->configHelper->getValue('email_from'))
  97.                     ->to($this->configHelper->getValue('contact_email'))
  98.                     ->subject('New Quote Request')
  99.                     ->html($this->render('Frontend/Quote/quoteEmail.html.twig', array(
  100.                         'formFields' => $formFields,
  101.                         'subject' => "New Quote Request"
  102.                     ))->getContent());
  103.                 // Got a reply to?
  104.                 if($formFields['email']->getData())
  105.                 {
  106.                     // Add it
  107.                     $email->replyTo($formFields['email']->getData());
  108.                 }
  109.                 // Send it
  110.                 $this->mailer->send($email);
  111.                 // Redirect to thanks page
  112.                 return $this->redirect($this->generateUrl('frontend_viewpage', array(
  113.                     'path' => "quick-quote-thanks"
  114.                 )));
  115.             }
  116.         }
  117.         // Render view
  118.         return $this->render('Frontend/Quote/quickQuote.html.twig', array(
  119.             'page' => $page,
  120.             'renderedContent' => $renderedContent,
  121.             'quoteForm' => $quoteForm->createView()
  122.         ));
  123.     }
  124. }