src/Controller/TourController.php line 294

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\CanonicalRedirectHelper;
  4. use App\Twig\LinkGenerator;
  5. use Elements\Bundle\AlpsteinBundle\Services\AlpsteinConfig;
  6. use Elements\Bundle\CmsToolsBundle\Tool\Helper\FunctionsHelper;
  7. use Knp\Component\Pager\PaginatorInterface;
  8. use Pimcore\Model\DataObject;
  9. use Pimcore\Model\DataObject\AlpsteinCategory;
  10. use Pimcore\Model\DataObject\AlpsteinRegion;
  11. use Pimcore\Model\DataObject\AlpsteinTour;
  12. use Pimcore\Model\DataObject\Region;
  13. use Pimcore\Model\Document\Editable;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. class TourController extends AbstractController
  19. {
  20.     public function overviewAction(Request $requestPaginatorInterface $paginator)
  21.     {
  22.         $viewParams = [];
  23.         // Load Kategorien
  24.         $alpsteinFiltering = new AlpsteinTour\Listing();
  25.         $alpsteinFiltering->addConditionParam("name != '' AND name IS NOT NULL");
  26.         // caching filterdata using a file in private var folder. not sure why pimcore cache doesn't work here. it hits some kind of limit of 50 elements in cache
  27.         // also this way the generation of filter data can be outsources to a command
  28.         $filename PIMCORE_PRIVATE_VAR '/tourFilterData-Cache-' $request->getLocale() . '.json';
  29.         if (file_exists($filename) && filemtime($filename) > strtotime('-1 day') && !$request->get('cache-new')) {
  30.             $filteringData json_decode(file_get_contents($filename), true);
  31.         } else {
  32.             $filteringData = [];
  33.         }
  34.         if (empty($filteringData)) {
  35.             $filteringData = [
  36.                 'length' => [
  37.                     'min' => 0.0,
  38.                     'max' => 0.0
  39.                 ],
  40.                 'duration' => [
  41.                     'min' => 0.0,
  42.                     'max' => 0.0
  43.                 ],
  44.                 'altitude' => [
  45.                     'min' => 0.0,
  46.                     'max' => 0.0
  47.                 ],
  48.                 'categories' => [],
  49.                 'regions' => []
  50.             ];
  51.             foreach( $alpsteinFiltering as $tour ){
  52.                 // get All Filters
  53.                 // Length
  54.                 $length = ( (float)$tour->getLength() / 1000) + 1;
  55.                 if( $filteringData['length']['max'] < $length ){
  56.                     $filteringData['length']['max'] = number_format$length1'.''' );
  57.                 }
  58.                 // Duration
  59.                 $duration = (int)($tour->getTimeMin()/60) + 1;
  60.                 if( $filteringData['duration']['max'] < $duration ){
  61.                     $filteringData['duration']['max'] = $duration;
  62.                 }
  63.                 // Altitude
  64.                 $altitude = (int)$tour->getElevationAscent();
  65.                 if( $filteringData['altitude']['max'] < $altitude ){
  66.                     $filteringData['altitude']['max'] = $altitude;
  67.                 }
  68.                 // Categories
  69.                 foreach( $tour->getCategory() as $cat ){
  70.                     if( $cat instanceof AlpsteinCategory ){
  71.                         $filteringData['categories'][$cat->getId()] = [
  72.                             'id' => $cat->getId(),
  73.                             'name' => $cat->getName()
  74.                         ];
  75.                     }
  76.                 }
  77.             }
  78.             asort$filteringData['categories'] );
  79.             // Regions
  80.             $regions = new Region\Listing();
  81.             $regions->addConditionParam('alpsteinRegions != "" AND alpsteinRegions IS NOT NULL');
  82.             $regions->setOrderKey('sorting');
  83.             foreach($regions as $region) {
  84.                 $filteringData['regions'][$region->getId()] = [
  85.                     'id' => $region->getId(),
  86.                     'name' => $region->getName()
  87.                 ];
  88.             }
  89.             file_put_contents($filenamejson_encode($filteringData));
  90.         }
  91.         // Filter Ergebnis
  92.         $tourListing = new AlpsteinTour\Listing();
  93.         $tourListing->addConditionParam("name != '' AND name IS NOT NULL");
  94.         /** @var Editable\Numeric $qsEditable */
  95.         $qsEditable $this->getDocumentEditable('numeric''qs');
  96.         // disable for now --> should be reactivated when ranking is filled in every tour
  97. //        if (!$qsEditable->isEmpty()) {
  98. //            $tourListing->addConditionParam("ranking >= " . $qsEditable->getData());
  99. //        }
  100.         // Prefilter Region/Tour Type
  101.         /** @var AlpsteinRegion[] $regions */
  102.         if ($regions $this->getDocumentEditable('relations''regions')) {
  103.             $regionsCond = [];
  104.             foreach ($regions as $region) {
  105.                 if ($region instanceof Region) {
  106.                     foreach ($region->getAlpsteinRegions() ?: [] as $alpsteinRegion) {
  107.                         // not needed as alpstein adds all regions and subregions to tour
  108. //                        foreach (self::getAllSubRegionIds($alpsteinRegion) ?: [] as $subRegionId) {
  109. //                            $regionsCond[] = 'regions LIKE "%,' . $subRegionId . ',%"';
  110. //                        }
  111.                         $regionsCond[] = 'regions LIKE "%,' $alpsteinRegion->getId() . ',%"';
  112.                     }
  113.                 } else if ($region instanceof AlpsteinRegion) {
  114.                     foreach (self::getAllSubRegionIds($region) ?: [] as $subRegionId) {
  115.                         $regionsCond[] = 'regions LIKE "%,' $subRegionId ',%"';
  116.                     }
  117.                     $regionsCond[] = 'regions LIKE "%,' $region->getId() . ',%"';
  118.                 }
  119.             }
  120.             if (!empty($regionsCond)) {
  121.                 $tourListing->addConditionParam('(' implode(' OR '$regionsCond) . ')');
  122.             }
  123.         }
  124.         /** @var AlpsteinCategory[] $categories */
  125.         $filteringData['childCategories'] = [];
  126.         if ($categories $this->getDocumentEditable('relations''categories')) {
  127.             $categoriesCond = [];
  128.             foreach ($categories as $category) {
  129.                 foreach ($category->getChildren() ?: [] as $childCategory) {
  130.                     if ($childCategory instanceof AlpsteinCategory) {
  131.                         $categoriesCond[] = 'category LIKE "%,' $childCategory->getId() . ',%"';
  132.                         $filteringData['childCategories'][] = $childCategory;
  133.                     }
  134.                 }
  135.                 $categoriesCond[] = 'category LIKE "%,' $category->getId() . ',%"';
  136.             }
  137.             if (!empty($categoriesCond)) {
  138.                 $tourListing->addConditionParam('(' implode(' OR '$categoriesCond) . ')');
  139.             }
  140.         }
  141.         if ($community DataObject\Community::getById($request->get('community'))) {
  142.             $viewParams['community'] = $community;
  143.             $functionsHelper = new FunctionsHelper();
  144.             if (false) { // seems to be more acurate than radius search because radius search only considers starting point
  145.                 $communityCond = [];
  146.                 foreach ($community->getAlpsteinObjects() ?: [] as $alpsteinRegion) {
  147.                     if ($alpsteinRegion instanceof DataObject\AlpsteinCommunity) {
  148.                         $communityCond[] = 'communities LIKE "%,' $alpsteinRegion->getId() . ',%"';
  149.                     } else if ($alpsteinRegion instanceof AlpsteinRegion) {
  150.                         $communityCond[] = 'regions LIKE "%,' $alpsteinRegion->getId() . ',%"';
  151.                     }
  152.                 }
  153.                 if (!empty($communityCond)) {
  154.                     $tourListing->addConditionParam('(' implode(' OR '$communityCond) . ')');
  155.                 }
  156.             } else if (!empty($community->getDetailRelatedTours())) {
  157.                 $ids = [];
  158.                 foreach($community->getDetailRelatedTours() as $relatedTour) {
  159.                     $ids[] = $relatedTour->getId();
  160.                 }
  161.                 if (!empty($ids)) {
  162.                     $tourListing->addConditionParam('o_id IN (' implode(','$ids) . ')');
  163.                 }
  164.             } else {
  165.                 $radius $community->getDetailRadiusTour() ?: 10;
  166.                 $tourListing->addConditionParam($functionsHelper->getGeoDistanceQuery(new DataObject\Data\GeoCoordinates($community->getGeoposition()->getLatitude(), $community->getGeoposition()->getLongitude()), 'startingPoint') . ' <= ' $radius);
  167.             }
  168.         }
  169.         // Filter Keyword
  170.         if( $request->get('tourKeyword') ){
  171.             $tourListing->addConditionParam('name LIKE :keyword', [ 'keyword' => '%' $request->get('tourKeyword') . '%' ]);
  172.         }
  173.         // Filter Public Transport Friendly
  174.         if ($request->get('publicTransportFriendly')) {
  175.             $tourListing->addConditionParam('IFNULL(publicTransportFriendly, 0) = 1');
  176.         }
  177.         // Filter Categories
  178.         if( $request->get('tourCategory'0) && $request->get('tourCategory') != 'all' ){
  179.             $category AlpsteinCategory::getById($request->get('tourCategory'));
  180.             if ($category instanceof AlpsteinCategory) {
  181.                 $categoriesCond = [];
  182.                 foreach ($category->getChildren() ?: [] as $childCategory) {
  183.                     if ($childCategory instanceof AlpsteinCategory) {
  184.                         $categoriesCond[] = 'category LIKE "%,' $childCategory->getId() . ',%"';
  185.                     }
  186.                 }
  187.                 $categoriesCond[] = 'category LIKE "%,' $category->getId() . ',%"';
  188.                 if (!empty($categoriesCond)) {
  189.                     $tourListing->addConditionParam('(' implode(' OR '$categoriesCond) . ')');
  190.                 }
  191.             }
  192.             $tourListing->addConditionParam('category LIKE :catId', [ 'catId' => '%,' $request->get('tourCategory') . ',%' ]);
  193.         }
  194.         // Filter Difficulty
  195.         if( $request->get('tourDifficulty'0) && $request->get('tourDifficulty') != 'all' ){
  196.             $tourListing->addConditionParam('ratingDifficulty = :difficulty', [ 'difficulty' => $request->get('tourDifficulty') ]);
  197.         }
  198.         // Filter Startpunkt
  199.         if( $request->get('tourStart'0) && $request->get('tourStart') != 'all' ){
  200.             $tourListing->addConditionParam('startingPointDesc = :tourStart', [ 'tourStart' => $request->get('tourStart') ]);
  201.         }
  202.         // Filter Region
  203.         if( $request->get('tourRegion'0) && $request->get('tourRegion') != 'all' ){
  204.             $region DataObject::getById($request->get('tourRegion'));
  205.             $regionsCond = [];
  206.             if ($region instanceof Region) {
  207.                 foreach ($region->getAlpsteinRegions() ?: [] as $alpsteinRegion) {
  208.                     $regionsCond[] = 'regions LIKE "%,' $alpsteinRegion->getId() . ',%"';
  209.                 }
  210.             } else if ($region instanceof AlpsteinRegion) {
  211.                 foreach (self::getAllSubRegionIds($region) ?: [] as $subRegionId) {
  212.                     $regionsCond[] = 'regions LIKE "%,' $subRegionId ',%"';
  213.                 }
  214.                 $regionsCond[] = 'regions LIKE "%,' $region->getId() . ',%"';
  215.             }
  216.             if (!empty($regionsCond)) {
  217.                 $tourListing->addConditionParam('(' implode(' OR '$regionsCond) . ')');
  218.             }
  219.         }
  220.         // Filter Properties
  221.         if( $request->get('tourProperty'0) && $request->get('tourProperty') != 'all' ){
  222.             $tourListing->addConditionParam('tourProperties LIKE :property', ['property' => '%,' $request->get('tourProperty') . ',%']);
  223.         }
  224.         // Filter Strecke
  225.         if( $request->get('rangeLengthMin'0) || $request->get('rangeLengthMax'0) ){
  226.             $streckeMin = ((float) $request->get('rangeLengthMin'0)) * 1000;
  227.             $streckeMax = ((float) $request->get('rangeLengthMax'$filteringData['length']['max'])) * 1000;
  228.             $tourListing->addConditionParam('( IFNULL(length, 0) >= :lengthMin AND IFNULL(length, 0) <= :lengthMax )', [ 'lengthMin' => $streckeMin'lengthMax' => $streckeMax ]);
  229.         }
  230.         // Filter Dauer
  231.         if( $request->get('durationMin'0) || $request->get('durationMax'0) ){
  232.             $timeMin = ((float) $request->get('durationMin'0)) * 60;
  233.             $timeMax = ((float) $request->get('durationMax'$filteringData['duration']['max'])) * 60;
  234.             $tourListing->addConditionParam('( IFNULL(timeMin, 0) >= :timeMin AND IFNULL(timeMin, 0) <= :timeMax )', [ 'timeMin' => $timeMin'timeMax' => $timeMax ]);
  235.         }
  236.         // Filter Altitude
  237.         if( $request->get('altitudeMin'0) || $request->get('altitudeMax'0) ){
  238.             $altitudeMin = (float) $request->get('altitudeMin'0);
  239.             $altitudeMax = (float) $request->get('altitudeMax'$filteringData['altitude']['max']);
  240. //            $tourListing->addConditionParam('( (elevationMaxAltitude - elevationMinAltitude) >= :altitudeMin AND (elevationMaxAltitude - elevationMinAltitude) <= :altitudeMax )', [ 'altitudeMin' => $altitudeMin, 'altitudeMax' => $altitudeMax ]);
  241.             $tourListing->addConditionParam('( IFNULL(elevationAscent, 0) >= :altitudeMin AND IFNULL(elevationAscent, 0) <= :altitudeMax )', [ 'altitudeMin' => $altitudeMin'altitudeMax' => $altitudeMax ]);
  242.         }
  243.         $sorting $this->getDocumentEditable('select''tour_sorting')?->getData();
  244.         if(!empty($sorting)) {
  245.             $tourListing->setOrderKey($sortingfalse);
  246.         }
  247.         // Paginator
  248.         $paginator $paginator->paginate$tourListing$request->get('page'1), 16 );
  249.         $paginator->setPageRange);
  250.         $viewParams['tourListing'] = $paginator;
  251.         $viewParams['filterData'] = $filteringData;
  252.         if ($request->isXmlHttpRequest()) {
  253.             return $this->json([
  254.                 'success' => true,
  255.                 'html' => $this->renderView('Tour/container.html.twig'$viewParams)
  256.             ]);
  257.         }
  258.         return $this->renderTemplate('Tour/overview.html.twig'$viewParams);
  259.     }
  260.     public function detailAction(Request $requestAlpsteinConfig $alpsteinConfigAlpsteinTour $idCanonicalRedirectHelper $redirectHelper)
  261.     {
  262.         $object $id;
  263.         if( !$object || ( !$object->isPublished() && !$this->editmode && !$request->get('pimcore_object_preview') && !$_COOKIE['pimcore_admin_sid'] ) ) {
  264.             throw new NotFoundHttpException("The requested object doesn't exist anymore");
  265.         }
  266.         if (CanonicalRedirectHelper::ENABLE_CANONICAL_REDIRECT && $redirect $redirectHelper->canonicalRedirect($object)) {
  267.             return $redirect;
  268.         }
  269.         if (!empty($object->getManualRecommendation())) {
  270.             $recommendations $object->getManualRecommendation();
  271.         } else {
  272.             $recommendations = new AlpsteinTour\Listing();
  273.             $recommendations->addConditionParam('name != "" AND name IS NOT NULL AND o_id != ' $object->getId());
  274.             if (!$object->getDisableRadiusLimitation()) {
  275.                 $functionsHelper = new FunctionsHelper();
  276.                 $recommendations->addConditionParam($functionsHelper->getGeoDistanceQuery($object->getStartingPoint(), 'startingPoint') . ' < 10');
  277.             }
  278.             $categoryCondition = [];
  279.             if (!empty($object->getSimilarCategories())) {
  280.                 $categories $object->getSimilarCategories();
  281.             } else {
  282.                 $categories $object->getCategory();
  283.             }
  284.             foreach ($categories ?: [] as $category) {
  285.                 $categoryCondition[] = 'category LIKE "%,' $category->getId() . ',%"';
  286.             }
  287.             if (!empty($categoryCondition)) {
  288.                 $recommendations->addConditionParam('(' implode(' OR '$categoryCondition) . ')');
  289.             }
  290.             $recommendations->setLimit(7);
  291.             $recommendations->setOrderKey('RAND()'false);
  292.         }
  293.         return $this->renderTemplate('Tour/detail.html.twig', [
  294.             'tour' => $object,
  295.             'alpsteinConfig' => $alpsteinConfig,
  296.             'recommendations' => $recommendations
  297.         ]);
  298.     }
  299.     public static function getAllSubRegionIds(AlpsteinRegion $region) {
  300.         $ids = [];
  301.         foreach($region->getSubRegions() ?: [] as $subRegion) {
  302.             $ids[] = $subRegion->getId();
  303.             $ids array_merge($idsself::getAllSubRegionIds($subRegion));
  304.         }
  305.         return $ids;
  306.     }
  307.     /**
  308.      * @param Request $request
  309.      * @return RedirectResponse
  310.      * @Route("{_locale}/api/{type}/326442/tour-redirect/{foreignId}", name="pia_map", requirements={"type"="alpstein|contwise"})
  311.      */
  312.     public function mapPiaLink(Request $requestLinkGenerator $linkGenerator) {
  313.         $tour AlpsteinTour::getByAlpsteinId($request->get('foreignId'), 1) ?? AlpsteinTour::getByGeneralSolutionsId($request->get('foreignId'), 1);
  314.         if(!$tour) {
  315.             throw new NotFoundHttpException("invalid tour");
  316.         }
  317.         $url $linkGenerator->generate($tour);
  318.         if(!$url) {
  319.             throw new NotFoundHttpException("Redirect not possible");
  320.         }
  321.         return new RedirectResponse($url);
  322.     }
  323. }