From 34da6d4ba3c44b0e8c26ea131cfe2716dc580be4 Mon Sep 17 00:00:00 2001 From: Dmytro Androshchuk Date: Fri, 19 Jun 2020 17:38:24 +0300 Subject: [PATCH] Fixes #1858 Optimizer preview for inactive category --- .../Product/Form/Categories/Options.php | 125 ++++++++++++++++++ ...le_elasticsuite_catalog_optimizer_form.xml | 2 +- 2 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 src/module-elasticsuite-catalog-optimizer/Ui/Component/Product/Form/Categories/Options.php diff --git a/src/module-elasticsuite-catalog-optimizer/Ui/Component/Product/Form/Categories/Options.php b/src/module-elasticsuite-catalog-optimizer/Ui/Component/Product/Form/Categories/Options.php new file mode 100644 index 000000000..092b23949 --- /dev/null +++ b/src/module-elasticsuite-catalog-optimizer/Ui/Component/Product/Form/Categories/Options.php @@ -0,0 +1,125 @@ + + * @copyright 2020 Smile + * @license Open Software License ("OSL") v. 3.0 + */ +namespace Smile\ElasticsuiteCatalogOptimizer\Ui\Component\Product\Form\Categories; + +use Magento\Framework\Data\OptionSourceInterface; +use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory; +use Magento\Catalog\Model\ResourceModel\Category\Collection; +use Magento\Framework\App\RequestInterface; +use Magento\Catalog\Model\Category as CategoryModel; + +/** + * Options tree for "Categories" field + * + * @category Smile + * @package Smile\ElasticsuiteCatalogOptimizer + * @author Dmytro ANDROSHCHUK + */ +class Options implements OptionSourceInterface +{ + /** + * @var CategoryCollectionFactory + */ + protected $categoryCollectionFactory; + + /** + * @var RequestInterface + */ + protected $request; + + /** + * @var array + */ + protected $categoriesTree; + + /** + * @param CategoryCollectionFactory $categoryCollectionFactory Category Collection Factory. + * @param RequestInterface $request Request. + */ + public function __construct( + CategoryCollectionFactory $categoryCollectionFactory, + RequestInterface $request + ) { + $this->categoryCollectionFactory = $categoryCollectionFactory; + $this->request = $request; + } + + /** + * {@inheritdoc} + */ + public function toOptionArray() + { + return $this->getCategoriesTree(); + } + + /** + * Retrieve categories tree + * + * @return array + */ + protected function getCategoriesTree() + { + if ($this->categoriesTree === null) { + $storeId = $this->request->getParam('store'); + /* @var $matchingNamesCollection Collection */ + $matchingNamesCollection = $this->categoryCollectionFactory->create(); + + $matchingNamesCollection->addAttributeToSelect('path') + ->addAttributeToFilter('entity_id', ['neq' => CategoryModel::TREE_ROOT_ID]) + ->setStoreId($storeId); + + $shownCategoriesIds = []; + + /** @var \Magento\Catalog\Model\Category $category */ + foreach ($matchingNamesCollection as $category) { + foreach (explode('/', $category->getPath()) as $parentId) { + $shownCategoriesIds[$parentId] = 1; + } + } + + $collection = $this->categoryCollectionFactory->create(); + + $collection->addAttributeToFilter('entity_id', ['in' => array_keys($shownCategoriesIds)]) + ->addAttributeToSelect(['name', 'is_active', 'parent_id']) + ->setStoreId($storeId); + + $categoryById = [ + CategoryModel::TREE_ROOT_ID => [ + 'value' => CategoryModel::TREE_ROOT_ID, + ], + ]; + + foreach ($collection as $category) { + // Don't display inactive categories. + if (!$category->getIsActive()) { + continue; + } + + foreach ([$category->getId(), $category->getParentId()] as $categoryId) { + if (!isset($categoryById[$categoryId])) { + $categoryById[$categoryId] = ['value' => $categoryId]; + } + } + + $categoryById[$category->getId()]['is_active'] = $category->getIsActive(); + $categoryById[$category->getId()]['label'] = $category->getName(); + $categoryById[$category->getParentId()]['optgroup'][] = &$categoryById[$category->getId()]; + } + + $this->categoriesTree = $categoryById[CategoryModel::TREE_ROOT_ID]['optgroup']; + } + + return $this->categoriesTree; + } +} diff --git a/src/module-elasticsuite-catalog-optimizer/view/adminhtml/ui_component/smile_elasticsuite_catalog_optimizer_form.xml b/src/module-elasticsuite-catalog-optimizer/view/adminhtml/ui_component/smile_elasticsuite_catalog_optimizer_form.xml index 6235af17b..396246ff1 100644 --- a/src/module-elasticsuite-catalog-optimizer/view/adminhtml/ui_component/smile_elasticsuite_catalog_optimizer_form.xml +++ b/src/module-elasticsuite-catalog-optimizer/view/adminhtml/ui_component/smile_elasticsuite_catalog_optimizer_form.xml @@ -740,7 +740,7 @@ - Magento\Catalog\Ui\Component\Product\Form\Categories\Options + Smile\ElasticsuiteCatalogOptimizer\Ui\Component\Product\Form\Categories\Options Category Preview field