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

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