<?php declare(strict_types=1);
namespace Pixel\LexikonSW6\Controller;
use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Uuid\Uuid;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Pixel\LexikonSW6\Component\FrontHelpers;
use Pixel\LexikonSW6\Component\CoreFunctions;
use Shopware\Storefront\Page\GenericPageLoader;
use Shopware\Core\PlatformRequest;
use Shopware\Storefront\Framework\Routing\RequestTransformer;
use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
use Pixel\LexikonSW6\Entities\LexikonPage;
use Shopware\Storefront\Page\MetaInformation;
/**
* @RouteScope(scopes={"storefront"})
*/
class FrontendController extends StorefrontController
{
/**
* @var SystemConfigService
*/
private $systemConfigService;
private $config;
/**
* @var FrontHelpers
*/
private $FrontHelpers;
/**
* @var EntityRepositoryInterface
*/
private $lexikonRepository;
/**
* @var GenericPageLoader
*/
private $genericLoader;
/**
* @var CoreFunctions
*/
private $lexikonRewriter;
/**
* @var Connection
*/
private $connection;
/**
* @var SeoUrlPlaceholderHandlerInterface
*/
private $seoUrlRewriter;
private $lexikonCatRepository;
private $cmsLoader;
public function __construct(
SeoUrlPlaceholderHandlerInterface $seoUrlRewriter,
SystemConfigService $systemConfigService,
FrontHelpers $FrontHelpers,
CoreFunctions $lexikonRewriter,
EntityRepositoryInterface $lexikonRepository,
EntityRepositoryInterface $lexikonCatRepository,
GenericPageLoader $genericLoader,
Connection $connection,
SalesChannelCmsPageLoaderInterface $cmsLoader
) {
$this->systemConfigService = $systemConfigService;
$this->FrontHelpers = $FrontHelpers;
$this->lexikonRewriter = $lexikonRewriter;
$this->lexikonRepository = $lexikonRepository;
$this->lexikonCatRepository = $lexikonCatRepository;
$this->genericLoader = $genericLoader;
$this->seoUrlRewriter = $seoUrlRewriter;
$this->connection = $connection;
$this->cmsLoader = $cmsLoader;
}
/**
* @HttpCache()
* @Route("/lexikon", name="frontend.lexikon.index", options={"seo"=false}, methods={"GET"})
* @param Request $request
* @param SalesChannelContext $salesChannelContext
* @param Context $criteriaContext
* @return Response
* @throws CategoryNotFoundException
* @throws InconsistentCriteriaIdsException
* @throws MissingRequestParameterException
*/
public function index(Request $request, SalesChannelContext $salesChannelContext, Context $context): Response
{
$systemConfig = $this->systemConfigService->get('PixelLexikonSW6.config', $salesChannelContext->getSalesChannel()->getId());
if ( $systemConfig['active'] !== true) {
return $this->forwardToRoute('frontend.home.page');
}
$sales_channel_id = $salesChannelContext->getSalesChannel()->getId();
$page = $this->genericLoader->load($request, $salesChannelContext);
$categoryId = $request->get('categoryId');
$navigation = $this->FrontHelpers->getAllLexikon($context, $sales_channel_id, $categoryId, $systemConfig);
$pixelLexikon = array(
//'Categories' => $this->FrontHelpers->getCategories($context),
'Navigation' => $navigation,
'page' => 'index'
);
$breadcrumbLinkName = $this->container->get('translator')->trans('pixelLexikon.page.breadcrumb.name');
$breadcrumb[] = array(
'link' => $this->seoUrlRewriter->generate('frontend.lexikon.index'),
'name' => $breadcrumbLinkName,
'active' => true,
'translated' => array(
'name' => $breadcrumbLinkName
)
);
if (array_key_exists('cmspage', $systemConfig) && ($systemConfig['cmspage'] !== '' || $systemConfig['cmspage'] != null)) {
$cmsLexikonPage = $this->cmsLoader->load($request, new Criteria([$systemConfig['cmspage']]), $salesChannelContext)->first();
$page = LexikonPage::createFrom($this->genericLoader->load($request, $salesChannelContext));
$page->setCmsPage($cmsLexikonPage);
$template = '@Storefront/storefront/page/content/index.html.twig';
return $this->renderStorefront('@Storefront/storefront/base.html.twig', [
'pixelLexikon' => $pixelLexikon,
'page' => $page,
'breadcrumbList' => $breadcrumb
]);
} else {
return $this->renderStorefront('@Storefront/storefront/base.html.twig', [
'pixelLexikon' => $pixelLexikon,
'page' => $page,
'breadcrumbList' => $breadcrumb
]);
}
}
/**
* @HttpCache()
* @Route("/lexikon/listing/{categoryId}", name="frontend.lexikon.listing", options={"seo"=true}, methods={"GET"})
* @param Request $request
* @param SalesChannelContext $salesChannelContext
* @param Context $criteriaContext
* @param $categoryId
* @return Response
* @throws CategoryNotFoundException
* @throws InconsistentCriteriaIdsException
* @throws MissingRequestParameterException
*/
public function listing(
Request $request,
SalesChannelContext $salesChannelContext,
Context $context,
$categoryId
): Response {
$systemConfig = $this->systemConfigService->get('PixelLexikonSW6.config', $salesChannelContext->getSalesChannel()->getId());
if ($systemConfig['active'] !== true) {
return $this->forwardToRoute('frontend.home.page');
}
$limit = intval($request->get('limit', $systemConfig['LEXIKONLIMIT']));
$offset = intval($request->get('p', 1));
if($offset <= 0)
$offset = 1;
$sales_channel_id = $salesChannelContext->getSalesChannel()->getId();
$page = $this->genericLoader->load($request, $salesChannelContext);
$pixelLexikon['Category'] = $this->FrontHelpers->getCategory($context, $categoryId, $systemConfig);
$pixelLexikon['Navigation'] = $this->FrontHelpers->getAllLexikon($context, $sales_channel_id, $categoryId, $systemConfig, $limit, $offset);
$pixelLexikon['categoryId'] = $categoryId;
$pixelLexikon['entries'] = $this->FrontHelpers->getEntries($context, $sales_channel_id, $categoryId, $systemConfig, $limit, $offset);
if(! $pixelLexikon['entries']) {
return $this->redirectToRoute('frontend.lexikon.index');
}
$pixelLexikon['page'] = 'listing';
$pixelLexikon['limit'] = $limit;
$pixelLexikon['offset'] = $offset;
$breadcrumbLinkName = $this->container->get('translator')->trans('pixelLexikon.page.breadcrumb.name');
$breadcrumb[] = array(
'link' => $this->seoUrlRewriter->generate('frontend.lexikon.index'),
'name' => $breadcrumbLinkName,
'translated' => array(
'name' => $breadcrumbLinkName
)
);
$breadcrumb[] = array(
'link' => $this->seoUrlRewriter->generate('frontend.lexikon.listing', ['categoryId' => $categoryId]),
'name' => $pixelLexikon['Category']->getTranslated()['title'],
'active' => true,
'translated' => array(
'name' => $pixelLexikon['Category']->getTranslated()['title']
)
);
return $this->renderStorefront('@Storefront/storefront/base.html.twig', [
'pixelLexikon' => $pixelLexikon,
'page' => $page,
'breadcrumbList' => $breadcrumb
]);
}
/**
* @HttpCache()
* @Route("/lexikon/detail/{id?}", name="frontend.lexikon.detail", options={"seo"=true}, methods={"GET"})
* @param Request $request
* @param SalesChannelContext $salesChannelContext
* @param Context $criteriaContext
* @param $id
* @return Response
* @throws CategoryNotFoundException
* @throws InconsistentCriteriaIdsException
* @throws MissingRequestParameterException
*/
public function detail(Request $request, SalesChannelContext $salesChannelContext, Context $context, $id): Response
{
$systemConfig = $this->systemConfigService->get('PixelLexikonSW6.config', $salesChannelContext->getSalesChannel()->getId());
if ($systemConfig['active'] !== true) {
return $this->forwardToRoute('frontend.home.page');
}
$sales_channel_id = $salesChannelContext->getSalesChannel()->getId();
$page = $this->genericLoader->load($request, $salesChannelContext);
$lexikonentry = $this->FrontHelpers->getLexikonById($context, $id);
$categoryId = $lexikonentry->get('categoryid');
$this->lexikonRepository->update(
[
['id' => $id, 'shows' => $lexikonentry->get('shows') + 1],
],
$context
);
$pixelLexikon['Navigation'] = $this->FrontHelpers->getAllLexikon($context, $sales_channel_id, $categoryId, $systemConfig);
$pixelLexikon['categoryId'] = $categoryId;
$pixelLexikon['entry'] = $lexikonentry;
$pixelLexikon['page'] = 'detail';
$cmsLexikonPage = $this->cmsLoader->load($request, new Criteria([$lexikonentry->get('cmspageid')]), $salesChannelContext)->first();
$page = LexikonPage::createFrom($this->genericLoader->load($request, $salesChannelContext));
$page->setCmsPage($cmsLexikonPage);
$breadcrumbLinkName = $this->container->get('translator')->trans('pixelLexikon.page.breadcrumb.name');
$breadcrumb[] = array(
'link' => $this->seoUrlRewriter->generate('frontend.lexikon.index'),
'name' => $breadcrumbLinkName,
'translated' => array(
'name' => $breadcrumbLinkName
)
);
$breadcrumb[] = array(
'link' => $this->seoUrlRewriter->generate('frontend.lexikon.listing', ['categoryId' => $categoryId]),
'name' => $lexikonentry->get('category')->getTranslated()['title'],
'translated' => array(
'name' => $lexikonentry->get('category')->getTranslated()['title']
)
);
$breadcrumb[] = array(
'link' => $this->seoUrlRewriter->generate('frontend.lexikon.detail', ['id' => $id]),
'name' => $lexikonentry->getTranslated()['title'],
'active' => true,
'translated' => array(
'name' => $lexikonentry->getTranslated()['title']
)
);
return $this->renderStorefront('@Storefront/storefront/base.html.twig', [
'pixelLexikon' => $pixelLexikon,
'page' => $page,
'breadcrumbList' => $breadcrumb
]);
}
/**
* @HttpCache()
* @Route("/lexikon/ajax/{id?}", name="frontend.lexikon.ajax", options={"seo"=false}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
* @param Request $request
* @param SalesChannelContext $salesChannelContext
* @param Context $criteriaContext
* @param $id
* @return Response
* @throws CategoryNotFoundException
* @throws InconsistentCriteriaIdsException
* @throws MissingRequestParameterException
*/
public function ajax(Request $request, SalesChannelContext $salesChannelContext, Context $context, $id): Response
{
$systemConfig = $this->systemConfigService->get('PixelLexikonSW6.config', $salesChannelContext->getSalesChannel()->getId());
if ($systemConfig['active'] !== true) {
return $this->forwardToRoute('frontend.home.page');
}
if (!$request->isXmlHttpRequest()) {
return $this->forwardToRoute('frontend.lexikon.detail', array('id' => $id));
}
$pixelLexikon['entry'] = $this->FrontHelpers->getLexikonItem($context, $id);
$lexikon_id = $pixelLexikon['entry']->get('id');
$this->lexikonRepository->update(
[
['id' => $lexikon_id, 'shows' => $pixelLexikon['entry']->get('shows') + 1],
],
$context
);
return $this->renderStorefront('@Storefront/storefront/pixel-lexikon/page/ajax/ajax.html.twig', [
'pixelLexikon' => $pixelLexikon
]);
}
/**
*
*
* @param LexikonPage $page
*/
private function loadMeta(LexikonPage $page): void
{
$lexikonEntry = $page->getLexikonEntry();
$metaInformation = new MetaInformation();
$metaTitle = $lexikonEntry->getTranslation('metaTitle') ?? $lexikonEntry->getTranslation('title');
$metaInformation->setMetaTitle($metaTitle);
$metaDescription = $lexikonEntry->getTranslation('metaDescription') ?? $lexikonEntry->getTranslation('description');
$metaInformation->setMetaDescription((string) $metaDescription);
$keywords = $lexikonEntry->getTranslation('keywords');
$metaInformation->setMetaKeywords((string) $keywords);
$page->setMetaInformation($metaInformation);
}
}