<?php
/**
* Created by Elements.at New Media Solutions GmbH
*
*/
namespace App\Controller;
use App\Service\FacilityService;
use App\Service\RenderletService;
use Elements\Bundle\TicketShopFrameworkBundle\Service\CartService;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\Fieldcollection\Data\ArrivalTabbingContent;
use Pimcore\Model\DataObject\Fieldcollection\Data\Citation;
use Pimcore\Model\DataObject\Fieldcollection\Data\Gallery;
use Pimcore\Model\DataObject\Fieldcollection\Data\LargeImage;
use Pimcore\Model\DataObject\Fieldcollection\Data\MixedTeaser;
use Pimcore\Model\DataObject\Fieldcollection\Data\PortalTabbingContent;
use Pimcore\Model\DataObject\Fieldcollection\Data\Wysiwyg;
use Pimcore\Model\DataObject\Fieldcollection\Data\WysiwygMedia;
use Pimcore\Model\DataObject\TabbingContent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;
class TabbingController extends AbstractController
{
public function __construct(CartService $cartService, Environment $twig, protected RenderletService $renderletService)
{
parent::__construct($cartService, $twig);
}
/**
* @Route("/{_locale}/portal-tab", name="portal_tab")
*
* @param Request $request
*
* @return Response
*/
public function portalTabAction(Request $request): Response
{
$template = '';
$tabbingContentObject = TabbingContent::getById($request->get('tab'));
$tabbingContent = $tabbingContentObject->getContent()->current();
if ($tabbingContent instanceof PortalTabbingContent) {
$template = $this->renderTemplate('includes/tabbing/dynamic-text-media.html.twig', [
'tabbingObject' => $tabbingContent,
])->getContent();
}
return $this->json([ 'html' => $template, 'success' => true ]);
}
/**
* @Route("/{_locale}/career-tab", name="career_tab")
*
* @param Request $request
*
* @return Response
*/
public function careerTabAction(Request $request): Response
{
$jobs = new DataObject\Job\Listing();
$category = DataObject\JobCategory::getById($request->get('tab'));
if ($category instanceof DataObject\JobCategory) {
$jobs->addConditionParam('category__id = :categoryId', [ 'categoryId' => $request->get('tab') ]);
}
$team = DataObject\JobTeam::getById($request->get('team'));
if ($team instanceof DataObject\JobTeam) {
$jobs->addConditionParam('team__id = :teamId', [ 'teamId' => $request->get('team') ]);
}
$template = $this->renderTemplate('job/partials/career-tabbing-content.html.twig', [ 'jobs' => $jobs->load()]);
return $this->json([ 'html' => $template->getContent(), 'success' => true ]);
}
/**
* @Route("/{_locale}/weather-tab", name="weather_tab")
*
* @param Request $request
*
* @return Response
*/
public function weatherTabAction(Request $request): Response
{
$weather = DataObject\Weather::getById($request->get('tab'));
$template = $this->renderTemplate('weather/partials/weather-tabbing-content.html.twig', [ 'weather' => $weather ]);
return $this->json([ 'html' => $template->getContent(), 'success' => true ]);
}
/**
* @Route("/{_locale}/facility-tab", name="facility_tab")
*
* @param Request $request
* @param FacilityService $service
*
* @return Response
*/
public function facilityTabAction(Request $request, FacilityService $service): Response
{
$resort = DataObject\Resort::getById($request->get('tab'));
$data = $service->getFacilitiesViewData($resort, $request->get('date') ?: null);
$template = $this->renderTemplate('facility/partials/facility-tabbing-content.html.twig', $data);
return $this->json([ 'html' => $template->getContent(), 'success' => true ]);
}
/**
* @Route("/{_locale}/arrival-tab", name="arrival_tab")
*
* @param Request $request
*
* @return Response
*/
public function arrivalTabAction(Request $request): Response
{
$tabbingContentObject = TabbingContent::getById($request->get('tab'));
$tabbingContent = $tabbingContentObject->getContent()->current();
if ($tabbingContent instanceof ArrivalTabbingContent) {
$links = $tabbingContent->getLinks();
$destination = $request->get('destination') ?: 'zermatt';
$links = array_filter($links, function ($link) use ($destination) {
return $link->getDestination() == $destination;
});
$meanOfTransport = $request->get('meanOfTransport') ?: 'train';
$links = array_filter($links, function ($link) use ($meanOfTransport) {
return $link->getMeanOfTransport() == $meanOfTransport;
});
if ($request->get('destination') || $request->get('meanOfTransport')) {
$template = $this->renderTemplate('includes/tabbing/partials/arrival-links.html.twig', [ 'links' => $links ])->getContent();
} else {
$template = $this->renderTemplate('includes/tabbing/arrival.html.twig', [
'elementId' => $tabbingContentObject->getId(),
'content' => $tabbingContent,
'links' => $links,
])->getContent();
}
} else {
$template = $this->getContentTabTemplate($tabbingContent);
}
return $this->json([ 'html' => $template, 'success' => true ]);
}
/**
* @Route("/{_locale}/content-tab", name="content_tab")
*
* @param Request $request
*
* @return Response
*/
public function contentTabAction(Request $request): Response
{
$tabbingContentObject = TabbingContent::getById($request->get('tab'));
$tabbingContent = $tabbingContentObject->getContent()->current();
return $this->json([ 'html' => $this->getContentTabTemplate($tabbingContent), 'success' => true ]);
}
private function getContentTabTemplate(DataObject\Fieldcollection\Data\AbstractData $tabbingContent): string
{
if ($tabbingContent instanceof Wysiwyg) {
return $this->renderTemplate('includes/content/wysiwyg.html.twig', [
'topTitle' => $tabbingContent->getTopTitle(),
'title' => $tabbingContent->getTitle(),
'text' => $tabbingContent->getText(),
'link' => $tabbingContent->getLink(),
'buttonType' => $tabbingContent->getButtonType(),
])->getContent();
} elseif ($tabbingContent instanceof WysiwygMedia) {
$template = $this->renderTemplate('includes/content/wysiwyg-media-two-cols.html.twig', [
'badgeText' => $tabbingContent->getBadgeText(),
'topTitle' => $tabbingContent->getTopTitle(),
'title' => $tabbingContent->getTitle(),
'text' => $tabbingContent->getText(),
'link' => $tabbingContent->getLink(),
'buttonType' => $tabbingContent->getButtonType(),
'image' => $tabbingContent->getImage(),
'video' => $tabbingContent->getVideo(),
'mediaType' => $tabbingContent->getVideo() ? 'video' : 'image',
'textPosition' => $tabbingContent->getTextPosition() ?: 'left',
])->getContent();
return preg_replace('#<script(.*?)>(.*?)</script>#is', '', $template);
} elseif ($tabbingContent instanceof Citation) {
$snippet = $tabbingContent->getSnippet() && $tabbingContent->getSnippet()->getTemplate() == 'snippets/citation.html.twig' ? $tabbingContent->getSnippet() : null;
return $this->renderTemplate('includes/content/citation.html.twig', [
'author' => $snippet ? $snippet->getEditable('author') : $tabbingContent->getAuthor(),
'position' => $snippet ? $snippet->getEditable('position') : $tabbingContent->getPosition(),
'quote' => $snippet ? $snippet->getEditable('quote') : $tabbingContent->getQuote(),
])->getContent();
} elseif ($tabbingContent instanceof Gallery) {
$template = '<section class="wysiwyg-three-images-area">';
foreach ($tabbingContent->getRows() as $row) {
$template .= $this->renderTemplate('includes/content/image-grid.html.twig', [
'images' => $row['images']->getData(),
'positionBigImage' => $row['positionBigImage']->getData(),
])->getContent();
}
return $template . '</section>';
} elseif ($tabbingContent instanceof LargeImage) {
$returnArray = $this->renderletService->getLargeImageTeaserDataFromSnippet($tabbingContent->getSnippet());
$templateFile = count($returnArray) == 1 ? 'teaser/large-image-teaser.html.twig' : 'includes/content/large-image-slider.html.twig';
return $this->renderTemplate($templateFile, $returnArray)->getContent();
} elseif ($tabbingContent instanceof MixedTeaser) {
return $this->renderTemplate('includes/content/mixed-teaser-slider.html.twig', [
'topTitle' => $tabbingContent->getTopTitle(),
'title' => $tabbingContent->getTitle(),
'teasers' => array_filter($tabbingContent->getTeaser(), function ($teaser) {
return $teaser instanceof DataObject || $teaser->getTemplate() == 'snippets/card-teaser.html.twig';
}),
])->getContent();
}
return '';
}
}