<?php
namespace App\Twig;
use App\Model\DataObject\WeatherStation;
use Pimcore\Model\DataObject\AlpsteinTour;
use Pimcore\Model\DataObject\AlpsteinRoadblock;
use Pimcore\Model\DataObject\Community;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class AlpsteinExtension extends AbstractExtension
{
public function getName(): string
{
return "Alpstein Extension";
}
public function getFunctions(): array
{
return [
new TwigFunction('getStartingRegionsFromTour', [$this, 'getStartingRegionsFromTour']),
new TwigFunction('getTourLocation', [$this, 'getTourLocation']),
new TwigFunction('getClosureStatesForTour', [$this, 'getClosureStatesForTour']),
new TwigFunction('getWeatherStationByAlpsteinTour', [$this, 'getWeatherStationByAlpsteinTour']),
];
}
public function getTourLocation(AlpsteinTour $tour) {
$location = false;
$communities = $tour->getCommunityObjects();
if (isset($communities[0]) && $communities[0]->getElement()) {
$location = $communities[0]->getElement();
}
foreach($communities as $communityData){
if ($communityData->getIsStartingRegion()) {
$location = $communityData->getElement();
break;
}
}
if ($location instanceof Community) {
return $location->getNameLocalized() ?? $location->getName();
}
return false;
}
public function getStartingRegionsFromTour(AlpsteinTour $alpsteinTour) :array {
$startingRegions = [];
$communities = $alpsteinTour->getCommunityObjects();
foreach($communities as $communityData){
if ($communityData->getIsStartingRegion()) {
$startingRegions[] = $communityData->getElement();
}
}
return $startingRegions;
}
public function getWeatherStationByAlpsteinTour(AlpsteinTour $tour) : ?WeatherStation {
$prognosis = null;
foreach($tour->getCommunityObjects() as $communityData) {
$communityObject = $communityData->getElement();
if($communityObject instanceof Community && $communityObject->getPrognosis()) {
if($communityData->getIsStartingRegion()) {
return $communityObject->getPrognosis();
}
$prognosis = $communityObject->getPrognosis();
}
}
return $prognosis;
}
public function getClosureStatesForTour(AlpsteinTour $tour): array
{
$closureStatesBadges = [];
$relatedRoadblocks = new AlpsteinRoadblock\Listing();
$tourId = $tour->getId();
$relatedRoadblocks->addConditionParam('affectedTourObject LIKE :tourId', ['tourId' => '%,' . $tourId . ',%']);
$closureStateMapping = [
'IN_PREPERATION' => [
'text' => 'tour.in-Bearbeitung',
'color' => 'danger',
'icon' => 'in-preparation',
],
'CLOSED' => [
'text' => 'tour.geschlossen',
'color' => 'danger',
'icon' => 'closed',
],
'PARTIALLY_OPEN' => [
'text' => 'tour.teilweise-geöffnet',
'color' => 'info',
'icon' => 'partially-open',
],
'OPEN_NOT_PREPARED' => [
'text' => 'tour.geöffnet-nicht-präpariert',
'color' => 'info',
'icon' => 'open-not-prepared',
],
'OPEN' => [
'text' => 'tour.geöffnet',
'color' => 'success',
'icon' => 'open',
],
];
foreach ($relatedRoadblocks as $roadblock) {
$relatedTours = $roadblock->getAffectedTourObject();
foreach ($relatedTours as $relatedTour) {
$relatedTourGeneralSolutionId = $relatedTour->getObjectId();
if ($relatedTourGeneralSolutionId == $tourId) {
$closureState = $relatedTour->getData()['closureState'] ?? '';
if (isset($closureStateMapping[$closureState])) {
if(!isset($closureStatesBadges[$closureState])) {
$closureStatesBadges[$closureState] = $closureStateMapping[$closureState];
}
$title = $closureStatesBadges[$closureState]['title'] ?? '';
$title = $title . ' ' . $roadblock->getName() . ' ' . $roadblock->getPublicDescription() . ' ' . $roadblock->getDiversionDescription();
$closureStatesBadges[$closureState]['title'] = trim($title);
}
break;
}
}
}
return $closureStatesBadges;
}
}