<?php declare(strict_types=1);
namespace fourtwosix\PopupsGenerator\Subscriber;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
use Shopware\Core\Framework\Struct\ArrayEntity;
class PageIndex implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
private $systemConfigService;
private $container;
public function __construct(SystemConfigService $systemConfigService, $container)
{
$this->systemConfigService = $systemConfigService;
$this->container = $container;
}
public static function getSubscribedEvents(): array
{
return[
FooterPageletLoadedEvent::class => 'onPageIndex'
];
}
public function onPageIndex(FooterPageletLoadedEvent $event)
{
$settings = $this->systemConfigService->getDomain('fourtwosixPopupsGenerator.settings');
$mediaRepository = $this->container->get('media.repository');
$currentMedia = "";
if(isset($settings['fourtwosixPopupsGenerator.settings.popupImage'])){
$mediaId = $settings['fourtwosixPopupsGenerator.settings.popupImage'];
$criteria = new Criteria([$mediaId]);
$criteria->addAssociation('mediaFolder');
$currentMedia = $mediaRepository
->search($criteria, $event->getContext())
->get($mediaId);
if($currentMedia == null){
$currentMedia = "";
}else{
$currentMedia = $currentMedia->getUrl();
}
}
$config = array(
"id" => '0p1o2p3u4p5',
"title" => isset($settings['fourtwosixPopupsGenerator.settings.popupTitle']) ? $settings['fourtwosixPopupsGenerator.settings.popupTitle'] : "",
"description" => isset($settings['fourtwosixPopupsGenerator.settings.popupDescription']) ? $settings['fourtwosixPopupsGenerator.settings.popupDescription'] : "",
"link" => isset($settings['fourtwosixPopupsGenerator.settings.popupLink']) ? $settings['fourtwosixPopupsGenerator.settings.popupLink'] : "",
"button_text" => isset($settings['fourtwosixPopupsGenerator.settings.popupButton']) ? $settings['fourtwosixPopupsGenerator.settings.popupButton'] : "",
"display" => isset($settings['fourtwosixPopupsGenerator.settings.popupActive']) ? $settings['fourtwosixPopupsGenerator.settings.popupActive'] : false,
"position" => isset($settings['fourtwosixPopupsGenerator.settings.popupPosition']) ? $settings['fourtwosixPopupsGenerator.settings.popupPosition'] : "center",
"background" => $currentMedia
);
$extensions = new ArrayEntity;
$extensions['popupConfig'] = $config;
$event->getPagelet()->addExtension("popupExtension",$extensions);
}
}