<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use GuzzleHttp\Client;
use App\Entity\commande;
use App\Entity\Transaction;
use App\Entity\Typologie;
use App\Entity\Residence;
use App\Entity\mediaResidence;
use App\Entity\Language;
use App\Entity\InfosResidence;
use App\Entity\Package;
use App\Entity\InfosFormuleBase;
use App\Entity\InfosDiscover;
use App\Entity\InfosDiscoverLink;
use App\Entity\InfosEvent;
use App\Repository\TransactionRepository;
use App\Utils\Residences;
use App\Utils\IresaManager;
use App\Utils\UpdateTraductions;
use App\Services\ToolService;
use App\Services\TravellerService;
use App\Services\SeasonService;
/**
* @Route("/{_locale}", requirements={"_locale": "en|fr"})
*/
class ProductController extends AbstractController
{
/**
* @var EntityManagerInterface
*/
private $em;
/**
* @var SessionInterface
*/
private $session;
private $flashbag;
private $user;
private $seasonService;
private $residenceService;
private $iresaManager;
private $toolService;
private $updateTraductions;
private $travellerService;
function __construct(EntityManagerInterface $em, SessionInterface $session, SeasonService $seasonService, Residences $residenceService, IresaManager $iresaManager, ToolService $toolService, UpdateTraductions $updateTraductions, TravellerService $travellerService) {
$this->em = $em;
$session = $session;
$this->flashbag = $session->getFlashBag();
$this->seasonService = $seasonService;
$this->residenceService = $residenceService;
$this->iresaManager = $iresaManager;
$this->toolService = $toolService;
$this->updateTraductions = $updateTraductions;
$this->travellerService = $travellerService;
}
/**
* @Route("/produit/{slug}/{id}", name="ac_platform_produit")
*/
public function produit(Request $request, $slug, $id)
{
$session = $request->getSession();
$message = [];
$themes = null;
$prestationDefault = null;
$listDurees = [];
$listDatesDebut = [];
$services = null;
$selectedFormule = 0;
$selectedHebergement = null;
$packages = [];
$locale = $request->getLocale();
if ($session->has('formule')) {
$message[] = "Formule en session";
$selectedFormule = $session->get('formule');
}
$lang = $this->em->getRepository(Language::class)->findOneByCode($locale);
$lang_default = $this->em->getRepository(Language::class)->findOneByCode('fr');
/* Instanciation des services */
$season = $this->seasonService->getSeason();
$this->residenceService->setLocale($locale);
$comm_status = true;
if($id != $this->travellerService->travellers['idResidence'] || $session == "renew") {
$this->travellerService->init();
$this->travellerService->travellers['idResidence'] = $id;
$session->remove('idPrestation');
$session->remove('idPresta');
$session->remove('hebergement');
$session->remove('formule');
$session->remove('resas');
$session->remove('comment');
$session->remove('tarifs');
$session->remove('codepromo');
$session->remove('idPack');
$session->remove('idtyposelected');
$session->remove('residence');
}
$session->set('residence' , ['slug' => $slug, 'id' => $id]);
$residence = $this->em->getRepository(Residence::class)->findOneBy(['idiresa' => $id]);
if(!$residence) {
throw $this->createNotFoundException('RĂ©sidence inexistante');
}
$residence_id = $residence->getId();
$params_default = [
'lang' => $lang_default,
'residence' => $residence
];
$params_trad = [
'lang' => $lang,
'residence' => $residence
];
$breadCrumb = $this->residenceService->getBreadcrumb($residence);
$infosResidence = $this->em->getRepository(InfosResidence::class)->findOneBy($params_trad);
if(!$infosResidence) {
$infosResidence = $this->em->getRepository(InfosResidence::class)->findOneBy($params_default);
}
$formules = $this->em->getRepository(Package::class)->findOnPageBy($params_trad);
if(!$formules) {
$formules = $this->em->getRepository(Package::class)->findOnPageBy($params_default);
}
$formuleBase = $this->em->getRepository(InfosFormuleBase::class)->findOneBy($params_trad);
if(!$formuleBase) {
$formuleBase = $this->em->getRepository(InfosFormuleBase::class)->findOneBy($params_default);
}
$formules = array_merge([$formuleBase], $formules);
$typologies = $this->em->getRepository(Typologie::class)->findOnPageBy($params_trad);
if(!$typologies) {
$typologies = $this->em->getRepository(Typologie::class)->findOnPageBy($params_default);
}
$listDiscover = $this->em->getRepository(InfosDiscover::class)->findBy($params_trad);
if(!$listDiscover) {
$listDiscover = $this->em->getRepository(InfosDiscover::class)->findBy($params_default);
}
$listDiscoverLink = $this->em->getRepository(InfosDiscoverLink::class)->findBy($params_trad);
if(!$listDiscoverLink) {
$listDiscoverLink = $this->em->getRepository(InfosDiscoverLink::class)->findBy($params_default);
}
$listEvent = $this->em->getRepository(InfosEvent::class)->findBy($params_trad);
if(!$listEvent) {
$listEvent = $this->em->getRepository(InfosEvent::class)->findBy($params_default);
}
$slidesHeaderDefault = $this->em->getRepository(mediaResidence::class)->findByLocation($residence_id, "header", $lang_default, $season);
$slidesContentDefault = $this->em->getRepository(mediaResidence::class)->findByLocation($residence_id, "content", $lang_default, $season);
$slidesHeader = $this->em->getRepository(mediaResidence::class)->findByLocation($residence_id, "header", $lang, $season);
if(is_null($slidesHeader)) {
$slidesHeader = $slidesHeaderDefault;
}
$slidesContent = $this->em->getRepository(mediaResidence::class)->findByLocation($residence_id, "content", $lang, $season);
if(is_null($slidesContent)) {
$slidesContent = $slidesContentDefault;
}
/* on récupère les paramètres de recherche s'il y en a */
/*
* par défaut on teste les dispos pour une durée de 7 jours
*/
// Recherche des données colonne de droite
$now = new \DateTime();
$duree = 7;
// On regarde s'il y a des prestations dans l'année
$datearrivee = (new \Datetime())->modify('+ 1 day')->format("Y-m-d");
$prestations = $this->residenceService->getPrestations($id, $datearrivee, $duree, $request->getLocale(), 360);
if(count($prestations['ListReponses']) > 0) {
if ($request->getMethod() == 'POST') { // Maj des participants
$this->travellerService->setTravellers($request->request);
}
if($request->query->has("datearrivee")) {
$session->remove('formule');
$session->remove('hebergement');
$tdatearrivee = $request->query->get("datearrivee");
$duree = (int) $request->query->has("duree") ? $request->query->get("duree") : 7;
if($this->toolService->validateDate($tdatearrivee, 'Y-m-d') || $this->toolService->validateDate($tdatearrivee, 'd/m/Y')) {
$datearrivee = $tdatearrivee;
$access = false;
$this->travellerService->setTravellers($request->query);
$this->travellerService->setDateDuree($datearrivee, $duree);
}
} else {
if(empty($this->travellerService->travellers['actdatearrivee'])) {
/*
On recherche une prestation par défaut,
la première date disponible
et le tarif le moins cher en affichant Ă partir de
*/
// si oui on recherche la prestation par défaut et la première date de disponibilité à partir de tomorrow
$obj = [
"IdLieuHebergement" => $id,
"NbDates" => 1
];
$dispoPrice = $this->iresaManager->getDispoPrice($obj);
if ($dispoPrice['ListPrestations']) { // plusieurs formules existent
foreach ($dispoPrice['ListPrestations'] as $formule => $prestation) {
$dispoPricePrestations [] = $prestation;
}
}
$tdatearrivee = $dispoPricePrestations[0]['DateDebut'] ;
$duree = $dispoPricePrestations[0]['Duree'];
$datearrivee = \DateTime::createFromFormat("Y-m-d", $tdatearrivee)->format('d/m/Y');
$this->travellerService->setTravellers($request->query);
$this->travellerService->setDateDuree($datearrivee, $duree);
}
}
$dateDebutDefault = $this->travellerService->travellers['actdatearrivee'];
$datearrivee = $this->travellerService->travellers['actdatearrivee'];
$duree = $this->travellerService->travellers['actnbnuits'];
$type = $request->query->get('type');
$delta = $type == "planning" ? 31 : 0;
/* BEGIN DUPLICATE CODE BLOC 1*/
$planning = $this->residenceService->getPlanning($request, $datearrivee, $duree, $type, $delta, $residence );
if ($planning == "redirect")
{
if($request->isXmlHttpRequest()) {// Envoi signale de redirection via ajax
$response = new Response("{'redirect': true}");
$response->headers->set('Content-Type', 'application/json');
return $response;
} else { // Redirection vers le produit
return $this->redirectToRoute('ac_platform_produit', ['slug' => $slug, 'id' => $id]);
}
}
$packages = $planning['packages'];
$prestations = $planning['prestations'];
$listDatesDebut = $planning['listDatesDebut'];
$listDurees = $planning['listDurees'];
$idPrestationDefault = $planning['idPrestationDefault'];
/* END DUPLICATE CODE BLOC 1*/
}
$comm_status = true;
if(count($prestations['ListReponses']) == 0) {
$comm_status = false;
$idPrestationDefault = null;
}
if(!$comm_status) {
$produit = null;
$formules = null;
$reponses = null;
}
$comm_status = true;
$vars = array(
"id" => $id,
"comm_status" => $comm_status,
"slug" => $slug,
"residence" => $residence,
"travellers" => $this->travellerService->getTravellers(),
"typos" => $typologies, // Contenu des typologies
"formuleBase" => $formuleBase, // contenu des packages et formule de base
"formules" => $formules,
"packages" => $packages,
"prestations" => $prestations,
"infosResidence" => $infosResidence,
"listDiscover" => $listDiscover,
"listDiscoverLink" =>$listDiscoverLink,
"listEvent" => $listEvent,
"slidesHeader" => $slidesHeader,
"slidesContent" => $slidesContent,
"themes" => $themes,
"prestationDefault" => $prestationDefault,
"listDurees" => json_encode($listDurees, JSON_UNESCAPED_SLASHES),
"duree" => $duree, //$listDurees[0],
"listDatesDebut" => json_encode($listDatesDebut),
"idPrestationDefault" => $idPrestationDefault,
'selectedFormule' => $selectedFormule,
'selectedHebergement' => $selectedHebergement,
'toolService' => $this->toolService,
'breadCrumb' => $breadCrumb
);
return $this->render('/front/Produit/produit.html.twig', $vars);
}
/**
* @Route("/produit/refresh", name="ac_platform_produit_refresh")
*/
public function productRefresh(Request $request)
{
$session = $request->getSession();
$type = $request->query->get('type');
$delta = $type == "month" ? 31 : 0;
$message = [];
$message[] = "Refresh typologies";
$locale = $request->getLocale();
$now = new \DateTime();
$lang = $this->em->getRepository(Language::class)->findOneByCode($locale);
$this->residenceService->setLocale($locale);
$travellers = $this->travellerService->travellers;
if($travellers['idResidence'] != $request->query->get('id')) {
$travellers['idResidence'] = $request->query->get('id');
$this->travellerService->travellers = $travellers;
}
//$id = $request->query->get('id');
$id = $this->travellerService->travellers['idResidence'];
$datearrivee = $request->query->get('datearrivee');
$duree = $request->query->get('duree');
$datearrivee = $this->toolService->dateFrToEn($datearrivee);
$dateDebutDefault = $datearrivee;
$this->travellerService->setDateDuree($datearrivee, $duree);
$residence = $this->em->getRepository(Residence::class)->findOneBy(['idiresa' => $id]);
if(!$residence) {
throw $this->createNotFoundException('RĂ©sidence inexistante');
}
$params = [
'lang' => $lang,
'residence' => $residence
];
$typologies = $this->em->getRepository(Typologie::class)->findOnPageBy($params);
$packages = $this->em->getRepository(Package::class)->findOnPageBy($params);
/* BEGIN DUPLICATE CODE BLOC 1*/
$planning = $this->residenceService->getPlanning($request, $datearrivee, $duree, $type, $delta, $residence );
if (isset($planning['redirect'])) {
if($request->isXmlHttpRequest()) {// Envoi signale de redirection via ajax
$response = new Response("{'redirect': true}");
$response->headers->set('Content-Type', 'application/json');
return $response;
} else { // Redirection vers le produit
$residence_nom = str_replace("*", "", $residence->getNom());
$residence_slug = $this->toolService->slugify($residence_nom);
return $this->redirectToRoute('ac_platform_produit', ['slug' => $residence_slug, 'id' => $id]);
}
}
$packages = $planning['packages'];
$formuleBase = $packages[0];
$prestations = $planning['prestations'];
$listDatesDebut = $planning['listDatesDebut'];
$listDatesFin = $planning['listDatesFin'];
$listDurees = $planning['listDurees'];
$idPrestationDefault = $planning['idPrestationDefault'];
/* END DUPLICATE CODE BLOC 1*/
$session->set('idPrestation', $idPrestationDefault);
$selectedFormule = 0;
$selectedHebergement = null;
if ($session->has('formule')) {
$message[] = "Formule en session";
$selectedFormule = $session->get('formule');
}
$hebergement = $session->get('hebergement');
if (!is_null($hebergement)) {
$hebergement = $session->get('hebergement');
$selectedHebergement = $hebergement->IdPrestation;
}
$vars = [
"message" => $message,
'delta' => $delta,
"listDatesDebut" => $listDatesDebut,
"listDatesFin" => $listDatesFin,
"typos" => $typologies,
"listDurees" => $listDurees,
"prestations" => $prestations, // contenu des packages et formule de base
"formuleBase" => $formuleBase,
"packages" => $packages,
"planning" => $planning,
"idPrestationDefault" => $idPrestationDefault,
"dateDebutDefault" => $datearrivee,
"duree" => $duree,
"travellers" => $this->travellerService->getTravellers(),
"debug" => [
'ListReponses' => $prestations['ListReponses'] ,
'packages' => $packages,
'Travellers' => $this->travellerService->getTravellers()
]
];
$response = new Response();
$response->setContent(json_encode($vars, JSON_UNESCAPED_SLASHES));
$response->headers->set('Symfony-Debug-Toolbar-Replace', 1);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
* @Route("/getHebergements", name="ac_platform_getHebergements")
*/
public function getHebergements(Request $request) {
$locale = $request->getLocale();
$session = $request->getSession();
$mode = "";
$method = "html";
$prestations = $request->request->get('prestations');
if($request->query->has('edit')) {$mode = "edit";}
if($request->query->has('method')) {$method = $request->request->get('method');}
$this->residenceService->setLocale($locale);
$selectedHebergementId = null;
if ($session->has('hebergement')) {
$selectedHebergement = $session->get('hebergement');
$selectedHebergementId = $selectedHebergement->IdPrestation;
}
$hebergements = $this->residenceService->getHebergements($prestations, $selectedHebergementId, $mode);
if($method == "ajax") {
return new JsonResponse($hebergements);
}
return new Response($hebergements);
}
/**
* @Route("/setcodepromo", name="ac_platform_setcodepromo")
*/
public function setcodepromo(Request $request)
{
$session = $request->getSession();
$codepromo = $request->request->get("codepromo");
$session->set('codepromo', $codepromo);
return new JsonResponse($codepromo);
}
/**
* @Route("/setSessionVar", name="product_setSessionVar")
*/
public function setSessionVar(Request $request) {
$session = $request->getSession();
$var = $request->request->get("var");
$value = $request->request->get("value");
$ret = $session->set($var, $value);
return new JsonResponse($ret);
}
/**
* @Route("/setSession", name="ac_platform_setSession")
*/
public function setSession(Request $request)
{
$session = $request->getSession();
$id = $request->request->get("id");
$val = $request->request->get("val");
$session->set($id, $val);
return new JsonResponse($id."done".$val);
}
/**
* @Route("/participants", name="ac_platform_participants")
*/
public function participants(Request $request)
{
$retour = array();
return $this->render('/front/Recherche/participants.html.twig', $retour);
}
/**
* @Route("/setages", name="ac_platform_setages")
*/
public function setages(Request $request)
{
$travellers = $this->travellerService->travellers;
if($travellers['idResidence'] != $request->request->get('id')) {
$travellers['idResidence'] = $request->request->get('id');
$this->travellerService->travellers = $travellers;
}
$result = $this->travellerService->setTravellers($request->request);
return new JsonResponse($result);
}
/**
* @Route("/getages", name="ac_platform_getages")
*/
public function getages(Request $request)
{
$type = $request->query->get("type", "enfants");
$travellers = $this->travellerService->travellers;
$nb = 0;
if($request->request->has("nb" . $type)) {
$nb = $request->request->get("nb" . $type);
} else {
$nb = $travellers['actnb' . $type];
}
$error = 0;
foreach($travellers['ages' . $type] as $ages) {
if (empty($ages)) {$error ++;}
}
$maxDate = date("Y-m-d");
if($type == "enfants") {
$date18YearsAgo = strtotime("-18 years");
$minDate = date("Y-m-d", $date18YearsAgo);
} elseif($type == "bebes") {
$date2YearsAgo = strtotime("-2 years");
$minDate = date("Y-m-d", $date2YearsAgo);
}
$retour = array(
"nb" . $type => $nb,
"minDate" => $minDate,
"maxDate" => $maxDate,
"travellers" => $travellers,
"error" => $error
);
return $this->render('/front/Recherche/ages_' . $type . '.html.twig', $retour);
}
/**
* @Route("/majFormule", name="ac_platform_majFormule")
*/
public function majFormule()
{
return $this->render('/front/Reservation/majformule.html.twig');
}
public function mobile()
{
return $this->render('/front/Recherche/mobile.html.twig');
}
public function gettarif(Request $request)
{
$idpack = $request->request->get("idpack");
$idtypo = $request->request->get("idtypo");
$retour = array(
"reponse" => null,
"defaultcontenutypo" => null,
"defaultcontenuformule" => null
);
return $this->render('/front/Produit/recaptarif.html.twig', $retour);
}
public function adultes($nbadultes = 1)
{
return $this->render('/front/Moteur/adultes.html.twig', ['nbadultes' => $nbadultes]);
}
public function enfants($nbenfants = 0)
{
return $this->render('/front/Moteur/enfants.html.twig', ['nbenfants' => $nbenfants]);
}
public function bebes($nbbebes = 0)
{
return $this->render('/front/Moteur/bebes.html.twig', ['nbbebes' => $nbbebes]);
}
}