<?php
namespace App\Controller;
use App\Form\ContactType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Service\Api;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
class ContactController extends AbstractController
{
/**
* @Route("/contact", name="contact")
* @param Request $request
* @param MailerInterface $mailer
* @return Response
*/
public function index(Request $request, MailerInterface $mailer, TranslatorInterface $translator, EntityManagerInterface $manager, Api $apiService): Response
{
$form = $this->createForm(ContactType::class);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$email = (new Email())
->from(new Address($this->getParameter('app.contactEmail'), 'CONTACT FORM'))
->to(new Address($this->getParameter('app.supportEmail')))
->subject($form->getData()['subject'])
->html(
$this->renderView(
'emails/contact-us.html.twig',
['contact' => $form->getData()]
)
);
$mailer->send($email);
$this->addFlash('success', $translator->trans("Your message has been sent successfully, one of our experts will contact you as soon as possible. Thank you for your trust."));
return $this->redirect($request->getUri());
} else {
$this->addFlash("danger", $translator->trans("An error has occurred while sending your request! please check the form for errors and try again."));
}
}
$data['contactForm'] = $form->createView();
$data['services'] = $apiService->getAllServices();
$data['icons'] = $apiService->getServicesIcons();
return $this->render('contact/index.html.twig', $data);
}
}