src/Controller/EstimateController.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Estimate;
  4. use App\Form\EstimateType;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Mailer\MailerInterface;
  12. use Symfony\Component\Mime\Email;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. class EstimateController extends AbstractController
  15. {
  16.     private EntityManagerInterface $em;
  17.     private MailerInterface $mailer;
  18.     public function __construct(EntityManagerInterface $em,
  19.                                 MailerInterface $mailer)
  20.     {
  21.         $this->em $em;
  22.         $this->mailer $mailer;
  23.     }
  24.     #[Route('/devis'name'devis')]
  25.     public function estimate(Request $request): Response
  26.     {
  27.         $estimate = new Estimate();
  28.         $form $this->createForm(EstimateType::class, $estimate);
  29.         $form->handleRequest($request);
  30.         if ($form->isSubmitted() && $form->isValid()) {
  31.             $this->em->persist($estimate);
  32.             $this->em->flush();
  33.             $this->sendEmail($estimate);
  34.             $session $this->get('session');
  35.             $session->set('success'true);
  36.             $session->set('name'$estimate->getName());
  37.             $session->set('firstname'$estimate->getFirstname());
  38.             return $this->redirectToRoute('devis_success');
  39.         }
  40.         return $this->render('frontend/devis.html.twig', [
  41.             'form' => $form->createView(),
  42.             'success' => false
  43.         ]);
  44.     }
  45.     /**
  46.      * @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
  47.      */
  48.     private function sendEmail(Estimate $estimate) {
  49.         $content "<p style=\"text-align:justify;\">";
  50.         $content strtoupper($estimate->getName()) . ' ' ucfirst($estimate->getFirstname());
  51.         $content .= "<br>";
  52.         $content .= $estimate->getPostcode() . " " ucfirst($estimate->getTown());
  53.         $content .= "<br>";
  54.         $content .= $estimate->getEmail();
  55.         $content .= "<br>";
  56.         $content .= $estimate->getPhone();
  57.         $content .= "</p>";
  58.         $content .= "<br>";
  59.         $content .= "<br>";
  60.         $content .= "<p style=\"text-align:justify;text-decoration:underline;padding:0\">Demande :</p>";
  61.         $content .= nl2br($estimate->getDescription());
  62.         $content .= "</p>";
  63.         $email = (new Email())
  64.             ->from('contact@jgaweb.fr')
  65.             ->to('contact@novellisala.fr')
  66.             ->priority(Email::PRIORITY_HIGH)
  67.             ->subject('[' strtoupper($estimate->getName()) . ' ' ucfirst($estimate->getFirstname()) . ' / ' ucfirst($estimate->getTown()). ']' ' Demande de Devis')
  68.             ->html($this->renderView(
  69.                 'frontend/emails/email_devis.html.twig',
  70.                 [
  71.                     "content" => $content
  72.                 ]
  73.             ));
  74.         $this->mailer->send($email);
  75.     }
  76.     #[Route('/devis-success'name'devis_success')]
  77.     public function estimateSuccess(): Response
  78.     {
  79.         $session $this->get('session');
  80.         if (!$session->get('success')) {
  81.             return $this->redirectToRoute('home');
  82.         }
  83.         $session->set('success'false);
  84.         return $this->render('frontend/devis_success.html.twig', [
  85.             'success' => true,
  86.             'firstname' => $session->get('firstname'),
  87.             'name' => $session->get('name')
  88.         ]);
  89.     }
  90. }