custom/plugins/HuebertAccountAttributes/src/Subscriber/AccountSubscriber.php line 33

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace HuebertAccountAttributes\Subscriber;
  3. use HuebertAccountAttributes\Core\Content\Download\DownloadEntity;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class AccountSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var EntityRepositoryInterface
  12.      */
  13.     private $downloadRepository;
  14.     public function __construct(EntityRepositoryInterface $downloadRepository)
  15.     {
  16.         $this->downloadRepository $downloadRepository;
  17.     }
  18.     /**
  19.      * @inheritDoc
  20.      */
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             GenericPageLoadedEvent::class => 'onGenericPageLoaded'
  25.         ];
  26.     }
  27.     public function onGenericPageLoaded(GenericPageLoadedEvent $event) {
  28.         $requestUri $event->getRequest()->getRequestUri();
  29.         if(strpos($requestUri'/account') === false && strpos($requestUri'/download/') === false) {
  30.             return true;
  31.         }
  32.         $context $event->getContext();
  33.         $salesChannelContext $event->getSalesChannelContext();
  34.         $customerGroup $salesChannelContext->getCurrentCustomerGroup()->getName();
  35.         $page $event->getPage();
  36.         $downloads $this->downloadRepository->search(new Criteria(), $context)->getElements();
  37.         $customerDownloads = [];
  38.         /**
  39.          * @var DownloadEntity $download
  40.          */
  41.         foreach($downloads as $download) {
  42.             $customerGroups $this->getCustomerGroupsArray($download);
  43.             if(empty($download->getCustomerGroups())) {
  44.                 $customerDownloads[] = $download->getName();
  45.             } else if (in_array($customerGroup$customerGroups)) {
  46.                 $customerDownloads[] = $download->getName();
  47.             }
  48.         }
  49.         if(!empty($customerDownloads)) {
  50.             $page->assign([
  51.                 'huebert_downloads' => $customerDownloads
  52.             ]);
  53.         }
  54.         return true;
  55.     }
  56.     /**
  57.      * @param DownloadEntity $download
  58.      * @return array
  59.      */
  60.     private function getCustomerGroupsArray($download): ?array {
  61.         $customerGroups $download->getCustomerGroups();
  62.         if(empty($customerGroups)) {
  63.             return null;
  64.         }
  65.         $customerGroupsArray = [];
  66.         if(strpos($customerGroups",") !== false) {
  67.             $customerGroupsExplode explode(","$customerGroups);
  68.             foreach($customerGroupsExplode as $customerGroup) {
  69.                 $customerGroupsArray[] = ltrim(trim($customerGroup));
  70.             }
  71.             return $customerGroupsArray;
  72.         } else {
  73.             $customerGroupsArray[] = ltrim(trim($customerGroups));
  74.             return $customerGroupsArray;
  75.         }
  76.     }
  77.     private function replaceSonderzeichen($str): string {
  78.         $search = array("Ä""Ö""Ü""ä""ö""ü""ß""´");
  79.         $replace = array("Ae""Oe""Ue""ae""oe""ue""ss""");
  80.         return str_replace($search$replace$str);
  81.     }
  82. }