<?php
namespace App\Controller;
use App\Entity\Estimate;
use App\Form\EstimateType;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;
class EstimateController extends AbstractController
{
private EntityManagerInterface $em;
private MailerInterface $mailer;
public function __construct(EntityManagerInterface $em,
MailerInterface $mailer)
{
$this->em = $em;
$this->mailer = $mailer;
}
#[Route('/devis', name: 'devis')]
public function estimate(Request $request): Response
{
$estimate = new Estimate();
$form = $this->createForm(EstimateType::class, $estimate);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->persist($estimate);
$this->em->flush();
$this->sendEmail($estimate);
$session = $this->get('session');
$session->set('success', true);
$session->set('name', $estimate->getName());
$session->set('firstname', $estimate->getFirstname());
return $this->redirectToRoute('devis_success');
}
return $this->render('frontend/devis.html.twig', [
'form' => $form->createView(),
'success' => false
]);
}
/**
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
*/
private function sendEmail(Estimate $estimate) {
$content = "<p style=\"text-align:justify;\">";
$content = strtoupper($estimate->getName()) . ' ' . ucfirst($estimate->getFirstname());
$content .= "<br>";
$content .= $estimate->getPostcode() . " " . ucfirst($estimate->getTown());
$content .= "<br>";
$content .= $estimate->getEmail();
$content .= "<br>";
$content .= $estimate->getPhone();
$content .= "</p>";
$content .= "<br>";
$content .= "<br>";
$content .= "<p style=\"text-align:justify;text-decoration:underline;padding:0\">Demande :</p>";
$content .= nl2br($estimate->getDescription());
$content .= "</p>";
$email = (new Email())
->from('contact@jgaweb.fr')
->to('contact@novellisala.fr')
->priority(Email::PRIORITY_HIGH)
->subject('[' . strtoupper($estimate->getName()) . ' ' . ucfirst($estimate->getFirstname()) . ' / ' . ucfirst($estimate->getTown()). ']' . ' Demande de Devis')
->html($this->renderView(
'frontend/emails/email_devis.html.twig',
[
"content" => $content
]
));
$this->mailer->send($email);
}
#[Route('/devis-success', name: 'devis_success')]
public function estimateSuccess(): Response
{
$session = $this->get('session');
if (!$session->get('success')) {
return $this->redirectToRoute('home');
}
$session->set('success', false);
return $this->render('frontend/devis_success.html.twig', [
'success' => true,
'firstname' => $session->get('firstname'),
'name' => $session->get('name')
]);
}
}