src/Controller/ContactController.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Contact;
  4. use App\Entity\Category;
  5. use App\Form\ContactType;
  6. use App\Service\ConfigsService;
  7. use App\Controller\AppController;
  8. use Symfony\Component\Mime\Address;
  9. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Mailer\MailerInterface;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. class ContactController extends AppController
  15. {
  16.     /**
  17.      * @Route("/{_locale<%app.supported_locales%>}/contact", name="contact_index")
  18.      */
  19.     public function index(Request $requestMailerInterface $mailerConfigsService $configsService): Response
  20.     {
  21.         $contact = new Contact;
  22.         $form $this->createForm(ContactType::class, $contact);
  23.         $form->handleRequest($request);
  24.         if ($form->isSubmitted() && $form->isValid()) {
  25.             /**
  26.              * @var ContactSlideRepository
  27.              */
  28.             $repo $this->em->getRepository(Contact::class);
  29.             $mailService $repo->getMailService($contact->getService());
  30.             $this->em->persist($contact);
  31.             $this->em->flush();
  32.             $email = new TemplatedEmail();
  33.             $email
  34.                 // ->from(new Address($configsService->getConfig('MAILCONTACT', 'From'), $configsService->getConfig('MAILCONTACT', 'Label')))
  35.                 ->from(new Address($mailService$configsService->getConfig('MAILCONTACT''Label')))
  36.                 ->to($contact->getEmail())
  37.                 ->cc($configsService->getConfig('MAILCONTACT''From'))
  38.                 ->subject($configsService->getConfig('MAILCONTACT''Subject'))
  39.                 ->text($configsService->getConfig('MAILCONTACT''Subject'))
  40.                 ->htmlTemplate('emails/' $this->getParameter('PROJECT') . '/contact.html.twig')
  41.                 ->context([
  42.                     'contact' => $contact
  43.                 ]);
  44.             $mailer->send($email);
  45.             $successMessage $this->translator->trans('messageSent', [], 'commun');
  46.             $this->addFlash('success'$successMessage);
  47.             return $this->redirectToRoute('contact_index');
  48.         }
  49.         /**
  50.          * @var CategoryRepository
  51.          */
  52.         $categoryRepository $this->em->getRepository(Category::class);
  53.         $category $categoryRepository->findOneBy([
  54.             'formContact'    => 1
  55.         ]);
  56.         $formView $form->createView();
  57.         return $this->render('contact/index.html.twig', [
  58.             'controller_name' => 'Contact',
  59.             'formView' => $formView,
  60.             'category' => $category
  61.         ]);
  62.     }
  63. }