<?php
declare(strict_types=1);
/**
* @copyright 2020 Crehler Sp. z o. o.
* @link https://crehler.com/
* @support support@crehler.com
*
* @author Mateusz Flasiński
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\Catalog\Controller\Storefront;
use Crehler\Catalog\Service\CatalogReader\BookmarksTreeService;
use Crehler\Catalog\Service\CatalogReader\Loader\CatalogPageLoader as ApiCatalogPageLoader;
use Crehler\Catalog\Service\CatalogReader\Page\CatalogPage\CatalogPageLoader;
use Crehler\Catalog\Service\PdfFileLoaderService;
use Crehler\Catalog\Service\SnippetsService;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @RouteScope(scopes={"storefront"})
*/
class CatalogController extends StorefrontController
{
private BookmarksTreeService $bookmarksTreeService;
private CatalogPageLoader $catalogPageLoader;
private SnippetsService $snippetsService;
private ApiCatalogPageLoader $catalogPage;
private PdfFileLoaderService $fileLoaderService;
public function __construct(BookmarksTreeService $bookmarksTreeService, CatalogPageLoader $catalogPageLoader, SnippetsService $snippetsService, ApiCatalogPageLoader $catalogPage, PdfFileLoaderService $fileLoaderService)
{
$this->bookmarksTreeService = $bookmarksTreeService;
$this->catalogPageLoader = $catalogPageLoader;
$this->snippetsService = $snippetsService;
$this->catalogPage = $catalogPage;
$this->fileLoaderService = $fileLoaderService;
}
/**
* @Route("/catalog/print/{catalogId}", name="frontend.catalog.print", options={"seo"="true"}, methods={"GET"})
*/
public function print(string $catalogId, SalesChannelContext $context): Response
{
return $this->fileLoaderService->loadCatalog($catalogId, $context);
}
/**
* @Route("/catalog/print-page/{pdfId}", name="frontend.catalog.print-page", options={"seo"="true"}, methods={"GET"})
*/
public function printPage(string $pdfId, SalesChannelContext $context): Response
{
return $this->fileLoaderService->loadPage($pdfId, $context);
}
/**
* @Route("/catalog/print-merge/{leftPdfId}/{rightPdfId}", name="frontend.catalog.print-merge", options={"seo"="true"}, methods={"GET"})
*/
public function printMerge(string $leftPdfId, string $rightPdfId, SalesChannelContext $context): Response
{
return $this->fileLoaderService->loadMerge($leftPdfId, $rightPdfId, $context);
}
/**
* @HttpCache()
* @Route("/catalog/detail/{catalogId}", name="frontend.catalog.detail", options={"seo"="true"}, methods={"GET"})
*/
public function detail(Request $request, SalesChannelContext $context): ?Response
{
return $this->load($request, $context, 'detail');
}
/**
* @HttpCache()
* @Route("/catalog/test", name="frontend.catalog.test", options={"seo"="true"}, methods={"GET"})
*/
public function test(Request $request, SalesChannelContext $context): ?Response
{
$page = $this->catalogPage->getPageDetail('2764d9c75ce14c7680221452071847a1', $context);
}
/**
* @HttpCache()
* @Route("/catalog/page/{pageId}", name="frontend.catalog.page", options={"seo"="true"}, methods={"GET"})
*/
public function page(Request $request, SalesChannelContext $context): ?Response
{
return $this->load($request, $context, 'page');
}
/**
* @HttpCache()
* @Route("/catalog/bookmark/{bookmarkId}", name="frontend.catalog.bookmark", options={"seo"="true"}, methods={"GET"})
*/
public function bookmark(Request $request, SalesChannelContext $context): ?Response
{
return $this->load($request, $context, 'bookmark');
}
private function load(Request $request, SalesChannelContext $context, string $action = 'detail'): ?Response
{
$currencySettings = [
'isoCode' => $context->getCurrency()->getIsoCode(),
'language' => 'pl-PL', // TODO wyjąć skądeś
];
$page = $this->catalogPageLoader->load($request, $context);
$apiPageData = $this->catalogPage->getPageDetail($page->getCatalogPageEntity()->getId(), $context);
if ($apiPageData->getRight() !== null && $apiPageData->getRight()->getPage()->getId() === $apiPageData->getRequestedId()) {
$apiRequestedPage = $apiPageData->getRight();
} else {
$apiRequestedPage = $apiPageData->getLeft();
}
$page->getMetaInformation()->setMetaTitle($apiPageData->getSeo()->getSeoTitle());
$page->getMetaInformation()->setMetaDescription($apiPageData->getSeo()->getSeoDescription());
$response = $this->renderStorefront('@CrehlerCatalog/storefront/page/catalog/detail.html.twig', [
'page' => $page,
'appSnippets' => $this->snippetsService->load(),
'currencySettings' => $currencySettings,
'apiPageData' => $apiPageData,
'apiRequestedPage' => $apiRequestedPage,
'controllerName' => 'catalog',
'controllerAction' => $action,
]);
return $response;
}
}