custom/plugins/WabsBannerNotifications/src/Service/AddBannerNotificationsToPage.php line 36

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Wabs\BannerNotifications\Service;
  3. use Shopware\Core\Framework\Struct\ArrayStruct;
  4. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  5. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  6. use Shopware\Storefront\Pagelet\PageletLoadedEvent;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class AddBannerNotificationsToPage implements EventSubscriberInterface
  10. {
  11.     private SystemConfigService $systemConfigService;
  12.     /**
  13.      * @param SystemConfigService $systemConfigService
  14.      */
  15.     public function __construct(SystemConfigService $systemConfigService)
  16.     {
  17.         $this->systemConfigService $systemConfigService;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             HeaderPageletLoadedEvent::class => 'addBannerNotificationToPagelet',
  23.             FooterPageletLoadedEvent::class => 'addBannerNotificationToPagelet'
  24.         ];
  25.     }
  26.     /**
  27.      * @throws \Exception
  28.      */
  29.     public function addBannerNotificationToPagelet(PageletLoadedEvent $event): void {
  30.         $activeConfig $this->systemConfigService->get('WabsBannerNotifications.config.active');
  31.         $positionConfig $this->systemConfigService->get('WabsBannerNotifications.config.position');
  32.         $activeFromConfig $this->systemConfigService->get('WabsBannerNotifications.config.activeFrom');
  33.         $activeUntilConfig $this->systemConfigService->get('WabsBannerNotifications.config.activeUntil');
  34.         $backgroundColorConfig $this->systemConfigService->get('WabsBannerNotifications.config.backgroundColor');
  35.         $textColorConfig $this->systemConfigService->get('WabsBannerNotifications.config.textColor');
  36.         $paddingLeftConfig $this->systemConfigService->get('WabsBannerNotifications.config.paddingLeft');
  37.         $paddingRightConfig $this->systemConfigService->get('WabsBannerNotifications.config.paddingRight');
  38.         $paddingTopConfig $this->systemConfigService->get('WabsBannerNotifications.config.paddingTop');
  39.         $paddingBottomConfig $this->systemConfigService->get('WabsBannerNotifications.config.paddingBottom');
  40.         $contentConfig $this->systemConfigService->get('WabsBannerNotifications.config.content');
  41.         if( $activeConfig ) {
  42.             $currentDate = new \DateTimeImmutable();
  43.             $formattedDate $currentDate->setTimezone(new \DateTimeZone('Europe/Berlin'));
  44.             if ($activeFromConfig) {
  45.                 $fromDateTime = new \DateTimeImmutable($activeFromConfig, new \DateTimeZone('Europe/Berlin'));
  46.                 if( $fromDateTime $formattedDate ) {
  47.                     $activeConfig false;
  48.                 }
  49.             }
  50.             if ($activeUntilConfig) {
  51.                 $untilDateTime = new \DateTimeImmutable($activeUntilConfig, new \DateTimeZone('Europe/Berlin'));
  52.                 if( $untilDateTime $formattedDate ) {
  53.                     $activeConfig false;
  54.                 }
  55.             }
  56.         }
  57.         if ($activeConfig) {
  58.             $event->getPagelet()->addExtension('bannerNotification',
  59.                 new ArrayStruct([
  60.                     'active' => $activeConfig,
  61.                     'position' => $positionConfig,
  62.                     'activeFrom' => $activeFromConfig,
  63.                     'activeUntil' => $activeUntilConfig,
  64.                     'backgroundColor' => $backgroundColorConfig,
  65.                     'textColor' => $textColorConfig,
  66.                     'paddingLeft' => $paddingLeftConfig,
  67.                     'paddingRight' => $paddingRightConfig,
  68.                     'paddingTop' => $paddingTopConfig,
  69.                     'paddingBottom' => $paddingBottomConfig,
  70.                     'content' => $contentConfig
  71.                 ]),
  72.             );
  73.         } else {
  74.             $event->getPagelet()->addExtension('bannerNotification',
  75.                 new ArrayStruct([
  76.                     'active' => false,
  77.                 ]),
  78.             );
  79.         }
  80.     }
  81. }