diff --git a/src/module-elasticsuite-catalog-optimizer/Controller/Adminhtml/Optimizer/Preview.php b/src/module-elasticsuite-catalog-optimizer/Controller/Adminhtml/Optimizer/Preview.php index c49ab4886..e3562c61e 100644 --- a/src/module-elasticsuite-catalog-optimizer/Controller/Adminhtml/Optimizer/Preview.php +++ b/src/module-elasticsuite-catalog-optimizer/Controller/Adminhtml/Optimizer/Preview.php @@ -11,6 +11,7 @@ * @copyright 2020 Smile * @license Open Software License ("OSL") v. 3.0 */ + namespace Smile\ElasticsuiteCatalogOptimizer\Controller\Adminhtml\Optimizer; use Magento\Backend\App\Action; @@ -18,9 +19,11 @@ use Magento\Catalog\Api\CategoryRepositoryInterface; use Magento\Catalog\Api\Data\CategoryInterface; use Magento\Framework\Json\Helper\Data as JsonHelper; +use Magento\Search\Model\QueryFactory; use Smile\ElasticsuiteCatalogOptimizer\Api\Data\OptimizerInterface; use Smile\ElasticsuiteCatalogOptimizer\Api\Data\OptimizerInterfaceFactory; use Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\PreviewFactory; +use Smile\ElasticsuiteCore\Api\Search\ContextInterface; use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface; use Smile\ElasticsuiteCore\Search\Request\ContainerConfigurationFactory; @@ -58,6 +61,16 @@ class Preview extends Action */ private $containerConfigFactory; + /** + * @var \Magento\Search\Model\QueryFactory + */ + private $queryFactory; + + /** + * @var \Smile\ElasticsuiteCore\Api\Search\ContextInterface + */ + private $searchContext; + /** * Constructor. * @@ -67,6 +80,8 @@ class Preview extends Action * @param OptimizerInterfaceFactory $optimizerFactory OptimzerFactory * @param ContainerConfigurationFactory $containerConfigFactory Container Configuration Factory * @param JsonHelper $jsonHelper JSON Helper. + * @param QueryFactory $queryFactory Query Factory. + * @param ContextInterface $searchContext Search context. */ public function __construct( Context $context, @@ -74,7 +89,9 @@ public function __construct( CategoryRepositoryInterface $categoryRepository, OptimizerInterfaceFactory $optimizerFactory, ContainerConfigurationFactory $containerConfigFactory, - JsonHelper $jsonHelper + JsonHelper $jsonHelper, + QueryFactory $queryFactory, + ContextInterface $searchContext ) { parent::__construct($context); @@ -83,6 +100,8 @@ public function __construct( $this->previewModelFactory = $previewModelFactory; $this->containerConfigFactory = $containerConfigFactory; $this->jsonHelper = $jsonHelper; + $this->queryFactory = $queryFactory; + $this->searchContext = $searchContext; } /** @@ -120,6 +139,8 @@ private function getPreviewObject() $category = $this->getCategory(); $containerConfig = $this->getContainerConfiguration(); + $this->updateSearchContext($this->getStoreId(), $category, $queryText); + return $this->previewModelFactory->create( [ 'optimizer' => $optimizer, @@ -225,4 +246,34 @@ private function getStoreId() { return $this->getRequest()->getParam('store_id'); } + + /** + * Update the search context using current store id, category or query text. + * + * @param integer $storeId Store id. + * @param CategoryInterface $category Category. + * @param string $queryText Fulltext query text. + * + * @return void + */ + private function updateSearchContext($storeId, $category, $queryText) + { + $this->searchContext->setStoreId($storeId); + + if ((string) $queryText !== '') { + try { + $query = $this->queryFactory->create(); + $query->setStoreId($storeId); + $query->loadByQueryText($queryText); + + if ($query->getId()) { + $this->searchContext->setCurrentSearchQuery($query); + } + } catch (\Magento\Framework\Exception\LocalizedException $exception) { + // Do not break if we fail to retrieve the query. + } + } elseif ($category && $category->getId()) { + $this->searchContext->setCurrentCategory($category); + } + } } diff --git a/src/module-elasticsuite-catalog-optimizer/Plugin/Adminhtml/OptimizerFilter.php b/src/module-elasticsuite-catalog-optimizer/Plugin/Adminhtml/OptimizerFilter.php new file mode 100644 index 000000000..0a7e9da30 --- /dev/null +++ b/src/module-elasticsuite-catalog-optimizer/Plugin/Adminhtml/OptimizerFilter.php @@ -0,0 +1,66 @@ + + * @copyright 2020 Smile + * @license Open Software License ("OSL") v. 3.0 + */ + +namespace Smile\ElasticsuiteCatalogOptimizer\Plugin\Adminhtml; + +use Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\OptimizerFilterInterface; + +/** + * Adminhtml only plugin, used to append the currently previewed optimizers (if any) to the list of allowed ones. + * + * @category Smile + * @package Smile\ElasticsuiteCatalogOptimizer + * @author Romain Ruaud + */ +class OptimizerFilter +{ + /** + * @var \Magento\Framework\App\RequestInterface + */ + private $request; + + /** + * OptimizerFilter constructor. + * + * @param \Magento\Framework\App\RequestInterface $request The request + */ + public function __construct(\Magento\Framework\App\RequestInterface $request) + { + $this->request = $request; + } + + /** + * Append currently edited optimizer into available list. + * + * @param \Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\OptimizerFilterInterface $subject Optimizer Filter + * @param $result Available Optimizers + * + * @return mixed + */ + public function afterGetOptimizerIds(OptimizerFilterInterface $subject, $result) + { + $result[] = $this->getCurrentOptimizerId(); + + return $result; + } + + /** + * Get current optimizer id + * + * @return string + */ + private function getCurrentOptimizerId() + { + return $this->request->getPostValue('optimizer_id') ?? ''; + } +} diff --git a/src/module-elasticsuite-catalog-optimizer/etc/adminhtml/di.xml b/src/module-elasticsuite-catalog-optimizer/etc/adminhtml/di.xml index 7ca84fca7..ba9b138d3 100644 --- a/src/module-elasticsuite-catalog-optimizer/etc/adminhtml/di.xml +++ b/src/module-elasticsuite-catalog-optimizer/etc/adminhtml/di.xml @@ -47,4 +47,8 @@ Smile\ElasticsuiteCatalogOptimizer\Ui\Component\Optimizer\Form\Modifier\Pool + + + +