src/Service/FacilityService.php line 257

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by Elements.at New Media Solutions GmbH
  4.  *
  5.  */
  6. namespace App\Service;
  7. use Carbon\Carbon;
  8. use Elements\Bundle\SeoHelperBundle\Service\PrettyUrl;
  9. use Exception;
  10. use Pimcore\Model\DataObject;
  11. use Pimcore\Model\DataObject\Lift;
  12. use Pimcore\Model\DataObject\Listing\Concrete;
  13. use Pimcore\Model\DataObject\Miscellaneous;
  14. use Pimcore\Model\DataObject\Resort;
  15. use Pimcore\Model\DataObject\Slope;
  16. use Pimcore\Model\Document\Editable;
  17. class FacilityService
  18. {
  19.     const NO_INFORMATION 0;
  20.     const OPEN 1;
  21.     const IN_PREPARATION 2;
  22.     const CLOSED_TECHNICAL 3;
  23.     const CLOSED_OPERATIONAL 5;
  24.     const CLOSED_ATMOSPHERIC 6;
  25.     const OUT_OF_ORDER 7;
  26.     const SLOPE_OPEN 8;
  27.     const SLOPE_IN_PREPARATION 9;
  28.     const SLOPE_CLOSED_AVALANCHE 10;
  29.     const SLOPE_CLOSED_ICE 11;
  30.     const SLOPE_CLOSED_WIND 12;
  31.     const SLOPE_CLOSED_STORM 13;
  32.     const SLOPE_OUT_OF_ORDER 14;
  33.     const SLOPE_CLOSED 15;
  34.     const CLOSED 16;
  35.     const DAY_OF_REST 17;
  36.     const COMPANY_VACATION 18;
  37.     public function __construct(protected PrettyUrl $prettyUrl, protected FormService $formService)
  38.     {
  39.     }
  40.     /**
  41.      * @param Editable|null $resortRelations
  42.      *
  43.      * @return array<mixed>
  44.      */
  45.     public function getResortTabs(Editable $resortRelations null, ?string $date null): array
  46.     {
  47.         $listing = new Resort\Listing();
  48.         $resorts $resortRelations && !$resortRelations->isEmpty() && method_exists($resortRelations'getElements') ? $resortRelations->getElements() : $listing->load();
  49.         return array_map(function ($r) use ($resorts$date) {
  50.             return [
  51.                 'text' => $r->getName(),
  52.                 'id' => $r->getId(),
  53.                 'targetId' => 'tab-' $r->getId(),
  54.                 'isActive' => $r->getId() == $resorts[0]->getId(),
  55.                 'contentUrl' => $this->prettyUrl->prettyUrl([ 'tab' => $r->getId(), 'date' => $date ], 'facility_tab'),
  56.             ];
  57.         }, $resorts);
  58.     }
  59.     /**
  60.      * @return array<mixed>
  61.      */
  62.     public function getFacilitiesCount(): array
  63.     {
  64.         $resort = new Resort\Listing();
  65.         $resort->addConditionParam('openLiftCount != 0 OR totalLiftCount != 0 OR openSlopeLength != 0 OR totalSlopeLength != 0');
  66.         $resort->setLimit(1);
  67.         $resort $resort->current();
  68.         return [
  69.             'openLifts' => $resort $resort->getOpenLiftCount() : 0,
  70.             'totalLifts' => $resort $resort->getTotalLiftCount() : 0,
  71.             'openSlopes' => $resort $resort->getOpenSlopeLength() : 0,
  72.             'totalSlopes' => $resort $resort->getTotalSlopeLength() : 0,
  73.         ];
  74.     }
  75.     /**
  76.      * @param Resort|null $resort
  77.      *
  78.      * @return array<mixed>
  79.      *
  80.      * @throws Exception
  81.      */
  82.     public function getFacilitiesViewData(Resort $resort nullstring $date null): array
  83.     {
  84.         if (!$resort instanceof Resort) {
  85.             return [];
  86.         }
  87.         $conditions = [ 'area__id = :resortId' ];
  88.         $params = [ 'resortId' => $resort->getId() ];
  89.         if ($date) {
  90.             $date Carbon::createFromFormat('Y-m-d\\T00:00:00'$date)->startOfDay();
  91.             $conditions[] = 'o_id IN (select distinct id from bundle_recurringdates_occurrences where (dateFrom >= :dateFrom and dateTo <= :dateTo))';
  92.             $params['dateFrom'] = $date->format('Y-m-d H:i:s');
  93.             $params['dateTo'] = $date->endOfDay()->format('Y-m-d H:i:s');
  94.             $slopes = [];
  95.         } else {
  96.             $slopes $this->filterAndSortListing(new Slope\Listing(), $conditions$params);
  97.         }
  98.         $lifts $this->filterAndSortListing(new Lift\Listing(), $conditions$params);
  99.         $misc $this->filterAndSortListing(new Miscellaneous\Listing(), $conditions$params);
  100.         $salesPoints $this->filterAndSortListing(new Miscellaneous\Listing(), $conditions$paramstrue);
  101.         $lifts $this->customSortFunction($lifts->load());
  102.         $mappedLiftTypes = [];
  103.         foreach ($lifts as $lift) {
  104.             $mappedLiftType $this->getTypeImageMapping($lift);
  105.             if (!in_array($mappedLiftType$mappedLiftTypes)) {
  106.                 $mappedLiftTypes[] = $mappedLiftType;
  107.             }
  108.         }
  109.         return [
  110.             'modificationDate' => $this->getModificationDate(),
  111.             'snippet' => $resort->getSnippet(),
  112.             'liftTypes' => $mappedLiftTypes,
  113.             'slopeTypes' => $slopes $this->formService->getRelatedObjects($slopes'facilityType') : [],
  114.             'lifts' => $lifts,
  115.             'slopes' => $slopes $this->customSortFunction($slopes->load()) : [],
  116.             'misc' => $this->customSortFunction($misc->load()),
  117.             'salesPoints' => $this->customSortFunction($salesPoints->load()),
  118.             'weather' => DataObject\Weather::getByArea($resort1),
  119.         ];
  120.     }
  121.     public function getTypeImageMapping(Lift $lift): string
  122.     {
  123.         $type $lift->getFacilityType();
  124.         if (in_array($type, ['4''5''6''7'])) {
  125.             $type '9';
  126.         }
  127.         return $type;
  128.     }
  129.     public function isOpen(?DataObject $object null): bool
  130.     {
  131.         if ($object && method_exists($object'getStatus') &&
  132.             ($object->getStatus() == FacilityService::OPEN || $object->getStatus() == FacilityService::SLOPE_OPEN)) {
  133.             return true;
  134.         }
  135.         return false;
  136.     }
  137.     /**
  138.      * @param Concrete $listing
  139.      * @param array<mixed> $conditions
  140.      * @param array<mixed> $params
  141.      * @param bool $salesPoint
  142.      *
  143.      * @return Concrete
  144.      */
  145.     private function filterAndSortListing(Concrete $listing, array $conditions = [], array $params = [], bool $salesPoint false): Concrete
  146.     {
  147.         if ($listing instanceof Miscellaneous\Listing) {
  148.             $listing->addConditionParam('showOnWebsite = 1');
  149.             if($salesPoint) {
  150.                 $listing->addConditionParam('isSalesPoint = 1');
  151.             } else {
  152.                 $listing->addConditionParam('isSalesPoint IS NULL OR isSalesPoint != 1');
  153.             }
  154.         } else {
  155.             $listing->addConditionParam('hideOnWebsite IS NULL OR hideOnWebsite != 1');
  156.         }
  157.         if ($listing instanceof Lift\Listing) {
  158.             if (!array_key_exists('dateFrom'$params) && !array_key_exists('dateTo'$params) || array_key_exists('dateFrom'$params) && Carbon::parse($params['dateFrom'])->isToday()) {
  159.                 $listing->addConditionParam('status = :open', [
  160.                     'open' => FacilityService::OPEN,
  161.                 ]);
  162.                 $conditions[] = 'o_id IN (select distinct id from bundle_recurringdates_occurrences where (dateFrom >= :dateFrom and dateTo <= :dateTo))';
  163.                 $params['dateFrom'] = Carbon::now()->startOfDay()->format('Y-m-d H:i:s');
  164.                 $params['dateTo'] = Carbon::now()->endOfDay()->format('Y-m-d H:i:s');
  165.             }
  166.         } else {
  167.             $listing->addConditionParam('status != :outOfOrder AND status != :slopeOutOfOrder', [
  168.                 'outOfOrder' => FacilityService::OUT_OF_ORDER,
  169.                 'slopeOutOfOrder' => FacilityService::SLOPE_OUT_OF_ORDER,
  170.             ]);
  171.             if ($listing instanceof Slope\Listing) {
  172.                 $listing->addConditionParam('status != :noInformation', [
  173.                     'noInformation' => FacilityService::NO_INFORMATION,
  174.                 ]);
  175.             }
  176.         }
  177.         if ($conditions != []) {
  178.             $listing->addConditionParam(implode(' AND '$conditions), $params);
  179.         }
  180.         $listing->setOrderKey(['status''sort']);
  181.         $listing->setOrder('ASC');
  182.         return $listing;
  183.     }
  184.     private function getModificationDate(): int
  185.     {
  186.         $lifts = new Lift\Listing();
  187.         $lifts->addConditionParam('hideOnWebsite IS NULL OR hideOnWebsite != 1');
  188.         $lifts->setOrderKey('o_modificationDate');
  189.         $lifts->setOrder('DESC');
  190.         $lifts->setLimit(1);
  191.         $lastModificatedLift $lifts->current();
  192.         return $lastModificatedLift->getModificationDate();
  193.     }
  194.     /**
  195.      * @param array<mixed> $objects
  196.      *
  197.      * @return array<mixed>
  198.      */
  199.     private function customSortFunction(array $objects): array
  200.     {
  201.         usort($objects, function ($a$b) {
  202.             $statusA $a->getStatus();
  203.             $statusB $b->getStatus();
  204.             if ($statusA == 0) {
  205.                 return 1;
  206.             }
  207.             if ($statusB == 0) {
  208.                 return -1;
  209.             }
  210.             $statusComparison $statusA $statusB;
  211.             if ($statusComparison !== 0) {
  212.                 return $statusComparison;
  213.             }
  214.             return $a->getSort() - $b->getSort();
  215.         });
  216.         return $objects;
  217.     }
  218. }