custom/plugins/NetzpPowerPack6/src/Core/Cms/SalesChannelCmsPageLoaderDecorator.php line 28

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace NetzpPowerPack6\Core\Cms;
  3. use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  6. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  9. use Shopware\Core\Content\Cms\Aggregate\CmsBlock\CmsBlockEntity;
  10. use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionEntity;
  11. class SalesChannelCmsPageLoaderDecorator implements SalesChannelCmsPageLoaderInterface
  12. {
  13.     private $inner;
  14.     public function __construct(SalesChannelCmsPageLoaderInterface $inner)
  15.     {
  16.         $this->inner $inner;
  17.     }
  18.     public function load(Request $requestCriteria $criteriaSalesChannelContext $context,
  19.                          ?array  $config null, ?ResolverContext $resolverContext null): EntitySearchResult
  20.     {
  21.         $timeZone $request->cookies->get('timezone''Europe/Berlin');
  22.         $pages $this->inner->load($request$criteria$context$config$resolverContext);
  23.         foreach ($pages as $page) {
  24.             $sections $page->getSections();
  25.             if ($sections === null || count($sections) === 0) {
  26.                 continue;
  27.             }
  28.             foreach ($sections as $section) {
  29.                 $blocks $section->getBlocks();
  30.                 if ($blocks === null || count($blocks) === 0) {
  31.                     continue;
  32.                 }
  33.                 $filteredBlocks $blocks->filter(function (CmsBlockEntity $thisBlock) use ($context$timeZone) {
  34.                     $showThisBlock true;
  35.                     if (!$this->checkVisibilityDates($thisBlock$timeZone)) {
  36.                         $showThisBlock false;
  37.                     }
  38.                     if (!$this->checkVisibilityRule($thisBlock$context)) {
  39.                         $showThisBlock false;
  40.                     }
  41.                     return $showThisBlock;
  42.                 });
  43.                 $section->setBlocks($filteredBlocks);
  44.             }
  45.             $filteredSections $sections->filter(function (CmsSectionEntity $thisSection) use ($context$timeZone) {
  46.                 $blocks $thisSection->getBlocks();
  47.                 $showThisSection true;
  48.                 if (!$this->checkVisibilityDates($thisSection$timeZone)) {
  49.                     $showThisSection false;
  50.                 }
  51.                 if (!$this->checkVisibilityRule($thisSection$context)) {
  52.                     $showThisSection false;
  53.                 }
  54.                 return $blocks !== null && count($blocks) > && $showThisSection;
  55.             });
  56.             $page->setSections($filteredSections);
  57.         }
  58.         return $pages;
  59.     }
  60.     private function checkVisibilityDates($blockOrSectionstring $timeZone)
  61.     {
  62.         if ($blockOrSection->getCustomFields() && array_key_exists('netzp_pp'$blockOrSection->getCustomFields())) {
  63.             $customFields $blockOrSection->getCustomFields()['netzp_pp'];
  64.             $showFrom null;
  65.             $showUntil null;
  66.             $now = new \DateTime();
  67.             // ************************************
  68.             // TODO ACHTUNG: Wenn im UserProfil eine andere Zeitzone als UTC eingetragen ist, klappen die Zeiten hier mal wieder nicht
  69.             // ************************************
  70.             if (array_key_exists('showFrom'$customFields) && $customFields['showFrom'] !== null) {
  71.                 $showFrom = new \DateTime($customFields['showFrom']);
  72.                 $showFrom->setTimezone(new \DateTimeZone($timeZone));
  73.                 $showFrom->setTime(000);
  74.             }
  75.             if (array_key_exists('showUntil'$customFields) && $customFields['showUntil'] !== null) {
  76.                 $showUntil = new \DateTime($customFields['showUntil']);
  77.                 $showUntil->setTimezone(new \DateTimeZone($timeZone));
  78.                 $showUntil->setTime(235959);
  79.             }
  80.             if ($showFrom !== null && $showUntil !== null) {
  81.                 return $showFrom <= $now && $now <= $showUntil;
  82.             } elseif ($showFrom !== null) {
  83.                 return $showFrom <= $now;
  84.             } elseif ($showUntil !== null) {
  85.                 return $now <= $showUntil;
  86.             }
  87.         }
  88.         return true;
  89.     }
  90.     private function checkVisibilityRule($blockOrSectionSalesChannelContext $salesChannelContext)
  91.     {
  92.         if ($blockOrSection->getCustomFields() && array_key_exists('netzp_pp'$blockOrSection->getCustomFields())) {
  93.             $customFields $blockOrSection->getCustomFields()['netzp_pp'];
  94.             $ruleId null;
  95.             if (array_key_exists('ruleId'$customFields)) {
  96.                 $ruleId $customFields['ruleId'];
  97.                 if($ruleId == null) {
  98.                     return true;
  99.                 }
  100.                 $activeRuleIds $salesChannelContext->getRuleIds();
  101.                 return in_array($ruleId$activeRuleIdstrue);
  102.             }
  103.         }
  104.         return true;
  105.     }
  106. }