<?php
/**
* Created by Elements.at New Media Solutions GmbH
*
*/
namespace App\Controller;
use App\Service\LinkGenerator\NewsLinkGenerator;
use App\Service\RenderletService;
use Elements\Bundle\TicketShopFrameworkBundle\Service\CartService;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\News;
use Pimcore\Model\Document;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
class RenderletController extends AbstractController
{
public function __construct(
CartService $cartService,
Environment $twig,
protected NewsLinkGenerator $newsLinkGenerator
) {
parent::__construct($cartService, $twig);
}
/**
*
* @param Request $request
* @param RenderletService $service
*
* @return Response
*/
public function largeImageTeaserAction(Request $request, RenderletService $service): Response
{
if ($request->get('type') == 'document') {
$document = Document::getById((int)$request->get('id'));
if ($document instanceof Document\Snippet && $document->getTemplate() == 'snippets/image-text.html.twig') {
return $this->renderTemplate('teaser/large-image-teaser.html.twig', $service->getLargeImageTeaserDataFromSnippet([ $document ])[0]);
}
}
// return information for customer:
if ($request->get('editmode')) {
return $this->renderTemplate('teaser/error-teaser.html.twig', [
'message' => 'Please use only "Offer Teaser" snippets or PackageOffer objects!',
]);
}
// return empty response to prevent error
return new Response();
}
public function citationAction(Request $request): Response
{
if ($request->get('type') == 'document') {
$document = Document::getById((int)$request->get('id'));
if ($document instanceof Document\Snippet && $document->getTemplate() == 'snippets/citation.html.twig') {
$returnArray = [
'author' => $document->getEditable('author'),
'position' => $document->getEditable('position'),
'image' => $document->getEditable('image'),
'quote' => $document->getEditable('quote'),
];
return $this->renderTemplate($request->get('template'), $returnArray);
}
}
// return information for customer:
if ($request->get('editmode')) {
return $this->renderTemplate('teaser/error-teaser.html.twig', [
'message' => 'Please use only "Offer Teaser" snippets or PackageOffer objects!',
]);
}
// return empty response to prevent error
return new Response();
}
public function productTeaserAction(Request $request): Response
{
if ($request->get('type') == 'object') {
$product = Concrete::getById((int)$request->get('id'));
if ($product instanceof Concrete) {
$returnArray = [
'product' => $product,
'className' => $request->get('className'),
'singleButton' => $request->get('singleButton'),
];
return $this->renderTemplate($request->get('template'), $returnArray);
}
} elseif ($request->get('type') == 'document') {
$document = Document::getById((int)$request->get('id'));
if ($document instanceof Document\Snippet && $document->getTemplate() == 'snippets/card-teaser.html.twig') {
return $this->renderTemplate('teaser/card-teaser.html.twig', [
'image' => $document->getEditable('image'),
'toptitle' => $document->getEditable('toptitle') ? $document->getEditable('toptitle')->getData() : '',
'title' => $document->getEditable('title'),
'text' => $document->getEditable('text') ? $document->getEditable('text')->getData() : ' ',
'link' => $document->getEditable('link'),
'useRedButton' => $request->get('useRedButton'),
'key' => $request->get('key'),
]);
}
}
// return information for customer:
if ($request->get('editmode')) {
return $this->renderTemplate('teaser/error-teaser.html.twig', [
'message' => 'Please use only Ticket objects!',
]);
}
// return empty response to prevent error
return new Response();
}
public function newsTeaserAction(Request $request, TranslatorInterface $translator): Response
{
if ($request->get('type') == 'object') {
$news = News::getById((int)$request->get('id'));
$tagNames = [];
foreach ($news->getCategories() as $category) {
$tagNames[]= $category->getName();
}
if ($news instanceof News) {
$returnArray = [
'title' => $news->getTitle(),
'toptitle' => $news->getPublishDate()->format('d.m.Y'),
'className' => $request->get('className'),
'type' => $request->get('teaserType'),
'tag' => $request->get('tag') ? implode(', ', $tagNames) : false,
'image' => $request->get('image') ? $news->getImage() : false,
'content' => $request->get('content') ? $news->getIntro() : false,
'link' => [
'href' => $this->newsLinkGenerator->generate($news),
'text' => $translator->trans('news.overview.read-more'),
],
];
return $this->renderTemplate($request->get('template'), $returnArray);
}
}
return new Response();
}
}