src/Twig/AlpsteinExtension.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use App\Model\DataObject\WeatherStation;
  4. use Pimcore\Model\DataObject\AlpsteinTour;
  5. use Pimcore\Model\DataObject\AlpsteinRoadblock;
  6. use Pimcore\Model\DataObject\Community;
  7. use Twig\Extension\AbstractExtension;
  8. use Twig\TwigFunction;
  9. class AlpsteinExtension extends AbstractExtension
  10. {
  11.     public function getName(): string
  12.     {
  13.         return "Alpstein Extension";
  14.     }
  15.     public function getFunctions(): array
  16.     {
  17.         return [
  18.             new TwigFunction('getStartingRegionsFromTour', [$this'getStartingRegionsFromTour']),
  19.             new TwigFunction('getTourLocation', [$this'getTourLocation']),
  20.             new TwigFunction('getClosureStatesForTour', [$this'getClosureStatesForTour']),
  21.             new TwigFunction('getWeatherStationByAlpsteinTour', [$this'getWeatherStationByAlpsteinTour']),
  22.         ];
  23.     }
  24.     public function getTourLocation(AlpsteinTour $tour) {
  25.         $location false;
  26.         $communities $tour->getCommunityObjects();
  27.         if (isset($communities[0]) && $communities[0]->getElement()) {
  28.             $location $communities[0]->getElement();
  29.         }
  30.         foreach($communities as $communityData){
  31.             if ($communityData->getIsStartingRegion()) {
  32.                 $location $communityData->getElement();
  33.                 break;
  34.             }
  35.         }
  36.         if ($location instanceof Community) {
  37.             return $location->getNameLocalized() ?? $location->getName();
  38.         }
  39.         return false;
  40.     }
  41.     public function getStartingRegionsFromTour(AlpsteinTour $alpsteinTour) :array {
  42.         $startingRegions = [];
  43.         $communities $alpsteinTour->getCommunityObjects();
  44.         foreach($communities as $communityData){
  45.             if ($communityData->getIsStartingRegion()) {
  46.                 $startingRegions[] = $communityData->getElement();
  47.             }
  48.         }
  49.         return $startingRegions;
  50.     }
  51.     public function getWeatherStationByAlpsteinTour(AlpsteinTour $tour) : ?WeatherStation {
  52.         $prognosis null;
  53.         foreach($tour->getCommunityObjects() as $communityData) {
  54.             $communityObject $communityData->getElement();
  55.             if($communityObject instanceof Community && $communityObject->getPrognosis()) {
  56.                 if($communityData->getIsStartingRegion()) {
  57.                     return $communityObject->getPrognosis();
  58.                 }
  59.                 $prognosis $communityObject->getPrognosis();
  60.             }
  61.         }
  62.         return $prognosis;
  63.     }
  64.     public function getClosureStatesForTour(AlpsteinTour $tour): array
  65.     {
  66.         $closureStatesBadges = [];
  67.         $relatedRoadblocks = new AlpsteinRoadblock\Listing();
  68.         $tourId $tour->getId();
  69.         $relatedRoadblocks->addConditionParam('affectedTourObject LIKE :tourId', ['tourId' =>  '%,' $tourId ',%']);
  70.         $closureStateMapping = [
  71.             'IN_PREPERATION' => [
  72.                 'text' => 'tour.in-Bearbeitung',
  73.                 'color' => 'danger',
  74.                 'icon' => 'in-preparation',
  75.             ],
  76.             'CLOSED' => [
  77.                 'text' => 'tour.geschlossen',
  78.                 'color' => 'danger',
  79.                 'icon' => 'closed',
  80.             ],
  81.             'PARTIALLY_OPEN' => [
  82.                 'text' => 'tour.teilweise-geöffnet',
  83.                 'color' => 'info',
  84.                 'icon' => 'partially-open',
  85.             ],
  86.             'OPEN_NOT_PREPARED' => [
  87.                 'text' => 'tour.geöffnet-nicht-präpariert',
  88.                 'color' => 'info',
  89.                 'icon' => 'open-not-prepared',
  90.             ],
  91.             'OPEN' => [
  92.                 'text' => 'tour.geöffnet',
  93.                 'color' => 'success',
  94.                 'icon' => 'open',
  95.             ],
  96.         ];
  97.         foreach ($relatedRoadblocks as $roadblock) {
  98.             $relatedTours $roadblock->getAffectedTourObject();
  99.             foreach ($relatedTours as $relatedTour) {
  100.                 $relatedTourGeneralSolutionId $relatedTour->getObjectId();
  101.                 if ($relatedTourGeneralSolutionId == $tourId) {
  102.                     $closureState $relatedTour->getData()['closureState'] ?? '';
  103.                     if (isset($closureStateMapping[$closureState])) {
  104.                         if(!isset($closureStatesBadges[$closureState])) {
  105.                             $closureStatesBadges[$closureState] = $closureStateMapping[$closureState];
  106.                         }
  107.                         $title $closureStatesBadges[$closureState]['title'] ?? '';
  108.                         $title $title ' ' $roadblock->getName() . ' ' $roadblock->getPublicDescription() . ' ' $roadblock->getDiversionDescription();
  109.                         $closureStatesBadges[$closureState]['title'] = trim($title);
  110.                     }
  111.                     break;
  112.                 }
  113.             }
  114.         }
  115.         return $closureStatesBadges;
  116.     }
  117. }