<?php
namespace App\Controller;
use Doctrine\ORM\EntityManagerInterface;
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 Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Exception;
use Symfony\Component\String\UnicodeString;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use App\Entity\TemporaryReservation;
use App\Entity\Promo;
use App\Entity\Venteflash;
use App\Entity\Residence;
use App\Entity\mediaResidence;
use App\Entity\Language;
use App\Services\SeasonService;
use App\Services\ToolService;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
/**
* @Route("/{_locale}/promos", requirements={"_locale": "en|fr"})
*/
class PromosController extends AbstractController
{
/**
* @var EntityManagerInterface
*/
private $em;
private $session;
private $flashbag;
private $user;
private $filtre;
private $seasonService;
private $toolService;
private $cacheManager;
private $helper;
private $translator;
private $appKernel;
function __construct(EntityManagerInterface $em, SessionInterface $session, SeasonService $seasonService, ToolService $toolService, CacheManager $cacheManager, UploaderHelper $helper, TranslatorInterface $translator, KernelInterface $appKernel) {
$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->appKernel = $appKernel;
$this->filtre = [
'environnement' => 'environnement',
'lieu' => 'slug_lieu',
'ville' => 'slug_ville',
'region' => 'slug_region'
];
}
/**
* @Route("/", name="_list")
*/
public function promosList (Request $request)
{
$promos = $this->em->getRepository(Promo::class)->findAll();
$photoProfil = [];
foreach($promos as $promo) {
$residence = $this->em->getRepository(Residence::class)->findOneByIdiresa($promo->getIdLieuHebergement());
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[$promo->getId()] = $image_link;
}
}
}
$vars = [
"page_code" => "promo",
"filtres" => false,
"category" => "all",
"promos" => $promos,
"photoProfil" => $photoProfil
];
return $this->render('/front/Promos/index.html.twig', $vars);
}
/**
* @Route("/{offre_category}", name="ac_platform_promos_category_list", options={"expose"=true})
*/
public function promosCategoryList (Request $request, $offre_category)
{
$locale = $request->getLocale();
$lang = $this->em->getRepository(Language::class)->findOneByCode($locale);
$lang_default = $this->em->getRepository(Language::class)->findOneByCode('fr');
$method = $this->toolService->snakeToCamelCase('get-'. $offre_category);
$repository = $this->em->getRepository(Promo::class);
if(!method_exists( $repository, $method)) {
$vars = [
"category" => 'error.unknown',
"promos" => []
];
return $this->render('/front/Promos/index.html.twig', $vars);
}
$promos = $repository->{$method}();
$environnements = [];
$regions = [];
$villes = [];
$photoProfil = [];
$listPromos = [];
foreach($promos as $promo) {
if(!in_array(ucfirst($promo->getEnvironnement()), $environnements)) {
$environnements[ $promo->getEnvironnement()] = ucfirst($promo->getEnvironnement());
}
if(!in_array($promo->getRegion(), $regions)) {
$regions[$promo->getSlugRegion()] = $promo->getRegion();
}
if(!in_array($promo->getVille(), $villes)) {
$villes[$promo->getSlugVille()] = $promo->getVille();
}
$residence = $this->em->getRepository(Residence::class)->findOneByIdiresa($promo->getIdLieuHebergement());
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[$promo->getId()] = $image_link;
}
}
}
asort($environnements);
asort($regions);
asort($villes);
if($offre_category == "ventes-flash") {
$parameter = Yaml::parse(file_get_contents($this->appKernel->getProjectDir() .'/var/config/vente-flash.yaml'));
$venteFlash = new VenteFlash();
$venteFlash->setParameter($parameter);
$title = $venteFlash->{'operationTitre' . ucfirst($locale)};
$description = $venteFlash->{'operationDescription' . ucfirst($locale)};
} else {
$title = $this->translator->trans("promos.".$offre_category . ".title" , [], 'promos');
$description = $this->translator->trans("promos.".$offre_category . ".description" , [], 'promos');
}
$vars = [
"page_code" => "promo",
"filtres" => false,
"category" => $offre_category,
"promos" => $promos,
"environnements" => $environnements,
"regions" => $regions,
"villes" => $villes,
"photoProfil" => $photoProfil,
"title" => $title,
"description" => $description,
];
return $this->render('/front/Promos/index.html.twig', $vars);
}
}