src/Controller/Contact/CommercialController.php line 95

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Contact;
  3. use App\Entity\Contact;
  4. use App\Entity\Emails\Emails;
  5. use App\Entity\Emails\GeneratorMail;
  6. use App\Form\Contact\CommercialType;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  9. use Symfony\Component\String\ByteString;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. class CommercialController extends AbstractController
  17. {
  18.     protected $em;
  19.     protected $verifyEmailHelper;
  20.     public function __construct(EntityManagerInterface $entityManager)
  21.     {
  22.         $this->em $entityManager;
  23.     }
  24.     /**
  25.      * @Route("/send/commercial-contact", name="send-commercial")
  26.      */
  27.     public function sendCommercialContact(Request $requestMailerInterface $mailer): Response
  28.     {
  29.         $success false;
  30.         $contact = new Contact;
  31.         $form $this->createForm(CommercialType::class, $contact);
  32.         $form->handleRequest($request);
  33.         try{
  34.             if($form->isSubmitted() && $form->isValid()){
  35.                 $contact->setHashTalk((new ByteString)->fromRandom(20)->toString());
  36.                 $this->em->persist($contact);
  37.                 $this->em->flush();
  38.                 $form $this->createForm(CommercialType::class, new Contact);
  39.                 $success true;
  40.                 $emailTo "service-commercial@sport-adhesion.fr";
  41.                 extract(["prenom" => $contact->getName(),"nom" => $contact->getLastName(), "compagny" => $contact->getCompagnyName(),
  42.                     "email" => $contact->getEmail(), "structure"=> $contact->getCompagnyType(),"phone" => $contact->getPhone(), "getMessage"  => $contact->getMessage()
  43.                 ]);
  44.                 $message = <<<HTML
  45.                     <p style="text-align:center">
  46.                         <b>Prénom :</b> $prenom
  47.                         <br/>
  48.                         <b>Nom :</b> $nom
  49.                         <br/>
  50.                         <b>Compagnie :</b> $compagny
  51.                         <br/>
  52.                         <b>Structure :</b> $structure
  53.                         <br/>
  54.                         <b>E-mail :</b> $email
  55.                         <br/>
  56.                         <b>Téléphone :</b> $phone
  57.                         <br/><br/>
  58.                     </p>
  59.                     <p style="text-align:left">
  60.                         <span style="color:#00a5db;font-weight:bold;">Informations :</span> Leads [URGENT] depuis le site internet.
  61.                         <br/>
  62.                         <span style="color:#00a5db;font-weight:bold;">Type :</span> Service commercial
  63.                         <br/><br/>
  64.                         <span style="font-weight:bold;color:#00a5db;width:100%;text-align:center;">Message</span>
  65.                         <br/><br/>
  66.                         $getMessage
  67.                     </p>
  68.                 HTML;
  69.                 $configMail $this->em->getRepository(Emails::class)->findOneBy(["template_name" => "default""category" => "commercial"]);
  70.                 if(!empty($configMail)){
  71.                     $emailBody = (new GeneratorMail(new TemplatedEmail$configMail))->setMail(
  72.                         $emailTo,
  73.                         [
  74.                             'mail'              => $emailTo,
  75.                             'compagny_name'     => $contact->getCompagnyName(),
  76.                             'message'           => $message
  77.                         ]
  78.                     );
  79.                     $mailer->send($emailBody);
  80.                 }
  81.             }
  82.         }catch(\Exception $e){
  83.             $success false;
  84.         }
  85.         return new JsonResponse($success);
  86.     }
  87.     
  88.     public function index(Request $request): Response
  89.     {
  90.         $contact = new Contact;
  91.         $form $this->createForm(CommercialType::class, $contact);
  92.         return $this->render('contact/commercial/index.html.twig', [
  93.             'form'          => $form->createView()
  94.         ]);
  95.     }
  96. }