<?php
namespace App\Controller\Front;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mime\Address;
use App\Repository\Banner\BannerRepository;
use App\Form\Contact\ContactType;
class ContactController extends AbstractController
{
/** @var BannerRepository */
private $bannerRepository;
/** @var MailerInterface */
private $mailer;
/** @var Session */
private $session;
/** @var TranslatorInterface */
private $translator;
/** @var string */
private $theme;
/**
* @param BannerRepository $bannerRepository
* @param Session $session
*/
public function __construct(
BannerRepository $bannerRepository,
MailerInterface $mailer,
SessionInterface $session,
TranslatorInterface $translator
) {
$this->bannerRepository = $bannerRepository;
$this->mailer = $mailer;
$this->session = $session;
$this->translator = $translator;
$this->theme = $this->session->get('theme', 'horse');
}
public function index(Request $request): Response
{
$contactMail = $this->getParameter('contact_email');
$banner = $this->bannerRepository->findOneBy(['type' => $this->theme]);
$form = $this->createForm(ContactType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
extract($data);
$templatedEmail = new TemplatedEmail();
$from = new Address($contactMail, 'Ranch Family Horse');
$emailSubject = $this->translator->trans('app.contact.email.subject');
$email = $templatedEmail->from($from)
->to($contactMail)
->subject($emailSubject)
->htmlTemplate('front/contact/mail.html.twig')
->context(compact('name', 'contactEmail', 'subject', 'message', 'contactMail'));
$this->mailer->send($email);
$this->session->getFlashBag()->add('success', $this->translator->trans('app.contact.messages.sent'));
}
$contactForm = $form->createView();
return $this->render('front/contact/index.html.twig', compact('contactForm', 'banner'));
}
}