src/Controller/ContactController.php line 23
<?php
namespace App\Controller;
use App\Entity\Contact;
use App\Form\ContactType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Repository\ReferencementRepository;
use App\Service\Recaptcha;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
class ContactController extends AbstractController
{
#[Route('/contact', name: 'app_contact')]
public function index(Request $request, EntityManagerInterface $manager, MailerInterface $mailer, ReferencementRepository $referencementRepository, ContainerBagInterface $bag, Recaptcha $sRecaptcha): Response
{
$contact = new Contact();
$form = $this->createForm(ContactType::class, $contact);
$form->handleRequest($request);
$fichier = null;
if($form->isSubmitted() && $form->isValid()){
$recaptchaToken = $form->get('recaptchaToken')->getData();
$check = $sRecaptcha->verifyCaptcha($bag->get('app.google_site_key'), $recaptchaToken ?? '', $bag->get('app.google_project'),'OFFRE');
if($check){
$params = $request->get('contact');
$file = $form->get('extension')->getData();
if ($file) {
$ext = $file->guessExtension();
$contact->setExtension($ext);
$pathUpload = "fichier_contact/";
$name = $contact->getId().'.'.$ext;
try {
$file->move($pathUpload, $name);
} catch (FileException $e) {
// handle exception
}
$fichier = "Ce message est accompagné d'une pièce jointe, pour le visualiser il faut vous connecter sur le site.";
}
$manager->persist($contact);
$manager->flush();
if($fichier){
$html = "<p>Vous avez reçu un message de ".$params['Nom']." ".$params['prenom']."<br> Son adresse mail est: ". $params['email'] ." <br><br> ".$params['message']."<br><br>" . $fichier . "</p>";
}else{
$html = "<p>Vous avez reçu un message de ".$params['Nom']." ".$params['prenom']."<br> Son adresse mail est: ". $params['email'] ." <br><br> ".$params['message']."</p>";
}
$subject = $params['sujet'];
$email = (new Email())
->from('r.bergery@ferguss.com')
->to('e.daloia@ferguss.com')
->subject($subject)
->html($html);
$mailer->send($email);
$this->addFlash(
'success',
'Votre message a bien été envoyé, merci !'
);
return $this->redirectToRoute("app_contact");
}else{
$this->addFlash('error', 'Échec de la vérification reCAPTCHA.');
return $this->redirectToRoute('app_contact');
}
}
return $this->render('contact/index.html.twig', [
'form' => $form->createView(),
'google_sitekey' => $bag->get('app.google_site_key'),
'controller_name' => 'ContactController',
'referencements' => $referencementRepository->findAll(),
]);
}
#[Route('/download-document/{id}', name: 'app_download_doc')]
public function download(Contact $contact)
{
$filepath = $contact->getDownloadPath();
if (is_file($filepath)) {
$fileName = "fichier_".$contact->getId().'.'.$contact->getExtension();
$filename = filter_var($fileName, FILTER_SANITIZE_URL);
$response = new Response();
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, $filename);
$response->headers->set('Content-Disposition', $disposition);
$response->headers->set('Content-Type', mime_content_type($filepath));
$response->setContent(file_get_contents($filepath));
return $response;
}
$response = new Response();
return $response;
}
}