<?php
namespace App\Controller\Web;
use App\Entity\Pages;
use App\Entity\Products;
use App\Entity\Sitewide;
use App\Form\Type\EmailType;
use App\Repository\PagesRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;
class WebController extends AbstractController
{
/**
* @var Sitewide|object|null
*/
private $sitewide;
/**
* @var EntityManagerInterface
*/
private $em;
/**
* DefaultController constructor.
*
* @param EntityManagerInterface $em
*/
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
$this->sitewide = $this->em->getRepository(Sitewide::class)->findOneBy([], ['id' => 'asc']);
}
/**
* @Route("/", name="home", options={"sitemap" = {"priority" = 1}})
* @Route("/about/", name="about")
* @Route("/services/", name="about")
* @Route("/how-we-work/", name="how-we-work")
* @Route("/technologies/")
* @return Response
*/
public function indexAction(): Response
{
/** @var PagesRepository $pagesRepo */
$pagesRepo = $this->em->getRepository(Pages::class);
$mainNav = $pagesRepo->findPubMainNavPageNames();
$otherNav = $pagesRepo->findPubOtherNavPageNames();
$servicesSection = $pagesRepo->findOneByName('Services');
$home = $pagesRepo->findOneByName('Home');
$projects = $pagesRepo->findOneByName('Projects');
$blog = $pagesRepo->findOneByName('Blog');
$howWeWork = $pagesRepo->findOneByName('How We Work');
$services = $this->em->getRepository(Products::class)->findBy([
'publish' => true,
'category' => 'service'
], [
'position' => 'asc',
]);
$formEmail = $this->createForm(EmailType::class, null, $this->getEmailFormOptions($services));
return $this->render('web/Default/index.html.twig', [
'mainNav' => $mainNav,
'otherNav' => $otherNav,
'servicesSection' => $servicesSection,
'home' => $home,
'projects' => $projects,
'blog' => $blog,
'howWeWork' => $howWeWork,
'services' => $services,
'formEmail' => $formEmail->createView(),
]);
}
/**
* @Route("/expertise", name="expertise", options={"sitemap" = false})
*/
public function expertiseAction(): Response
{
$mainNav = $this->em->getRepository(Pages::class)->findPubMainNavPageNames();
$otherNav = $this->em->getRepository(Pages::class)->findPubOtherNavPageNames();
$page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'Expertise']);
return $this->render('web/Default/product.html.twig', [
'route' => 'expertise',
'page' => $page,
'sitewide' => $this->sitewide,
'mainNav' => $mainNav,
'otherNav' => $otherNav,
]);
}
/**
* @Route("/products", name="products", options={"sitemap" = false})
*/
public function productsAction(): Response
{
$mainNav = $this->em->getRepository(Pages::class)->findPubMainNavPageNames();
$otherNav = $this->em->getRepository(Pages::class)->findPubOtherNavPageNames();
$page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'Products']);
return $this->render('web/Default/product.html.twig', [
'route' => 'admin_products',
'page' => $page,
'sitewide' => $this->sitewide,
'mainNav' => $mainNav,
'otherNav' => $otherNav,
]);
}
/**
* @Route("/features", name="features", options={"sitemap" = false})
*/
public function featuresAction(): Response
{
$mainNav = $this->em->getRepository(Pages::class)->findPubMainNavPageNames();
$otherNav = $this->em->getRepository(Pages::class)->findPubOtherNavPageNames();
$page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'Features']);
return $this->render('web/Default/product.html.twig', [
'route' => 'features',
'page' => $page,
'sitewide' => $this->sitewide,
'mainNav' => $mainNav,
'otherNav' => $otherNav,
]);
}
/**
* @Route("/symfony", name="symfony", options={"sitemap" = false})
*/
public function symfonyAction(): Response
{
$mainNav = $this->em->getRepository(Pages::class)->findPubMainNavPageNames();
$otherNav = $this->em->getRepository(Pages::class)->findPubOtherNavPageNames();
$page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'Symfony']);
return $this->render('web/Default/product.html.twig', [
'route' => 'symfony',
'page' => $page,
'sitewide' => $this->sitewide,
'mainNav' => $mainNav,
'otherNav' => $otherNav,
]);
}
/**
* @Route("/email", name="alpha_werk_web_email")
* @param Request $request
* @param MailerInterface $mailer
*
* @return JsonResponse|RedirectResponse
*/
public function emailAction(Request $request, MailerInterface $mailer)
{
$products = $this->em->getRepository(Products::class)->findBy([
'publish' => true,
], [
'position' => 'asc',
]);
$formEmail = $this->createForm(EmailType::class, null, $this->getEmailFormOptions($products));
if ($request->isMethod('POST')) {
$formEmail->handleRequest($request);
if ($formEmail->isSubmitted() && $formEmail->isValid()) {
$recapthcaResponse = $request->request->get('g-recaptcha-response');
if (true === $this->areTheyARobot($recapthcaResponse, $request->getClientIp())) {
$this->sendEmail($formEmail, $mailer);
return $this->redirect($this->generateUrl('alpha_werk_web_email'));
} else {
return new JsonResponse(
[
'errors' => 'You are a robot!',
'success' => false,
]
);
}
} else {
return new JsonResponse(
[
'errors' => implode('</br>', $this->getErrorMessages($formEmail)),
'success' => false,
]
);
}
} else {
return new JsonResponse(
[
'errors' => null,
'success' => true,
]
);
}
}
/**
* @Route("/ucm-pi", name="ucm-pi", options={"sitemap" = true})
*
* @return Response
*/
public function ucmPiAction()
{
$mainNav = $this->em->getRepository(Pages::class)->findPubMainNavPageNames();
$otherNav = $this->em->getRepository(Pages::class)->findPubOtherNavPageNames();
$page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'UCM-Pi']);
return $this->render('web/Default/product.html.twig', [
'route' => 'ucm-pi',
'page' => $page,
'sitewide' => $this->sitewide,
'mainNav' => $mainNav,
'otherNav' => $otherNav,
]);
}
/**
* @Route("/actionable-intelligence", name="actionable-intelligence", options={"sitemap" = false})
*
* @return Response
*/
public function actionableIntelligenceAction()
{
$mainNav = $this->em->getRepository(Pages::class)->findPubMainNavPageNames();
$otherNav = $this->em->getRepository(Pages::class)->findPubOtherNavPageNames();
$page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'Actionable Intelligence']);
return $this->render('web/Default/product.html.twig', [
'route' => 'actionable-intelligence',
'page' => $page,
'sitewide' => $this->sitewide,
'mainNav' => $mainNav,
'otherNav' => $otherNav,
]);
}
/**
* @Route("/projects", name="projects", options={"sitemap" = false})
* @return Response
*/
public function projectsAction()
{
$about = null;
return $this->render('web/Default/projects.html.twig',
[
'about' => $about,
]
);
}
protected function sendEmail(
FormInterface $formEmail,
MailerInterface $mailer
): bool {
$toName = 'Ben Stinton';
$toEmail = 'ben.stinton@alphawerk.co.uk';
$subject = 'alphaWerk website enquiry';
$companyName = $formEmail['companyName']->getData();
$firstName = $formEmail['firstName']->getData();
$lastName = $formEmail['lastName']->getData();
$fromName = $firstName . ' ' . $lastName;
$fromEmail = $formEmail['email']->getData();
$body = $formEmail['body']->getData();
$requirement = $formEmail['requirement']->getData();
$prefix = (!empty($companyName)) ? $fromName . ' @ ' . $companyName : $fromName;
$email = (new Email())
->from($toEmail)
->to(new Address($toEmail, $toName))
->replyTo(new Address($fromEmail, $fromName))
->subject($subject)
->text($this->renderView(
'web/Email/email.txt.twig', [
'message' => strip_tags($body),
'prefix' => $prefix,
'requirement' => $requirement
]
))
->html($this->renderView(
'web/Email/email.html.twig', [
'message' => $body,
'prefix' => $prefix,
'requirement' => $requirement
]
));
$mailer->send($email);
return true;
}
private function getErrorMessages(
FormInterface $form
): array {
$errors = [];
foreach ($form->getErrors() as $error) {
$errors[] = $error->getMessage();
}
foreach ($form->all() as $key => $child) {
if ($err = $child->getErrors()) {
$errors[$key] = $err;
}
}
return $errors;
}
/**
* @param $recaptchaResponse
* @param $remoteIp
*
* @return mixed
*/
public function areTheyARobot(
$recaptchaResponse,
$remoteIp
)
{
$secret = '6Ld9Du4UAAAAAMPap_rwFSCi7eRoRArnOCbXsFNo';
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" . $secret . "&response=" . $recaptchaResponse . "&remoteip=" . $remoteIp);
$obj = json_decode($response);
return $obj->success;
}
private function getEmailFormOptions(array $products): array
{
$choices = [];
/** @var Products $product */
foreach ($products as $product) {
$choices[$product->getName()] = $product->getName();
}
$choices['All of the above'] = 'All of the above';
return [
'action' => $this->generateUrl('alpha_werk_web_email'),
'requirement_choices' => $choices,
];
}
}