<?php
namespace AppBundle\Controller;
use AppBundle\Service\PartnersService;
use AppBundle\Service\PermissionService;
use AppBundle\Service\ProductsService;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\DataObject;
use Pimcore\Twig\Extension\Templating\HeadTitle;
use Pimcore\Twig\Extension\Templating\Placeholder;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use AppBundle\LinkGenerator\NewsLinkGenerator;
class ObjectViewsController extends FrontendController
{
public function indexAction(Request $request): Response
{
return new Response('Hello world');
}
public function partnerDetailsAction(Request $request, PartnersService $partnersService): Response
{
$partnerId = (int)$request->get('partnerId');
if (!$partnerId) {
throw new BadRequestHttpException( 'Partner ID not set');
}
$partner = DataObject\Partner::getById( $partnerId);
if (!$partner) {
throw new BadRequestHttpException( 'Partner not found');
}
$viewParams = array();
$viewParams = [
"partnerObject" => $partner,
"validPartnerTypes" => $partnersService->getPartnerTypes(),
"navInverted" => true
];
$request->attributes->set('partnerObject', $partner);
//$headTitleHelper( $news->getHeadline() . ' ' . $news->getSubline());
return $this->renderTemplate('@App/ObjectViews/partner-details.html.twig', $viewParams);
}
public function newsDetailsAction(Request $request, HeadTitle $headTitleHelper, PermissionService $permissionService, Placeholder $placeholder): RedirectResponse|Response
{
//$this->setDocument(1);
$newsId = (int)$request->get('newsId');
if (!$newsId) {
throw new BadRequestHttpException( 'News ID not set');
}
$news = DataObject\News::getById( $newsId);
if (!$news) {
throw new BadRequestHttpException( 'News not found');
}
$headTitleHelper( $news->getHeadline());
if ($this->document) {
$newsDetailsBase = $this->document->getProperty('newsDetailsPage');
if ($newsDetailsBase) {
if ($newsDetailsBase->getId() !== $this->document->getId()) {
// Wrong parent page for the news
if ($correctedUrl = NewsLinkGenerator::generate( $news,['document'=>$newsDetailsBase])) {
// => Redirect as it might be an old url
return $this->redirect($correctedUrl);
}
// The url generator could not generate a url
throw new NotFoundHttpException('News details url not found');
}
} else {
// The news details pages should be below a page that has the newsDetailsBase property set
throw new NotFoundHttpException('News details base not found');
}
if (!$permissionService->checkRequiredObjectPermissions( $news, $this->getUser())) {
// The news details pages should be below a page that has the newsDetailsBase property set
throw new AccessDeniedHttpException('Access denied');
}
$placeholder('addBreadcrumb')->append([
'parentId' => $this->document->getId(),
'id' => 'object-'.$news->getId(),
'label' => $news->getHeadline(),
'titel' => $news->getHeadline()
]);
} else {
// The news details pages should have an url that leads to a document
throw new NotFoundHttpException('Product page not found');
}
$viewParams = array();
$viewParams = [
"news" => $news,
"breadcrumbObjectDetails" => $news
];
return $this->renderTemplate('@App/ObjectViews/news-details.html.twig', $viewParams);
}
public function productsDetailsAction(Request $request, ProductsService $productsService, Placeholder $placeholder): Response
{
$productId = (int)$request->get('productId');
$locale = $request->getLocale();
if (!$productId) {
throw new BadRequestHttpException('Product ID not found');
}
$product = DataObject\Schweissgeraet::getById($productId);
if (!$product) {
throw new BadRequestHttpException('Product not found');
}
if ($this->document) {
$seriesObject = $this->document->getProperty('seriesObject');
if (!$seriesObject instanceof DataObject\Schweissgeraet) {
// The product details pages should be below a product series page
throw new NotFoundHttpException('Product series page for product not found');
}
$placeholder('addBreadcrumb')->append([
'parentId' => $this->document->getId(),
'id' => 'object-' . $product->getId(),
'label' => $product->getName(),
'titel' => $product->getName()
]);
$placeholder('navigation_name')->set($product->getName());
} else {
// The product pages should have an url that leads to a document
throw new NotFoundHttpException('Product page not found');
}
$viewParams = array();
$viewParams = [
"breadcrumbObjectDetails" => $product,
"productName" => $product->getLocalizedfields()->getLocalizedValue('name', $locale),
"productSubline" => $product->getLocalizedfields()->getLocalizedValue('subline', $locale),
"headerKeyBenefits" => $productsService->buildKeyBenefitsArrayToDisplay($product),
"galleryHeader" => $productsService->buildHeaderGallery($product),
"galleryApplication" => $productsService->buildApplicationGallery($product),
"productImage" => $product->getImage(),
"productCertificates" => $product->getZertifikat(),
"highlightsList" => $productsService->buildHighlightsList($product),
"operatingConceptItems" => $productsService->buildBedienkonzepteList($product->getBedienkonzepte(), $locale),
"otherProducts" => $productsService->buildSimilarProductsList($product, $locale),
"headerDetailsDescription" => $product->getLocalizedfields()->getLocalizedValue('description', $locale),
"downloads" => $productsService->buildDownloadList($product, $locale),
"technicalDataDetails" => $productsService->buildTechnicalDataTable($product)
];
return $this->renderTemplate('@App/ObjectViews/product-details.html.twig',$viewParams);
}
}