custom/plugins/EmcgnStandardCmsPages/src/Core/Content/Category/EmcgnCategoryRoute.php line 214

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace EmcgnStandardCmsPages\Core\Content\Category;
  3. use EmcgnStandardCmsPages\Core\Exception\EmcgnConfigNotFound;
  4. use Shopware\Core\Content\Category\CategoryDefinition;
  5. use Shopware\Core\Content\Category\CategoryEntity;
  6. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  7. use Shopware\Core\Content\Category\SalesChannel\AbstractCategoryRoute;
  8. use Shopware\Core\Content\Category\SalesChannel\CategoryRouteResponse;
  9. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext;
  10. use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
  11. use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  15. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  16. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
  17. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  18. use Shopware\Core\System\SystemConfig\SystemConfigService;
  19. use Symfony\Component\HttpFoundation\Request;
  20. class EmcgnCategoryRoute extends AbstractCategoryRoute
  21. {
  22.     /**
  23.      * @var string
  24.      */
  25.     public const HOME 'home';
  26.     private SalesChannelRepository $categoryRepository;
  27.     private CategoryDefinition $categoryDefinition;
  28.     private SalesChannelCmsPageLoaderInterface $cmsPageLoader;
  29.     private SystemConfigService $config;
  30.     private EntityRepository $entityRepository;
  31.     /**
  32.      * @param SalesChannelRepository $categoryRepository
  33.      * @param CategoryDefinition $categoryDefinition
  34.      * @param SalesChannelCmsPageLoaderInterface $cmsPageLoader
  35.      * @param SystemConfigService $config
  36.      * @param EntityRepository $entityRepository
  37.      */
  38.     public function __construct(
  39.         SalesChannelRepository $categoryRepository,
  40.         CategoryDefinition $categoryDefinition,
  41.         SalesChannelCmsPageLoaderInterface $cmsPageLoader,
  42.         SystemConfigService $config,
  43.         EntityRepository $entityRepository
  44.     )
  45.     {
  46.         $this->categoryRepository $categoryRepository;
  47.         $this->categoryDefinition $categoryDefinition;
  48.         $this->cmsPageLoader $cmsPageLoader;
  49.         $this->config $config;
  50.         $this->entityRepository $entityRepository;
  51.     }
  52.     /**
  53.      * @return AbstractCategoryRoute
  54.      */
  55.     public function getDecorated(): AbstractCategoryRoute
  56.     {
  57.         throw new DecorationPatternException(self::class);
  58.     }
  59.     /**
  60.      * @param string $navigationId
  61.      * @param Request $request
  62.      * @param SalesChannelContext $context
  63.      * @return CategoryRouteResponse
  64.      */
  65.     public function load(string $navigationIdRequest $requestSalesChannelContext $context): CategoryRouteResponse
  66.     {
  67.         $salesChannel $context->getSalesChannel();
  68.         if ($navigationId === self::HOME) {
  69.             $navigationId $salesChannel->getNavigationCategoryId();
  70.             $request->attributes->set('navigationId'$navigationId);
  71.             $routeParams $request->attributes->get('_route_params', []);
  72.             $routeParams['navigationId'] = $navigationId;
  73.             $request->attributes->set('_route_params'$routeParams);
  74.         }
  75.         $category $this->loadCategory($navigationId$context);
  76.         if (($category->getType() === CategoryDefinition::TYPE_FOLDER
  77.                 || $category->getType() === CategoryDefinition::TYPE_LINK)
  78.             && $salesChannel->getNavigationCategoryId() !== $navigationId)
  79.         {
  80.             throw new CategoryNotFoundException($navigationId);
  81.         }
  82.         $pageId $category->getCmsPageId();
  83.         $slotConfig $category->getTranslation('slotConfig');
  84.         if ($category->getId() === $salesChannel->getNavigationCategoryId() && $salesChannel->getHomeCmsPageId()) {
  85.             $pageId $salesChannel->getHomeCmsPageId();
  86.             $slotConfig $salesChannel->getTranslation('homeSlotConfig');
  87.         }
  88.         if($pageId) {
  89.             $cmsPage $this->entityRepository->search(new Criteria([$pageId]), $context->getContext())->first();
  90.             $cmsLocked $cmsPage->getLocked();
  91.         }
  92.         if ($pageId && $cmsLocked == false) {
  93.             $this->setCmsPage($context$pageId$request$category$slotConfig);
  94.         } else {
  95.             $pageIdConfig $this->config->get('EmcgnStandardCmsPages.config.selectedCmsCategoryPage');
  96.             if($pageId && $cmsLocked == true) {
  97.                 if(!empty($pageIdConfig)) {
  98.                     $cmsPageConfig $this->entityRepository->search(new Criteria([$pageIdConfig]), $context->getContext())->first();
  99.                     if(!empty($cmsPageConfig)) {
  100.                         $this->setConfigCmsPage($context$pageIdConfig$request$category$slotConfig);
  101.                     } else {
  102.                         $this->setCmsPage($context$pageId$request$category$slotConfig);
  103.                     }
  104.                 } else {
  105.                     $this->setCmsPage($context$pageId$request$category$slotConfig);
  106.                 }
  107.             } else {
  108.                 if(!empty($pageIdConfig)) {
  109.                     $cmsPageConfig $this->entityRepository->search(new Criteria([$pageIdConfig]), $context->getContext())->first();
  110.                     if(!empty($cmsPageConfig)) {
  111.                         $this->setConfigCmsPage($context$pageIdConfig$request$category$slotConfig);
  112.                     } else {
  113.                         throw new EmcgnConfigNotFound('standard CMS category page');
  114.                     }
  115.                 } else {
  116.                     throw new EmcgnConfigNotFound('standard CMS category page');
  117.                 }
  118.             }
  119.         }
  120.         return new CategoryRouteResponse($category);
  121.     }
  122.     /**
  123.      * @param SalesChannelContext $context
  124.      * @param string $pageId
  125.      * @param Request $request
  126.      * @param CategoryEntity $category
  127.      * @param $slotConfig
  128.      * @return void
  129.      */
  130.     private function setConfigCmsPage(SalesChannelContext $contextstring $pageIdRequest $requestCategoryEntity $category$slotConfig): void
  131.     {
  132.         $cmsPage $this->entityRepository->search(new Criteria([$pageId]), $context->getContext())->first();
  133.         if (!empty($cmsPage)) {
  134.             $cmsType $cmsPage->getType();
  135.             if ($cmsType == 'product_list') {
  136.                 $this->setCmsPage($context$pageId$request$category$slotConfig);
  137.             }
  138.         }
  139.     }
  140.     /**
  141.      * @param string $categoryId
  142.      * @param SalesChannelContext $context
  143.      * @return CategoryEntity
  144.      */
  145.     private function loadCategory(string $categoryIdSalesChannelContext $context): CategoryEntity
  146.     {
  147.         $criteria = new Criteria([$categoryId]);
  148.         $criteria->setTitle('category::data');
  149.         $criteria->addAssociation('media');
  150.         $category $this->categoryRepository
  151.             ->search($criteria$context)
  152.             ->get($categoryId);
  153.         if (!$category) {
  154.             throw new CategoryNotFoundException($categoryId);
  155.         }
  156.         return $category;
  157.     }
  158.     /**
  159.      * @param string $pageId
  160.      * @param Request $request
  161.      * @return Criteria
  162.      */
  163.     private function createCriteria(string $pageIdRequest $request): Criteria
  164.     {
  165.         $criteria = new Criteria([$pageId]);
  166.         $criteria->setTitle('category::cms-page');
  167.         $slots $request->get('slots');
  168.         if (is_string($slots)) {
  169.             $slots explode('|'$slots);
  170.         }
  171.         if (!empty($slots) && is_array($slots)) {
  172.             $criteria
  173.                 ->getAssociation('sections.blocks')
  174.                 ->addFilter(new EqualsAnyFilter('slots.id'$slots));
  175.         }
  176.         return $criteria;
  177.     }
  178.     /**
  179.      * @param SalesChannelContext $context
  180.      * @param string $pageId
  181.      * @param Request $request
  182.      * @param CategoryEntity $category
  183.      * @param $slotConfig
  184.      * @return void
  185.      */
  186.     private function setCmsPage(SalesChannelContext $contextstring $pageIdRequest $requestCategoryEntity $category$slotConfig): void
  187.     {
  188.         $resolverContext = new EntityResolverContext($context$request$this->categoryDefinition$category);
  189.         $pages $this->cmsPageLoader->load(
  190.             $request,
  191.             $this->createCriteria($pageId$request),
  192.             $context,
  193.             $slotConfig,
  194.             $resolverContext
  195.         );
  196.         if (!$pages->has($pageId)) {
  197.             throw new PageNotFoundException($pageId);
  198.         }
  199.         $category->setCmsPage($pages->get($pageId));
  200.         $category->setCmsPageId($pageId);
  201.     }
  202. }