<?php
namespace App\Controller\FrontEnd;
use App\classe\Mails;
use App\classe\Search;
use App\Entity\Contact;
use App\Form\ContactType;
use App\Form\SearchType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ContactController extends AbstractController
{
private $em;
public function __construct(EntityManagerInterface $em)
{
return $this->em = $em;
}
#[Route('/front/end/contact', name: 'frontend_contact')]
public function index(Request $request, Mails $mails): Response
{
$contact = new Contact();
$search = new Search();
$form = $this->createForm(SearchType::class, $search);
$form->handleRequest($request);
$formcontact = $this->createForm(ContactType::class, $contact);
$formcontact->handleRequest($request);
if ($formcontact->isSubmitted() && $formcontact->isValid()){
//Enregistrer les données dans la base de données
$date = new \DateTime('now');
$contact->setCreatedAt($date);
$this->em->persist($contact);
$this->em->flush();
$this->addFlash('success', "Votre message à bien été envoyer nous vous reviendrons très bientôt ");
//Envoie d'email à l'administracteur en utilisant l'API de MailJet
$objet = "SCI CAWALLY | ".$contact->getSubject();
$content = "<p style='font-size: large;'>Message venant du site internet SCI CAWALLY. <br/> Information sur l'expéditeur: </p>";
$content .= "<h4></strong>Nom et Prenom: </strong>".$contact->getFullName()."</h4>".
"<h4> E-mail: ".$contact->getEmail()."</h4><h4>Sujet: ".$contact->getSubject()."</h4>";
$content .="<h4><strong>Message:</strong> </h4><br/><p style='font-size: large;'>".$contact->getMessage()."</p>";
$mails->sendMail($objet, $content);
return $this->redirectToRoute('frontend_contact');
}
return $this->render('front_end/contact/index.html.twig', [
'controller_name' => 'ContactController',
'Formcontact'=>$formcontact->createView(),
'Form'=>$form->createView()
]);
}
}