src/Controller/ContactController.php line 78

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\Type\ContactAgenceType;
  4. use Doctrine\Persistence\ManagerRegistry;
  5. use Sylius\Bundle\CoreBundle\Doctrine\ORM\UserRepository;
  6. use Sylius\Bundle\UserBundle\Mailer\Emails;
  7. use Sylius\Component\Channel\Context\ChannelContextInterface;
  8. use Sylius\Component\Mailer\Sender\SenderInterface;
  9. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\Form\FormFactoryInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  15. use Symfony\Component\Mailer\MailerInterface;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. class ContactController extends AbstractController
  18. {
  19.     private MailerInterface         $mailer;
  20.     private ChannelContextInterface $context;
  21.     private ManagerRegistry         $managerRegistry;
  22.     public function __construct(MailerInterface         $mailer,
  23.                                 ChannelContextInterface $context,
  24.                                 ManagerRegistry         $managerRegistry
  25.     )
  26.     {
  27.         $this->context $context;
  28.         $this->mailer $mailer;
  29.         $this->managerRegistry $managerRegistry;
  30.     }
  31.     //    /**
  32.     //     * @Route("/nos-agences/", name="contact_agences")
  33.     //     */
  34.     //    public function agences(Request $request)
  35.     //    {
  36.     //        $repository = $this->managerRegistry->getRepository(Contact::class);
  37.     //
  38.     //        $contacts = $repository->findContactsAgences();
  39.     //
  40.     //        $form_contact = $this->createForm(ContactAgenceType::class);
  41.     //
  42.     //        $form_contact->handleRequest($request);
  43.     //        if ($form_contact->isSubmitted() && $form_contact->isValid()) {
  44.     //            $data = $form_contact->getData();
  45.     //            $message = (new TemplatedEmail())
  46.     //                ->to($data['destinataire']['contact']->getEmail())
  47.     //                ->subject('Contact via site Internet - ' . ucfirst($this->context->getChannel()->getName()))
  48.     //                ->from($_ENV['EMAIL_CONTACT'])
  49.     //                ->replyTo($data['formulaire']['Email'])
  50.     //                ->htmlTemplate('contact/contact_form.html.twig')
  51.     //                ->context(['data' => $data['formulaire'], 'channel' => $this->context->getChannel()])
  52.     //            ;
  53.     //            try {
  54.     //                $this->mailer->send($message);
  55.     //                return $this->redirectToRoute('contact_merci');
  56.     //            }
  57.     //            catch (TransportExceptionInterface $e) {
  58.     //                return $this->redirectToRoute('contact_agences');
  59.     //            }
  60.     //        }
  61.     //
  62.     //
  63.     //        return $this->render('agences/index.html.twig', [
  64.     //            'contacts' => $contacts,
  65.     //            'form'     => $form_contact->createView(),
  66.     //        ]);
  67.     //    }
  68.     /**
  69.      * @Route("/contact/", name="contact", priority="1000000")
  70.      */
  71.     public function index(Request $requestFormFactoryInterface $formFactory)
  72.     {
  73.         $form_contact $formFactory->createNamed('contact_form'ContactAgenceType::class, NULL, [
  74.             'contactAgence' => NULL,
  75.         ]);
  76.         $form_contact->handleRequest($request);
  77.         if ($form_contact->isSubmitted() && $form_contact->isValid()) {
  78.             $data $form_contact->getData();
  79.             $message = (new TemplatedEmail())->subject(
  80.                 'Contact via site Internet - ' ucfirst(
  81.                     $this->context->getChannel()
  82.                         ->getName()
  83.                 )
  84.             )
  85.                 ->to($this->context->getChannel()->getContactEmail())
  86.                 ->subject('Contact via site Internet - ' ucfirst($this->context->getChannel()->getName()))
  87.                 ->from($_ENV['EMAIL_CONTACT'])
  88.                 ->replyTo($data['formulaire']['Email'])
  89.                 ->htmlTemplate('contact/contact_form.html.twig')
  90.                 ->context(['data' => $data['formulaire'], 'channel' => $this->context->getChannel()])
  91.             ;
  92.             try {
  93.                 $this->mailer->send($message);
  94.                 return $this->redirectToRoute('contact_merci');
  95.             }
  96.             catch (TransportExceptionInterface $e) {
  97.                 return $this->redirectToRoute('contact');
  98.             }
  99.         }
  100.         return $this->render('contact/index.html.twig', [
  101.             'form_contact' => $form_contact->createView()
  102.         ]);
  103.     }
  104.     /**
  105.      * @Route("/contact/message-envoye/", name="contact_merci")
  106.      */
  107.     public function merci()
  108.     {
  109.         return $this->render('contact/merci.html.twig');
  110.     }
  111.     /**
  112.      * @Route("/emails/test/", name="email_test_dev")
  113.      */
  114.     public function testEmail(SenderInterface $senderUserRepository $userRepositoryChannelContextInterface $context)
  115.     {
  116.         $user $userRepository->findOneByEmail('v.adomian@tradenart.com');
  117.         $channel $context->getChannel();
  118.         $user->setPasswordResetToken('123456');
  119.         $sender->send(Emails::RESET_PASSWORD_TOKEN, ['v.adomian@tradenart.com'], [
  120.             'localeCode' => 'fr',
  121.             'user'       => $user,
  122.             'channel'    => $channel
  123.         ]);
  124.         return new Response('Email envoyé');
  125.     }
  126. }