diff --git a/src/Controller/Pwa.php b/src/Controller/Pwa.php index c4984aa..ced18ed 100644 --- a/src/Controller/Pwa.php +++ b/src/Controller/Pwa.php @@ -161,6 +161,12 @@ public function setCmsPage($cmsPage): self return $this; } + public function setSlider($slider): self + { + $this->slider = $slider; + return $this; + } + /** * @param string $description * @return Pwa @@ -208,6 +214,7 @@ public function __construct( $this->name = ''; $this->display_mode = ''; $this->cmsPage = null; + $this->slider = null; $this->description = ''; $this->catalogDefaultSortBy = ''; $this->storeConfig = null; @@ -229,6 +236,7 @@ public function execute() $resultLayout->setSku($this->sku); $resultLayout->setName($this->name); $resultLayout->setCmsPage($this->cmsPage); + $resultLayout->setSlider($this->slider); $resultLayout->setDisplayMode($this->display_mode); $resultLayout->setDescription($this->description); $resultLayout->setCatalogDefaultSortBy($this->catalogDefaultSortBy); diff --git a/src/Controller/Router.php b/src/Controller/Router.php index 626fc84..2dc1a41 100644 --- a/src/Controller/Router.php +++ b/src/Controller/Router.php @@ -34,7 +34,12 @@ use Magento\Catalog\Model\ProductRepository; use Magento\Catalog\Api\CategoryRepositoryInterface; use Magento\Catalog\Model\Product\Attribute\Source\Status; +use Magento\Cms\Api\PageRepositoryInterface; use ScandiPWA\CmsGraphQl\Model\Resolver\DataProvider\Page as PageProvider; +use Magento\Cms\Api\GetPageByIdentifierInterface; +use Magento\Framework\Filter\Template as WidgetTemplate; +use Magento\Framework\Filter\Template\Tokenizer\Parameter as TokenizerParameter; +use ScandiPWA\SliderGraphQl\Model\Resolver\Slider as SliderResolver; class Router extends BaseRouter { @@ -111,46 +116,76 @@ class Router extends BaseRouter */ protected PageProvider $pageProvider; + /** + * @var PageRepositoryInterface + */ + protected $pageRepository; + + /** + * @var GetPageByIdentifierInterface + */ + protected $pageByIdentifier; + + /** + * @var TokenizerParameter + */ + protected $tokenizerParameter; + + /** + * @var SliderResolver + */ + protected $sliderResolver; + + /** + * @var WidgetTemplate + */ + protected $widgetTemplate; + /** * Router constructor. - * @param ActionList $actionList - * @param ActionFactory $actionFactory - * @param DefaultPathInterface $defaultPath - * @param ResponseFactory $responseFactory - * @param ConfigInterface $routeConfig - * @param UrlInterface $url - * @param NameBuilder $nameBuilder - * @param PathConfigInterface $pathConfig + * @param ActionList $actionList + * @param ActionFactory $actionFactory + * @param DefaultPathInterface $defaultPath + * @param ResponseFactory $responseFactory + * @param ConfigInterface $routeConfig + * @param UrlInterface $url + * @param NameBuilder $nameBuilder + * @param PathConfigInterface $pathConfig * @param ValidationManagerInterface $validationManager - * @param UrlFinderInterface $urlFinder - * @param StoreManagerInterface $storeManager - * @param ScopeConfigInterface $scopeConfig - * @param ThemeProviderInterface $themeProvider + * @param UrlFinderInterface $urlFinder + * @param StoreManagerInterface $storeManager + * @param ScopeConfigInterface $scopeConfig + * @param ThemeProviderInterface $themeProvider * @param PageFactory $pageFactory * @param ProductRepository $productRepository * @param CategoryRepositoryInterface $categoryRepository * @param PageProvider $pageProvider */ public function __construct( - ActionList $actionList, - ActionFactory $actionFactory, - DefaultPathInterface $defaultPath, - ResponseFactory $responseFactory, - ConfigInterface $routeConfig, - UrlInterface $url, - NameBuilder $nameBuilder, - PathConfigInterface $pathConfig, - ValidationManagerInterface $validationManager, - UrlFinderInterface $urlFinder, - StoreManagerInterface $storeManager, - ScopeConfigInterface $scopeConfig, - ThemeProviderInterface $themeProvider, - PageFactory $pageFactory, - ProductRepository $productRepository, - CategoryRepositoryInterface $categoryRepository, - PageProvider $pageProvider, - array $ignoredURLs = [] - ) { + ActionList $actionList, + ActionFactory $actionFactory, + DefaultPathInterface $defaultPath, + ResponseFactory $responseFactory, + ConfigInterface $routeConfig, + UrlInterface $url, + NameBuilder $nameBuilder, + PathConfigInterface $pathConfig, + ValidationManagerInterface $validationManager, + UrlFinderInterface $urlFinder, + StoreManagerInterface $storeManager, + ScopeConfigInterface $scopeConfig, + ThemeProviderInterface $themeProvider, + PageFactory $pageFactory, + ProductRepository $productRepository, + CategoryRepositoryInterface $categoryRepository, + PageProvider $pageProvider, + PageRepositoryInterface $pageRepository, + GetPageByIdentifierInterface $pageByIdentifier, + TokenizerParameter $tokenizerParameter, + SliderResolver $sliderResolver, + array $ignoredURLs = [], + ) + { $this->scopeConfig = $scopeConfig; $this->themeProvider = $themeProvider; $this->validationManager = $validationManager; @@ -159,9 +194,13 @@ public function __construct( $this->pageFactory = $pageFactory; $this->productRepository = $productRepository; $this->categoryRepository = $categoryRepository; + $this->pageRepository = $pageRepository; + $this->pageByIdentifier = $pageByIdentifier; $this->pageProvider = $pageProvider; $this->ignoredURLs = $ignoredURLs; $this->storeId = $this->storeManager->getStore()->getId(); + $this->tokenizerParameter = $tokenizerParameter; + $this->sliderResolver = $sliderResolver; parent::__construct( $actionList, @@ -243,7 +282,7 @@ public function match(RequestInterface $request) $action->setCode(404)->setPhrase('Not Found'); } - if ($this->isHomePage($request, $action)) { + if ($this->isHomePage($request)) { $this->setResponseHomePage($action); } @@ -287,16 +326,43 @@ protected function setResponseHomePage(ActionInterface $action) ); try { - $page = $this->pageProvider->getDataByPageIdentifier($homePageIdentifier, (int) $this->storeId); + $page = $this->pageByIdentifier->execute($homePageIdentifier, (int)$this->storeId); + + $action->setId($page['page_id'] ?? ''); $action->setType(self::PAGE_TYPE_CMS_PAGE); $action->setId($page['page_id'] ?? ''); - $action->setCmsPage($page); + $action->setCmsPage($this->pageProvider->convertPageData($page)); + $action->setSlider($this->getSliderInformation($page['content'])); } catch (\Throwable $th) { $action->setType('PWA_ROUTER'); } } + /** + * @param string $content + * + * This function gets first widget from page content + * In case if it is slider, then it gets slider it. + */ + protected function getSliderInformation($content) + { + preg_match('/{{(.*?)}}/m', $content, $match); + + $this->tokenizerParameter->setString($match[0]); + $params = $this->tokenizerParameter->tokenize(); + + foreach ($params as $key => $value) { + if (substr($value, 0, 1) === '$') { + $params[$key] = $this->widgetTemplate->getVariable(substr($value, 1), null); + } + } + + if (isset($params['slider_id'])) { + return $this->sliderResolver->getSlider($params['slider_id']); + } + } + /** * Validate that CMS page is assigned to current store and is enabled and return 404 if not * @@ -308,10 +374,10 @@ protected function setResponseHomePage(ActionInterface $action) protected function setResponseCmsPage($id, ActionInterface $action) { try { - $page = $this->pageProvider->getDataByPageId((int) $id, (int) $this->storeId); - + $page = $this->pageRepository->getById($id); $action->setId($page['page_id'] ?? ''); - $action->setCmsPage($page); + $action->setCmsPage($this->pageProvider->convertPageData($page)); + $action->setSlider($this->getSliderInformation($page['content'])); } catch (\Throwable $th) { $this->setNotFound($action); } diff --git a/src/View/Result/Page.php b/src/View/Result/Page.php index a1e4fd0..b073116 100644 --- a/src/View/Result/Page.php +++ b/src/View/Result/Page.php @@ -186,6 +186,7 @@ public function __construct( $this->name = ''; $this->display_mode = ''; $this->cmsPage = null; + $this->slider = null; $this->description = ''; $this->catalogDefaultSortBy = ''; $this->storeConfig = null; @@ -324,7 +325,7 @@ public function getCatalogDefaultSortBy() public function setStoreConfig($storeConfig) { - if($this->storeConfig === null) { + if ($this->storeConfig === null) { $this->storeConfig = $storeConfig; return $this; } @@ -332,6 +333,26 @@ public function setStoreConfig($storeConfig) return null; } + public function getStoreConfig() + { + return $this->storeConfig; + } + + public function setSlider($slider) + { + if ($this->slider === null) { + $this->slider = $slider; + return $this; + } + + return ''; + } + + public function getSlider() + { + return $this->slider; + } + public function getCatalogDefaultSortByConfig() { return $this->storeConfig;