<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\SaquitoContacto;
use App\Form\SaquitoContactoType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
/**
* Se encarga de los formularios de contacto
*
* @Route("/contacto")
*
* @category Controladores
* @package Saquito
* @copyright (c) 2022, APDiseño
* @author Andrés Pieruccioni <andrespieruccioni@gmail.com>
*/
class ContactoController extends AbstractController
{
/**
* Renderiza un formulario de contacto
*
* @author Andrés Pieruccioni <andrespieruccioni@gmail.com>
* @Route("/formulario", name="saquito_paginas_contacto_procesar")
*
* @category function
* @return string Código HTML del formulario de contacto
*/
public function armarFormulario(): Response
{
$contacto = new saquitoContacto();
$form = $this->createForm(SaquitoContactoType::class, $contacto);
return $this->render(
'contacto/formulario.html.twig', array(
'form' => $form->createView()
));
}
/**
* Procesa el formulario de contacto
*
* @author Andrés Pieruccioni <andrespieruccioni@gmail.com>
*
* @Route("/graciasporcontactarte", name="saquito_paginas_contacto_procesar")
*
* @category function
* @return string Código HTML de respuesta
*/
public function procesarContacto(MailerInterface $mailer, Request $request): Response
{
$config = $this->getParameter('proyecto');
if ($request->getMethod() == 'POST') {
$contacto = new saquitoContacto();
$form = $this->createForm(SaquitoContactoType::class, $contacto);
$form->handleRequest($request);
if ($form->isValid()) {
$email = (new Email())
->from($config['email']['from'])
->to($config['email']['to'])
//->cc('cc@example.com')
//->bcc('bcc@example.com')
//->replyTo('fabien@example.com')
//->priority(Email::PRIORITY_HIGH)
->subject('Mensaje desde el sitio web de ' . $config['titulo'])
//->text('Sending emails is fun again!')
->html($this->renderView('contacto/email.txt.twig',
array( 'contacto' => $contacto )) , 'text/html');
$mailer->send($email);
$this->get('session')->getFlashBag()->add('notice', 'Recibimos tu mensaje correctamente!, muchas gracias por comunicarte');
} else {
$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');
}
return $this->render('contacto/gracias.html.twig');
} else {
return $this->redirectToRoute('saquito_paginas_bienvenidos');
}
}
}