<?php
namespace App\Controller;
use App\Repository\Otpusk\GeoSeoUrlRepository;
use App\Services\PageGenerator;
use App\Services\HotelService;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
class HotelController extends AbstractController
{
private $em;
private $hotelService;
public function __construct(EntityManager $entityManager, HotelService $hotelService)
{
$this->em = $entityManager;
$this->hotelService = $hotelService;
}
/**
* @Route(
* "/sitemaps/hotels/{page}",
* name="app_hotel_sitemaps",
* methods={"GET"},
* priority="99",
* requirements={"page": "(|\d+)"},
* defaults={"page": "1"}
* )
*/
public function sitemapsHotels(Request $request)
{
return $this->render('hotel/sitemaps.html.twig', [
'data' => $this->hotelService->getSitemapsHotelsData(($request->get('page', 1) != "") ? $request->get('page', 1) : 1),
'seo' => $this->hotelService->getSeoSitemaps($request->getLocale()),
]);
}
/**
* @Route(
* "/hotel/{hotelId}-{hotelSlug}",
* name="app_hotel",
* methods={"GET"},
* priority="100"
* )
*/
public function hotel(Request $request, $hotelId, $hotelSlug)
{
$market = $request->getSession()->get('market');
$locale = $request->getLocale();
$offerId = $request->get('offerId');
$robotsRequest = (is_null($request->get('_escaped_fragment_'))) ? false : true;
$noIndex = isset($offerId) && $offerId || $robotsRequest;
try {
$hotel = $this->hotelService->getHotelInfo($market, $hotelId, $locale);
} catch (\Exception $exception) {
throw new NotFoundHttpException('This page not found.');
}
$seo = $this->hotelService->getSeo($hotel, $locale, $noIndex);
// $exchangeRates = $this->hotelService->getExchangeRates();
// $cityNames = $this->hotelService->getCityNames($market);
$mainServices = $this->hotelService->getMainServices($market, $hotel, $locale);
$similars = $this->hotelService->getSimilars($market, $hotel, $locale);
$nearests = $this->hotelService->getNearests($market, $hotel, $locale);
$recommended = $this->hotelService->getRecommended($market, $hotel, $locale);
$recommended = array_slice($recommended, 0, 5);
$reviewsData = $this->hotelService->getReviews($hotel, $locale);
$reasons = $this->hotelService->getReasons($hotel, $locale);
$chartsDesktop = $this->hotelService->getCharts($market, $hotelId);
$chartsMobile = $this->getChartsMobile($chartsDesktop);
$dateNextDesktop = new \DateTime();
$dateNextDesktop = $dateNextDesktop->modify('+15 days')->format('Y-m-d');
$dateNextMobile = new \DateTime();
$dateNextMobile = $dateNextMobile->modify('+9 days')->format('Y-m-d');
$response = new Response($this->renderView('hotel/index.html.twig', [
'hotel' => $hotel,
'hotelId' => $hotelId,
'seo' => $seo,
// 'exchangeRates' => $exchangeRates,
// 'countryCurrency' => $market['currency']['code'],
// 'currencyAliases' => HotelService::$currencyAliases,
// 'cityNames' => $cityNames,
'mainServices' => $mainServices,
'similars' => $similars,
'nearests' => $nearests,
'recommended' => $recommended,
'reviewsData' => $reviewsData,
'reasons' => $reasons,
'chartsDesktop' => $chartsDesktop,
'chartsMobile' => $chartsMobile,
'dateNextDesktop' => $dateNextDesktop,
'dateNextMobile' => $dateNextMobile,
]), 200);
if ($noIndex) $response->headers->set('X-Robots-Tag', 'noindex');
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 'true');
return $response;
}
/**
* @Route("/getCharts", name="app_get_charts", methods={"POST"})
*/
public function getHotels(Request $request)
{
$market = $request->getSession()->get('market');
$hotelId = $request->get('hotelId');
$countryId = $request->get('countryId');
$date = $request->get('date');
$count = $request->get('count');
if (empty($count) || $count<0) $count=1;
$dateNext = new \DateTime($date);
$dateNext = $dateNext->modify('+' . $count . ' days')->format('Y-m-d');
$charts = $this->hotelService->getCharts($market, $hotelId, $date, $count);
$charts = $this->renderView('hotel/parts/charts.html.twig', [
'charts' => $charts,
'hotelId' => $hotelId,
'countryId' => $countryId,
'market' => $market,
]);
return new JsonResponse([
'charts' => $charts,
'dateNext' => $dateNext,
]);
}
public function getChartsMobile($chartsDesktop)
{
$countMobile = 9;
$chartsMobile = [];
$i = 1;
foreach ($chartsDesktop as $k => $v) {
if ($i == 1 && count($v) >= $countMobile) {
$chartsMobile[$k] = array_slice($v, 0, $countMobile);
break;
} else {
if ($i == 1) {
$chartsMobile[$k] = $v;
$countFirst = count($v);
} elseif ($i == 2) {
$chartsMobile[$k] = array_slice($v, 0, $countMobile - $countFirst);
}
}
$i++;
}
return $chartsMobile;
}
}