<?php
namespace App\Controller;
use App\Twig\LinkGenerator;
use App\Twig\ObjectExtension;
use Elements\Bundle\CmsToolsBundle\Templating\Helper\CutStringRespectingWhitespace;
use Pimcore\Bundle\EcommerceFrameworkBundle\Model\Currency;
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject;
use Pimcore\Model\Document\Snippet;
use Elements\Demi\Model;
use Pimcore\Translation\Translator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class RenderletController extends AbstractController
{
/**
* @var LinkGenerator
*/
private $linkGenerator;
/**
* @var CutStringRespectingWhitespace
*/
private $cutStringRespectingWhitespace;
/**
* @var ObjectExtension
*/
private $objectExtension;
/**
* @var Translator
*/
private $translator;
public function __construct(
LinkGenerator $linkGenerator,
\Elements\Demi\LinkGenerator $demiLinkGenerator,
CutStringRespectingWhitespace $cutStringRespectingWhitespace,
ObjectExtension $objectExtension,
Translator $translator
)
{
$this->linkGenerator = $linkGenerator;
$this->cutStringRespectingWhitespace = $cutStringRespectingWhitespace;
$this->objectExtension = $objectExtension;
$this->translator = $translator;
}
public function imgTeaserAction(Request $request)
{
if ($request->get('type') == 'object') {
$object = DataObject::getById($request->get('id'));
$notShowPrice = $request->get('notShowPrice', false);
// TODO Demi Packages incl "erlebnisse/experience" consider "noExpiringObjects" param
if ($object instanceof DataObject\Community) {
return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
'img' => $object->getTeaserimage(),
'href' => $this->linkGenerator->generate($object),
'title' => $this->cutStringRespactingWitheSpaceAddDots(($object->getNameLocalized() ?: $object->getName()) ?: '', 50),
'badgeText' => null,
'styleModifier' => $request->get('styleModifier'),
'hasShadow' => $request->get('hasShadow')
]);
} else if ($object instanceof DataObject\DemiInfrastructure) {
return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
'img' => $object->getFirstImage(),
'href' => $this->linkGenerator->generate($object),
'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getName() ?: '', 50),
'badgeText' => !$notShowPrice && $object->getFoodAndBeveragePriceFrom() ? (new Currency('EUR'))->toCurrency($object->getFoodAndBeveragePriceFrom()) : null,
'styleModifier' => $request->get('styleModifier'),
'hasShadow' => $request->get('hasShadow')
]);
} else if($object instanceof DataObject\Package && !$request->get('noExpiringObjects')) {
//note: package shows price in img-slider => $notShowPrice ignored
if($this->objectExtension->isPackageNotExpired($object) ) {
return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
'img' => $object->getTeaserImage(),
'href' => $this->linkGenerator->generate($object),
'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getTitle() ?: '', 50),
'badgeText' => $object->getPrice() ? (new Currency('EUR'))->toCurrency($object->getPrice()) : null,
'styleModifier' => $request->get('styleModifier'),
'hasShadow' => $request->get('hasShadow')
]);
} elseif(($fallBackTeaserId = $request->get('fallback')) && ($fallbackSnippet = Snippet::getById($fallBackTeaserId))) {
return $this->renderTemplate('Renderlet/snippet.html.twig',
[
'snippet' => $fallbackSnippet,
'styleModifier' => $request->get('styleModifier'),
'hasShadow' => $request->get('hasShadow')
]);
}else {
return new Response('<div class="alert alert-danger">'. $this->translator->trans('renderlet.Package expired') . '</div>');
}
} elseif($object instanceof DataObject\DemiAccommodationServiceProvider) {
return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
'img' => $object->getFirstImage(),
'href' => !$request->get("editmode", false) ? $this->linkGenerator->generate($object) : '',
'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getName() ?: '', 50),
'badgeText' => null, //todo price
'styleModifier' => $request->get('styleModifier'),
'hasShadow' => $request->get('hasShadow')
]);
} elseif($object instanceof DataObject\AlpsteinTour) {
return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
'img' => $object->getPrimaryImage(),
'href' => $this->linkGenerator->generate($object, ['relative' => true]),
'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getName() ?: '', 50),
'badge' => null,
'styleModifier' => $request->get('styleModifier'),
'hasShadow' => $request->get('hasShadow')
]);
} elseif($object instanceof DataObject\BlogArticle) {
if ($object->getArticleDate() && $object->getArticleDate()->isFuture()) {
if(($fallBackTeaserId = $request->get('fallback')) && ($fallbackSnippet = Snippet::getById($fallBackTeaserId))) {
return $this->renderTemplate('Renderlet/snippet.html.twig',
[
'snippet' => $fallbackSnippet,
'styleModifier' => $request->get('styleModifier'),
'hasShadow' => $request->get('hasShadow')
]);
}else {
return new Response('<div class="alert alert-danger">'. $this->translator->trans('renderlet.Article is not yet published!') . '</div>');
}
}
return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
'img' => $object->getTeaserImage(),
'href' => $this->linkGenerator->generate($object),
'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getTitle() ?: '', 50),
'badge' => null,
'styleModifier' => $request->get('styleModifier'),
'hasShadow' => $request->get('hasShadow')
]);
} elseif ($object instanceof DataObject\DemiAdditionalService) {
return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
'img' => !empty($object->getImages()) ? $object->getImages()[0] : null,
'href' => $this->linkGenerator->generate($object),
'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getName() ?: '', 50),
'badge' => null, //todo
'styleModifier' => $request->get('styleModifier'),
'hasShadow' => $request->get('hasShadow')
]);
} elseif ($object instanceof DataObject\DemiHousePackageMaster) {
return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
'img' => $object->getFirstImage() ?: null,
'href' => $this->linkGenerator->generate($object),
'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getName() ?: '', 50),
'badge' => null, //todo
'styleModifier' => $request->get('styleModifier'),
'hasShadow' => $request->get('hasShadow')
]);
} elseif ($object instanceof DataObject\DemiEvent) {
if ($this->objectExtension->isEventNotExpired($object)) {
return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
'img' => $object->getFirstImage(),
'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getName() ?: '', 50),
'href' => $this->linkGenerator->generate($object),
'badgeText' => null, //todo
'styleModifier' => $request->get('styleModifier'),
'hasShadow' => $request->get('hasShadow')
]);
} elseif (($fallBackTeaserId = $request->get('fallback')) && ($fallbackSnippet = Snippet::getById($fallBackTeaserId))) {
return $this->renderTemplate('Renderlet/snippet.html.twig',
[
'snippet' => $fallbackSnippet,
'styleModifier' => $request->get('styleModifier'),
'hasShadow' => $request->get('hasShadow')
]);
} else {
return new Response('<div class="alert alert-danger">' . $this->translator->trans('renderlet.Event expired') . '</div>');
}
} elseif($object instanceof DataObject\YoungStyriaAccommodation) {
return $this->renderTemplate('Renderlet/img-teaser-object.html.twig',
[
'img' => $object->getTeaserImage(),
'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getName() ?: '', 50),
'href' => $this->linkGenerator->generate($object),
'badgeText' => $object->getCity(),
'styleModifier' => $request->get('styleModifier'),
'hasShadow' => $request->get('hasShadow'),
'isYoungStyria' => true,
]);
}
} else if ($request->get('type') == 'document') {
$document = \Pimcore\Model\Document::getById($request->get('id'));
if ($document instanceof Snippet) {
return $this->renderTemplate('Renderlet/snippet.html.twig',
[
'snippet' => $document,
'styleModifier' => $request->get('styleModifier'),
'hasShadow' => $request->get('hasShadow')
]);
}
}
if (!$this->editmode) {
return new Response();
} else {
return new Response('<div class="alert alert-danger">Not Supported!</div>');
}
}
public function contentTeaserAction(Request $request, CutStringRespectingWhitespace $cutStringRespectingWhitespace) {
$removeBadge = $request->get('removeBadge', false);
if ($request->get('type') == 'object' && !$request->get('snippet-only', false)) {
$object = DataObject::getById($request->get('id'));
if ($object instanceof DataObject\DemiInfrastructure) {
return $this->renderTemplate('Renderlet/contentTeaser.html.twig', [
'image' => $object->getFirstImage(),
'title' => $cutStringRespectingWhitespace->__invoke($object->getName() ?: '', 150),
'badgeText' => !$removeBadge && !empty($object->getTopics()) ? $object->getTopics()[0]->getName() : '',
'subtitle' => !empty($object->getTowns()) ? $object->getTowns()[0]->getName() : '',
'orientation' => $request->get('orientation'),
'href' => $this->linkGenerator->generate($object),
]);
} else if($object instanceof DataObject\Package) {
return $this->renderTemplate('Renderlet/contentTeaser.html.twig', [
'image' => $object->getTeaserImage(),
'title' => $cutStringRespectingWhitespace->__invoke($object->getTitle() ?: '', 150),
'subtitle' => $cutStringRespectingWhitespace->__invoke($object->getSubtitle() ?: '', 150),
'href' => $this->linkGenerator->generate($object),
'orientation' => $request->get('orientation')
]);
} else if($object instanceof DataObject\Event && !$request->get('noExpiringObjects')) {
//note: package shows price in img-slider => $notShowPrice ignored
return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
'img' => $object->getTeaserImage(),
'href' => $this->linkGenerator->generate($object),
'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getTitle(), 50),
'badgeText' => !$removeBadge && $object->getPrice() ? (new Currency('EUR'))->toCurrency($object->getPrice()) : null,
'styleModifier' => $request->get('styleModifier'),
'hasShadow' => $request->get('hasShadow')
]);
} elseif($object instanceof DataObject\DemiAccommodationServiceProvider) {
return $this->renderTemplate('Renderlet/contentTeaser.html.twig', [
'image' => $object->getFirstImage(),
'title' => $cutStringRespectingWhitespace->__invoke($object->getName() ?: '', 150),
'href' => !$request->get("editmode", false) ? $this->linkGenerator->generate($object) : '',
'orientation' => $request->get('orientation')
]);
} elseif($object instanceof DataObject\AlpsteinTour) {
return $this->renderTemplate('Renderlet/contentTeaser.html.twig', [
'image' => $object->getPrimaryImage(),
'title' => $cutStringRespectingWhitespace->__invoke($object->getName() ?: '', 150),
'href' => $this->linkGenerator->generate($object, ['relative' => true]),
'orientation' => $request->get('orientation')
]);
} elseif($object instanceof DataObject\BlogArticle) {
if ($object->getArticleDate()->isFuture()) {
return new Response('<div class="alert alert-danger">Article is not yet published!</div>');
}
return $this->renderTemplate('Renderlet/contentTeaser.html.twig', [
'image' => $object->getTeaserImage(),
'title' => $cutStringRespectingWhitespace->__invoke($object->getTitle() ?: '', 150),
'href' => $this->linkGenerator->generate($object),
'orientation' => $request->get('orientation')
]);
} elseif ($object instanceof DataObject\DemiAdditionalService) {
return $this->renderTemplate('Renderlet/contentTeaser.html.twig', [
'image' => !empty($object->getImages()) ? $object->getImages()[0] : null,
'href' => $this->linkGenerator->generate($object),
'title' => $cutStringRespectingWhitespace->__invoke($object->getName() ?: '', 150),
'orientation' => $request->get('orientation')
]);
} elseif ($object instanceof DataObject\DemiEvent) {
return $this->renderTemplate('Renderlet/contentTeaser.html.twig', [
'image' => $object->getFirstImage(),
'title' => $cutStringRespectingWhitespace->__invoke($object->getName() ?: '', 150),
'href' => $this->linkGenerator->generate($object),
'orientation' => $request->get('orientation'),
]);
} elseif ($object instanceof DataObject\YoungStyriaAccommodation) {
return $this->renderTemplate('Renderlet/contentTeaser.html.twig', [
'image' => $object->getTeaserImage(),
'title' => $cutStringRespectingWhitespace->__invoke($object->getName() ?: '', 150),
'badgeText' => $object->getCity(),
'href' => $this->linkGenerator->generate($object),
'orientation' => $request->get('orientation'),
'isYoungStyria' => true,
]);
}
} else if ($request->get('type') == 'document') {
$document = \Pimcore\Model\Document::getById($request->get('id'));
if ($document instanceof Snippet) {
return $this->renderTemplate('Renderlet/snippet.html.twig',
[
'snippet' => $document,
'styleModifier' => $request->get('styleModifier'),
'orientation' => $request->get('orientation'),
'thumbnail' => $request->get('thumbnail'),
'removeBadge' => $removeBadge,
]);
}
} else if ($request->get('snippet-only', false) && $this->editmode) {
return new Response('<div class="alert alert-danger">Supports Simple Text Teaser Snippet only!</div>');
}
if (!$this->editmode) {
return new Response();
} else {
return new Response('<div class="alert alert-danger">Not Supported!</div>');
}
}
public function downloadItemAction(Request $request) {
if ($request->get('type') == 'asset') {
$asset = Asset::getById($request->get('id'));
if ($asset instanceof Asset) {
$image = false;
$viewLink = false;
if ($asset instanceof Asset\Image) {
$image = $asset->getThumbnail('download-list-item');
$viewLink = $asset->getFullPath();
} else if ($asset instanceof Asset\Video) {
$image = $asset->getImageThumbnail('download-list-item');
} else if ($asset instanceof Asset\Document) {
$image = $asset->getImageThumbnail('download-list-item');
if ($asset->getMimetype() == 'application/pdf') {
$viewLink = $asset->getFullPath();
}
}
$filename = $asset->getFilename();
$fileEnding = pathinfo($filename)['extension'];
return $this->renderTemplate('Renderlet/downloadItem.html.twig',
[
'file' => strtoupper($fileEnding),
'image' => $image,
'href' => $this->generateUrl('app_asset_download', ['id' => $asset->getId()]),
'text' => $asset->getFilename(),
'fileSize' => $asset->getFileSize(true),
'viewLink' => $viewLink
]);
}
} elseif($request->get('type') == 'document') {
if($request->get('subtype') == 'snippet' && $snippet = Snippet::getById($request->get('id'))) {
return $this->renderTemplate('Renderlet/snippet.html.twig', [
'snippet' => $snippet,
]);
}
}
if (!$this->editmode) {
return new Response();
} else {
return new Response('<div class="alert alert-danger">Not Supported!</div>');
}
}
private function cutStringRespactingWitheSpaceAddDots(string $name = null, int $length) : string {
$title = $this->cutStringRespectingWhitespace->__invoke($name ?: '', $length);
if($title != $name) $title .= ' ...';
return $title;
}
}