<?php
namespace Wyn\Theme\Subscriber;
// services
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
use Shopware\Storefront\Theme\ThemeService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Wyn\Theme\Classes\StorefrontFooterMedia;
use Shopware\Storefront\Theme\StorefrontPluginConfiguration\StorefrontPluginConfiguration;
/**
*
*/
class StorefrontSubscriber implements EventSubscriberInterface
{
public const pluginName = "WynTheme";
/**
* @var Context
*/
private $context;
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var EntityRepositoryInterface
*/
private $themeRepository;
/**
* @var EntityRepositoryInterface
*/
private $mediaRepository;
/**
*
* /**
* OrderSubscriber constructor.
* @param SystemConfigService $systemConfigService
*/
public function __construct(
SystemConfigService $systemConfigService,
EntityRepositoryInterface $themeRepository,
EntityRepositoryInterface $mediaRepository)
{
$this->systemConfigService = $systemConfigService;
$this->themeRepository = $themeRepository;
$this->mediaRepository = $mediaRepository;
}
public static function getSubscribedEvents()
{
return [
FooterPageletLoadedEvent::class => 'onFooterPageletLoadedEvent',
];
}
/**
* @param FooterPageletLoadedEvent $event
*/
public function onFooterPageletLoadedEvent(FooterPageletLoadedEvent $event)
{
$this->context = $event->getContext();
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter("technicalName", "WynTheme"));
$themeDatabaseEntry = $this->themeRepository->search($criteria, $this->context)->first();
$folderIdSocialMedia = $this->_getThemeConfigValue("wyn-footer-logo-socialmedia", $themeDatabaseEntry);
$folderIdPayment = $this->_getThemeConfigValue("wyn-footer-logo-payment", $themeDatabaseEntry);
$folderIdShipping = $this->_getThemeConfigValue("wyn-footer-logo-shipping", $themeDatabaseEntry);
$folderIds = [
"socialMedia" => $folderIdSocialMedia,
"shipping" => $folderIdShipping,
"payment" => $folderIdPayment
];
$footerMedia = [
"socialMedia" => [],
"shipping" => [],
"payment" => []
];
$criteria = new Criteria();
$criteria->addFilter(new EqualsAnyFilter("mediaFolderId", [
$folderIds["socialMedia"],
$folderIds["shipping"],
$folderIds["payment"],
]));
$collectedMediaEntities = $this->mediaRepository->search($criteria, $this->context)->getEntities();
foreach ($collectedMediaEntities as $item) {
if ($item->hasFile()) {
preg_match('/media(.*)/', $item->getUrl(), $output_array);
$relativePath = "/media{$output_array[1]}";
$itemType = "";
switch ($item->getMediaFolderId()) {
case $folderIds["socialMedia"]:
$itemType = "socialMedia";
break;
case $folderIds["shipping"]:
$itemType = "shipping";
break;
case $folderIds["payment"]:
$itemType = "payment";
break;
default:
}
$object = new StorefrontFooterMedia();
$object->setMediaId($item->getId());
$object->setName($item->getFileName());
$object->setType($itemType);
$object->setPath($relativePath);
$object->setOriginalEntity($item);
$footerMedia[$itemType][] = $object;
}
}
$extensionStruct = new ArrayStruct($footerMedia);
$event->getPagelet()->addExtension("footerMedia", $extensionStruct);
}
/**
* @param $technicalName
* @param $themeDatabaseEntry
* @return string
*/
private function _getThemeConfigValue($technicalName, $themeDatabaseEntry): string
{
$themeConfigValues = $themeDatabaseEntry->getConfigValues();
$themeBaseConfigs = $themeDatabaseEntry->getBaseConfig()["fields"];
if (isset($themeConfigValues[$technicalName])) {
return $themeConfigValues[$technicalName]["value"];
} else {
if (isset($themeBaseConfigs[$technicalName]["value"])) {
return $themeBaseConfigs[$technicalName]["value"];
} else {
return "";
}
}
}
}