src/Controller/ContactController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Contact;
  4. use App\Repository\ContactConfigRepository;
  5. use App\Service\Mail;
  6. use App\Service\SiteConfig;
  7. use App\Form\ContactType;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use Symfony\Contracts\HttpClient\HttpClientInterface;
  15. class ContactController extends AbstractController
  16. {
  17.     public function __construct(
  18.         private EntityManagerInterface $em,
  19.         private SiteConfig $config,
  20.         private TranslatorInterface $translator,
  21.         private Mail $mail,
  22.         private ContactConfigRepository $contactConfigRepo,
  23.     ) {}
  24.     /**
  25.      * @Route("/contact", name="contact")
  26.      */
  27.     public function new(Request $request): Response
  28.     {
  29.         if ($this->config->isContactActivated() === false)
  30.             return $this->redirectToRoute('not_found_url');
  31.         if (!$this->getUser() && !$this->config->isPublicFrontOffice())
  32.             return $this->redirectToRoute('app_login');
  33.         $contact = new Contact();
  34.         $contact->setCreatedAt(new \DateTime('now'));
  35.         $message_send false;
  36.         $message_error false;
  37.         $message $this->translator->trans('contact.flash-message.contact.done');
  38.         $form $this->createForm(ContactType::class, $contact);
  39.         $form->handleRequest($request);
  40.         if ($form->isSubmitted() && $form->isValid()) {
  41.             $contact $form->getData();
  42.             $this->em->persist($contact);
  43.             $this->em->flush();
  44.             $contactConfig $this->contactConfigRepo->findOneBy(['isActivated' => true]);
  45.             if ($contactConfig) {
  46.                 $message $contactConfig->getMessage();
  47.             }
  48.             $this->addFlash(
  49.                 'success',
  50.                 $message
  51.             );
  52.             $subject "{$contact->getSubject()} [{$contact->getEmail()}]";
  53.             $content "{$contact->getName()} [{$contact->getEmail()}]\n{$contact->getMessage()}";
  54.             $this->mail->sendingblue_email($subject$content$this->config->getContactTemplateId());
  55.             $message_send true;
  56.             return $this->redirectToRoute('contact');
  57.         }
  58.         return $this->render('contact/index.html.twig', [
  59.             'form' => $form->createView(),
  60.             'contact_info' => $this->config->getContactInfo(),
  61.             'staticNav' => false,
  62.             'message_send' => $message_send,
  63.             'message_error' =>  $message_error,
  64.             'message' => $message
  65.         ]);
  66.     }
  67. }