src/Controller/RenderletController.php line 205

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Twig\LinkGenerator;
  4. use App\Twig\ObjectExtension;
  5. use Elements\Bundle\CmsToolsBundle\Templating\Helper\CutStringRespectingWhitespace;
  6. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\Currency;
  7. use Pimcore\Model\Asset;
  8. use Pimcore\Model\DataObject;
  9. use Pimcore\Model\Document\Snippet;
  10. use Elements\Demi\Model;
  11. use Pimcore\Translation\Translator;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. class RenderletController extends AbstractController
  15. {
  16.     /**
  17.      * @var LinkGenerator
  18.      */
  19.     private $linkGenerator;
  20.     /**
  21.      * @var CutStringRespectingWhitespace
  22.      */
  23.     private $cutStringRespectingWhitespace;
  24.     /**
  25.      * @var ObjectExtension
  26.      */
  27.     private $objectExtension;
  28.     /**
  29.      * @var Translator
  30.      */
  31.     private $translator;
  32.     public function __construct(
  33.         LinkGenerator $linkGenerator,
  34.         \Elements\Demi\LinkGenerator $demiLinkGenerator,
  35.         CutStringRespectingWhitespace $cutStringRespectingWhitespace,
  36.         ObjectExtension $objectExtension,
  37.         Translator $translator
  38.     )
  39.     {
  40.         $this->linkGenerator $linkGenerator;
  41.         $this->cutStringRespectingWhitespace $cutStringRespectingWhitespace;
  42.         $this->objectExtension $objectExtension;
  43.         $this->translator $translator;
  44.     }
  45.     public function imgTeaserAction(Request $request)
  46.     {
  47.         if ($request->get('type') == 'object') {
  48.             $object DataObject::getById($request->get('id'));
  49.             $notShowPrice $request->get('notShowPrice'false);
  50.             // TODO Demi Packages incl "erlebnisse/experience" consider "noExpiringObjects" param
  51.             if ($object instanceof DataObject\Community) {
  52.                 return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
  53.                     'img' => $object->getTeaserimage(),
  54.                     'href' => $this->linkGenerator->generate($object),
  55.                     'title' => $this->cutStringRespactingWitheSpaceAddDots(($object->getNameLocalized() ?: $object->getName()) ?: ''50),
  56.                     'badgeText' => null,
  57.                     'styleModifier' => $request->get('styleModifier'),
  58.                     'hasShadow' => $request->get('hasShadow')
  59.                 ]);
  60.             } else if ($object instanceof DataObject\DemiInfrastructure) {
  61.                 return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
  62.                     'img' => $object->getFirstImage(),
  63.                     'href' => $this->linkGenerator->generate($object),
  64.                     'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getName() ?: ''50),
  65.                     'badgeText' => !$notShowPrice && $object->getFoodAndBeveragePriceFrom() ? (new Currency('EUR'))->toCurrency($object->getFoodAndBeveragePriceFrom()) : null,
  66.                     'styleModifier' => $request->get('styleModifier'),
  67.                     'hasShadow' => $request->get('hasShadow')
  68.                 ]);
  69.             } else if($object instanceof DataObject\Package && !$request->get('noExpiringObjects')) {
  70.                 //note: package shows price in img-slider => $notShowPrice ignored
  71.                 if($this->objectExtension->isPackageNotExpired($object) ) {
  72.                     return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
  73.                         'img' => $object->getTeaserImage(),
  74.                         'href' => $this->linkGenerator->generate($object),
  75.                         'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getTitle() ?: ''50),
  76.                         'badgeText' => $object->getPrice() ? (new Currency('EUR'))->toCurrency($object->getPrice()) : null,
  77.                         'styleModifier' => $request->get('styleModifier'),
  78.                         'hasShadow' => $request->get('hasShadow')
  79.                     ]);
  80.                 } elseif(($fallBackTeaserId $request->get('fallback')) && ($fallbackSnippet Snippet::getById($fallBackTeaserId))) {
  81.                     return $this->renderTemplate('Renderlet/snippet.html.twig',
  82.                         [
  83.                             'snippet' => $fallbackSnippet,
  84.                             'styleModifier' => $request->get('styleModifier'),
  85.                             'hasShadow' => $request->get('hasShadow')
  86.                         ]);
  87.                 }else {
  88.                     return new Response('<div class="alert alert-danger">'$this->translator->trans('renderlet.Package expired') . '</div>');
  89.                 }
  90.             } elseif($object instanceof DataObject\DemiAccommodationServiceProvider) {
  91.                 return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
  92.                     'img' => $object->getFirstImage(),
  93.                     'href' => !$request->get("editmode"false) ? $this->linkGenerator->generate($object) : '',
  94.                     'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getName() ?: ''50),
  95.                     'badgeText' => null//todo price
  96.                     'styleModifier' => $request->get('styleModifier'),
  97.                     'hasShadow' => $request->get('hasShadow')
  98.                 ]);
  99.             } elseif($object instanceof DataObject\AlpsteinTour) {
  100.                 return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
  101.                     'img' => $object->getPrimaryImage(),
  102.                     'href' => $this->linkGenerator->generate($object, ['relative' => true]),
  103.                     'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getName() ?: ''50),
  104.                     'badge' => null,
  105.                     'styleModifier' => $request->get('styleModifier'),
  106.                     'hasShadow' => $request->get('hasShadow')
  107.                 ]);
  108.             } elseif($object instanceof DataObject\BlogArticle) {
  109.                 if ($object->getArticleDate() && $object->getArticleDate()->isFuture()) {
  110.                     if(($fallBackTeaserId $request->get('fallback')) && ($fallbackSnippet Snippet::getById($fallBackTeaserId))) {
  111.                         return $this->renderTemplate('Renderlet/snippet.html.twig',
  112.                             [
  113.                                 'snippet' => $fallbackSnippet,
  114.                                 'styleModifier' => $request->get('styleModifier'),
  115.                                 'hasShadow' => $request->get('hasShadow')
  116.                             ]);
  117.                     }else {
  118.                         return new Response('<div class="alert alert-danger">'$this->translator->trans('renderlet.Article is not yet published!') . '</div>');
  119.                     }
  120.                 }
  121.                 return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
  122.                     'img' => $object->getTeaserImage(),
  123.                     'href' => $this->linkGenerator->generate($object),
  124.                     'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getTitle() ?: ''50),
  125.                     'badge' => null,
  126.                     'styleModifier' => $request->get('styleModifier'),
  127.                     'hasShadow' => $request->get('hasShadow')
  128.                 ]);
  129.             } elseif ($object instanceof DataObject\DemiAdditionalService) {
  130.                 return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
  131.                     'img' => !empty($object->getImages()) ? $object->getImages()[0] : null,
  132.                     'href' => $this->linkGenerator->generate($object),
  133.                     'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getName() ?: ''50),
  134.                     'badge' => null//todo
  135.                     'styleModifier' => $request->get('styleModifier'),
  136.                     'hasShadow' => $request->get('hasShadow')
  137.                 ]);
  138.             } elseif ($object instanceof DataObject\DemiHousePackageMaster) {
  139.                 return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
  140.                     'img' => $object->getFirstImage() ?: null,
  141.                     'href' => $this->linkGenerator->generate($object),
  142.                     'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getName() ?: ''50),
  143.                     'badge' => null//todo
  144.                     'styleModifier' => $request->get('styleModifier'),
  145.                     'hasShadow' => $request->get('hasShadow')
  146.                 ]);
  147.             } elseif ($object instanceof DataObject\DemiEvent) {
  148.                 if ($this->objectExtension->isEventNotExpired($object)) {
  149.                     return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
  150.                         'img' => $object->getFirstImage(),
  151.                         'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getName() ?: ''50),
  152.                         'href' => $this->linkGenerator->generate($object),
  153.                         'badgeText' => null//todo
  154.                         'styleModifier' => $request->get('styleModifier'),
  155.                         'hasShadow' => $request->get('hasShadow')
  156.                     ]);
  157.                 } elseif (($fallBackTeaserId $request->get('fallback')) && ($fallbackSnippet Snippet::getById($fallBackTeaserId))) {
  158.                     return $this->renderTemplate('Renderlet/snippet.html.twig',
  159.                         [
  160.                             'snippet' => $fallbackSnippet,
  161.                             'styleModifier' => $request->get('styleModifier'),
  162.                             'hasShadow' => $request->get('hasShadow')
  163.                         ]);
  164.                 } else {
  165.                     return new Response('<div class="alert alert-danger">' $this->translator->trans('renderlet.Event expired') . '</div>');
  166.                 }
  167.             } elseif($object instanceof DataObject\YoungStyriaAccommodation) {
  168.                 return $this->renderTemplate('Renderlet/img-teaser-object.html.twig',
  169.                     [
  170.                         'img' => $object->getTeaserImage(),
  171.                         'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getName() ?: ''50),
  172.                         'href' => $this->linkGenerator->generate($object),
  173.                         'badgeText' => $object->getCity(),
  174.                         'styleModifier' => $request->get('styleModifier'),
  175.                         'hasShadow' => $request->get('hasShadow'),
  176.                         'isYoungStyria' => true,
  177.                     ]);
  178.             }
  179.         } else if ($request->get('type') == 'document') {
  180.             $document \Pimcore\Model\Document::getById($request->get('id'));
  181.             if ($document instanceof Snippet) {
  182.                 return $this->renderTemplate('Renderlet/snippet.html.twig',
  183.                     [
  184.                         'snippet' => $document,
  185.                         'styleModifier' => $request->get('styleModifier'),
  186.                         'hasShadow' => $request->get('hasShadow')
  187.                     ]);
  188.             }
  189.         }
  190.         if (!$this->editmode) {
  191.             return new Response();
  192.         } else {
  193.             return new Response('<div class="alert alert-danger">Not Supported!</div>');
  194.         }
  195.     }
  196.     public function contentTeaserAction(Request $requestCutStringRespectingWhitespace $cutStringRespectingWhitespace) {
  197.         $removeBadge $request->get('removeBadge'false);
  198.         if ($request->get('type') == 'object' && !$request->get('snippet-only'false)) {
  199.             $object DataObject::getById($request->get('id'));
  200.             if ($object instanceof DataObject\DemiInfrastructure) {
  201.                 return $this->renderTemplate('Renderlet/contentTeaser.html.twig', [
  202.                     'image' => $object->getFirstImage(),
  203.                     'title' => $cutStringRespectingWhitespace->__invoke($object->getName() ?: ''150),
  204.                     'badgeText' => !$removeBadge && !empty($object->getTopics()) ? $object->getTopics()[0]->getName() : '',
  205.                     'subtitle' => !empty($object->getTowns()) ? $object->getTowns()[0]->getName() : '',
  206.                     'orientation' => $request->get('orientation'),
  207.                     'href' => $this->linkGenerator->generate($object),
  208.                 ]);
  209.             } else if($object instanceof DataObject\Package) {
  210.                 return $this->renderTemplate('Renderlet/contentTeaser.html.twig', [
  211.                     'image' => $object->getTeaserImage(),
  212.                     'title' => $cutStringRespectingWhitespace->__invoke($object->getTitle() ?: ''150),
  213.                     'subtitle' => $cutStringRespectingWhitespace->__invoke($object->getSubtitle() ?: ''150),
  214.                     'href' => $this->linkGenerator->generate($object),
  215.                     'orientation' => $request->get('orientation')
  216.                 ]);
  217.             } else if($object instanceof DataObject\Event && !$request->get('noExpiringObjects')) {
  218.                 //note: package shows price in img-slider => $notShowPrice ignored
  219.                 return $this->renderTemplate('Renderlet/img-teaser-object.html.twig', [
  220.                     'img' => $object->getTeaserImage(),
  221.                     'href' => $this->linkGenerator->generate($object),
  222.                     'title' => $this->cutStringRespactingWitheSpaceAddDots($object->getTitle(), 50),
  223.                     'badgeText' => !$removeBadge && $object->getPrice() ? (new Currency('EUR'))->toCurrency($object->getPrice()) : null,
  224.                     'styleModifier' => $request->get('styleModifier'),
  225.                     'hasShadow' => $request->get('hasShadow')
  226.                 ]);
  227.             } elseif($object instanceof DataObject\DemiAccommodationServiceProvider) {
  228.                 return $this->renderTemplate('Renderlet/contentTeaser.html.twig', [
  229.                     'image' => $object->getFirstImage(),
  230.                     'title' => $cutStringRespectingWhitespace->__invoke($object->getName() ?: ''150),
  231.                     'href' =>   !$request->get("editmode"false) ? $this->linkGenerator->generate($object) : '',
  232.                     'orientation' => $request->get('orientation')
  233.                 ]);
  234.             } elseif($object instanceof DataObject\AlpsteinTour) {
  235.                 return $this->renderTemplate('Renderlet/contentTeaser.html.twig', [
  236.                     'image' => $object->getPrimaryImage(),
  237.                     'title' => $cutStringRespectingWhitespace->__invoke($object->getName() ?: ''150),
  238.                     'href' => $this->linkGenerator->generate($object, ['relative' => true]),
  239.                     'orientation' => $request->get('orientation')
  240.                 ]);
  241.             } elseif($object instanceof DataObject\BlogArticle) {
  242.                 if ($object->getArticleDate()->isFuture()) {
  243.                     return new Response('<div class="alert alert-danger">Article is not yet published!</div>');
  244.                 }
  245.                 return $this->renderTemplate('Renderlet/contentTeaser.html.twig', [
  246.                     'image' => $object->getTeaserImage(),
  247.                     'title' => $cutStringRespectingWhitespace->__invoke($object->getTitle() ?: ''150),
  248.                     'href' => $this->linkGenerator->generate($object),
  249.                     'orientation' => $request->get('orientation')
  250.                 ]);
  251.             } elseif ($object instanceof DataObject\DemiAdditionalService) {
  252.                 return $this->renderTemplate('Renderlet/contentTeaser.html.twig', [
  253.                     'image' => !empty($object->getImages()) ? $object->getImages()[0] : null,
  254.                     'href' => $this->linkGenerator->generate($object),
  255.                     'title' => $cutStringRespectingWhitespace->__invoke($object->getName() ?: ''150),
  256.                     'orientation' => $request->get('orientation')
  257.                 ]);
  258.             } elseif ($object instanceof DataObject\DemiEvent) {
  259.                 return $this->renderTemplate('Renderlet/contentTeaser.html.twig', [
  260.                     'image' => $object->getFirstImage(),
  261.                     'title' => $cutStringRespectingWhitespace->__invoke($object->getName() ?: ''150),
  262.                     'href' => $this->linkGenerator->generate($object),
  263.                     'orientation' => $request->get('orientation'),
  264.                 ]);
  265.             } elseif ($object instanceof DataObject\YoungStyriaAccommodation) {
  266.                 return $this->renderTemplate('Renderlet/contentTeaser.html.twig', [
  267.                     'image' => $object->getTeaserImage(),
  268.                     'title' => $cutStringRespectingWhitespace->__invoke($object->getName() ?: ''150),
  269.                     'badgeText' => $object->getCity(),
  270.                     'href' => $this->linkGenerator->generate($object),
  271.                     'orientation' => $request->get('orientation'),
  272.                     'isYoungStyria' => true,
  273.                 ]);
  274.             }
  275.         } else if ($request->get('type') == 'document') {
  276.             $document \Pimcore\Model\Document::getById($request->get('id'));
  277.             if ($document instanceof Snippet) {
  278.                 return $this->renderTemplate('Renderlet/snippet.html.twig',
  279.                     [
  280.                         'snippet' => $document,
  281.                         'styleModifier' => $request->get('styleModifier'),
  282.                         'orientation' => $request->get('orientation'),
  283.                         'thumbnail' => $request->get('thumbnail'),
  284.                         'removeBadge' => $removeBadge,
  285.                     ]);
  286.             }
  287.         } else if ($request->get('snippet-only'false) && $this->editmode) {
  288.             return new Response('<div class="alert alert-danger">Supports Simple Text Teaser Snippet only!</div>');
  289.         }
  290.         if (!$this->editmode) {
  291.             return new Response();
  292.         } else {
  293.             return new Response('<div class="alert alert-danger">Not Supported!</div>');
  294.         }
  295.     }
  296.     public function downloadItemAction(Request $request) {
  297.         if ($request->get('type') == 'asset') {
  298.             $asset Asset::getById($request->get('id'));
  299.             if ($asset instanceof Asset) {
  300.                 $image false;
  301.                 $viewLink false;
  302.                 if ($asset instanceof Asset\Image) {
  303.                     $image $asset->getThumbnail('download-list-item');
  304.                     $viewLink $asset->getFullPath();
  305.                 } else if ($asset instanceof Asset\Video) {
  306.                     $image $asset->getImageThumbnail('download-list-item');
  307.                 } else if ($asset instanceof Asset\Document) {
  308.                     $image $asset->getImageThumbnail('download-list-item');
  309.                     if ($asset->getMimetype() == 'application/pdf') {
  310.                         $viewLink $asset->getFullPath();
  311.                     }
  312.                 }
  313.                 $filename $asset->getFilename();
  314.                 $fileEnding pathinfo($filename)['extension'];
  315.                 return $this->renderTemplate('Renderlet/downloadItem.html.twig',
  316.                     [
  317.                         'file' => strtoupper($fileEnding),
  318.                         'image' => $image,
  319.                         'href' => $this->generateUrl('app_asset_download', ['id' => $asset->getId()]),
  320.                         'text' => $asset->getFilename(),
  321.                         'fileSize' => $asset->getFileSize(true),
  322.                         'viewLink' =>  $viewLink
  323.                     ]);
  324.             }
  325.         } elseif($request->get('type') == 'document') {
  326.             if($request->get('subtype') == 'snippet' && $snippet Snippet::getById($request->get('id'))) {
  327.                 return $this->renderTemplate('Renderlet/snippet.html.twig', [
  328.                         'snippet' => $snippet,
  329.                     ]);
  330.             }
  331.         }
  332.         if (!$this->editmode) {
  333.             return new Response();
  334.         } else {
  335.             return new Response('<div class="alert alert-danger">Not Supported!</div>');
  336.         }
  337.     }
  338.     private function cutStringRespactingWitheSpaceAddDots(string $name nullint $length) : string {
  339.         $title $this->cutStringRespectingWhitespace->__invoke($name ?: ''$length);
  340.         if($title != $name$title .= ' ...';
  341.         return $title;
  342.     }
  343. }