custom/plugins/WynTheme/src/Subscriber/StorefrontSubscriber.php line 72

Open in your IDE?
  1. <?php
  2. namespace Wyn\Theme\Subscriber;
  3. // services
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\Struct\ArrayStruct;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  12. use Shopware\Storefront\Theme\ThemeService;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Wyn\Theme\Classes\StorefrontFooterMedia;
  15. use Shopware\Storefront\Theme\StorefrontPluginConfiguration\StorefrontPluginConfiguration;
  16. /**
  17.  *
  18.  */
  19. class StorefrontSubscriber implements EventSubscriberInterface
  20. {
  21.     public const pluginName "WynTheme";
  22.     /**
  23.      * @var Context
  24.      */
  25.     private $context;
  26.     /**
  27.      * @var SystemConfigService
  28.      */
  29.     private $systemConfigService;
  30.     /**
  31.      * @var EntityRepositoryInterface
  32.      */
  33.     private $themeRepository;
  34.     /**
  35.      * @var EntityRepositoryInterface
  36.      */
  37.     private $mediaRepository;
  38.     /**
  39.      *
  40.      * /**
  41.      * OrderSubscriber constructor.
  42.      * @param SystemConfigService $systemConfigService
  43.      */
  44.     public function __construct(
  45.         SystemConfigService       $systemConfigService,
  46.         EntityRepositoryInterface $themeRepository,
  47.         EntityRepositoryInterface $mediaRepository)
  48.     {
  49.         $this->systemConfigService $systemConfigService;
  50.         $this->themeRepository $themeRepository;
  51.         $this->mediaRepository $mediaRepository;
  52.     }
  53.     public static function getSubscribedEvents()
  54.     {
  55.         return [
  56.             FooterPageletLoadedEvent::class => 'onFooterPageletLoadedEvent',
  57.         ];
  58.     }
  59.     /**
  60.      * @param FooterPageletLoadedEvent $event
  61.      */
  62.     public function onFooterPageletLoadedEvent(FooterPageletLoadedEvent $event)
  63.     {
  64.         $this->context $event->getContext();
  65.         $criteria = new Criteria();
  66.         $criteria->addFilter(new EqualsFilter("technicalName""WynTheme"));
  67.         $themeDatabaseEntry $this->themeRepository->search($criteria$this->context)->first();
  68.         $folderIdSocialMedia $this->_getThemeConfigValue("wyn-footer-logo-socialmedia"$themeDatabaseEntry);
  69.         $folderIdPayment $this->_getThemeConfigValue("wyn-footer-logo-payment"$themeDatabaseEntry);
  70.         $folderIdShipping $this->_getThemeConfigValue("wyn-footer-logo-shipping"$themeDatabaseEntry);
  71.         $folderIds = [
  72.             "socialMedia" => $folderIdSocialMedia,
  73.             "shipping" => $folderIdShipping,
  74.             "payment" => $folderIdPayment
  75.         ];
  76.         $footerMedia = [
  77.             "socialMedia" => [],
  78.             "shipping" => [],
  79.             "payment" => []
  80.         ];
  81.         $criteria = new Criteria();
  82.         $criteria->addFilter(new EqualsAnyFilter("mediaFolderId", [
  83.             $folderIds["socialMedia"],
  84.             $folderIds["shipping"],
  85.             $folderIds["payment"],
  86.         ]));
  87.         $collectedMediaEntities $this->mediaRepository->search($criteria$this->context)->getEntities();
  88.         foreach ($collectedMediaEntities as $item) {
  89.             if ($item->hasFile()) {
  90.                 preg_match('/media(.*)/'$item->getUrl(), $output_array);
  91.                 $relativePath "/media{$output_array[1]}";
  92.                 $itemType "";
  93.                 switch ($item->getMediaFolderId()) {
  94.                     case $folderIds["socialMedia"]:
  95.                         $itemType "socialMedia";
  96.                         break;
  97.                     case $folderIds["shipping"]:
  98.                         $itemType "shipping";
  99.                         break;
  100.                     case $folderIds["payment"]:
  101.                         $itemType "payment";
  102.                         break;
  103.                     default:
  104.                 }
  105.                 $object = new StorefrontFooterMedia();
  106.                 $object->setMediaId($item->getId());
  107.                 $object->setName($item->getFileName());
  108.                 $object->setType($itemType);
  109.                 $object->setPath($relativePath);
  110.                 $object->setOriginalEntity($item);
  111.                 $footerMedia[$itemType][] = $object;
  112.             }
  113.         }
  114.         $extensionStruct = new ArrayStruct($footerMedia);
  115.         $event->getPagelet()->addExtension("footerMedia"$extensionStruct);
  116.     }
  117.     /**
  118.      * @param $technicalName
  119.      * @param $themeDatabaseEntry
  120.      * @return string
  121.      */
  122.     private function _getThemeConfigValue($technicalName$themeDatabaseEntry): string
  123.     {
  124.         $themeConfigValues $themeDatabaseEntry->getConfigValues();
  125.         $themeBaseConfigs $themeDatabaseEntry->getBaseConfig()["fields"];
  126.         if (isset($themeConfigValues[$technicalName])) {
  127.             return $themeConfigValues[$technicalName]["value"];
  128.         } else {
  129.             if (isset($themeBaseConfigs[$technicalName]["value"])) {
  130.                 return $themeBaseConfigs[$technicalName]["value"];
  131.             } else {
  132.                 return "";
  133.             }
  134.         }
  135.     }
  136. }