Skip to content

Commit

Permalink
Fix Smile-SA#1846 optimizer preview with search context
Browse files Browse the repository at this point in the history
  • Loading branch information
romainruaud committed Aug 3, 2020
1 parent 207c5d9 commit ce45618
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
* @copyright 2020 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteCatalogOptimizer\Controller\Adminhtml\Optimizer;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
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;

Expand Down Expand Up @@ -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.
*
Expand All @@ -67,14 +80,18 @@ 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,
PreviewFactory $previewModelFactory,
CategoryRepositoryInterface $categoryRepository,
OptimizerInterfaceFactory $optimizerFactory,
ContainerConfigurationFactory $containerConfigFactory,
JsonHelper $jsonHelper
JsonHelper $jsonHelper,
QueryFactory $queryFactory,
ContextInterface $searchContext
) {
parent::__construct($context);

Expand All @@ -83,6 +100,8 @@ public function __construct(
$this->previewModelFactory = $previewModelFactory;
$this->containerConfigFactory = $containerConfigFactory;
$this->jsonHelper = $jsonHelper;
$this->queryFactory = $queryFactory;
$this->searchContext = $searchContext;
}

/**
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* DISCLAIMER
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCatalogOptimizer
* @author Romain Ruaud <[email protected]>
* @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 <[email protected]>
*/
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') ?? '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@
<argument name="modifierPool" xsi:type="object">Smile\ElasticsuiteCatalogOptimizer\Ui\Component\Optimizer\Form\Modifier\Pool</argument>
</arguments>
</type>

<type name="Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\OptimizerFilterInterface">
<plugin name="optimizer_limitation_preview" type="Smile\ElasticsuiteCatalogOptimizer\Plugin\Adminhtml\OptimizerFilter"/>
</type>
</config>

0 comments on commit ce45618

Please sign in to comment.