src/Elements/Bundle/DemiFrontendBundle/Controller/AbstractDemiController.php line 108

Open in your IDE?
  1. <?php
  2. /**
  3.  * Elements DeMI
  4.  *
  5.  * This source file is available under the elements DeMI license version 1
  6.  *
  7.  *  @copyright  Copyright (c) elements.at New Media Solutions GmbH (https://www.elements.at/)
  8.  *  @license    elements DeMI Lizenz Version 1 (https://www.elements.at/de/demi-lizenz)
  9.  */
  10. namespace Elements\Bundle\DemiFrontendBundle\Controller;
  11. use Carbon\Carbon;
  12. use Elements\Bundle\CmsToolsBundle\Tool\Helper\ElementsCustomDateFormat;
  13. use Elements\Demi\Frontend\Service\Configuration;
  14. use Elements\Demi\Accommodation\Search\Parameter;
  15. use Elements\Bundle\DemiFrontendBundle\Service\DemiUrl;
  16. use Elements\Demi\Model\AccommodationServiceProvider;
  17. use Elements\Demi\Model\Event;
  18. use Elements\Demi\Model\Infrastructure;
  19. use Pimcore\Controller\FrontendController;
  20. use Pimcore\Model\DataObject\Concrete;
  21. use Pimcore\Model\DataObject\DemiAccommodationServiceProvider;
  22. use Pimcore\Model\DataObject\DemiConfig;
  23. use Pimcore\Model\DataObject\DemiEvent;
  24. use Pimcore\Model\DataObject\DemiInfrastructure;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpFoundation\Response;
  27. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  28. abstract class AbstractDemiController extends FrontendController
  29. {
  30.     protected ?ElementsCustomDateFormat $elementsCustomDateFormat;
  31.     protected ?Configuration $configuration;
  32.     protected string $testIp '89.26.34.69';
  33.     public function redirectViaFeratelIdProperty(Request $request,Concrete $objectDemiUrl $demiUrl){
  34.         $feratelId $object->getProperty("feratelId");
  35.         if(empty($feratelId)){
  36.             throw new NotFoundHttpException("feratel id missing for redirect");
  37.         }
  38.         $feratelId trim($feratelId);
  39.         $targetRoute "";
  40.         $configObject $this->document->getProperty('demi_config');
  41.         $target=null;
  42.         $params=[];
  43.         if($object instanceof AccommodationServiceProvider){
  44.             $target DemiAccommodationServiceProvider::getByFid($feratelId,1);
  45.             $targetRoute "demi_acco_detail_page";
  46.             $params = [
  47.                 'accommodation' => $target
  48.             ];
  49.         } else if ($object instanceof Event){
  50.             $target DemiEvent::getByFid($feratelId,1);
  51.             $targetRoute "demi_event_detail";
  52.             $params = [
  53.                 'path' => $configObject->getEventDetailPath(),
  54.                 'name' => $target->getName(),
  55.                 'id' => $target->getId()
  56.             ];
  57.         } else if ($object instanceof Infrastructure) {
  58.             $target DemiInfrastructure::getByFid($feratelId1);
  59.             $targetRoute "demi_infrastructure_detail";
  60.             $params = [
  61.                 'path' => $configObject->getInfrastructureDetailPath(),
  62.                 'name' => $target->getName(),
  63.                 'id' => $target->getId()
  64.             ];
  65.         }
  66.         if($target != null && !$target->isPublished()){
  67.             $target null;
  68.         }
  69.         if($target != null){
  70.             $correctPath $demiUrl->__invoke($params$targetRoutetrue);
  71.             return $this->redirect($correctPath301);
  72.         } else {
  73.             throw new NotFoundHttpException("could not find a published target object with feratel fid ".$feratelId);
  74.         }
  75.     }
  76.     public function unavailableAction(Request $request$unavailableType null): Response
  77.     {
  78.         if ($this->document->getProperty('demi_unavailable_button')) {
  79.             $viewParams['buttonTarget'] = $this->document->getProperty('demi_unavailable_button');
  80.         }
  81.         if ($this->document->getProperty('demi_unavailable_button_text')) {
  82.             $viewParams['buttonText'] = $this->document->getProperty('demi_unavailable_button_text');
  83.         }
  84.         $viewParams['type'] = $unavailableType;
  85.         $this->addResponseHeader('X-Robots-Tag''noindex,nofollow');
  86.         $response $this->renderTemplate('@ElementsDemiFrontend/Error/unavailable.html.twig'$viewParams);
  87.         $response->setStatusCode(404);
  88.         return $response;
  89.     }
  90.     public function forwardingAction(Request $request): Response
  91.     {
  92.         $forwardController 'Elements\\Bundle\\DemiFrontendBundle\\Controller\\' ucfirst($request->get('controller')) . 'Controller::' $request->get('action') . 'Action';
  93.         return $this->forward($forwardController$request->attributes->all(), $request->query->all());
  94.     }
  95.     public function setElementsCustomDateFormat(?ElementsCustomDateFormat $elementsCustomDateFormat): void
  96.     {
  97.         $this->elementsCustomDateFormat $elementsCustomDateFormat;
  98.     }
  99.     public function setConfiguration(?Configuration $configuration): void
  100.     {
  101.         $this->configuration $configuration;
  102.     }
  103.     protected function needsTimeStringRedirect(Request $request): array|bool|string
  104.     {
  105.         $from $request->get('from');
  106.         $to $request->get('to');
  107.         if (($from && is_numeric($from)) || ($to && is_numeric($to))) {
  108.             $uri $request->getUri();
  109.             if ($from) {
  110.                 $date Carbon::createFromTimestamp((int) ($from 1000))->setTime(00);
  111.                 $uri str_replace('&from='.$from'&from='.$this->elementsCustomDateFormat->dateToString($date), $uri);
  112.             }
  113.             if ($to) {
  114.                 $date Carbon::createFromTimestamp((int) ($to 1000))->setTime(00);
  115.                 $uri str_replace('&to='.$to'&to='.$this->elementsCustomDateFormat->dateToString($date), $uri);
  116.             }
  117.             return $uri;
  118.         }
  119.         return false;
  120.     }
  121.     protected function getAlternativeSearchParams(Parameter $paramsint $extendFrom 3int $extendTo 3): Parameter
  122.     {
  123.         $alternativeParams = clone $params;
  124.         $to Carbon::createFromTimestamp($params->getDateTo()?->getTimestamp());
  125.         $from Carbon::createFromTimestamp($params->getDateFrom()?->getTimestamp());
  126.         $diffInDays $to->diffInDays($from);
  127.         $diffFromToday Carbon::now()->diffInDays($fromfalse);
  128.         // if date is in the past
  129.         if ($diffFromToday 0) {
  130.             $diffFromToday abs($diffFromToday);
  131.             $from->addDays($diffFromToday);
  132.             $to->addDays($diffFromToday);
  133.         }
  134.         // Subtract 3 days max from check-in date and add 3 days to check-out date
  135.         $subDaysFrom min($extendFrom$diffFromToday);
  136.         $addDaysTo $extendTo;
  137.         $newFrom $from->subDays($subDaysFrom);
  138.         $newTo $to->addDays($addDaysTo);
  139.         $mappingArray explode(','$this->configuration->getVariableNightMappings());
  140.         $mappings = [];
  141.         foreach ($mappingArray as $item) {
  142.             $mappings[] = array_map(static function($element) {
  143.                 return (int)$element;
  144.             }, explode('-'$item));
  145.         }
  146.         $nearestNights = -1;
  147.         $mappingsCount count($mappings);
  148.         // Find nearest
  149.         foreach ($mappings as $i => $iValue) {
  150.             if ($iValue[0] <= $diffInDays && $iValue[1] >= $diffInDays) {
  151.                 $nearestNights $i;
  152.                 break;
  153.             }
  154.         }
  155.         if ($nearestNights >= $mappingsCount || $nearestNights === -1) {
  156.             $startNight = ($diffInDays 1);
  157.             $endNight = ($diffInDays 1);
  158.         } else {
  159.             [$startNight$endNight] = $mappings[$nearestNights];
  160.         }
  161.         $nights = [];
  162.         for ($i $startNight$i <= $endNight$i++) {
  163.             $nights[] = $i;
  164.         }
  165.         $alternativeParams->setDateFrom($newFrom);
  166.         $alternativeParams->setDateTo($newTo);
  167.         $alternativeParams->setNights($nights);
  168.         return $alternativeParams;
  169.     }
  170. }