<?php declare(strict_types=1);
namespace Wabs\BannerNotifications\Service;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
use Shopware\Storefront\Pagelet\PageletLoadedEvent;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AddBannerNotificationsToPage implements EventSubscriberInterface
{
private SystemConfigService $systemConfigService;
/**
* @param SystemConfigService $systemConfigService
*/
public function __construct(SystemConfigService $systemConfigService)
{
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array
{
return [
HeaderPageletLoadedEvent::class => 'addBannerNotificationToPagelet',
FooterPageletLoadedEvent::class => 'addBannerNotificationToPagelet'
];
}
/**
* @throws \Exception
*/
public function addBannerNotificationToPagelet(PageletLoadedEvent $event): void {
$activeConfig = $this->systemConfigService->get('WabsBannerNotifications.config.active');
$positionConfig = $this->systemConfigService->get('WabsBannerNotifications.config.position');
$activeFromConfig = $this->systemConfigService->get('WabsBannerNotifications.config.activeFrom');
$activeUntilConfig = $this->systemConfigService->get('WabsBannerNotifications.config.activeUntil');
$backgroundColorConfig = $this->systemConfigService->get('WabsBannerNotifications.config.backgroundColor');
$textColorConfig = $this->systemConfigService->get('WabsBannerNotifications.config.textColor');
$paddingLeftConfig = $this->systemConfigService->get('WabsBannerNotifications.config.paddingLeft');
$paddingRightConfig = $this->systemConfigService->get('WabsBannerNotifications.config.paddingRight');
$paddingTopConfig = $this->systemConfigService->get('WabsBannerNotifications.config.paddingTop');
$paddingBottomConfig = $this->systemConfigService->get('WabsBannerNotifications.config.paddingBottom');
$contentConfig = $this->systemConfigService->get('WabsBannerNotifications.config.content');
if( $activeConfig ) {
$currentDate = new \DateTimeImmutable();
$formattedDate = $currentDate->setTimezone(new \DateTimeZone('Europe/Berlin'));
if ($activeFromConfig) {
$fromDateTime = new \DateTimeImmutable($activeFromConfig, new \DateTimeZone('Europe/Berlin'));
if( $fromDateTime > $formattedDate ) {
$activeConfig = false;
}
}
if ($activeUntilConfig) {
$untilDateTime = new \DateTimeImmutable($activeUntilConfig, new \DateTimeZone('Europe/Berlin'));
if( $untilDateTime < $formattedDate ) {
$activeConfig = false;
}
}
}
if ($activeConfig) {
$event->getPagelet()->addExtension('bannerNotification',
new ArrayStruct([
'active' => $activeConfig,
'position' => $positionConfig,
'activeFrom' => $activeFromConfig,
'activeUntil' => $activeUntilConfig,
'backgroundColor' => $backgroundColorConfig,
'textColor' => $textColorConfig,
'paddingLeft' => $paddingLeftConfig,
'paddingRight' => $paddingRightConfig,
'paddingTop' => $paddingTopConfig,
'paddingBottom' => $paddingBottomConfig,
'content' => $contentConfig
]),
);
} else {
$event->getPagelet()->addExtension('bannerNotification',
new ArrayStruct([
'active' => false,
]),
);
}
}
}