<?php
namespace App\Controller;
use App\Twig\LinkGenerator;
use App\Twig\ObjectExtension;
use Carbon\Carbon;
use Elements\Bundle\CmsToolsBundle\Tool\Helper\FunctionsHelper;
use Knp\Component\Pager\PaginatorInterface;
use Pimcore\Model\DataObject\BusTravel;
use Pimcore\Model\DataObject\Package;
use Pimcore\Model\DataObject\YoungStyriaAccommodation;
use Pimcore\Model\DataObject\YoungStyriaInfrastructure;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
class YoungStyriaController extends AbstractController
{
private PaginatorInterface $paginator;
private LinkGenerator $linkGenerator;
private ObjectExtension $objectExtension;
public function __construct(PaginatorInterface $paginator, LinkGenerator $linkGenerator, ObjectExtension $objectExtension) {
$this->paginator = $paginator;
$this->linkGenerator = $linkGenerator;
$this->objectExtension = $objectExtension;
}
public function overviewAction(Request $request) {
$select = $this->getDocumentEditable('select', 'youngStyriaOverviewSelect')->getData();
$randomize = $this->getDocumentEditable('checkbox', 'randomizeYoungStyria')->getData();
if($select == 'accommodation') {
$listing = new YoungStyriaAccommodation\Listing();
} else {
$listing = new YoungStyriaInfrastructure\Listing();
}
if($randomize) {
$listing->setOrderKey('Rand(' . Carbon::today()->unix() . ')', false);
}
$perPage = $this->getDocumentEditable('numeric', 'perPage')->getData() ?: 8;
$paginator = $this->paginator->paginate($listing, $request->get('page', 1), $perPage);
if ($request->isXmlHttpRequest() && $request->get('pois', false)) {
return $this->getMapdata($paginator);
}
if($request->get('ajax') && $request->isXmlHttpRequest()) {
$response =[
"success" => true,
"content" => [
'result-main' => $this->render('YoungStyria/Includes/young-styria-list.html.twig', [
'items' => $paginator,
])->getContent(),
'result-paging' => $this->renderView('Navigation/paging.html.twig', [
'styleModifier' => 'justify-content-center',
'ajaxClass' => 'js-ajax-form-map__link',
'paginator' => $paginator
]),
],
];
return new JsonResponse($response);
}
return $this->renderTemplate('YoungStyria/overview.html.twig', [
'paginator' => $paginator
]);
}
public function youngStyriaAccommodationDetailAction(Request $request) {
$ysAcco = YoungStyriaAccommodation::getById($request->get('id'));
if(!$ysAcco || !$ysAcco->getName()) {
throw new NotFoundHttpException('object not found');
}
return $this->renderTemplate('YoungStyria/youngStyriaAccommodationDetail.html.twig', [
'ysAcco' => $ysAcco,
'weatherStation' => $this->getWeatherStationByYSObject($ysAcco),
'keyData' => $this->prepareYSKeydata($ysAcco),
'recommendations' => $this->getRecommendationInfrastructure($ysAcco),
'packages' => $this->getPackageTeaserByYoungStyriaObject($ysAcco),
]);
}
public function youngStyriaInfrastructureDetailAction(Request $request) {
$ysInfra = YoungStyriaInfrastructure::getById($request->get('id'));
if(!$ysInfra || !$ysInfra->getName()) {
throw new NotFoundHttpException('object not found');
}
return $this->renderTemplate('YoungStyria/youngStyriaInfrastructureDetail.html.twig', [
'ysInfra' => $ysInfra,
'weatherStation' => $this->getWeatherStationByYSObject($ysInfra),
'keyData' => $this->prepareYSKeydata($ysInfra),
'recommendations' => $this->getRecommendationAccommondation($ysInfra),
'packages' => $this->getPackageTeaserByYoungStyriaObject($ysInfra),
]);
}
/**
* @Route("/{_locale}/interactive-young-styria-map-info-box", name="interactive-young-styria-map-info-box")
*/
public function getBusMapInfoBox(Request $request) {
$ysObject = YoungStyriaInfrastructure::getById($request->get('id'));
if(!$ysObject) {
$ysObject = YoungStyriaAccommodation::getById($request->get('id'));
}
$document = $this->document;
$locale = $document->getProperty('language') ?? $request->getLocale();
if($ysObject) {
$wysiwyg = '<ul>';
foreach($ysObject->getBulletPoints($locale) as $bulletpoint) {
$wysiwyg .= '<li>' . $bulletpoint['bulletPointText']->getData() . '</li>';
}
$wysiwyg .= '</ul>';
return $this->json([
'success' => true,
'html' => $this->renderView('Convention/Includes/interactive-map-info-box.html.twig', [
'image' => $ysObject->getTeaserImage(),
'subtitle' => $ysObject->getShortDescription($locale),
'title' => $ysObject->getName($locale),
'wysiwyg' => $wysiwyg,
'link' => $this->linkGenerator->generate($ysObject, ['document' => $document]),
'id' => $request->get('id')
])
]);
}
}
private function getMapdata(\Knp\Component\Pager\Pagination\PaginationInterface $paginator)
{
$geoData = [];
foreach ($paginator as $ysObject) {
if($ysObject) {
$geo = $ysObject->getMap();
if($geo) {
$geoData[] = [
"id" => $ysObject->getId(),
"lat" => $geo->getLatitude(),
"lng" => $geo->getLongitude(),
"detailInfoBoxUrl" => $this->generateUrl('interactive-young-styria-map-info-box',[
'id' => $ysObject->getId(),
])
];
}
}
}
return new JsonResponse([
"success" => true,
"pois" => $geoData
]);
}
/**
* @param YoungStyriaInfrastructure|YoungStyriaAccommodation $ysObject
* @return mixed
*/
private function getWeatherStationByYSObject($ysObject) {
$weatherStation = $ysObject->getWeatherStation();
if(!$weatherStation) {
if(count($ysObject->getCommunity()) > 0) {
$weatherStation = $ysObject->getCommunity()[0]->getPrognosis();
} elseif(count($ysObject->getRegion()) > 0) {
$weatherStation = $ysObject->getRegion()[0]->getWeatherStation();
}
}
return $weatherStation;
}
/**
* @param YoungStyriaAccommodation|YoungStyriaInfrastructure $ysInfra
* @return array
*/
private function prepareYSKeydata($ysInfra) : array {
$keyData = [];
if($ysInfra instanceof YoungStyriaInfrastructure) {
foreach ($ysInfra->getOpeningTimes() as $blockElement) {
if($blockElement['localizedfields'] && $blockElement['localizedfields']->getData() && $blockElement['localizedfields']->getData()->getLocalizedValue('time')) {
$keyData['openingTimes'][] = $blockElement['localizedfields']->getData()->getLocalizedValue('time');
}
}
if($ysInfra->getOpeningPeriod()) {
$keyData['openingPeriod'] = $ysInfra->getOpeningPeriod();
}
if($ysInfra->getGuides()) {
$keyData['guides'] = $ysInfra->getGuides();
} if($ysInfra->getVisitDuration()) {
$keyData['visitDuration'] = $ysInfra->getVisitDuration();
}
} else {
foreach ($ysInfra->getHotelFacilitiesBlock() as $blockElement) {
if($blockElement['localizedfields'] && $blockElement['localizedfields']->getData() && $blockElement['localizedfields']->getData()->getLocalizedValue('facility')) {
$keyData['facilities'][] = $blockElement['localizedfields']->getData()->getLocalizedValue('facility');
}
}
foreach ($ysInfra->getGastronomyBlock() as $blockElement) {
if($blockElement['localizedfields'] && $blockElement['localizedfields']->getData() && $blockElement['localizedfields']->getData()->getLocalizedValue('gastronomy')) {
$keyData['gastronomy'][] = $blockElement['localizedfields']->getData()->getLocalizedValue('gastronomy');
}
}
foreach ($ysInfra->getLeisureFacilitiesBlock() as $blockElement) {
if($blockElement['localizedfields'] && $blockElement['localizedfields']->getData() && $blockElement['localizedfields']->getData()->getLocalizedValue('leisureFacility')) {
$keyData['leisureFacilities'][] = $blockElement['localizedfields']->getData()->getLocalizedValue('leisureFacility');
}
}
}
foreach ($ysInfra->getGettingThereBlock() as $blockElement) {
if($blockElement['localizedfields'] && $blockElement['localizedfields']->getData() && $blockElement['localizedfields']->getData()->getLocalizedValue('gettingThere')) {
$keyData['gettingThere'][] = $blockElement['localizedfields']->getData()->getLocalizedValue('gettingThere');
}
}
foreach ($ysInfra->getSpecialFeaturesBlock() as $blockElement) {
// p_r($blockElement['localizedfields']->getData());
if($blockElement['localizedfields'] && $blockElement['localizedfields']->getData() && $blockElement['localizedfields']->getData()->getLocalizedValue('specialFeature')) {
$keyData['specialFeatures'][] = $blockElement['localizedfields']->getData()->getLocalizedValue('specialFeature');
}
}
$keyData['motorwayExit'] = $ysInfra->getMotorwayExit();
return $keyData;
}
/**
* @param YoungStyriaInfrastructure $ysObject
*/
private function getRecommendationAccommondation($ysObject)
{
$recommendation = $ysObject->getManualdetailRadiusYSAcco();
if(empty($recommendation)) {
$accos = new YoungStyriaAccommodation\Listing();
if($ysObject->getDetailRadius()) {
$functionsHelper = new FunctionsHelper();
$distance = $functionsHelper->getGeoDistanceQuery($ysObject->getMap(), 'map');
$accos->addConditionParam('(' . $distance . ') <= :detailRadius', ['detailRadius' => $ysObject->getDetailRadius()]);
}
$accos->setOrderKey('RAND()', false);
$accos->setLimit(4);
$recommendation = $accos->getObjects();
} else {
shuffle($recommendation);
$recommendation = array_slice($recommendation, 0, 4);
}
$returnData = [];
foreach ($recommendation as $reco) {
$returnData[] = $this->objectExtension->getYoungStyriaFlatrateTeaserData($reco);
}
return $returnData;
}
private function getRecommendationInfrastructure(YoungStyriaAccommodation $ysObject) {
$recommendation = $ysObject->getManualdetailRadiusYSInfra();
if(empty($recommendation)) {
$accos = new YoungStyriaInfrastructure\Listing();
if($ysObject->getDetailRadius()) {
$functionsHelper = new FunctionsHelper();
$distance = $functionsHelper->getGeoDistanceQuery($ysObject->getMap(), 'geoposition');
$accos->addConditionParam('(:distance) <= :detailRadius', ['distance' => $distance, 'detailRadius' => $ysObject->getDetailRadius()]);
}
$accos->setOrderKey('RAND()', false);
$accos->setLimit(4);
$recommendation = $accos->getObjects();
} else {
shuffle($recommendation);
$recommendation = array_slice($recommendation, 0, 4);
}
$returnData = [];
foreach ($recommendation as $reco) {
$returnData[] = $this->objectExtension->getYoungStyriaFlatrateTeaserData($reco);
}
return $returnData;
}
/**
* @param YoungStyriaInfrastructure|YoungStyriaAccommodation $ysObject
* @param int $numOfTeaser
* @return array
*/
private function getPackageTeaserByYoungStyriaObject($ysObject, $numOfTeaser = 3) {
if($ysObject->getPackages()) {
$tmpPackages = $ysObject->getPackages();
shuffle($tmpPackages);
$teaserPackages = array_slice($tmpPackages, 0, $numOfTeaser);
} else {
$packageListing = new Package\Listing();
$packageListing->addConditionParam('isYoungStyria is not null and isYoungStyria = 1');
$packageListing->setOrderKey('RAND()', false);
$packageListing->setLimit($numOfTeaser);
$teaserPackages = $packageListing->getObjects();
}
$teaserdata = [];
/**
* @var Package $package
*/
foreach ($teaserPackages as $package) {
$teaserdata[] = [
"title" => $package->getTitle(),
"href" => $this->linkGenerator->generate($package),
"image" => $package->getTeaserImage(),
// "badgeText" => $infra->get(),
"isInNav" => false,
"isSearchTeaser" => false,
"ratio" => null
];
}
return $teaserdata;
}
}