<?php
namespace App\Controller;
use App\Entity\Contact;
use App\Repository\ContactConfigRepository;
use App\Service\Mail;
use App\Service\SiteConfig;
use App\Form\ContactType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class ContactController extends AbstractController
{
public function __construct(
private EntityManagerInterface $em,
private SiteConfig $config,
private TranslatorInterface $translator,
private Mail $mail,
private ContactConfigRepository $contactConfigRepo,
) {}
/**
* @Route("/contact", name="contact")
*/
public function new(Request $request): Response
{
if ($this->config->isContactActivated() === false)
return $this->redirectToRoute('not_found_url');
if (!$this->getUser() && !$this->config->isPublicFrontOffice())
return $this->redirectToRoute('app_login');
$contact = new Contact();
$contact->setCreatedAt(new \DateTime('now'));
$message_send = false;
$message_error = false;
$message = $this->translator->trans('contact.flash-message.contact.done');
$form = $this->createForm(ContactType::class, $contact);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$contact = $form->getData();
$this->em->persist($contact);
$this->em->flush();
$contactConfig = $this->contactConfigRepo->findOneBy(['isActivated' => true]);
if ($contactConfig) {
$message = $contactConfig->getMessage();
}
$this->addFlash(
'success',
$message
);
$subject = "{$contact->getSubject()} [{$contact->getEmail()}]";
$content = "{$contact->getName()} [{$contact->getEmail()}]\n{$contact->getMessage()}";
$this->mail->sendingblue_email($subject, $content, $this->config->getContactTemplateId());
$message_send = true;
return $this->redirectToRoute('contact');
}
return $this->render('contact/index.html.twig', [
'form' => $form->createView(),
'contact_info' => $this->config->getContactInfo(),
'staticNav' => false,
'message_send' => $message_send,
'message_error' => $message_error,
'message' => $message
]);
}
}