src/Controller/ContactoController.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use App\Entity\SaquitoContacto;
  7. use App\Form\SaquitoContactoType;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Mailer\MailerInterface;
  10. use Symfony\Component\Mime\Email;
  11. /**
  12.  * Se encarga de los formularios de contacto
  13.  * 
  14.  * @Route("/contacto")
  15.  *
  16.  * @category Controladores
  17.  * @package Saquito
  18.  * @copyright (c) 2022, APDiseño
  19.  * @author Andrés Pieruccioni <andrespieruccioni@gmail.com>
  20.  */
  21. class ContactoController extends AbstractController
  22. {
  23.     /**
  24.      * Renderiza un formulario de contacto
  25.      * 
  26.      * @author Andrés Pieruccioni <andrespieruccioni@gmail.com>
  27.      * @Route("/formulario", name="saquito_paginas_contacto_procesar") 
  28.      * 
  29.      * @category function
  30.      * @return string Código HTML del formulario de contacto
  31.      */
  32.     public function armarFormulario(): Response
  33.     {
  34.         $contacto = new saquitoContacto();
  35.         $form $this->createForm(SaquitoContactoType::class, $contacto);
  36.         return $this->render(
  37.             'contacto/formulario.html.twig', array(
  38.                 'form' => $form->createView()
  39.             ));
  40.     }    
  41.     
  42.     /**
  43.      * Procesa el formulario de contacto
  44.      * 
  45.      * @author Andrés Pieruccioni <andrespieruccioni@gmail.com>
  46.      * 
  47.      * @Route("/graciasporcontactarte", name="saquito_paginas_contacto_procesar")
  48.      * 
  49.      * @category function
  50.      * @return string Código HTML de respuesta
  51.      */
  52.     public function procesarContacto(MailerInterface $mailerRequest $request): Response
  53.     {
  54.         $config $this->getParameter('proyecto');
  55.         if ($request->getMethod() == 'POST') {
  56.             $contacto = new saquitoContacto();
  57.             $form $this->createForm(SaquitoContactoType::class, $contacto);
  58.             $form->handleRequest($request);
  59.             if ($form->isValid()) {
  60.                 $email = (new Email())
  61.                 ->from($config['email']['from'])
  62.                 ->to($config['email']['to'])
  63.                 //->cc('cc@example.com')
  64.                 //->bcc('bcc@example.com')
  65.                 //->replyTo('fabien@example.com')
  66.                 //->priority(Email::PRIORITY_HIGH)
  67.                 ->subject('Mensaje desde el sitio web de ' $config['titulo'])
  68.                 //->text('Sending emails is fun again!')
  69.                 ->html($this->renderView('contacto/email.txt.twig'
  70.                     array( 'contacto' => $contacto )) , 'text/html');
  71.     
  72.                 $mailer->send($email);                
  73.                 $this->get('session')->getFlashBag()->add('notice''Recibimos tu mensaje correctamente!, muchas gracias por comunicarte');
  74.             } else {
  75.                 $this->get('session')->getFlashBag()->add('notice''No pudimos recibir el mensaje porque alguno de los datos no son correctos, observa el formulario y vuélvelo a enviar, muchas gracias');
  76.             }
  77.             return $this->render('contacto/gracias.html.twig');
  78.         } else {
  79.             return $this->redirectToRoute('saquito_paginas_bienvenidos');
  80.         }
  81.     }       
  82. }