src/Controller/InscriptionController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use App\Form\FicheProspectType;
  8. use App\Entity\FicheProspect;
  9. use App\Service\MailMessageFactory;
  10. use Swift_Mailer;
  11. class InscriptionController extends AbstractController
  12. {
  13.     /** @var MailMessageFactory */
  14.     private $mailMessageFactory;
  15.     /** @var Swift_Mailer */
  16.     private $mailer;
  17.     public function __construct(
  18.         Swift_Mailer $mailer,
  19.         MailMessageFactory $mailMessageFactory
  20.     ) {
  21.         $this->mailer $mailer;
  22.         $this->mailMessageFactory $mailMessageFactory;
  23.     }
  24.     /**
  25.      * @Route("/inscription", name="inscription")
  26.      */
  27.     public function index(Request $request)
  28.     {
  29.         /*
  30.         Si case cochée : g.marti@onpc.fr avec copie technique-aep@onpc.fr
  31.         Si non cochée : technique-aep@onpc.fr
  32.         */
  33.         $prospect = new FicheProspect();
  34.         $success false;
  35.         $form $this->createForm(FicheProspectType::class, $prospect);
  36.         $form->handleRequest($request);
  37.         if ($form->isSubmitted() && $form->isValid() && empty($form->get("email2")->getData())) {
  38.             // Envoi email
  39.             $mailHtml $this->renderView('emails/inscription.html.twig', ['prospect' => $prospect]);
  40.             $mailTxt $this->renderView('emails/inscription.txt.twig', ['prospect' => $prospect]);
  41.             $mailTo $prospect->getAutoriseContact()
  42.                 ? $to = array('g.marti@onpc.fr''technique-aep@onpc.fr')
  43.                 : $to 'technique-aep@onpc.fr';
  44.             $mail $this
  45.                 ->mailMessageFactory
  46.                 ->create()
  47.                 ->setSubject('Demande d\'inscription')
  48.                 ->setTo($mailTo)
  49.                 ->setReplyTo($prospect->getEmail(), $prospect->getResponsable())
  50.                 ->setBody($mailHtml'text/html')
  51.                 ->addPart(str_replace("\n""\r\n"$mailTxt), 'text/plain');
  52.             $this->mailer->send($mail);
  53.             $this->addFlash('success''Vos informations ont été envoyées');
  54.             $success true;
  55.             $prospect null;
  56.             $form $this->createForm(FicheProspectType::class, $prospect);
  57.         }
  58.         return $this->render('inscription/index.html.twig', [
  59.             'form' => $form->createView(),
  60.             'success' => $success
  61.         ]);
  62.     }
  63. }