![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Cnc/Catalog/Controller/ |
<?php /** * Copyright (c) 2020 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * Cnc * Radosław Stępień <[email protected]> <[email protected]> */ namespace Cnc\Catalog\Controller; use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory; use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory; use Magento\Framework\App\Action\Forward; use Magento\Framework\App\Action\Redirect; use Magento\Framework\App\ActionFactory; use Magento\Framework\App\ActionInterface; use Magento\Framework\App\RequestInterface; use Magento\Framework\App\ResponseInterface; use Magento\Framework\App\RouterInterface; use Magento\Framework\Exception\LocalizedException; class CncProductMatch implements RouterInterface { /** @var ActionFactory */ protected $actionFactory; /** @var ResponseInterface */ protected $_response; /** @var ProductCollectionFactory */ private $productCollectionFactory; /** @var CategoryCollectionFactory */ private $categoryCollectionFactory; /** * CncProductMatch constructor. * @param ActionFactory $actionFactory * @param ResponseInterface $response * @param ProductCollectionFactory $productCollectionFactory * @param CategoryCollectionFactory $categoryCollectionFactory */ public function __construct( ActionFactory $actionFactory, ResponseInterface $response, ProductCollectionFactory $productCollectionFactory, CategoryCollectionFactory $categoryCollectionFactory ) { $this->actionFactory = $actionFactory; $this->_response = $response; $this->productCollectionFactory = $productCollectionFactory; $this->categoryCollectionFactory = $categoryCollectionFactory; } /** * Custom router to fetch product or category by -(p/c)-ID part in url_key in case of 404 or mistakes in url typing * * @param RequestInterface $request * @return false|ActionInterface * @throws LocalizedException */ public function match(RequestInterface $request) { $productMagentoId = false; $categoryMagentoId = false; $pathInfo = $request->getPathInfo(); if (strrpos($pathInfo, '-p-')) { $productCncId = substr($pathInfo, strrpos($pathInfo, '-p-') + 3); if (strpos($productCncId, '.html')) { $productCncId = str_replace('.html', '', $productCncId); } $productCollection = $this->productCollectionFactory->create(); $product = $productCollection->addAttributeToFilter('cnc_oscom_id', ['eq' => $productCncId]) ->addAttributeToFilter('visibility', ['neq' => 1]) ->getFirstItem(); if ($product->getId()) { $productMagentoId = $product->getId(); } if ($productMagentoId) { $request->setModuleName('catalog') ->setControllerName('product') ->setActionName('view') ->setParam('id', $productMagentoId); $this->_response->setRedirect($product->getProductUrl()); $request->setDispatched(true); return $this->actionFactory->create( Redirect::class ); } } elseif (strrpos($pathInfo, '-c-')) { $categoryCncId = substr($pathInfo, strrpos($pathInfo, '-c-') + 3); if (strpos($categoryCncId, '.html')) { $categoryCncId = str_replace('.html', '', $categoryCncId); } $categoryCollection = $this->categoryCollectionFactory->create(); $category = $categoryCollection->addAttributeToFilter('cnc_oscommerce_cat_id', ['eq' => $categoryCncId]) ->getFirstItem(); if ($category->getId()) { $categoryMagentoId = $category->getId(); $url = $category->getUrl(); if ($url) { $this->_response->setRedirect($url); $request->setDispatched(true); return $this->actionFactory->create( Redirect::class ); } } if ($categoryMagentoId) { $request->setModuleName('catalog') ->setControllerName('category') ->setActionName('view') ->setParam('id', $categoryMagentoId); return $this->actionFactory->create( Forward::class, ['request' => $request] ); } } return false; } }