src/Controller/Web/WebController.php line 224

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Web;
  3. use App\Entity\Pages;
  4. use App\Entity\Products;
  5. use App\Entity\Sitewide;
  6. use App\Form\Type\EmailType;
  7. use App\Repository\PagesRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\Form\FormInterface;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Mailer\MailerInterface;
  16. use Symfony\Component\Mime\Address;
  17. use Symfony\Component\Mime\Email;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. class WebController extends AbstractController
  20. {
  21.     /**
  22.      * @var Sitewide|object|null
  23.      */
  24.     private $sitewide;
  25.     /**
  26.      * @var EntityManagerInterface
  27.      */
  28.     private $em;
  29.     /**
  30.      * DefaultController constructor.
  31.      *
  32.      * @param EntityManagerInterface $em
  33.      */
  34.     public function __construct(EntityManagerInterface $em)
  35.     {
  36.         $this->em $em;
  37.         $this->sitewide $this->em->getRepository(Sitewide::class)->findOneBy([], ['id' => 'asc']);
  38.     }
  39.     /**
  40.      * @Route("/", name="home", options={"sitemap" = {"priority" = 1}})
  41.      * @Route("/about/", name="about")
  42.      * @Route("/services/", name="about")
  43.      * @Route("/how-we-work/", name="how-we-work")
  44.      * @Route("/technologies/")
  45.      * @return Response
  46.      */
  47.     public function indexAction(): Response
  48.     {
  49.         /** @var PagesRepository $pagesRepo */
  50.         $pagesRepo $this->em->getRepository(Pages::class);
  51.         $mainNav $pagesRepo->findPubMainNavPageNames();
  52.         $otherNav $pagesRepo->findPubOtherNavPageNames();
  53.         $servicesSection $pagesRepo->findOneByName('Services');
  54.         $home $pagesRepo->findOneByName('Home');
  55.         $projects $pagesRepo->findOneByName('Projects');
  56.         $blog $pagesRepo->findOneByName('Blog');
  57.         $howWeWork $pagesRepo->findOneByName('How We Work');
  58.         $services $this->em->getRepository(Products::class)->findBy([
  59.             'publish' => true,
  60.             'category' => 'service'
  61.         ], [
  62.             'position' => 'asc',
  63.         ]);
  64.         $formEmail $this->createForm(EmailType::class, null$this->getEmailFormOptions($services));
  65.         return $this->render('web/Default/index.html.twig', [
  66.             'mainNav' => $mainNav,
  67.             'otherNav' => $otherNav,
  68.             'servicesSection' => $servicesSection,
  69.             'home' => $home,
  70.             'projects' => $projects,
  71.             'blog' => $blog,
  72.             'howWeWork' => $howWeWork,
  73.             'services' => $services,
  74.             'formEmail' => $formEmail->createView(),
  75.         ]);
  76.     }
  77.     /**
  78.      * @Route("/expertise", name="expertise", options={"sitemap" = false})
  79.      */
  80.     public function expertiseAction(): Response
  81.     {
  82.         $mainNav $this->em->getRepository(Pages::class)->findPubMainNavPageNames();
  83.         $otherNav $this->em->getRepository(Pages::class)->findPubOtherNavPageNames();
  84.         $page $this->em->getRepository(Pages::class)->findOneBy(['name' => 'Expertise']);
  85.         return $this->render('web/Default/product.html.twig', [
  86.             'route' => 'expertise',
  87.             'page' => $page,
  88.             'sitewide' => $this->sitewide,
  89.             'mainNav' => $mainNav,
  90.             'otherNav' => $otherNav,
  91.         ]);
  92.     }
  93.     /**
  94.      * @Route("/products", name="products", options={"sitemap" = false})
  95.      */
  96.     public function productsAction(): Response
  97.     {
  98.         $mainNav $this->em->getRepository(Pages::class)->findPubMainNavPageNames();
  99.         $otherNav $this->em->getRepository(Pages::class)->findPubOtherNavPageNames();
  100.         $page $this->em->getRepository(Pages::class)->findOneBy(['name' => 'Products']);
  101.         return $this->render('web/Default/product.html.twig', [
  102.             'route' => 'admin_products',
  103.             'page' => $page,
  104.             'sitewide' => $this->sitewide,
  105.             'mainNav' => $mainNav,
  106.             'otherNav' => $otherNav,
  107.         ]);
  108.     }
  109.     /**
  110.      * @Route("/features", name="features", options={"sitemap" = false})
  111.      */
  112.     public function featuresAction(): Response
  113.     {
  114.         $mainNav $this->em->getRepository(Pages::class)->findPubMainNavPageNames();
  115.         $otherNav $this->em->getRepository(Pages::class)->findPubOtherNavPageNames();
  116.         $page $this->em->getRepository(Pages::class)->findOneBy(['name' => 'Features']);
  117.         return $this->render('web/Default/product.html.twig', [
  118.             'route' => 'features',
  119.             'page' => $page,
  120.             'sitewide' => $this->sitewide,
  121.             'mainNav' => $mainNav,
  122.             'otherNav' => $otherNav,
  123.         ]);
  124.     }
  125.     /**
  126.      * @Route("/symfony", name="symfony", options={"sitemap" = false})
  127.      */
  128.     public function symfonyAction(): Response
  129.     {
  130.         $mainNav $this->em->getRepository(Pages::class)->findPubMainNavPageNames();
  131.         $otherNav $this->em->getRepository(Pages::class)->findPubOtherNavPageNames();
  132.         $page $this->em->getRepository(Pages::class)->findOneBy(['name' => 'Symfony']);
  133.         return $this->render('web/Default/product.html.twig', [
  134.             'route' => 'symfony',
  135.             'page' => $page,
  136.             'sitewide' => $this->sitewide,
  137.             'mainNav' => $mainNav,
  138.             'otherNav' => $otherNav,
  139.         ]);
  140.     }
  141.     /**
  142.      * @Route("/email", name="alpha_werk_web_email")
  143.      * @param Request $request
  144.      * @param MailerInterface $mailer
  145.      *
  146.      * @return JsonResponse|RedirectResponse
  147.      */
  148.     public function emailAction(Request $requestMailerInterface $mailer)
  149.     {
  150.         $products $this->em->getRepository(Products::class)->findBy([
  151.             'publish' => true,
  152.         ], [
  153.             'position' => 'asc',
  154.         ]);
  155.         $formEmail $this->createForm(EmailType::class, null$this->getEmailFormOptions($products));
  156.         if ($request->isMethod('POST')) {
  157.             $formEmail->handleRequest($request);
  158.             if ($formEmail->isSubmitted() && $formEmail->isValid()) {
  159.                 $recapthcaResponse $request->request->get('g-recaptcha-response');
  160.                 if (true === $this->areTheyARobot($recapthcaResponse$request->getClientIp())) {
  161.                     $this->sendEmail($formEmail$mailer);
  162.                     return $this->redirect($this->generateUrl('alpha_werk_web_email'));
  163.                 } else {
  164.                     return new JsonResponse(
  165.                         [
  166.                             'errors' => 'You are a robot!',
  167.                             'success' => false,
  168.                         ]
  169.                     );
  170.                 }
  171.             } else {
  172.                 return new JsonResponse(
  173.                     [
  174.                         'errors' => implode('</br>'$this->getErrorMessages($formEmail)),
  175.                         'success' => false,
  176.                     ]
  177.                 );
  178.             }
  179.         } else {
  180.             return new JsonResponse(
  181.                 [
  182.                     'errors' => null,
  183.                     'success' => true,
  184.                 ]
  185.             );
  186.         }
  187.     }
  188.     /**
  189.      * @Route("/ucm-pi", name="ucm-pi", options={"sitemap" = true})
  190.      *
  191.      * @return Response
  192.      */
  193.     public function ucmPiAction()
  194.     {
  195.         $mainNav $this->em->getRepository(Pages::class)->findPubMainNavPageNames();
  196.         $otherNav $this->em->getRepository(Pages::class)->findPubOtherNavPageNames();
  197.         $page $this->em->getRepository(Pages::class)->findOneBy(['name' => 'UCM-Pi']);
  198.         return $this->render('web/Default/product.html.twig', [
  199.             'route' => 'ucm-pi',
  200.             'page' => $page,
  201.             'sitewide' => $this->sitewide,
  202.             'mainNav' => $mainNav,
  203.             'otherNav' => $otherNav,
  204.         ]);
  205.     }
  206.     /**
  207.      * @Route("/actionable-intelligence", name="actionable-intelligence", options={"sitemap" = false})
  208.      *
  209.      * @return Response
  210.      */
  211.     public function actionableIntelligenceAction()
  212.     {
  213.         $mainNav $this->em->getRepository(Pages::class)->findPubMainNavPageNames();
  214.         $otherNav $this->em->getRepository(Pages::class)->findPubOtherNavPageNames();
  215.         $page $this->em->getRepository(Pages::class)->findOneBy(['name' => 'Actionable Intelligence']);
  216.         return $this->render('web/Default/product.html.twig', [
  217.             'route' => 'actionable-intelligence',
  218.             'page' => $page,
  219.             'sitewide' => $this->sitewide,
  220.             'mainNav' => $mainNav,
  221.             'otherNav' => $otherNav,
  222.         ]);
  223.     }
  224.     /**
  225.      * @Route("/projects", name="projects", options={"sitemap" = false})
  226.      * @return Response
  227.      */
  228.     public function projectsAction()
  229.     {
  230.         $about null;
  231.         return $this->render('web/Default/projects.html.twig',
  232.             [
  233.                 'about' => $about,
  234.             ]
  235.         );
  236.     }
  237.     protected function sendEmail(
  238.         FormInterface $formEmail,
  239.         MailerInterface $mailer
  240.     ): bool {
  241.         $toName 'Ben Stinton';
  242.         $toEmail 'ben.stinton@alphawerk.co.uk';
  243.         $subject 'alphaWerk website enquiry';
  244.         $companyName $formEmail['companyName']->getData();
  245.         $firstName $formEmail['firstName']->getData();
  246.         $lastName $formEmail['lastName']->getData();
  247.         $fromName $firstName ' ' $lastName;
  248.         $fromEmail $formEmail['email']->getData();
  249.         $body $formEmail['body']->getData();
  250.         $requirement $formEmail['requirement']->getData();
  251.         $prefix = (!empty($companyName)) ? $fromName ' @ ' $companyName $fromName;
  252.         $email = (new Email())
  253.             ->from($toEmail)
  254.             ->to(new Address($toEmail$toName))
  255.             ->replyTo(new Address($fromEmail$fromName))
  256.             ->subject($subject)
  257.             ->text($this->renderView(
  258.                 'web/Email/email.txt.twig', [
  259.                     'message' => strip_tags($body),
  260.                     'prefix' => $prefix,
  261.                     'requirement' => $requirement
  262.                 ]
  263.             ))
  264.             ->html($this->renderView(
  265.                 'web/Email/email.html.twig', [
  266.                     'message' => $body,
  267.                     'prefix' => $prefix,
  268.                     'requirement' => $requirement
  269.                 ]
  270.             ));
  271.         $mailer->send($email);
  272.         return true;
  273.     }
  274.     private function getErrorMessages(
  275.         FormInterface $form
  276.     ): array {
  277.         $errors = [];
  278.         foreach ($form->getErrors() as $error) {
  279.             $errors[] = $error->getMessage();
  280.         }
  281.         foreach ($form->all() as $key => $child) {
  282.             if ($err $child->getErrors()) {
  283.                 $errors[$key] = $err;
  284.             }
  285.         }
  286.         return $errors;
  287.     }
  288.     /**
  289.      * @param $recaptchaResponse
  290.      * @param $remoteIp
  291.      *
  292.      * @return mixed
  293.      */
  294.     public function areTheyARobot(
  295.         $recaptchaResponse,
  296.         $remoteIp
  297.     )
  298.     {
  299.         $secret '6Ld9Du4UAAAAAMPap_rwFSCi7eRoRArnOCbXsFNo';
  300.         $response file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" $secret "&response=" $recaptchaResponse "&remoteip=" $remoteIp);
  301.         $obj json_decode($response);
  302.         return $obj->success;
  303.     }
  304.     private function getEmailFormOptions(array $products): array
  305.     {
  306.         $choices = [];
  307.         /** @var Products $product */
  308.         foreach ($products as $product) {
  309.             $choices[$product->getName()] = $product->getName();
  310.         }
  311.         $choices['All of the above'] = 'All of the above';
  312.         return [
  313.             'action' => $this->generateUrl('alpha_werk_web_email'),
  314.             'requirement_choices' => $choices,
  315.         ];
  316.     }
  317. }