<?php declare(strict_types=1);
namespace NetzpPowerPack6\Subscriber;
use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Content\Category\CategoryEvents;
use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Event\ThemeCompilerConcatenatedStylesEvent;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FrontendSubscriber implements EventSubscriberInterface
{
private $container;
private $cmsPageLoader;
private $config;
public function __construct(ContainerInterface $container,
SalesChannelCmsPageLoaderInterface $cmsPageLoader,
SystemConfigService $config)
{
$this->container = $container;
$this->cmsPageLoader = $cmsPageLoader;
$this->config = $config;
}
public static function getSubscribedEvents(): array
{
return [
HeaderPageletLoadedEvent::class => 'loadCmsHeader',
FooterPageletLoadedEvent::class => 'loadCmsFooter',
CheckoutFinishPageLoadedEvent::class => 'loadFinishPage',
ProductEvents::PRODUCT_LOADED_EVENT => 'onProductLoaded',
CategoryEvents::CATEGORY_LOADED_EVENT => 'onCategoryLoaded',
ThemeCompilerConcatenatedStylesEvent::class => 'onThemeCompiler'
];
}
public function onThemeCompiler(ThemeCompilerConcatenatedStylesEvent $event)
{
$config = $this->config->get('NetzpPowerPack6.config', $event->getSalesChannelId());
if (array_key_exists('excludefontawesome', $config) && $config['excludefontawesome']) {
return;
}
$bundle = $this->container->get('kernel')->getBundles()['NetzpPowerPack6'] ?? null;
if($bundle === null) {
return;
}
$projectDir = $bundle->getPath() ?? null;
if($projectDir === null) {
return;
}
$styles = $event->getConcatenatedStyles();
$includeFile = $projectDir . "/Resources/app/storefront/src/csslibs/fontawesome/fontawesome";
$styles .= "@import '" . $includeFile . "';" . PHP_EOL;
$event->setConcatenatedStyles($styles);
}
public function onCategoryLoaded(EntityLoadedEvent $event)
{
if($event->getContext()->getScope() != 'user') {
return;
}
if ( ! property_exists($event->getContext()->getSource(), 'salesChannelId')) {
return;
}
$config = $this->config->get('NetzpPowerPack6.config', $event->getContext()
->getSource()
->getSalesChannelId());
if ( ! $config['snippetsproduct']) {
return;
}
$twig = clone $this->container->get('twig');
foreach($event->getEntities() as $entity) {
if(get_class($entity) !== CategoryEntity::class) {
continue;
}
$translated = $entity->getTranslated();
$tplDescription = $twig->createTemplate($translated['description'] ?? '');
$description = $tplDescription->render();
$translated['description'] = $description;
$entity->setDescription($description);
// Name führt hier zu Problemen, u.a. <title> wird nicht ersetzt
/*
$tplName = $twig->createTemplate($translated['name'] ?? '');
$translated['name'] = $tplName->render();
$breadcrumbs = $translated['breadcrumb'];
foreach($breadcrumbs as &$bc) {
$tplBreadcrumb = $twig->createTemplate($bc ?? '');
$bc = $tplBreadcrumb->render();
}
$translated['breadcrumb'] = $breadcrumbs;
*/
$entity->assign(['translated' => $translated]);
}
}
public function onProductLoaded(EntityLoadedEvent $event)
{
if($event->getContext()->getScope() != 'user') {
return;
}
if (property_exists($event->getContext()->getSource(), 'salesChannelId')) {
$config = $this->config->get('NetzpPowerPack6.config', $event->getContext()->getSource()->getSalesChannelId());
}
else {
$config = $this->config->get('NetzpPowerPack6.config');
}
if( ! $config['snippetsproduct']) {
return;
}
$twig = clone $this->container->get('twig');
foreach($event->getEntities() as $entity) {
if(get_class($entity) !== SalesChannelProductEntity::class &&
get_class($entity) !== ProductEntity::class) {
continue;
}
$translated = $entity->getTranslated();
$tplName = $twig->createTemplate($translated['name'] ?? '');
$translated['name'] = $tplName->render();
$tplDescription = $twig->createTemplate($translated['description'] ?? '');
$translated['description'] = $tplDescription->render();
$entity->assign(['translated' => $translated]);
}
}
public function loadCmsheader(HeaderPageletLoadedEvent $event): void
{
$request = $event->getRequest();
$context = $event->getSalesChannelContext();
$customFields = $context->getSalesChannel()->getTranslated()['customFields'];
if($customFields && array_key_exists('netzp_powerpack6_header_cms_show', $customFields) &&
array_key_exists('netzp_powerpack6_header_cms_id', $customFields) &&
$customFields['netzp_powerpack6_header_cms_id'] != null) {
$id = strtolower(trim($customFields['netzp_powerpack6_header_cms_id']));
if ($id != '') {
$criteria = new Criteria([$id]);
$pages = $this->cmsPageLoader->load($request, $criteria, $context);
if (!$pages->has($id)) {
throw new PageNotFoundException($id);
}
}
$event->getPagelet()->assign([
'netzp_header_cms_page' => $pages->get($id),
'netzp_header_cms_position' => $customFields['netzp_powerpack6_header_cms_show'] ?? 0,
'netzp_header_cms_sticky' => $customFields['netzp_powerpack6_header_cms_sticky'] ?? false
]);
}
}
public function loadCmsFooter(FooterPageletLoadedEvent $event): void
{
$request = $event->getRequest();
$context = $event->getSalesChannelContext();
$customFields = $context->getSalesChannel()->getTranslated()['customFields'];
if($customFields && array_key_exists('netzp_powerpack6_footer_cms_show', $customFields)
&& array_key_exists('netzp_powerpack6_footer_cms_id', $customFields)
&& $customFields['netzp_powerpack6_footer_cms_id'] != null) {
$id = strtolower(trim($customFields['netzp_powerpack6_footer_cms_id']));
$position = $customFields['netzp_powerpack6_footer_cms_show'];
if ($id != '') {
$criteria = new Criteria([$id]);
$pages = $this->cmsPageLoader->load($request, $criteria, $context);
if (!$pages->has($id)) {
throw new PageNotFoundException($id);
}
}
$event->getPagelet()->assign([
'netzp_footer_cms_page' => $pages->get($id),
'netzp_footer_cms_position' => $position
]);
}
}
public function loadFinishPage(CheckoutFinishPageLoadedEvent $event): void
{
$request = $event->getRequest();
$context = $event->getSalesChannelContext();
$customFields = $context->getSalesChannel()->getTranslated()['customFields'];
if($customFields && array_key_exists('netzp_powerpack6_finish_cms_show', $customFields)
&& array_key_exists('netzp_powerpack6_finish_cms_id', $customFields)
&& $customFields['netzp_powerpack6_finish_cms_id'] != null) {
$id = strtolower(trim($customFields['netzp_powerpack6_finish_cms_id']));
$position = $customFields['netzp_powerpack6_finish_cms_show'];
if ($id != '') {
$criteria = new Criteria([$id]);
$pages = $this->cmsPageLoader->load($request, $criteria, $context);
if (!$pages->has($id)) {
throw new PageNotFoundException($id);
}
}
$event->getPage()->assign([
'netzp_finish_cms_page' => $pages->get($id),
'netzp_finish_cms_position' => $position
]);
}
}
}