<?php
namespace App\Controller;
use App\Form\Type\ContactAgenceType;
use Doctrine\Persistence\ManagerRegistry;
use Sylius\Bundle\CoreBundle\Doctrine\ORM\UserRepository;
use Sylius\Bundle\UserBundle\Mailer\Emails;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Mailer\Sender\SenderInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Annotation\Route;
class ContactController extends AbstractController
{
private MailerInterface $mailer;
private ChannelContextInterface $context;
private ManagerRegistry $managerRegistry;
public function __construct(MailerInterface $mailer,
ChannelContextInterface $context,
ManagerRegistry $managerRegistry
)
{
$this->context = $context;
$this->mailer = $mailer;
$this->managerRegistry = $managerRegistry;
}
// /**
// * @Route("/nos-agences/", name="contact_agences")
// */
// public function agences(Request $request)
// {
// $repository = $this->managerRegistry->getRepository(Contact::class);
//
// $contacts = $repository->findContactsAgences();
//
// $form_contact = $this->createForm(ContactAgenceType::class);
//
// $form_contact->handleRequest($request);
// if ($form_contact->isSubmitted() && $form_contact->isValid()) {
// $data = $form_contact->getData();
// $message = (new TemplatedEmail())
// ->to($data['destinataire']['contact']->getEmail())
// ->subject('Contact via site Internet - ' . ucfirst($this->context->getChannel()->getName()))
// ->from($_ENV['EMAIL_CONTACT'])
// ->replyTo($data['formulaire']['Email'])
// ->htmlTemplate('contact/contact_form.html.twig')
// ->context(['data' => $data['formulaire'], 'channel' => $this->context->getChannel()])
// ;
// try {
// $this->mailer->send($message);
// return $this->redirectToRoute('contact_merci');
// }
// catch (TransportExceptionInterface $e) {
// return $this->redirectToRoute('contact_agences');
// }
// }
//
//
// return $this->render('agences/index.html.twig', [
// 'contacts' => $contacts,
// 'form' => $form_contact->createView(),
// ]);
// }
/**
* @Route("/contact/", name="contact", priority="1000000")
*/
public function index(Request $request, FormFactoryInterface $formFactory)
{
$form_contact = $formFactory->createNamed('contact_form', ContactAgenceType::class, NULL, [
'contactAgence' => NULL,
]);
$form_contact->handleRequest($request);
if ($form_contact->isSubmitted() && $form_contact->isValid()) {
$data = $form_contact->getData();
$message = (new TemplatedEmail())->subject(
'Contact via site Internet - ' . ucfirst(
$this->context->getChannel()
->getName()
)
)
->to($this->context->getChannel()->getContactEmail())
->subject('Contact via site Internet - ' . ucfirst($this->context->getChannel()->getName()))
->from($_ENV['EMAIL_CONTACT'])
->replyTo($data['formulaire']['Email'])
->htmlTemplate('contact/contact_form.html.twig')
->context(['data' => $data['formulaire'], 'channel' => $this->context->getChannel()])
;
try {
$this->mailer->send($message);
return $this->redirectToRoute('contact_merci');
}
catch (TransportExceptionInterface $e) {
return $this->redirectToRoute('contact');
}
}
return $this->render('contact/index.html.twig', [
'form_contact' => $form_contact->createView()
]);
}
/**
* @Route("/contact/message-envoye/", name="contact_merci")
*/
public function merci()
{
return $this->render('contact/merci.html.twig');
}
/**
* @Route("/emails/test/", name="email_test_dev")
*/
public function testEmail(SenderInterface $sender, UserRepository $userRepository, ChannelContextInterface $context)
{
$user = $userRepository->findOneByEmail('v.adomian@tradenart.com');
$channel = $context->getChannel();
$user->setPasswordResetToken('123456');
$sender->send(Emails::RESET_PASSWORD_TOKEN, ['v.adomian@tradenart.com'], [
'localeCode' => 'fr',
'user' => $user,
'channel' => $channel
]);
return new Response('Email envoyé');
}
}