src/Controller/LocationController.php line 21

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by Elements.at New Media Solutions GmbH
  4.  *
  5.  */
  6. namespace App\Controller;
  7. use App\Twig\ConfigHelper;
  8. use Elements\Bundle\CmsToolsBundle\Tool\Helper\FunctionsHelper;
  9. use Pimcore\Cache;
  10. use Pimcore\Model\DataObject\Data\GeoCoordinates;
  11. use Pimcore\Model\DataObject\Location;
  12. use Pimcore\Model\Document\Page;
  13. use Pimcore\Model\Document\Snippet;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. class LocationController extends AbstractController
  18. {
  19.     public function __construct(private ConfigHelper $configHelper)
  20.     {
  21.     }
  22.     /**
  23.      *
  24.      * @param Request $request
  25.      *
  26.      * @return Response
  27.      */
  28.     public function overviewAction(Request $request): Response
  29.     {
  30.         $siteConfig $this->configHelper->getSiteConfig();
  31.         $resort $this->document->getProperty('resort');
  32.         $locations = new Location\Listing();
  33.         $locations->addConditionParam('config LIKE :config', ['config' => '%' $siteConfig->getId() . '%']);
  34.         $locations->addConditionParam("name != '' AND name IS NOT NULL AND (notShow IS NULL OR notShow = 0)");
  35.         $cacheKey str_replace(['{''}''('')''/''\\''@'':'], ''substr($request->getUri(), 0strpos($request->getUri(), '?') ?: strlen($request->getUri())));
  36.         $usedResorts Cache::load($cacheKey);
  37.         if (!$usedResorts) {
  38.             foreach ($locations as $location) {
  39.                 $resorts $location->getResorts();
  40.                 foreach ($resorts as $res) {
  41.                     $usedResorts[$res] = $res;
  42.                 }
  43.             }
  44.             $cacheTime 3600;
  45.             Cache::save($usedResorts$cacheKey, [], $cacheTime0true);
  46.         }
  47.         if ($usedResorts) {
  48.             if($resort != null && !$request->get('resort') && in_array($resort$usedResorts)){
  49.                 $locations->addConditionParam("resorts LIKE :resort", ['resort' => '%' $resort '%']);
  50.             }
  51.         }
  52.         if ($request->get('resort') && $request->get('resort') != 'all') {
  53.             $locations->addConditionParam('resorts LIKE :keyword', ['keyword' => '%' $request->get('resort') . '%']);
  54.         }
  55.         $locations->setOrderKey(['IFNULL(sorter, 999999)''name'], false);
  56.         $locations->setOrder(['ASC''ASC']);
  57.         $returnArray = [
  58.             'usedResorts' => $usedResorts,
  59.             'locations' => $locations
  60.         ];
  61.         if ($request->isXmlHttpRequest() && $request->get('ajax')) {
  62.             return $this->json([
  63.                 'success' => true,
  64.                 'html' => $this->render('location/includes/location-content.html.twig'$returnArray)->getContent(),
  65.             ]);
  66.         }
  67.         return $this->renderTemplate('location/overview.html.twig'$returnArray);
  68.     }
  69.     /**
  70.      *
  71.      * @param Request $request
  72.      *
  73.      * @return Response
  74.      */
  75.     public function detailAction(Request $requestLocation $id): Response
  76.     {
  77.         $location $id;
  78.         return $this->renderTemplate('location/detail.html.twig', [
  79.             'location' => $location
  80.         ]);
  81.     }
  82.     /**
  83.      * @param Request $request
  84.      * @param FunctionsHelper $functionsHelper
  85.      * @return JsonResponse|Response
  86.      */
  87.     public function locationfinderAction(Request $requestFunctionsHelper $functionsHelper)
  88.     {
  89.         $siteConfig $this->configHelper->getSiteConfig();
  90.         $resort $this->document->getProperty('resort');
  91.         $locations = new Location\Listing();
  92.         $locations->addConditionParam("name != '' AND name IS NOT NULL AND (notShow IS NULL OR notShow = 0)");
  93.         $locations->addConditionParam('config LIKE :config', ['config' => '%' $siteConfig->getId() . '%']);
  94.         $cacheKey str_replace(['{''}''('')''/''\\''@'':'], ''substr($request->getUri(), 0strpos($request->getUri(), '?') ?: strlen($request->getUri())));
  95.         $usedResorts Cache::load($cacheKey);
  96.         if (!$usedResorts) {
  97.             foreach ($locations as $location) {
  98.                 $resorts $location->getResorts();
  99.                 foreach ($resorts as $res) {
  100.                     $usedResorts[$res] = $res;
  101.                 }
  102.             }
  103.             $cacheTime 3600;
  104.             Cache::save($usedResorts$cacheKey, [], $cacheTime0true);
  105.         }
  106.         if ($usedResorts) {
  107.             if($resort != null && !$request->get('resort') && in_array($resort$usedResorts)){
  108.                 $locations->addConditionParam("resorts LIKE :resort", ['resort' => '%' $resort '%']);
  109.             }
  110.         }
  111.         if ($request->get('resort') && $request->get('resort') != 'all') {
  112.             $locations->addConditionParam('resorts LIKE :keyword', ['keyword' => '%' $request->get('resort') . '%']);
  113.         }
  114.         if($request->get('location''') && $request->get('searchLocationLat''') && $request->get('searchLocationLng''')) {
  115.             $currentLocationLat $request->get('searchLocationLat''');
  116.             $currentLocationLng $request->get('searchLocationLng''');
  117.         } else {
  118.             $currentLocationLat $request->get('currentLocationLat''');
  119.             $currentLocationLng $request->get('currentLocationLng''');
  120.         }
  121.         $geoPoint null;
  122.         if ($currentLocationLat && $currentLocationLng) {
  123.             $geoPoint = new GeoCoordinates($currentLocationLat$currentLocationLng);
  124.         }
  125.         if ($geoPoint && ($geoPoint->getLatitude() === null || $geoPoint->getLongitude() === null)) {
  126.             $geoPoint null;
  127.         }
  128.         if($geoPoint) {
  129.             $distanceQuery $functionsHelper->getGeoDistanceQuery($geoPoint'geo');
  130.             $locations->setOrderKey($distanceQueryfalse);
  131.             $locations->setOrder('ASC');
  132.         }else{
  133.             $locations->setOrderKey(['IFNULL(sorter, 999999)''name'], false);
  134.             $locations->setOrder(['ASC''ASC']);
  135.         }
  136.         $returnArray = [
  137.             'locations' => $locations,
  138.             'usedResorts' => $usedResorts,
  139.         ];
  140.         $pois = [];
  141.         $poiStyles = [
  142.             'construction' => 'bm',
  143.             'loader' => 'lt',
  144.             'group' => 'gp'
  145.         ];
  146.         foreach($locations as $key => $location) {
  147.             if ($position $location->getGeo()) {
  148.                 $poiData = [
  149.                     'id' => $location->getId(),
  150.                     'poiStyle' => $poiStyles[$location->getResorts()[0]],
  151.                     'lat' => $position->getLatitude(),
  152.                     'lng' => $position->getLongitude(),
  153.                     'detailInfo' => 'dealer-search-'$location->getId(),
  154.                 ];
  155.                 $pois[] = $poiData;
  156.             }
  157.         }
  158.         if ($request->isXmlHttpRequest() && $request->get('pois')) {
  159.             return $this->json([
  160.                 'success' => true,
  161.                 'pois' => $pois
  162.             ]);
  163.         }
  164.         if ($request->isXmlHttpRequest() && $request->get('ajax')) {
  165.             return $this->json([
  166.                 'success' => true,
  167.                 'html' => $this->render('location/includes/location-list.html.twig'$returnArray)->getContent(),
  168.             ]);
  169.         }
  170.         return $this->renderTemplate('location/locationfinder.html.twig'$returnArray);
  171.     }
  172. }