src/Controller/Front/ContactController.php line 85

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\{ContactPageContactUser};
  4. use App\Form\{ContactType};
  5. use App\Service\AccessClientService;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\Mime\Email;
  12. use Symfony\Component\Mime\Address;
  13. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  16. use Symfony\Contracts\HttpClient\HttpClientInterface;
  17. use DateTime;
  18. use DateTimeZone;
  19. class ContactController extends AbstractController
  20. {
  21.     /**
  22.      * @Route("/contact", name="app_front_contact")
  23.      */
  24.     public function contact(
  25.         AccessClientService $accessClientService
  26.         Request $request
  27.         EntityManagerInterface $entityManager,
  28.         MailerInterface $mailer
  29.         ): Response
  30.     {
  31.         $accessClientService->handleAccessControl();
  32.         
  33.         $contactPage $entityManager->getRepository(ContactPage::class)->find(1);
  34.         $contact = new Contact();
  35.         $form $this->createForm(ContactType::class, $contact);
  36.         $form->handleRequest($request);
  37.         if ($form->isSubmitted() && $form->isValid()) {
  38.             $emailUser $form->getData('email');
  39.             $user $entityManager->getRepository(User::class)->findOneBy(['email' => $emailUser]);
  40.             if ($user) {
  41.                 $userName $user->getFirstname() . ' ' .$user->getLastname();
  42.                 $userPhoto $user->getAvatar(); 
  43.             } else {
  44.                 $userName null;
  45.                 $userPhoto null;
  46.             }
  47.             $contact->setStatus(Contact::RECEIVED);
  48.             $timezone = new DateTimeZone('Europe/Paris');
  49.             $createdAt = new DateTime('now'$timezone);
  50.             $contact->setCreatedAt($createdAt);
  51.             $entityManager->persist($contact);
  52.             $entityManager->flush();   
  53.             
  54.             $email = (new Email())
  55.                 ->from($contact->getEmail())
  56.                 ->to('rabemanantsaina29@gmail.com')                                                            
  57.                 ->subject('Nouveau message de contact depuis RYVUP')
  58.                 ->html($this->renderView('emails/contact.html.twig', [
  59.                     'contact' => $contact,
  60.                     'userName' => $userName,
  61.                     'userPhoto' => $userPhoto,
  62.                     'messageUrl' => $this->generateUrl('app_admin_email_inbox', [], UrlGeneratorInterface::ABSOLUTE_URL)
  63.                 ]));
  64.             $mailer->send($email);
  65.             $this->addFlash('success'"Succès ! Votre message a bien été envoyé.");
  66.             return $this->redirectToRoute('app_front_contact'); 
  67.         }
  68.         return $this->render('front/contact/contact.html.twig', [
  69.             'contactPage' => $contactPage,
  70.             'form' => $form->createView()
  71.         ]);
  72.     }
  73. }