<?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\Contracts\Translation\TranslatorInterface;
use App\Utils\Residences as ResidencesUtil;
use App\Utils\IresaManager;
use App\Services\SeasonService;
use App\Services\iresaService;
use App\Entity\Residence;
use App\Entity\mediaResidence;
use App\Entity\Language;
use App\Entity\Theme;
use App\Services\ToolService;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
/**
* @Route("/{_locale}", requirements={"_locale": "en|fr"})
*/
class ThemeController extends AbstractController
{
private $em;
private $session;
private $flashbag;
private $user;
private $seasonService;
private $toolService;
private $cacheManager;
private $helper;
private $translator;
private $residencesUtil;
private $iresaManager;
function __construct(EntityManagerInterface $em, SessionInterface $session, SeasonService $seasonService, ToolService $toolService, CacheManager $cacheManager, UploaderHelper $helper, TranslatorInterface $translator, ResidencesUtil $residencesUtil, IresaManager $iresaManager) {
$this->em = $em;
$this->session = $session;
$this->flashbag = $this->session->getFlashBag();
$this->seasonService = $seasonService;
$this->toolService = $toolService;
$this->cacheManager = $cacheManager;
$this->helper = $helper;
$this->translator = $translator;
$this->residencesUtil = $residencesUtil;
$this->iresaManager = $iresaManager;
}
/**
* @Route("/theme/{slug}/{id}", name="ac_platform_theme")
*/
public function themeAction($slug, $id, Request $request)
{
$locale = $request->getLocale();
$lang = $this->em->getRepository(Language::class)->findOneByCode($locale);
/*
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
*/
if ($request->query->has('datearrivee') ) {
$datearrivee = $request->query->get('datearrivee');
$NbDeltaJour = 7;
} else {
// s'il n'y a pas de date d'arrivée, on prend la date du jour et on cherche les location autour de cette date
$datearrivee = date('d/m/Y');
$NbDeltaJour = 30;
}
$duree = 0;
if ($request->query->has('duree') ) {
$duree = $request->query->get('duree');
}
$currentPage = $request->query->get('page') ?? 1;
$pageSize = 25;
$obj = array(
"ListThemes" => $id,
"DateDebut" => $datearrivee,
'Duree' => $duree,
'NbDeltaJour' => $NbDeltaJour,
"DetailParPrestation" => true,
"pageSize" => $pageSize,
"page" => $currentPage
);
$result = $this->iresaManager->getDataResultDetail($obj);
$hebergements = $result["FichesLieuHebergement"];
$reponses = $result["ListReponses"];
$prestations = $result["FichesPrestation"];
$maxReponse = $result["MaxReponse"];
$nbPages = ceil($maxReponse / $pageSize);
$countheb = 0;
$listeHebergements = "";
foreach($hebergements as $heb) {
if($heb['IdElement'] > 0) {
$listeHebergements .= $heb['IdElement'] . ",";
}
if(count($heb["ListFiches"])>0) {
$countheb ++;
$hebergement = $heb;
}
}
$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', array('slug' => $slug, 'id' => $id)));
}
/*
Si plusieurs hébergements sont remontés => redirection vers la page destination multi produit correspondante
*/
$packages = [];
$titre = "";
$description = "";
if($request->query->has('search')) {
$titre = $this->translator->trans('themes.noResult.titre', [], 'app');;
$description = $this->translator->trans('themes.noResult.description', [], 'app');
} else {
/*
[Traductions] TODO
Mise à jour entité themes + édition BO
*/
$params = [
'code_traduction' => $id,
'lang_id' => $lang->getId()
];
$theme = $this->em->getRepository(Theme::class)->findOneByLang($params);
if ($theme instanceof Theme) {
$titre = $theme->getTitre() ?? $theme->getNom();
$description = $theme->getDescription();
}
}
$this->residencesUtil->setLocale($locale);
$fiches = $this->residencesUtil->getSejours($hebergements, $reponses, $lang);
$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');
if($image_path) {
$image_link = $this->cacheManager->getBrowserPath($image_path, 'offre');
$photoProfil[$k] = $image_link;
}
}
}
$k++;
}
$vars = [
"page_code" => "theme", // ok
"slug" => $slug, // ok
"id" => $id, // ok
"currentPage" => $currentPage, // ok
"nbPages" => $nbPages, // ok
"maxReponse" => $maxReponse, // ok
"mod" => "destination", // ok
"searchBloc" => true, // ok
"aff_date" => false, // ok
'titre' => $titre, // ok
'description' => $description, // ok
"hebergements" => $hebergements, // ok
"reponses" => $fiches, // ok
"photoProfil" => $photoProfil, // ok
];
return $this->render('/front/Recherche/theme.html.twig', $vars);
}
}