src/Controller/ContactController.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Quotation;
  4. //use App\Form\QuotationType;
  5. use App\Form\QuotationType;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Mailer\Mailer;
  10. use Symfony\Component\Mailer\MailerInterface;
  11. use Symfony\Component\Mime\Address;
  12. use Symfony\Component\Mime\Email;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use App\Repository\QuotationRepository;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  17. //use Doctrine\Common\Persistence\ObjectManager;
  18. class ContactController extends AbstractController
  19. {
  20.     /**
  21.      * @var QuotationRepository
  22.      */
  23.     private $repository;
  24.     public function __construct(QuotationRepository $repositoryEntityManagerInterface $em)
  25.     {
  26.         $this->repository $repository;
  27.         $this->em $em;
  28.     }
  29.     /**
  30.      * @Route("/contact", name="contact")
  31.      */
  32.     public function index()
  33.     {
  34.         return $this->render('quotation/index.html.twig', [
  35.             'controller_name' => 'QuotationController',
  36.         ]);
  37.     }
  38.     /**
  39.      * @Route("/contact/create", name="contact_create")
  40.      *
  41.      */
  42.     public function new(Request $requestMailerInterface $mailer)
  43.     {
  44.         $quotation = new Quotation();
  45.         $form $this->createForm(QuotationType::class, $quotation);
  46.         $form->handleRequest($request);
  47.         if ($form->isSubmitted() && $form->isValid()) {
  48.             $quotation->setRequestDate(new \DateTime('now'));
  49.             $this->em->persist($quotation);
  50.             $this->em->flush();
  51.             $this->addFlash('success''Votre demande a bien été envoyée');
  52.             $adminEmail 'contact@puush.io';
  53.             $useremail $quotation->getEmail();
  54.             $company $quotation->getCompany();
  55.             $firstname $quotation->getFirstname();
  56.             $lastname $quotation->getLastname();
  57.             $message $quotation->getMessage();
  58.             $phone $quotation->getPhone();
  59.             $email = (new TemplatedEmail())
  60.                 ->from('contact@puush.io')
  61.                 ->to(new Address($useremail))
  62.                 ->bcc($adminEmail)
  63.                 ->subject('Votre demande à Puush')
  64.                 ->htmlTemplate('/emails/quotation.html.twig')
  65.                 ->context([
  66.                     'expiration_date' => new \DateTime('+7 days'),
  67.                     'company' => $company,
  68.                     'firstname' => $firstname,
  69.                     'lastname' => $lastname,
  70.                     'message' => $message,
  71.                     'useremail' => $useremail,
  72.                     'phone' => $phone,
  73.                 ])
  74.             ;
  75.             $mailer->send($email);
  76.             return $this->redirectToRoute('home');
  77.         } elseif ($form->isSubmitted() && !($form->isValid())) {
  78.             $this->addFlash('error''Votre formulaire comporte une erreur');
  79.         }
  80.         return $this->render('contact/contact.html.twig', [
  81.             'contactForm' => $form->createView()
  82.         ]);
  83.     }
  84. }