<?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 Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Utils\IresaManager;
use App\Utils\Residences;
use App\Services\ToolService;
use App\Services\SeasonService;
use App\Entity\Typologie;
use App\Entity\Residence;
use App\Entity\mediaResidence;
use App\Entity\Language;
use App\Entity\Destination;
/**
* @Route("/{_locale}", requirements={"_locale": "en|fr"})
*/
class DestinationController extends AbstractController
{
private $em;
private $session;
private $flashbag;
private $user;
private $seasonService;
private $iresaManager;
private $toolService;
private $helper;
private $cacheManager;
private $translator;
private $residenceService;
function __construct(EntityManagerInterface $em, SessionInterface $session, SeasonService $seasonService, IresaManager $iresaManager, ToolService $toolService, UploaderHelper $helper, CacheManager $cacheManager, TranslatorInterface $translator, Residences $residenceService) {
$this->em = $em;
$this->session = $session;
$this->flashbag = $this->session->getFlashBag();
$this->seasonService = $seasonService;
$this->iresaManager = $iresaManager;
$this->toolService = $toolService;
$this->helper = $helper;
$this->cacheManager = $cacheManager;
$this->translator = $translator;
$this->residenceService = $residenceService;
}
/**
* @Route("/destination/{slug}/{id}", name="ac_platform_destination")
*/
public function destinationActionRequest (Request $request, $slug, $id)
{
if($id == 0) { $id =2;} // par défaut => n'importe quelle destination => id = 2 => France
$locale = $request->getLocale();
$lang = $this->em->getRepository(Language::class)->findOneByCode($locale);
$params = [
'lang_id' => $lang->getId(),
'code_traduction' => $id
];
$destination = $this->em->getRepository(Destination::class)->findOneByLang($params);
if(!$destination ) {
throw $this->createNotFoundException('Destination inexistante');
}
/* on récupère les paramètres de recherche s'il y en a */
if ($request->query->has('datearrivee') ) {
$datearrivee = $request->query->get('datearrivee');
$NbDeltaJour = 0;
} else {
// s'il n'y a pas de date d'arrivée, on prend la date du jour et on cherche les locations autour de cette date
$NbDeltaJour = 30;
$now = new \DateTime();
$now->add(new \DateInterval('P' . intval($NbDeltaJour/2) . 'D'));
//$datearrivee = $now->format('Y-m-d');
$datearrivee = "";
}
/*
* par défaut on teste les dispos pour une durée de 7 jours
*/
$listDurees = [];
if($request->query->has('duree') && $request->query->get('duree') != '') {
$listDurees[] = $request->query->get('duree');
} else {
$listDurees = [7, 14, 21, 5, 4, 3, 2];
}
//$idTypePrestations = [1, 2];
$nbpax = $request->query->get('nbpax');
$nbadultes = $request->query->get('nbadultes');
$nbenfants = $request->query->get('nbenfants');
$search = $request->query->get('search');
/*
interrogation iresa avec les paramètres donnés en paramétre
exemple pour la page multi-destination "Montagne" sans paramètre
http://ateya-imhotep-prod.i-resa.com/imhotep/DataResultDetail/1/e9eb5b74-2565-42ca-9c1a-d067c8aee882/?ListLieu=10&DateDebut=&Duree=7&DetailParPrestation=1&pageSize=150
*/
foreach($listDurees as $duree) {
$obj = [
"DateDebut" => $datearrivee,
'Duree' => $duree,
//'NbPax' => $nbpax, // Ce paramètre ne fonctionne pas
'NbDeltaJour' => $NbDeltaJour,
"DetailParPrestation" => true,
//"tri" => "PrixTotal",
"tri" => "DateDebut",
//"IdTypePrestation" => 1, // il ne faut pas utiliser ce paramètre car on loupe les formules packages
"pageSize" => 800
];
if ($id > 0) {
$obj["ListLieu"] = $id;
}
$result = $this->iresaManager->getDataResultDetail($obj);
$hebergements = $result["FichesLieuHebergement"];
// si on enleve "IdTypePrestation" => 1, le test ne marche pas
$count = 0;
foreach ($hebergements as $hebergement) {
if($hebergement['IdElement'] > 0) {
$count += count($hebergement["ListFiches"]);
break;
}
}
echo "<!-- Durée : " . $duree . " => Héb : " . $count . "-->";
if(array_key_exists('MaxReponse', $result) && $result['MaxReponse'] > 0 && $count > 0) {
break;
}
}
/*
Il n'y a pas de resultat, On teste la dispo sans prérequis de durée
*/
if(empty($result) || $result['MaxReponse'] == 0) {
$obj['Duree'] = null;
$result = $this->iresaManager->getDataResultDetail($obj);
/*
Il n'y a pas de resultat, la destination n'est pas disponible
On redirige vers la page d'erreur
*/
if(empty($result) || $result['MaxReponse'] == 0) {
$code = $this->translator->trans('erreur.erreurs.100', [], 'app');
return $this->redirect($this->generateUrl('ac_platform_error', array('code' => $code )));
}
}
echo "<!-- \n\nREQUETE IRESA \nhttp://ateya-imhotep-prod.i-resa.com/imhotep/DataResultDetail/1/e9eb5b74-2565-42ca-9c1a-d067c8aee882/?ListLieu=" . $id . "&DateDebut=" . $obj['DateDebut'] . "&Duree=" . $obj['Duree'] . "&NbDeltaJour=". $obj['NbDeltaJour'] ."&DetailParPrestation=1&pageSize=800 \n\n-->";
$hebergements = $result["FichesLieuHebergement"];
$reponses = $result["ListReponses"];
$prestations = $result["FichesPrestation"];
$countheb = 0;
$listeHebergements = "";
foreach($hebergements as $heb) {
if($heb['IdElement'] > 0) {
$listeHebergements .= $heb['IdElement'] . ",";
}
if(count($heb["ListFiches"])>0) {
$countheb ++;
// Sauvegarde le dernier hébergement trouvé, utilisé pour redirection en cas d'hébergement unique trouvé
$hebergement = $heb;
}
}
if($countheb == 0) {
$code = $this->translator->trans('erreur.erreurs.101', [], 'app');
return $this->redirect($this->generateUrl('ac_platform_error', array('code' => $code )));
}
$typologies = null;
/*
Si un seul hébergement est remonté => redirection vers la page produit correspondante
*/
if($countheb == 1 ) {
$slug = $this->toolService->slugify(str_replace("*", "", $hebergement["ListFiches"]["zero"]["nom"]));
$id = $hebergement["IdElement"];
return $this->redirect($this->generateUrl('ac_platform_produit', [
'slug' => $slug,
'id' => $id,
'datearrivee' => $datearrivee,
'duree' => $duree,
'nbadultes' => $nbadultes,
'nbenfants' => $nbenfants,
]));
}
/*
Si plusieurs hébergements sont remontés => redirection vers la page destination multi produit correspondante
*/
$packages = [];
if($request->query->has('search')) {
$titre = $this->translator->trans('themes.noResult.titre', [], 'app');
$description = $this->translator->trans('themes.noResult.description', [], 'app');
} else {
$titre = $destination->getTitreLong();
$description = $destination->getIntroduction();
}
$this->residenceService->setLocale($locale);
$fiches = $this->residenceService->getSejours($hebergements, $reponses, $lang, $nbpax);
$photoProfil = [];
$k=0;
foreach($fiches as $fiche) {
$residence = $fiche['residence'];
if($residence instanceof residence) {
$season = $this->seasonService->getSeason();
$slider = $this->em->getRepository(mediaResidence::class)->findOneByLocation($residence->getId(), "content", $lang, $season);
if($slider instanceof mediaResidence) {
$image_path = $this->helper->asset($slider, 'imageFile');
$image_link = $this->cacheManager->getBrowserPath($image_path, 'offre');
$photoProfil[$k] = $image_link;
}
}
$k++;
}
$aff_date = $request->query->has('aff_date') ? $request->query->get('aff_date') : false;
$vars = array(
"page" => "destination",
"id" => $id,
"mod" => "destination",
"hebergements" => $hebergements,
"searchBloc" => true,
"aff_date" => $aff_date,
"datearrivee" => $datearrivee,
"duree" => $duree,
"nbadultes" => $nbadultes,
"nbenfants" => $nbenfants,
'nbpax' => $nbpax,
'titre' => $titre,
'description' => $description,
"reponses" => $fiches,
"photoProfil" => $photoProfil,
"metaTitle" => $destination->getMetaTitle(),
"metaDescription" => $destination->getMetaDescription(),
"seoTitle" => $destination->getSeoTitle(),
"seoContent" => $destination->getSeoContent()
);
return $this->render('/front/Recherche/destination.html.twig', $vars);
}
}