<?php
namespace App\Controller;
use Twig\Environment;
use DateTimeImmutable;
use App\Entity\Booking;
use App\Service\Mailjet;
use App\Entity\Testimonial;
use App\Service\StaticData;
use App\Form\TestimonialType;
use App\Service\CheckRecaptcha;
use App\Form\CheckAvailabilityType;
use App\Repository\VideoRepository;
use App\Repository\AboutAreaRepository;
use App\Repository\BookingConstraintRepository;
use App\Repository\HobbyAreaRepository;
use App\Repository\IntroAreaRepository;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\GalleryAreaRepository;
use App\Repository\HobbySingleRepository;
use App\Repository\ServiceAreaRepository;
use App\Repository\TestimonialRepository;
use App\Repository\GalleryCategoryRepository;
use App\Repository\TestimonialAreaRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Repository\HomeMainSliderImageRepository;
use App\Repository\ServiceCategoryRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class HomeController extends AbstractController
{
public function __construct(EntityManagerInterface $manager, BookingConstraintRepository $bookingConstraintRepo, StaticData $statics, Environment $twig)
{
$this->manager = $manager;
$this->twig = $twig;
$this->statics = $statics->getStaticData();
$this->constraint = $bookingConstraintRepo->findOneBy([]);
}
/**
* @Route("/", name="home")
*/
public function index(Request $request, CheckRecaptcha $recaptcha, HomeMainSliderImageRepository $sliderRepo, AboutAreaRepository $aboutAreaRepo, IntroAreaRepository $introAreaRepo, VideoRepository $videoRepo, HobbyAreaRepository $hobbyAreaRepo, HobbySingleRepository $hobbySingleRepo, GalleryAreaRepository $galleryAreaRepo, GalleryCategoryRepository $galleryCategoryRepo, ServiceAreaRepository $serviceAreaRepo, ServiceCategoryRepository $serviceCatRepo, TestimonialAreaRepository $testimonialAreaRepo, TestimonialRepository $testimonialRepo): Response
{
$slides = $sliderRepo->findAll();
$introArea = $introAreaRepo->findOneBy([]);
$videoArea = $videoRepo->findOneBy([]);
$aboutArea = $aboutAreaRepo->findOneByIsHomeArea(true);
$hobbiesArea = $hobbyAreaRepo->findOneBy([]);
$hobbies = $hobbySingleRepo->findAll();
$galleryArea = $galleryAreaRepo->findOneBy([]);
$galleries = $galleryCategoryRepo->findAll();
$serviceArea = $serviceAreaRepo->findOneBy([]);
$serviceCategories = $serviceCatRepo->findAll();
$testimonialArea = $testimonialAreaRepo->findOneBy([]);
$testimonials = $testimonialRepo->findBy([], ['createdAt' => 'ASC']);
# Check availability
$constraint = $this->constraint;
$endDate = $this->getEndDate();
# Création du formulaire de verification de disponibilité
$bookingEntity = new Booking();
$checkForm = $this->createForm(CheckAvailabilityType::class, $bookingEntity);
# Fomulaire d'avis
$testimonialEntity = new Testimonial();
$testimonialForm = $this->createForm(TestimonialType::class, $testimonialEntity);
$testimonialForm->handleRequest($request);
if ($testimonialForm->isSubmitted() && $testimonialForm->isValid()) {
define('SITE_KEY','6Lf6ntcdAAAAAFIFOj0w_4Q3_q8fs-zXu7senO2n');
define('SECRETE_KEY','6Lf6ntcdAAAAAImt18bgJi8ajipSKwPYfIq3RQd3');
if ( $recaptcha->check($_POST['testimonial-recaptcha_response'], SECRETE_KEY) ){
$data = $testimonialForm->getData();
$statics = $this->statics['data'];
$testimonialEntity->setCreatedAt(new DateTimeImmutable())
->setDisplay(false);
# Envoi d'email MailJet
# client
$mailjet = new Mailjet();
$mail_content = $this->renderView('components/mail/_testimonial_recap.html.twig', compact('data', 'statics'));
$mailjet->send("caurettemarc@gmail.com", "Colombine", $data->getEmail(), $data->getFirstName()." ".$data->getLastName(), 'Colombine | Merci pour votre avis !', $mail_content, false, false, 3367969);
# Proprio
$mailjet = new Mailjet();
$mail_content = $this->renderView('components/mail/_testimonial.html.twig', compact('data', 'statics'));
$mailjet->send("caurettemarc@gmail.com", $data->getFirstName()." ".$data->getLastName(), "maximeavril.dev@gmail.com", $data->getFirstName()." ".$data->getLastName(), 'Colombine | Avis', $mail_content,false, false, 3367969);
$this->manager->persist($testimonialEntity);
$this->manager->flush();
return $this->json(true);
}else{
return $this->json(false);
}
}
$response = new Response($this->twig->render('home/index.html.twig', [
'statics' => $this->statics['data'],
'slides' => $slides,
'introArea' => $introArea,
'videoArea' => $videoArea,
'aboutArea' => $aboutArea,
'hobbiesArea' => $hobbiesArea,
'hobbies' => $hobbies,
'galleryArea' => $galleryArea,
'galleries' => $galleries,
'serviceArea' => $serviceArea,
'serviceCategories' => $serviceCategories,
'testimonialArea' => $testimonialArea,
'testimonials' => $testimonials,
'checkForm' => $checkForm->createView(),
'testimonialForm' => $testimonialForm->createView(),
'constraint' => $constraint,
'endDate' => $endDate,
]));
$response->setSharedMaxAge(3600);
return $response;
}
/**
* Permet de récupérer la date la plus éloignée parmis les periodes des saisons en BDD
* Afin de bloquer les reservations en ligne après cette date
*
* @Route("/booking/get-end-date", name="booking_end_date")
*/
public function getEndDate(){
$saisons = $this->constraint->getSaisons();
$endDates = [];
foreach ($saisons as $key => $saison) {
foreach ($saison->getPeriods() as $key => $period) {
$endDates[] = date('U', strtotime($period->getEndDate()->format('Y-m-d')));
}
}
sort($endDates, SORT_NUMERIC);
$endDate = date('d/m/Y', array_slice($endDates, -1, 1)[0]);
return $endDate;
}
}