src/Controller/Site/ContactController.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Site;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use App\Entity\Contact;
  7. use App\Form\ContactType;
  8. class ContactController extends Controller
  9. {
  10.     public function contactFormAction()
  11.     {
  12.         $form       =   $this->createForm(ContactType::class);
  13.         return $this->render(
  14.             'site\contact\contactForm.html.twig',
  15.             array(
  16.                 'form'    => $form->createView()
  17.             )
  18.         );
  19.     }
  20.     /**
  21.      * @Route("/{_locale}/contactar/thanks", name="contact_thanks", requirements={"_locale" = "es|en"})
  22.      */
  23.     public function contactUsThanksAction(Request $request)
  24.     {
  25.         return $this->render('site/contact/contactUsThanks.html.twig');
  26.     }
  27.     /**
  28.      * @Route("/{_locale}/contactar", name="contact", requirements={"_locale" = "es|en"})
  29.      */
  30.     public function contactUsAction(Request $request, \Swift_Mailer $mailer)
  31.     {
  32.         $contact    =   new Contact();
  33.         $em         =   $this->getDoctrine()->getManager();
  34.         $page       =   $em->getRepository('App\Entity\Page')->findOneBySlug('contactar');
  35.         $form       =   $this->createForm(ContactType::class, $contact);
  36.         $form->handleRequest($request);
  37.         if ($form->isSubmitted()) {
  38.             if ($form->isValid()) {
  39.                 // Save Contact
  40.                 $em->persist($contact);
  41.                 $em->flush();
  42.                 // Send email
  43.                 $message $this->renderView(
  44.                     'email\contact.html.twig',
  45.                     array('contact' => $contact)
  46.                 );
  47.                 $msg = (new \Swift_Message('Mensaje de contacto'))
  48.                     ->setFrom($contact->getEmail())
  49.                     ->setTo($this->container->getParameter('admin_email'))
  50.                     ->setBody($message'text/html')
  51.                     ->addPart(strip_tags($message), 'text/plain');
  52.                 $mailer->send($msg);
  53.                 // Show flash message
  54.                 $this->addFlash('success''Su mensaje ha sido enviado correctamente.');
  55.                 return $this->redirect($this->generateUrl('contact_thanks'));
  56.                 // Reset form
  57.                 $contact    =   new Contact();
  58.                 $form       =   $this->createForm(ContactType::class, $contact);
  59.             }
  60.         }
  61.         return $this->render('site/contact/contactUs.html.twig', array(
  62.             'form'  =>  $form->createView(),
  63.             'page'  =>  $page
  64.         ));
  65.     }
  66. }