src/Elements/Bundle/DemiFrontendBundle/Service/DemiStateHelper.php line 461

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\Service;
  11. use Carbon\Carbon;
  12. use Elements\Bundle\CmsToolsBundle\Tool\Helper\ElementsCustomDateFormat;
  13. use Elements\Demi\Model\AccommodationServiceProvider;
  14. use Pimcore\Event\Traits\RequestAwareTrait;
  15. use Pimcore\Http\Request\Resolver\DocumentResolver;
  16. use Pimcore\Model\Document;
  17. use stdClass;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. use Pimcore\Translation\Translator;
  21. class DemiStateHelper
  22. {
  23.     use RequestAwareTrait;
  24.     private array $sorting = [];
  25.     private array $search = [];
  26.     private array $searchForm = [];
  27.     private array $filter = [];
  28.     private array $accList = [];
  29.     private array $nearbySearch = [];
  30.     private bool $showMap;
  31.     private array $rooms = [
  32.         'occupancy' => [
  33.             'rooms' => [],
  34.             'type' => ''
  35.         ]
  36.     ];
  37.     protected bool $initialized;
  38.     protected ?Document $document null;
  39.     public function __construct(protected RequestStack $requestStack, protected DocumentResolver $documentResolver, protected Translator $translator, protected ElementsCustomDateFormat $elementsCustomDateFormat)
  40.     {
  41.         $this->initialized false;
  42.     }
  43.     protected function initOnce(): void
  44.     {
  45.         if ($this->requestStack->getMainRequest()) {
  46.             $this->request $this->requestStack->getMainRequest();
  47.             $this->document $this->request $this->documentResolver->getDocument($this->request) ?? $this->documentResolver->getDocument() : null;
  48.             $this->initialized true;
  49.             // page
  50.             if ($this->request->get('page')) {
  51.                 $this->setPage((int)$this->request->get('page'));
  52.             }
  53.             // From Timestamp -> JS timestamp
  54.             if ($this->request->get('from')) {
  55.                 $this->setSearchFrom((string)$this->request->get('from'));
  56.             }
  57.             // To Timestamp -> JS timestamp
  58.             if ($this->request->get('to')) {
  59.                 $this->setSearchTo((string)$this->request->get('to'));
  60.             }
  61.             // sort factory param
  62.             if ($this->request->get('sortFactoryParam')) {
  63.                 $this->setSortFactoryParam($this->request->get('sortFactoryParam'));
  64.             }
  65.             // Duration Type like exact/variable
  66.             if ($this->request->get('durationType')) {
  67.                 $this->setDurationType($this->request->get('durationType'));
  68.             }
  69.             // Duration Nights
  70.             if ($this->request->get('durationNights')) {
  71.                 $this->setDurationNights($this->request->get('durationNights'));
  72.             }
  73.             // Room type like: double-room
  74.             if ($this->request->get('occupancyType')) {
  75.                 $this->setOccupancyType($this->request->get('occupancyType'));
  76.             }
  77.             // Stars
  78.             if ($this->request->get('stars')) {
  79.                 $this->setFilterArray('stars[]'$this->request->get('stars'));
  80.             }
  81.             // Classifications
  82.             if ($this->request->get('classifications')) {
  83.                 $this->setFilterArray('classifications[]'$this->request->get('classifications'));
  84.             }
  85.             // Occupancy of Rooms
  86.             if ($this->request->get('a0')) {
  87.                 $this->setRooms($this->request);
  88.             }
  89.             // towns
  90.             if ($this->request->get('towns')) {
  91.                 $this->setFilterArray('towns[]'$this->request->get('towns'));
  92.             }
  93.             // regions
  94.             if ($this->request->get('regions')) {
  95.                 $this->setFilterArray('regions[]'$this->request->get('regions'));
  96.             }
  97.             // districts
  98.             if ($this->request->get('districts')) {
  99.                 $this->setFilterArray('districts[]'$this->request->get('districts'));
  100.             }
  101.             // Categories
  102.             if ($this->request->get('categories')) {
  103.                 $this->setFilterArray('categories[]'$this->request->get('categories'));
  104.             }
  105.             // Holiday Themes
  106.             if ($this->request->get('holidayThemes')) {
  107.                 $this->setFilterArray('holidayThemes[]'$this->request->get('holidayThemes'));
  108.             }
  109.             // Language
  110.             if ($this->request->get('language') && is_array($this->request->get('language'))) {
  111.                 $this->setFilterArray('language[]'$this->request->get('language'));
  112.             }
  113.             // AccommodationType
  114.             if (!empty($this->request->get('type')) && $this->request->get('type')) {
  115.                 $this->setFilterArray('type[]'$this->request->get('type'));
  116.             }
  117.             // AccommodationFacilities
  118.             if ($this->request->get('facilities')) {
  119.                 $this->setFilterArray('facilities[]'$this->request->get('facilities'));
  120.             }
  121.             // RoomFacilities
  122.             if ($this->request->get('roomFacilities')) {
  123.                 $this->setFilterArray('roomFacilities[]'$this->request->get('roomFacilities'));
  124.             }
  125.             // Facilities
  126.             if ($this->request->get('apartmentFacilities')) {
  127.                 $this->setFilterArray('apartmentFacilities[]'$this->request->get('apartmentFacilities'));
  128.             }
  129.             // Mealtypes
  130.             if ($this->request->get('mealtype')) {
  131.                 $this->setFilter('mealtype'$this->request->get('mealtype'));
  132.             }
  133.             // Marketing Groups
  134.             if ($this->request->get('marketingGroups')) {
  135.                 $this->setFilterArray('marketingGroups[]'$this->request->get('marketingGroups'));
  136.             }
  137.             // Book Only
  138.             if ($this->request->get('bookonly')) {
  139.                 $this->setFilter('bookonly'$this->request->get('bookonly'));
  140.             }
  141.             //Not Book ONly
  142.             if ($this->request->get('notBookOnly')) {
  143.                 $this->setFilter('notBookOnly'$this->request->get('notBookOnly'));
  144.             }
  145.             //Free Cancellation
  146.             if ($this->request->get('freeCancellation')) {
  147.                 $this->setFilter('freeCancellation'$this->request->get('freeCancellation'));
  148.             }
  149.             // Best price Provider
  150.             if ($this->request->get('bestPriceProvider')) {
  151.                 $this->setFilter('bestPriceProvider'$this->request->get('bestPriceProvider'));
  152.             }
  153.             // Best price
  154.             if ($this->request->get('bestpriceOnly')) {
  155.                 $this->setFilter('bestpriceOnly'$this->request->get('bestpriceOnly'));
  156.             }
  157.             //Price
  158.             if ($this->request->get('price') && !is_array($this->request->get('price'))) {
  159.                 $this->setFilter('price'$this->request->get('price'));
  160.             }
  161.             // Accommodation name
  162.             if ($this->request->get('acconame')) {
  163.                 $this->setFilter('acconame'$this->request->get('acconame'));
  164.             }
  165.             //Accommodation id
  166.             if ($this->request->get('accoId')) {
  167.                 $this->addHotel($this->request);
  168.             }
  169.             // Special Types
  170.             if ($this->request->get('specialTypes')) {
  171.                 $this->setFilterArray('specialTypes[]'$this->request->get('specialTypes'));
  172.             }
  173.             // Specials Only
  174.             if ($this->request->get('specialsOnly')) {
  175.                 $this->setFilter('specialsOnly'$this->request->get('specialsOnly'));
  176.             }
  177.             // Sales Channel (hidden filter)
  178.             if ($this->request->get('salesChannel')) {
  179.                 $this->setFilter('salesChannel'$this->request->get('salesChannel'));
  180.             }
  181.             // Filter Objects
  182.             if ($this->request->get('fo')) {
  183.                 $this->setFilterArray('fo[]'$this->request->get('fo'));
  184.             }
  185.             // Mapped Filter Objects
  186.             if ($this->request->get('mfo')) {
  187.                 $this->setFilterArray('mfo[]'$this->request->get('mfo'));
  188.             }
  189.             // Sorting
  190.             if ($this->request->get('sorting')) {
  191.                 $this->setSorting($this->request->get('sorting'));
  192.             }
  193.             // Nearby Search
  194.             if ($this->request->get('nearbySearchId')) {
  195.                 $this->setNearbySearch($this->request->get('nearbySearchId'), $this->request->get('nearbySearchLat'), $this->request->get('nearbySearchLong'));
  196.             }
  197.             // AccommodationType
  198.             if ($this->request->get('atsc')) {
  199.                 $this->setFilter('atsc'$this->request->get('atsc'));
  200.             }
  201.             //Bedrooms
  202.             if ($this->request->get('minBed')) {
  203.                 $this->setFilter('minBed'$this->request->get('minBed'));
  204.             }
  205.             // Show Map
  206.             if (!is_null($this->request->get('showMap'))) {
  207.                 $this->setShowMap($this->request->get('showMap'));
  208.             } elseif ($this->document && $this->document->getEditable('hideMapInitially')) {
  209.                 $hideMap $this->document->getEditable('hideMapInitially')->getData();
  210.                 $this->setShowMap(!$hideMap);
  211.             } else {
  212.                 $this->setShowMap(true);
  213.             }
  214.         }
  215.     }
  216.     public function addHotel(Request $request): void
  217.     {
  218.         $accoIds $request->get('accoId', []);
  219.         foreach ($accoIds as $hotelId) {
  220.             $hotelObj AccommodationServiceProvider::getById(htmlspecialchars($hotelId));
  221.             $this->filter[] = [
  222.                 'name' => 'accoId[]',
  223.                 'value' => $hotelObj?->getId(),
  224.                 'text' => $hotelObj?->getName()
  225.             ];
  226.         }
  227.     }
  228.     private function setFilterArray(string $namemixed $filter): void
  229.     {
  230.         if (is_array($filter)) {
  231.             foreach ($filter as $star) {
  232.                 $this->filter[] = [
  233.                     'name' => $name,
  234.                     'value' => htmlspecialchars($star),
  235.                 ];
  236.             }
  237.         }
  238.     }
  239.     private function setFilter(string $namestring $value): void
  240.     {
  241.         $value htmlspecialchars($value);
  242.         if ($name !== '' && $value !== '') {
  243.             $this->filter[] = [
  244.                 'name' => $name,
  245.                 'value' => $value,
  246.             ];
  247.         }
  248.     }
  249.     private function setNearbySearch(string $idstring|null $latstring|null $long): void
  250.     {
  251.         if($lat == null || $long == null){
  252.             //do nothing if lat/lon is missing
  253.             return;
  254.         }
  255.         $lat htmlspecialchars($lat);
  256.         $long htmlspecialchars($long);
  257.         $name $this->translator->trans('demi.search.nearby.my-location');
  258.         if(is_integer($id)){
  259.             $obj AbstractObject::getById((int)$id);
  260.             if($obj){
  261.                 $name=$obj->getName();
  262.             }
  263.         }
  264.         if ($id !== '' && $lat !== '' && $long !== '') {
  265.             $this->nearbySearch = [
  266.                 'nearbySearchId' => $id,
  267.                 'nearbySearchName' => $name,
  268.                 'nearbySearchLat' => $lat,
  269.                 'nearbySearchLong' => $long,
  270.             ];
  271.         }
  272.     }
  273.     private function setSorting(string $value): void
  274.     {
  275.         $value htmlspecialchars($value);
  276.         $this->sorting = [
  277.             'value' => $value,
  278.             'asc' => !strpos($value'Desc') || strpos($value'Asc'),
  279.             'field' => str_replace(['Desc''Asc'], ''$value),
  280.         ];
  281.     }
  282.     private function setShowMap(bool $value): void
  283.     {
  284.         $this->showMap $value;
  285.     }
  286.     /**
  287.      * Set Rooms / Adults / Children for Search
  288.      */
  289.     private function setRooms(Request $request): void
  290.     {
  291.         $maxRooms 100;
  292.         $roomArray $this->getRooms();
  293.         // iterate rooms (til 100)
  294.         for ($roomNumber 0$roomNumber <= $maxRooms$roomNumber++) {
  295.             if ($request->get('a' $roomNumberfalse) !== false) {
  296.                 // iterate children
  297.                 $childAgesArray = [];
  298.                 $maxAges = (int)$request->get('c' $roomNumber0);
  299.                 for ($childAge 0$childAge $maxAges$childAge++) {
  300.                     $age $request->get('ca' $roomNumber)[$childAge];
  301.                     if(!empty($age)  || $age === "0"){
  302.                         $childAgesArray[] = htmlspecialchars($age);
  303.                     } else {
  304.                         //faulty parameters - no matching child age found. Don't allow $childAgesArray to be filled with empty string
  305.                     }
  306.                 }
  307.                 // fill roomArray
  308.                 $roomArray['occupancy']['rooms'][$roomNumber] = [
  309.                     'adults' => (int)$request->get('a' $roomNumber),
  310.                     'childAges' => $childAgesArray,
  311.                     'quantity' => (int)$request->get('u' $roomNumber),
  312.                 ];
  313.             } else {
  314.                 break;
  315.             }
  316.         }
  317.         $this->rooms $roomArray;
  318.     }
  319.     public function getRooms(): array
  320.     {
  321.         if (!$this->initialized) {
  322.             $this->initOnce();
  323.         }
  324.         $this->rooms['occupancy']['type'] = $this->getOccupancyType();
  325.         return $this->rooms;
  326.     }
  327.     public function getOccupancyType()
  328.     {
  329.         if (!$this->initialized) {
  330.             $this->initOnce();
  331.         }
  332.         return $this->search['occupancyType'] ?? '' ;
  333.     }
  334.     private function setPage(int $page): void
  335.     {
  336.         $this->accList['page'] = htmlspecialchars($page);
  337.     }
  338.     private function setSearchFrom(string $timestring): void
  339.     {
  340.         $this->search['from'] = htmlspecialchars($timestring);
  341.     }
  342.     public function getSearchFrom(): Carbon
  343.     {
  344.         if (!$this->initialized) {
  345.             $this->initOnce();
  346.         }
  347.         if (isset($this->search['from'])) {
  348.             return $this->elementsCustomDateFormat->stringToCarbon($this->search['from']);
  349.         }
  350.         return Carbon::now();
  351.     }
  352.     private function setSearchTo(string $timestring): void
  353.     {
  354.         $this->search['to'] = htmlspecialchars($timestring);
  355.     }
  356.     private function setSortFactoryParam(string $sortFactoryParam): void
  357.     {
  358.         $this->search['sortFactoryParam'] = htmlspecialchars($sortFactoryParam);
  359.     }
  360.     public function getSearchTo(): Carbon
  361.     {
  362.         if (!$this->initialized) {
  363.             $this->initOnce();
  364.         }
  365.         if (isset($this->search['to'])) {
  366.             return $this->elementsCustomDateFormat->stringToCarbon($this->search['to']);
  367.         }
  368.         return Carbon::now()->addDays(7);
  369.     }
  370.     private function setDurationType(string $durationType): void
  371.     {
  372.         $this->search['durationType'] = htmlspecialchars($durationType);
  373.     }
  374.     private function setDurationNights(string $durationNights): void
  375.     {
  376.         $this->search['durationNights'] = htmlspecialchars($durationNights);
  377.     }
  378.     private function setOccupancyType(string $occupancyType): void
  379.     {
  380.         $this->search['occupancyType'] = htmlspecialchars($occupancyType);
  381.     }
  382.     public function getSearchArray(): array
  383.     {
  384.         if (!$this->initialized) {
  385.             $this->initOnce();
  386.         }
  387.         $stateArray = [];
  388.         $stateArray['search'] = empty($this->search) ? new stdClass() : $this->search;
  389.         $stateArray['searchForm'] = empty($this->searchForm) ? new stdClass() : $this->searchForm ;
  390.         $stateArray['filter'] = $this->filter ;
  391.         $stateArray['sorting'] = $this->sorting;
  392.         $stateArray['occupancy'] = $this->rooms['occupancy'];
  393.         $stateArray['accList'] = $this->accList;
  394.         $stateArray['nearbySearch'] = $this->nearbySearch;
  395.         $stateArray['showMap'] = $this->showMap;
  396.         return $stateArray;
  397.     }
  398. }