<?php
namespace App\Controller;
use App\Entity\Actualite;
use App\Entity\Demande;
use App\Entity\Page;
use App\Repository\FaqRepository;
use App\Repository\FormuleRepository;
use App\Repository\PageRepository;
use App\Repository\ActualiteRepository;
use App\Repository\AvisRepository;
use App\Repository\PopupRepository;
use App\Repository\SliderRepository;
use App\Service\MailService;
use Doctrine\ORM\EntityManagerInterface;
use Pagerfanta\Doctrine\ORM\QueryAdapter;
use Pagerfanta\Pagerfanta;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mime\Address;
use Symfony\Component\Routing\Annotation\Route;
class FrontController extends AbstractController
{
#[Route('/', name: 'app_default')]
public function index(
PageRepository $pageRepository,
ActualiteRepository $actualiteRepository,
AvisRepository $avisRepository,
PopupRepository $popupRepository,
SliderRepository $sliderRepository,
FormuleRepository $formuleRepository
) : Response
{
$page = $pageRepository->findOneBySlug('index.html');
if ($page)
{
$src = '';
preg_match( '#<img .* src="(.*)".*>#isU', $page->getContenu(), $matches );
if(isset($matches[1])) $src = $matches[1];
{
$slides = $sliderRepository->findBy(array('actif' => true),array('id' => 'ASC'));
$actualites = $actualiteRepository->findLast(3);
$avisListe = $avisRepository->findAll();
$popup = $popupRepository->findOneBy(array('actif' => true),array('id' => 'DESC'));
$formules = $formuleRepository->findAll();
return $this->render('front/accueil.html.twig', [
'page' => $page,
'src' => $src,
'slides' => $slides,
'actualites' => $actualites,
'avisListe' => $avisListe,
'popup' => $popup,
'formules' => $formules
]);
}
}
throw $this->createNotFoundException('Page introuvable');
}
#[Route('/{slug}', name: 'app_index')]
public function page(
Page $page = null,
ActualiteRepository $actualiteRepository,
AvisRepository $avisRepository,
FormuleRepository $formuleRepository
) : Response
{
if ($page)
{
$src = '';
$matches = [];
preg_match( '#<img .* src="(.*)".*>#isU', $page->getContenu(), $matches );
if(isset($matches[1])) $src = $matches[1];
$fomules = $formuleRepository->findAll();
if ($page->isAccueil())
{
$actualites = $actualiteRepository->findLast(3);
$avisListe = $avisRepository->findAll();
return $this->render('front/accueil.html.twig', [
'page' => $page,
'src' => $src,
'actualites' => $actualites,
'avisListe' => $avisListe,
'formules' => $fomules
]);
} elseif ($page->getTemplate() == 1 || $page->getTemplate() == null){
return $this->render('front/page.html.twig', [
'page' => $page,
'formules' => $fomules,
'src' => $src
]);
} elseif ($page->getTemplate() == 2){
return $this->render('front/cours.html.twig', [
'page' => $page,
'formules' => $fomules,
'src' => $src
]);
}
return $this->render('front/page.html.twig', [
'page' => $page,
'formules' => $fomules,
'src' => $src
]);
}
throw $this->createNotFoundException('Page introuvable');
}
#[Route('/actualites', name: 'app_actualites', priority: 1)]
public function actualites(
ActualiteRepository $actualiteRepository,
FormuleRepository $formuleRepository
) : Response
{
$queryBuilder = $actualiteRepository->createOrderedByDateQueryBuilder();
$adapter = new QueryAdapter($queryBuilder);
$formules = $formuleRepository->findAll();
$pagerfanta = Pagerfanta::createForCurrentPageWithMaxPerPage(
$adapter,
1,
9
);
return $this->render('front/actualites.html.twig', [
'pager' => $pagerfanta,
'formules' => $formules
]);
}
#[Route('/actualite/{slug}', name: 'app_actualite_detail')]
public function actualiteDetail(
Actualite $actualite = null,
ActualiteRepository $actualiteRepository,
FormuleRepository $formuleRepository
) : Response
{
$fomules = $formuleRepository->findAll();
$actualites = $actualiteRepository->findBy(array('categorie' => $actualite->getCategorie()->getId()), null, 3);
$fomules = $formuleRepository->findAll();
return $this->render('front/actu-detail.html.twig', [
'actualite' => $actualite,
'actualites' => $actualites,
'formules' => $fomules
]);
}
#[Route('/contact.html', name: 'app_contact', priority: 1)]
public function contact(Request $request, EntityManagerInterface $entityManager, MailService $mailService) : Response
{
$demande = new Demande();
$form = $this->createFormBuilder($demande)
->add('nom',TextType::class)
->add('prenom',TextType::class, [
'required' => false,
])
->add('telephone',TextType::class)
->add('email',EmailType::class)
->add('sujet',TextType::class, [
'required' => false,
])
->add('message',TextareaType::class);
$form = $form->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var Demande $demande */
$demande = $form->getData();
$to = new Address($this->getParameter('email_admin'),'Ecole de Surf Côte Sauvage');
$from = new Address($demande->getEmail(), $demande->getNom(). ' ' .$demande->getPrenom());
$sujet = $demande->getSujet();
$message = $demande->getMessage();
$data = array("secret" => "0xac8d6075a37A4ED5B28915701b14cff918D15726", "response" => $_POST["h-captcha-response"]);
$verify = curl_init();
curl_setopt($verify, CURLOPT_URL, "https://hcaptcha.com/siteverify");
curl_setopt($verify, CURLOPT_POST, true);
curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($verify);
// var_dump($response);
$responseData = json_decode($response);
if (!$responseData->success) {
$this->addFlash('error', '<p>Valider le captcha anti-robot.</p>');
} else {
$res = $mailService->sendEmail($to, $from, $sujet,'front/contact-email.html.twig',[
'nom' => $demande->getNom(),
'prenom' => $demande->getPrenom(),
'phone' => $demande->getTelephone(),
'add_email' => $demande->getEmail(),
'sujet' => $demande->getSujet(),
'message' => $message
]);
$entityManager->persist($demande);
$entityManager->flush();
$this->addFlash('success', '<p>Votre demande a bien été enregistrée et nous vous en remercions.</p>
<p>Nous allons vous contacter dans les plus brefs délais.</p>');
}
// return $this->redirectToRoute('app_contact');
}
return $this->render('front/contact.html.twig', [
'form' => $form->createView()
]);
}
#[Route('/faq.html', name: 'app_faq', priority: 1)]
public function faq(
Request $request,
FaqRepository $faqRepository,
FormuleRepository $formuleRepository
) : Response
{
$faqList = $faqRepository->findAll();
$forules = $formuleRepository->findAll();
return $this->render('front/faq.html.twig',[
'faqList' => $faqList,
'formules' => $forules
]);
}
}