<?php
namespace App\Controller;
use App\Entity\MailOutBox;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* ContactForm controller.
*
* @Route("/contactForm")
*/
class ContactFormController extends AbstractController
{
/**
* @Route("/", name="contactForm", methods={"GET", "POST"})
*/
public function contactAction(
Request $request
)
{
$em = $this->getDoctrine()->getManager();
$form = $this->createFormBuilder()
->setAction($this->generateUrl('contactForm'))
->add('email', EmailType::class,[
'label' => 'contact_form.email'
])
->add('name', TextType::class,[
'label' => 'contact_form.name'
])
->add('body', TextareaType::class,[
'label' => 'contact_form.message'
])
->add('add', SubmitType::class,[
'label' => 'contact_form.send'
])
->getForm()
;
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
$mailOut = new MailOutBox();
$mailOut->setRecipient('scorebox@fra-services.de');
$mailOut->setSubject('Kontaktformular anfrage von ' . $form->get('name')->getData());
$mailOut->setBody('E-Mail: ' . $form->get('email')->getData() . "<br>" .
'Name: ' . $form->get('name')->getData() . "<br>" .
'Text: ' . nl2br($form->get('body')->getData()));
// dump($mailOut);
// die;
$em->persist($mailOut);
$em->flush();
return $this->redirect($request->headers->get('referer'));
}
}
return $this->render('includes/_contactform.html.twig',
array('form' => $form->createView())
);
}
}