src/Controller/YoungStyriaController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Twig\LinkGenerator;
  4. use App\Twig\ObjectExtension;
  5. use Carbon\Carbon;
  6. use Elements\Bundle\CmsToolsBundle\Tool\Helper\FunctionsHelper;
  7. use Knp\Component\Pager\PaginatorInterface;
  8. use Pimcore\Model\DataObject\BusTravel;
  9. use Pimcore\Model\DataObject\Package;
  10. use Pimcore\Model\DataObject\YoungStyriaAccommodation;
  11. use Pimcore\Model\DataObject\YoungStyriaInfrastructure;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. class YoungStyriaController extends AbstractController
  17. {
  18.     private PaginatorInterface $paginator;
  19.     private LinkGenerator $linkGenerator;
  20.     private ObjectExtension $objectExtension;
  21.     public function __construct(PaginatorInterface $paginatorLinkGenerator $linkGeneratorObjectExtension $objectExtension) {
  22.         $this->paginator $paginator;
  23.         $this->linkGenerator $linkGenerator;
  24.         $this->objectExtension $objectExtension;
  25.     }
  26.     public function overviewAction(Request $request) {
  27.         $select $this->getDocumentEditable('select''youngStyriaOverviewSelect')->getData();
  28.         $randomize $this->getDocumentEditable('checkbox''randomizeYoungStyria')->getData();
  29.         if($select == 'accommodation') {
  30.             $listing = new YoungStyriaAccommodation\Listing();
  31.         } else {
  32.             $listing = new YoungStyriaInfrastructure\Listing();
  33.         }
  34.         if($randomize) {
  35.             $listing->setOrderKey('Rand(' Carbon::today()->unix() . ')'false);
  36.         }
  37.         $perPage $this->getDocumentEditable('numeric''perPage')->getData() ?: 8;
  38.         $paginator $this->paginator->paginate($listing$request->get('page'1), $perPage);
  39.         if ($request->isXmlHttpRequest() && $request->get('pois'false)) {
  40.             return $this->getMapdata($paginator);
  41.         }
  42.         if($request->get('ajax') && $request->isXmlHttpRequest()) {
  43.             $response =[
  44.                 "success" => true,
  45.                 "content" => [
  46.                     'result-main' => $this->render('YoungStyria/Includes/young-styria-list.html.twig', [
  47.                         'items' => $paginator,
  48.                     ])->getContent(),
  49.                     'result-paging' => $this->renderView('Navigation/paging.html.twig', [
  50.                         'styleModifier' => 'justify-content-center',
  51.                         'ajaxClass' => 'js-ajax-form-map__link',
  52.                         'paginator' => $paginator
  53.                     ]),
  54.                 ],
  55.             ];
  56.             return new JsonResponse($response);
  57.         }
  58.         return $this->renderTemplate('YoungStyria/overview.html.twig', [
  59.             'paginator' => $paginator
  60.         ]);
  61.     }
  62.     public function youngStyriaAccommodationDetailAction(Request $request) {
  63.         $ysAcco YoungStyriaAccommodation::getById($request->get('id'));
  64.         if(!$ysAcco || !$ysAcco->getName()) {
  65.             throw new NotFoundHttpException('object not found');
  66.         }
  67.         return $this->renderTemplate('YoungStyria/youngStyriaAccommodationDetail.html.twig', [
  68.             'ysAcco' => $ysAcco,
  69.             'weatherStation' => $this->getWeatherStationByYSObject($ysAcco),
  70.             'keyData' => $this->prepareYSKeydata($ysAcco),
  71.             'recommendations' => $this->getRecommendationInfrastructure($ysAcco),
  72.             'packages' => $this->getPackageTeaserByYoungStyriaObject($ysAcco),
  73.         ]);
  74.     }
  75.     public function youngStyriaInfrastructureDetailAction(Request $request) {
  76.         $ysInfra YoungStyriaInfrastructure::getById($request->get('id'));
  77.         if(!$ysInfra || !$ysInfra->getName()) {
  78.             throw new NotFoundHttpException('object not found');
  79.         }
  80.         return $this->renderTemplate('YoungStyria/youngStyriaInfrastructureDetail.html.twig', [
  81.             'ysInfra' => $ysInfra,
  82.             'weatherStation' => $this->getWeatherStationByYSObject($ysInfra),
  83.             'keyData' => $this->prepareYSKeydata($ysInfra),
  84.             'recommendations' => $this->getRecommendationAccommondation($ysInfra),
  85.             'packages' => $this->getPackageTeaserByYoungStyriaObject($ysInfra),
  86.         ]);
  87.     }
  88.     /**
  89.      * @Route("/{_locale}/interactive-young-styria-map-info-box", name="interactive-young-styria-map-info-box")
  90.      */
  91.     public function getBusMapInfoBox(Request $request) {
  92.         $ysObject YoungStyriaInfrastructure::getById($request->get('id'));
  93.         if(!$ysObject) {
  94.             $ysObject YoungStyriaAccommodation::getById($request->get('id'));
  95.         }
  96.         $document $this->document;
  97.         $locale $document->getProperty('language') ?? $request->getLocale();
  98.         if($ysObject) {
  99.             $wysiwyg '<ul>';
  100.             foreach($ysObject->getBulletPoints($locale) as $bulletpoint) {
  101.                 $wysiwyg .= '<li>'  $bulletpoint['bulletPointText']->getData() . '</li>';
  102.             }
  103.             $wysiwyg .= '</ul>';
  104.             return $this->json([
  105.                 'success' => true,
  106.                 'html' => $this->renderView('Convention/Includes/interactive-map-info-box.html.twig', [
  107.                     'image' => $ysObject->getTeaserImage(),
  108.                     'subtitle' => $ysObject->getShortDescription($locale),
  109.                     'title' => $ysObject->getName($locale),
  110.                     'wysiwyg' => $wysiwyg,
  111.                     'link' => $this->linkGenerator->generate($ysObject, ['document' => $document]),
  112.                     'id' => $request->get('id')
  113.                 ])
  114.             ]);
  115.         }
  116.     }
  117.     private function getMapdata(\Knp\Component\Pager\Pagination\PaginationInterface $paginator)
  118.     {
  119.         $geoData = [];
  120.         foreach ($paginator as $ysObject) {
  121.             if($ysObject) {
  122.                 $geo $ysObject->getMap();
  123.                 if($geo) {
  124.                     $geoData[] = [
  125.                         "id" => $ysObject->getId(),
  126.                         "lat" => $geo->getLatitude(),
  127.                         "lng" => $geo->getLongitude(),
  128.                         "detailInfoBoxUrl" => $this->generateUrl('interactive-young-styria-map-info-box',[
  129.                             'id' => $ysObject->getId(),
  130.                         ])
  131.                     ];
  132.                 }
  133.             }
  134.         }
  135.         return new JsonResponse([
  136.             "success" => true,
  137.             "pois" => $geoData
  138.         ]);
  139.     }
  140.     /**
  141.      * @param YoungStyriaInfrastructure|YoungStyriaAccommodation $ysObject
  142.      * @return mixed
  143.      */
  144.     private function getWeatherStationByYSObject($ysObject) {
  145.         $weatherStation $ysObject->getWeatherStation();
  146.         if(!$weatherStation) {
  147.             if(count($ysObject->getCommunity()) > 0) {
  148.                 $weatherStation $ysObject->getCommunity()[0]->getPrognosis();
  149.             } elseif(count($ysObject->getRegion()) > 0) {
  150.                 $weatherStation $ysObject->getRegion()[0]->getWeatherStation();
  151.             }
  152.         }
  153.         return $weatherStation;
  154.     }
  155.     /**
  156.      * @param YoungStyriaAccommodation|YoungStyriaInfrastructure $ysInfra
  157.      * @return array
  158.      */
  159.     private function prepareYSKeydata($ysInfra) : array {
  160.         $keyData = [];
  161.         if($ysInfra instanceof YoungStyriaInfrastructure) {
  162.             foreach ($ysInfra->getOpeningTimes() as $blockElement) {
  163.                 if($blockElement['localizedfields'] && $blockElement['localizedfields']->getData() && $blockElement['localizedfields']->getData()->getLocalizedValue('time')) {
  164.                     $keyData['openingTimes'][] = $blockElement['localizedfields']->getData()->getLocalizedValue('time');
  165.                 }
  166.             }
  167.             if($ysInfra->getOpeningPeriod()) {
  168.                 $keyData['openingPeriod'] = $ysInfra->getOpeningPeriod();
  169.             }
  170.             if($ysInfra->getGuides()) {
  171.                 $keyData['guides'] = $ysInfra->getGuides();
  172.             } if($ysInfra->getVisitDuration()) {
  173.                 $keyData['visitDuration'] = $ysInfra->getVisitDuration();
  174.             }
  175.         } else {
  176.             foreach ($ysInfra->getHotelFacilitiesBlock() as $blockElement) {
  177.                 if($blockElement['localizedfields'] && $blockElement['localizedfields']->getData() && $blockElement['localizedfields']->getData()->getLocalizedValue('facility')) {
  178.                     $keyData['facilities'][] = $blockElement['localizedfields']->getData()->getLocalizedValue('facility');
  179.                 }
  180.             }
  181.             foreach ($ysInfra->getGastronomyBlock() as $blockElement) {
  182.                 if($blockElement['localizedfields'] && $blockElement['localizedfields']->getData() && $blockElement['localizedfields']->getData()->getLocalizedValue('gastronomy')) {
  183.                     $keyData['gastronomy'][] = $blockElement['localizedfields']->getData()->getLocalizedValue('gastronomy');
  184.                 }
  185.             }
  186.             foreach ($ysInfra->getLeisureFacilitiesBlock() as $blockElement) {
  187.                 if($blockElement['localizedfields'] && $blockElement['localizedfields']->getData() && $blockElement['localizedfields']->getData()->getLocalizedValue('leisureFacility')) {
  188.                     $keyData['leisureFacilities'][] = $blockElement['localizedfields']->getData()->getLocalizedValue('leisureFacility');
  189.                 }
  190.             }
  191.         }
  192.         foreach ($ysInfra->getGettingThereBlock() as $blockElement) {
  193.             if($blockElement['localizedfields'] && $blockElement['localizedfields']->getData() && $blockElement['localizedfields']->getData()->getLocalizedValue('gettingThere')) {
  194.                 $keyData['gettingThere'][] = $blockElement['localizedfields']->getData()->getLocalizedValue('gettingThere');
  195.             }
  196.         }
  197.         foreach ($ysInfra->getSpecialFeaturesBlock() as $blockElement) {
  198. //            p_r($blockElement['localizedfields']->getData());
  199.             if($blockElement['localizedfields'] && $blockElement['localizedfields']->getData() && $blockElement['localizedfields']->getData()->getLocalizedValue('specialFeature')) {
  200.                 $keyData['specialFeatures'][] = $blockElement['localizedfields']->getData()->getLocalizedValue('specialFeature');
  201.             }
  202.         }
  203.         $keyData['motorwayExit'] = $ysInfra->getMotorwayExit();
  204.         return $keyData;
  205.     }
  206.     /**
  207.      * @param YoungStyriaInfrastructure $ysObject
  208.      */
  209.     private function getRecommendationAccommondation($ysObject)
  210.     {
  211.         $recommendation $ysObject->getManualdetailRadiusYSAcco();
  212.         if(empty($recommendation)) {
  213.             $accos = new YoungStyriaAccommodation\Listing();
  214.             if($ysObject->getDetailRadius()) {
  215.                 $functionsHelper = new FunctionsHelper();
  216.                 $distance $functionsHelper->getGeoDistanceQuery($ysObject->getMap(), 'map');
  217.                 $accos->addConditionParam('(' $distance ') <= :detailRadius', ['detailRadius' => $ysObject->getDetailRadius()]);
  218.             }
  219.             $accos->setOrderKey('RAND()'false);
  220.             $accos->setLimit(4);
  221.             $recommendation $accos->getObjects();
  222.         } else {
  223.             shuffle($recommendation);
  224.             $recommendation array_slice($recommendation04);
  225.         }
  226.         $returnData = [];
  227.         foreach ($recommendation as $reco) {
  228.             $returnData[] = $this->objectExtension->getYoungStyriaFlatrateTeaserData($reco);
  229.         }
  230.         return $returnData;
  231.     }
  232.     private function getRecommendationInfrastructure(YoungStyriaAccommodation $ysObject) {
  233.         $recommendation $ysObject->getManualdetailRadiusYSInfra();
  234.         if(empty($recommendation)) {
  235.             $accos = new YoungStyriaInfrastructure\Listing();
  236.             if($ysObject->getDetailRadius()) {
  237.                 $functionsHelper = new FunctionsHelper();
  238.                 $distance $functionsHelper->getGeoDistanceQuery($ysObject->getMap(), 'geoposition');
  239.                 $accos->addConditionParam('(:distance) <= :detailRadius', ['distance' => $distance'detailRadius' => $ysObject->getDetailRadius()]);
  240.             }
  241.             $accos->setOrderKey('RAND()'false);
  242.             $accos->setLimit(4);
  243.             $recommendation $accos->getObjects();
  244.         } else {
  245.             shuffle($recommendation);
  246.             $recommendation array_slice($recommendation04);
  247.         }
  248.         $returnData = [];
  249.         foreach ($recommendation as $reco) {
  250.             $returnData[] = $this->objectExtension->getYoungStyriaFlatrateTeaserData($reco);
  251.         }
  252.         return $returnData;
  253.     }
  254.     /**
  255.      * @param YoungStyriaInfrastructure|YoungStyriaAccommodation $ysObject
  256.      * @param int $numOfTeaser
  257.      * @return array
  258.      */
  259.     private function getPackageTeaserByYoungStyriaObject($ysObject$numOfTeaser 3) {
  260.         if($ysObject->getPackages()) {
  261.             $tmpPackages $ysObject->getPackages();
  262.             shuffle($tmpPackages);
  263.             $teaserPackages array_slice($tmpPackages0$numOfTeaser);
  264.         } else {
  265.             $packageListing = new Package\Listing();
  266.             $packageListing->addConditionParam('isYoungStyria is not null and isYoungStyria = 1');
  267.             $packageListing->setOrderKey('RAND()'false);
  268.             $packageListing->setLimit($numOfTeaser);
  269.             $teaserPackages $packageListing->getObjects();
  270.         }
  271.         $teaserdata = [];
  272.         /**
  273.          * @var Package $package
  274.          */
  275.         foreach ($teaserPackages as $package) {
  276.             $teaserdata[] = [
  277.                 "title" => $package->getTitle(),
  278.                 "href" => $this->linkGenerator->generate($package),
  279.                 "image" => $package->getTeaserImage(),
  280. //                "badgeText" =>  $infra->get(),
  281.                 "isInNav" =>  false,
  282.                 "isSearchTeaser" =>  false,
  283.                 "ratio" =>  null
  284.             ];
  285.         }
  286.         return $teaserdata;
  287.     }
  288. }