<?php declare(strict_types=1);
namespace WvPopups\Subscriber;
use DateTime;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\Framework\Struct\ArrayEntity;
class WvPopupsSubscriber implements EventSubscriberInterface
{
private SystemConfigService $systemConfigService;
public function __construct(SystemConfigService $systemConfigService)
{
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array
{
return [
GenericPageLoadedEvent::class => 'onPageLoaded',
];
}
public function onPageLoaded(GenericPageLoadedEvent $event): void
{
$plugin_config = array();
$voucherActive = $this->systemConfigService->get('WvPopups.config.voucherActive');
if($this->systemConfigService->get('WvPopups.config.voucherActiveUntil') != ''){
$voucherActiveUntil = new DateTime($this->systemConfigService->get('WvPopups.config.voucherActiveUntil'));
$now = new DateTime();
if($now > $voucherActiveUntil){
$voucherActive = false;
}
}
$plugin_config['voucherMedia'] = $this->systemConfigService->get('WvPopups.config.voucherMedia');
$plugin_config['voucherTitle'] = $this->systemConfigService->get('WvPopups.config.voucherTitle');
$plugin_config['voucherContent'] = $this->systemConfigService->get('WvPopups.config.voucherContent');
$plugin_config['voucherActive'] = $voucherActive;
$plugin_config['voucherActiveUntil'] = $this->systemConfigService->get('WvPopups.config.voucherActiveUntil');
$plugin_config['voucherCode'] = $this->systemConfigService->get('WvPopups.config.voucherCode');
$plugin_config['voucherLink'] = $this->systemConfigService->get('WvPopups.config.voucherLink');
$plugin_config['voucherLinkTitle'] = $this->systemConfigService->get('WvPopups.config.voucherLinkTitle');
$plugin_config['voucherButtonFirst'] = $this->systemConfigService->get('WvPopups.config.voucherButtonFirst');
$plugin_config['voucherBannerTitle'] = $this->systemConfigService->get('WvPopups.config.voucherBannerTitle');
$exitActive = $this->systemConfigService->get('WvPopups.config.exitActive');
if($this->systemConfigService->get('WvPopups.config.exitActiveUntil') != ''){
$exitActiveUntil = new DateTime($this->systemConfigService->get('WvPopups.config.exitActiveUntil'));
$now = new DateTime();
if($now > $exitActiveUntil){
$exitActive = false;
}
}
$plugin_config['exitMedia'] = $this->systemConfigService->get('WvPopups.config.exitMedia');
$plugin_config['exitTitle'] = $this->systemConfigService->get('WvPopups.config.exitTitle');
$plugin_config['exitContent'] = $this->systemConfigService->get('WvPopups.config.exitContent');
$plugin_config['exitActive'] = $exitActive;
$plugin_config['exitActiveUntil'] = $this->systemConfigService->get('WvPopups.config.exitActiveUntil');
$plugin_config['exitCode'] = $this->systemConfigService->get('WvPopups.config.exitCode');
$plugin_config['exitLink'] = $this->systemConfigService->get('WvPopups.config.exitLink');
$plugin_config['exitLinkTitle'] = $this->systemConfigService->get('WvPopups.config.exitLinkTitle');
$plugin_config['exitButtonFirst'] = $this->systemConfigService->get('WvPopups.config.exitButtonFirst');
//add the array to the page as an extension
$event->getPage()->addExtension('wvPopupsExtension', new ArrayEntity($plugin_config));
}
}