<?php
namespace App\Controller\FrontOffice;
use App\Entity\Message;
use App\Entity\Contact;
use App\Form\BackOffice\MessageType;
use App\Form\BackOffice\ContactType;
use App\Repository\EmployesRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mailer\MailerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class HomeController extends AbstractController
{
/**
* @Route("/", name="home")
*/
public function index(EmployesRepository $employesRepository, Request $request, EntityManagerInterface $manager)
{
$message = new Message();
$form = $this->createForm(MessageType::class, $message);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$message->setDate(new \DateTime());
$manager->persist($message);
$manager->flush();
$this->addFlash(
'success',
"Votre message a été bien envoyer, Merci..."
);
return $this->redirectToRoute('home');
}
return $this->render('FrontOffice/home.html.twig', [
'leaderships' => $employesRepository->findBy(
[],
['id' => 'DESC'],
8
),
'formMessage' => $form->createView()
]);
}
/**
* @Route("/contact", name="contact")
*/
public function contact( \Swift_Mailer $mailer, Request $request, EntityManagerInterface $manager)
{
$contact = new Contact();
$form = $this->createForm(ContactType::class, $contact);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid() )
{
dd($contact);
// $manager->persist($contact);
// $manager->flush();
//
//SwiftMailer
$message = (new \Swift_Message('Demande de rendez-vous'))
->setFrom('ot.freddino@gmail.com')
->setTo($contact->getEmail());
$message->setBody(
$this->renderView(
// templates/emails/registration.html.twig
'mail/mail.html.twig',
[
'sujet' => $contact->getSubject(),
'message' => $contact->getMessage()
]
)
);
$message->setContentType("text/html");
$_result = $mailer->send($message);
$_headers = $message->getHeaders();
$_headers->addTextHeader('MIME-Version', '1.0');
$_headers->addTextHeader('X-Mailer', 'PHP v' . phpversion());
$_headers->addParameterizedHeader('Content-type', 'text/html', ['charset' => 'utf-8']);
}
return $this->render('FrontOffice/contact.html.twig',[
'form' => $form->createView()
]);
}
}