<?php
namespace App\Controller;
use App\Entity\Contact;
use App\Entity\Category;
use App\Form\ContactType;
use App\Service\ConfigsService;
use App\Controller\AppController;
use Symfony\Component\Mime\Address;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ContactController extends AppController
{
/**
* @Route("/{_locale<%app.supported_locales%>}/contact", name="contact_index")
*/
public function index(Request $request, MailerInterface $mailer, ConfigsService $configsService): Response
{
$contact = new Contact;
$form = $this->createForm(ContactType::class, $contact);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/**
* @var ContactSlideRepository
*/
$repo = $this->em->getRepository(Contact::class);
$mailService = $repo->getMailService($contact->getService());
$this->em->persist($contact);
$this->em->flush();
$email = new TemplatedEmail();
$email
// ->from(new Address($configsService->getConfig('MAILCONTACT', 'From'), $configsService->getConfig('MAILCONTACT', 'Label')))
->from(new Address($mailService, $configsService->getConfig('MAILCONTACT', 'Label')))
->to($contact->getEmail())
->cc($configsService->getConfig('MAILCONTACT', 'From'))
->subject($configsService->getConfig('MAILCONTACT', 'Subject'))
->text($configsService->getConfig('MAILCONTACT', 'Subject'))
->htmlTemplate('emails/' . $this->getParameter('PROJECT') . '/contact.html.twig')
->context([
'contact' => $contact
]);
$mailer->send($email);
$successMessage = $this->translator->trans('messageSent', [], 'commun');
$this->addFlash('success', $successMessage);
return $this->redirectToRoute('contact_index');
}
/**
* @var CategoryRepository
*/
$categoryRepository = $this->em->getRepository(Category::class);
$category = $categoryRepository->findOneBy([
'formContact' => 1
]);
$formView = $form->createView();
return $this->render('contact/index.html.twig', [
'controller_name' => 'Contact',
'formView' => $formView,
'category' => $category
]);
}
}