From 5e1ef72300ddfb33b5a93691343af0fecbfc9682 Mon Sep 17 00:00:00 2001 From: Pierre Gauthier Date: Thu, 28 Nov 2024 16:59:43 +0100 Subject: [PATCH] Fix SourceField sync with Gally enabled by Website --- src/Decorator/ProductIndexFieldsProvider.php | 12 +++-- src/Decorator/SavePriceFilterUnit.php | 2 +- src/Indexer/IndexDataProvider.php | 8 +++- .../Normalizer/BrandDataNormalizer.php | 2 +- .../Normalizer/SelectDataNormalizer.php | 5 +- src/Provider/SearchMappingProvider.php | 48 +++++++++++++++++++ src/Resources/config/services.yml | 18 +++++++ src/Resources/config/services/indexer.yml | 9 ++-- src/Resources/config/services/search.yml | 22 ++++----- src/Search/Autocomplete/Category.php | 9 ++++ src/Search/Autocomplete/Product.php | 2 +- src/Search/EngineParameters.php | 1 + .../Extension/GallyDataGridExtension.php | 2 +- src/Search/GallyRequestBuilder.php | 1 + src/Search/SearchEngine.php | 9 +++- src/{Search => Service}/ContextProvider.php | 13 ++++- 16 files changed, 131 insertions(+), 32 deletions(-) create mode 100644 src/Provider/SearchMappingProvider.php rename src/{Search => Service}/ContextProvider.php (91%) diff --git a/src/Decorator/ProductIndexFieldsProvider.php b/src/Decorator/ProductIndexFieldsProvider.php index 41ce8ee..418329d 100644 --- a/src/Decorator/ProductIndexFieldsProvider.php +++ b/src/Decorator/ProductIndexFieldsProvider.php @@ -14,15 +14,17 @@ namespace Gally\OroPlugin\Decorator; -use Gally\OroPlugin\Search\SearchEngine; +use Gally\OroPlugin\Service\ContextProvider; use Oro\Bundle\ProductBundle\Search\ProductIndexAttributeProviderInterface; -use Oro\Bundle\SearchBundle\Engine\EngineParameters; +/** + * In gally context, send all attributes to the search engine and let gally decide which ones are searchable or filterable. + */ class ProductIndexFieldsProvider implements ProductIndexAttributeProviderInterface { public function __construct( private ProductIndexAttributeProviderInterface $productIndexAttributeProvider, - private EngineParameters $engineParameters, + private ContextProvider $contextProvider, ) { } @@ -33,7 +35,7 @@ public function addForceIndexed(string $field): void public function isForceIndexed(string $field): bool { - return SearchEngine::ENGINE_NAME === $this->engineParameters->getEngineName() - || $this->productIndexAttributeProvider->isForceIndexed($field); + // return true; + return $this->contextProvider->isGallyContext() || $this->productIndexAttributeProvider->isForceIndexed($field); } } diff --git a/src/Decorator/SavePriceFilterUnit.php b/src/Decorator/SavePriceFilterUnit.php index 7034e26..6b2ae90 100644 --- a/src/Decorator/SavePriceFilterUnit.php +++ b/src/Decorator/SavePriceFilterUnit.php @@ -14,7 +14,7 @@ namespace Gally\OroPlugin\Decorator; -use Gally\OroPlugin\Search\ContextProvider; +use Gally\OroPlugin\Service\ContextProvider; use Oro\Bundle\FilterBundle\Datasource\FilterDatasourceAdapterInterface; use Oro\Bundle\FilterBundle\Filter\FilterInterface; use Oro\Bundle\SearchBundle\Datagrid\Filter\SearchNumberFilter; diff --git a/src/Indexer/IndexDataProvider.php b/src/Indexer/IndexDataProvider.php index 0cc5696..cb6f390 100644 --- a/src/Indexer/IndexDataProvider.php +++ b/src/Indexer/IndexDataProvider.php @@ -15,6 +15,7 @@ namespace Gally\OroPlugin\Indexer; use Gally\OroPlugin\Indexer\Normalizer\AbstractNormalizer; +use Gally\OroPlugin\Service\ContextProvider; use Oro\Bundle\EntityBundle\ORM\EntityAliasResolver; use Oro\Bundle\LocaleBundle\Entity\Localization; use Oro\Bundle\UIBundle\Tools\HtmlTagHelper; @@ -46,6 +47,7 @@ public function __construct( HtmlTagHelper $htmlTagHelper, PlaceholderHelper $placeholderHelper, private WebsiteContextManager $websiteContextManager, + private ContextProvider $contextProvider, private array $attributeMapping, private iterable $normalizers, ) { @@ -62,6 +64,7 @@ public function getEntitiesData( array $entityConfig ): array { $entityAlias = $this->entityAliasResolver->getAlias($entityClass); + $this->contextProvider->setIsGallyContext(true); $indexEntityEvent = new Event\IndexEntityEvent($entityClass, $restrictedEntities, $context); $this->eventDispatcher->dispatch($indexEntityEvent, Event\IndexEntityEvent::NAME); @@ -70,7 +73,10 @@ public function getEntitiesData( sprintf('%s.%s', Event\IndexEntityEvent::NAME, $entityAlias) ); - return $this->prepareIndexData($entityClass, $indexEntityEvent->getEntitiesData(), $entityConfig, $context); + $data = $this->prepareIndexData($entityClass, $indexEntityEvent->getEntitiesData(), $entityConfig, $context); + $this->contextProvider->setIsGallyContext(false); + + return $data; } /** diff --git a/src/Indexer/Normalizer/BrandDataNormalizer.php b/src/Indexer/Normalizer/BrandDataNormalizer.php index a35452b..71eeb75 100644 --- a/src/Indexer/Normalizer/BrandDataNormalizer.php +++ b/src/Indexer/Normalizer/BrandDataNormalizer.php @@ -47,7 +47,7 @@ public function postProcess( if (isset($data['brand'])) { $preparedIndexData[$entityId]['brand'] = [[ 'value' => $data['brand'], - 'label' => $data['brand_name'] ?? '', // Todo fix + 'label' => $data['brand_name'] ?? '', ]]; } unset($preparedIndexData[$entityId]['brand_name']); diff --git a/src/Indexer/Normalizer/SelectDataNormalizer.php b/src/Indexer/Normalizer/SelectDataNormalizer.php index d01cf1e..1b15fba 100644 --- a/src/Indexer/Normalizer/SelectDataNormalizer.php +++ b/src/Indexer/Normalizer/SelectDataNormalizer.php @@ -90,14 +90,15 @@ public function normalize( ): void { foreach ($this->toArray($fieldsValues) as $fieldName => $values) { if (preg_match('/^(\w+)_enum\.(.+)$/', $fieldName, $matches)) { - foreach ($this->toArray($values) as $value) { - [$_, $cleanFieldName, $value] = $matches; + [$_, $cleanFieldName, $value] = $matches; + foreach ($this->toArray($values) as $_) { $preparedEntityData[$cleanFieldName][] = [ 'label' => $this->translatedOptionsByField[$cleanFieldName][$value] ?? $value, 'value' => $value, ]; } unset($fieldsValues[$fieldName]); + unset($fieldsValues[$cleanFieldName]); } } } diff --git a/src/Provider/SearchMappingProvider.php b/src/Provider/SearchMappingProvider.php new file mode 100644 index 0000000..2bc24c7 --- /dev/null +++ b/src/Provider/SearchMappingProvider.php @@ -0,0 +1,48 @@ + + * @copyright 2024-present Smile + * @license Open Software License v. 3.0 (OSL-3.0) + */ + +declare(strict_types=1); + +namespace Gally\OroPlugin\Provider; + +use Gally\OroPlugin\Service\ContextProvider; +use Oro\Bundle\SearchBundle\Configuration\MappingConfigurationProviderAbstract; +use Oro\Bundle\SearchBundle\Provider\SearchMappingProvider as BaseSearchMappingProvider; +use Psr\Cache\CacheItemPoolInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; + +/** + * The search mapping provider. + */ +class SearchMappingProvider extends BaseSearchMappingProvider +{ + public function __construct( + EventDispatcherInterface $dispatcher, + MappingConfigurationProviderAbstract $mappingConfigProvider, + CacheItemPoolInterface $cache, + private ContextProvider $contextProvider, + string $cacheKeyPrefix, + string $searchEngineName, + string $eventName + ) { + parent::__construct($dispatcher, $mappingConfigProvider, $cache, $cacheKeyPrefix, $searchEngineName, $eventName); + } + + public function getMappingConfig(): array + { + $this->contextProvider->setIsGallyContext(true); + $config = parent::getMappingConfig(); + $this->contextProvider->setIsGallyContext(false); + + return $config; + } +} diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 37bf8fa..c4a69bf 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -41,6 +41,24 @@ services: - '@oro_config.manager' - '@oro_security.encoder.default' + Gally\OroPlugin\Service\ContextProvider: + arguments: + - '@oro_config.manager' + - '@oro_website.manager' + - '@oro_locale.helper.localization' + - '@Gally\OroPlugin\Indexer\Provider\CatalogProvider' + - '@oro_web_catalog.request_web_content_variant_provider' + + Gally\OroPlugin\Provider\SearchMappingProvider: + arguments: + - '@event_dispatcher' + - '@oro_website_search.mapping_configuration.provider' + - '@oro_website_search.cache.mapping_configuration' + - '@Gally\OroPlugin\Service\ContextProvider' + - 'oro_search.mapping_config.gally:' + - "@=service('oro_website_search.engine.parameters').getEngineName()" + - 'oro_website_search.event.website_search_mapping.configuration' + Gally\OroPlugin\Placeholder\PlaceholderDecorator: arguments: - '@oro_website_search.placeholder.registry' diff --git a/src/Resources/config/services/indexer.yml b/src/Resources/config/services/indexer.yml index c938bc6..685dce0 100644 --- a/src/Resources/config/services/indexer.yml +++ b/src/Resources/config/services/indexer.yml @@ -10,7 +10,7 @@ services: decorates: oro_product.provider.index_fields arguments: - '@.inner' - - '@oro_website_search.engine.parameters' + - '@Gally\OroPlugin\Service\ContextProvider' Gally\OroPlugin\Indexer\Provider\CatalogProvider: arguments: @@ -23,7 +23,7 @@ services: Gally\OroPlugin\Indexer\Provider\SourceFieldProvider: arguments: - - '@oro_website_search.provider.search_mapping' + - '@Gally\OroPlugin\Provider\SearchMappingProvider' - '@oro_entity.entity_alias_resolver' - '@oro_entity_config.provider.entity' - '@Gally\OroPlugin\Indexer\Provider\CatalogProvider' @@ -40,7 +40,7 @@ services: Gally\OroPlugin\Indexer\Provider\SourceFieldOptionProvider: arguments: - - '@oro_website_search.provider.search_mapping' + - '@Gally\OroPlugin\Provider\SearchMappingProvider' - '@Gally\OroPlugin\Indexer\Provider\CatalogProvider' - '@oro_locale.settings' - '@doctrine.orm.entity_manager' @@ -130,13 +130,14 @@ services: - '@oro_ui.html_tag_helper' - '@oro_website_search.helper.placeholder_helper' - '@oro_website_search.manager.website_context_manager' + - '@Gally\OroPlugin\Service\ContextProvider' - '%gally_config.attribute_mapping%' - !tagged_iterator { tag: 'gally.indexer_normalizer' } Gally\OroPlugin\Indexer\Indexer: arguments: - '@oro_entity.doctrine_helper' - - '@oro_website_search.provider.search_mapping' + - '@Gally\OroPlugin\Provider\SearchMappingProvider' - '@oro_website_search.engine.entity_dependencies_resolver' - '@Gally\OroPlugin\Indexer\IndexDataProvider' - '@oro_website_search.placeholder_decorator' diff --git a/src/Resources/config/services/search.yml b/src/Resources/config/services/search.yml index 15ccfed..510dbe1 100644 --- a/src/Resources/config/services/search.yml +++ b/src/Resources/config/services/search.yml @@ -4,22 +4,14 @@ services: public: true arguments: - '%oro_website_search.engine_dsn%' - - '@Gally\OroPlugin\Search\ContextProvider' + - '@Gally\OroPlugin\Service\ContextProvider' - '@Gally\OroPlugin\Config\ConfigManager' - Gally\OroPlugin\Search\ContextProvider: - arguments: - - '@oro_config.manager' - - '@oro_website.manager' - - '@oro_locale.helper.localization' - - '@Gally\OroPlugin\Indexer\Provider\CatalogProvider' - - '@oro_web_catalog.request_web_content_variant_provider' - Gally\OroPlugin\Decorator\SavePriceFilterUnit: decorates: oro_pricing.filter.frontend_product_price arguments: - '@.inner' - - '@Gally\OroPlugin\Search\ContextProvider' + - '@Gally\OroPlugin\Service\ContextProvider' Gally\OroPlugin\Search\ExpressionVisitor: arguments: @@ -27,7 +19,7 @@ services: Gally\OroPlugin\Search\GallyRequestBuilder: arguments: - - '@Gally\OroPlugin\Search\ContextProvider' + - '@Gally\OroPlugin\Service\ContextProvider' - '@Gally\OroPlugin\Search\ExpressionVisitor' - '@Gally\OroPlugin\Resolver\PriceGroupResolver' - '@oro_website_search.placeholder.registry' @@ -55,7 +47,7 @@ services: # - '@oro_website_elastic_search.request_builder.registry' - '@Gally\Sdk\Service\SearchManager' - '@Gally\OroPlugin\Search\GallyRequestBuilder' - - '@Gally\OroPlugin\Search\ContextProvider' + - '@Gally\OroPlugin\Service\ContextProvider' - '%gally_config.attribute_mapping%' calls: - ['setMapper', ['@oro_website_search.engine.mapper']] @@ -74,7 +66,7 @@ services: arguments: - '@oro_website_search.engine.parameters' - '@Gally\Sdk\Service\SearchManager' - - '@Gally\OroPlugin\Search\ContextProvider' + - '@Gally\OroPlugin\Service\ContextProvider' tags: - { name: oro_datagrid.extension } @@ -89,7 +81,7 @@ services: Gally\OroPlugin\Search\Autocomplete\Product: arguments: - - '@Gally\OroPlugin\Search\ContextProvider' + - '@Gally\OroPlugin\Service\ContextProvider' tags: - { name: kernel.event_listener, event: Oro\Bundle\ProductBundle\Event\ProcessAutocompleteQueryEvent, method: onProcessAutocompleteQuery } @@ -98,5 +90,7 @@ services: - '@oro_website_search.query_factory' - '@oro_ui.twig.html_tag' - '@oro_config.manager' + - '@Gally\OroPlugin\Service\ContextProvider' + - '@Gally\OroPlugin\Config\ConfigManager' tags: - { name: kernel.event_listener, event: Oro\Bundle\ProductBundle\Event\ProcessAutocompleteDataEvent, method: onProcessAutocompleteData } diff --git a/src/Search/Autocomplete/Category.php b/src/Search/Autocomplete/Category.php index 469767f..b683ca4 100644 --- a/src/Search/Autocomplete/Category.php +++ b/src/Search/Autocomplete/Category.php @@ -14,6 +14,8 @@ namespace Gally\OroPlugin\Search\Autocomplete; +use Gally\OroPlugin\Config\ConfigManager as GallyConfigManager; +use Gally\OroPlugin\Service\ContextProvider; use Oro\Bundle\CatalogBundle\DependencyInjection\Configuration as CatalogConfiguration; use Oro\Bundle\ConfigBundle\Config\ConfigManager; use Oro\Bundle\ProductBundle\Event\ProcessAutocompleteDataEvent; @@ -30,11 +32,18 @@ public function __construct( private QueryFactoryInterface $queryFactory, private HtmlTagExtension $htmlTagExtension, private ConfigManager $configManager, + private ContextProvider $contextProvider, + private GallyConfigManager $gallyConfigManager, ) { } public function onProcessAutocompleteData(ProcessAutocompleteDataEvent $event): void { + $websiteId = $this->contextProvider->getCurrentWebsite()->getId(); + if (!$this->gallyConfigManager->isGallyEnabled($websiteId)) { + return; + } + $numberOfCategories = $this->configManager ->get(CatalogConfiguration::getConfigKeyByName(CatalogConfiguration::SEARCH_AUTOCOMPLETE_MAX_CATEGORIES)); diff --git a/src/Search/Autocomplete/Product.php b/src/Search/Autocomplete/Product.php index 9a1ade4..d6bc8c6 100644 --- a/src/Search/Autocomplete/Product.php +++ b/src/Search/Autocomplete/Product.php @@ -14,7 +14,7 @@ namespace Gally\OroPlugin\Search\Autocomplete; -use Gally\OroPlugin\Search\ContextProvider; +use Gally\OroPlugin\Service\ContextProvider; use Oro\Bundle\ProductBundle\Event\ProcessAutocompleteQueryEvent; /** diff --git a/src/Search/EngineParameters.php b/src/Search/EngineParameters.php index 6cb2747..b6efcfb 100644 --- a/src/Search/EngineParameters.php +++ b/src/Search/EngineParameters.php @@ -15,6 +15,7 @@ namespace Gally\OroPlugin\Search; use Gally\OroPlugin\Config\ConfigManager; +use Gally\OroPlugin\Service\ContextProvider; use Oro\Bundle\SearchBundle\Engine\EngineParameters as BaseEngineParameters; /** diff --git a/src/Search/Extension/GallyDataGridExtension.php b/src/Search/Extension/GallyDataGridExtension.php index 3d65907..3d124f1 100644 --- a/src/Search/Extension/GallyDataGridExtension.php +++ b/src/Search/Extension/GallyDataGridExtension.php @@ -14,8 +14,8 @@ namespace Gally\OroPlugin\Search\Extension; -use Gally\OroPlugin\Search\ContextProvider; use Gally\OroPlugin\Search\SearchEngine; +use Gally\OroPlugin\Service\ContextProvider; use Gally\Sdk\Entity\Metadata; use Gally\Sdk\Entity\SourceField; use Gally\Sdk\GraphQl\Request; diff --git a/src/Search/GallyRequestBuilder.php b/src/Search/GallyRequestBuilder.php index 0fa9da6..524590f 100644 --- a/src/Search/GallyRequestBuilder.php +++ b/src/Search/GallyRequestBuilder.php @@ -15,6 +15,7 @@ namespace Gally\OroPlugin\Search; use Gally\OroPlugin\Resolver\PriceGroupResolver; +use Gally\OroPlugin\Service\ContextProvider; use Gally\Sdk\Entity\Metadata; use Gally\Sdk\GraphQl\Request; use Oro\Bundle\PricingBundle\Placeholder\CPLIdPlaceholder; diff --git a/src/Search/SearchEngine.php b/src/Search/SearchEngine.php index 665ee16..fd1dfcc 100644 --- a/src/Search/SearchEngine.php +++ b/src/Search/SearchEngine.php @@ -14,6 +14,7 @@ namespace Gally\OroPlugin\Search; +use Gally\OroPlugin\Service\ContextProvider; use Gally\Sdk\Service\SearchManager; use Oro\Bundle\ProductBundle\Entity\Product; use Oro\Bundle\SearchBundle\Provider\AbstractSearchMappingProvider; @@ -77,13 +78,19 @@ protected function doSearch(Query $query, array $context = []) $item[$name] = $value; } - $results[] = new Item( + $itemObject = new Item( $request->getMetadata()->getEntity(), $item['id'], $item['url'] ?? null, $this->mapper->mapSelectedData($query, $item), $this->mappingProvider->getEntityConfig($request->getMetadata()->getEntity()) ); + if (\array_key_exists('tree', $item)) { + $selectedData = $itemObject->getSelectedData(); + $selectedData['tree'] = $item['tree']; + $itemObject->setSelectedData($selectedData); + } + $results[] = $itemObject; } $aggregations = []; diff --git a/src/Search/ContextProvider.php b/src/Service/ContextProvider.php similarity index 91% rename from src/Search/ContextProvider.php rename to src/Service/ContextProvider.php index 2474a3c..645a569 100644 --- a/src/Search/ContextProvider.php +++ b/src/Service/ContextProvider.php @@ -12,7 +12,7 @@ declare(strict_types=1); -namespace Gally\OroPlugin\Search; +namespace Gally\OroPlugin\Service; use Gally\OroPlugin\Indexer\Provider\CatalogProvider; use Gally\Sdk\Entity\LocalizedCatalog; @@ -27,6 +27,7 @@ class ContextProvider { + private bool $isGallyContext = false; private Response $response; private ?string $priceFilterUnit = null; private bool $isAutocompleteContext = false; @@ -40,6 +41,16 @@ public function __construct( ) { } + public function isGallyContext(): bool + { + return $this->isGallyContext; + } + + public function setIsGallyContext(bool $isGallyContext): void + { + $this->isGallyContext = $isGallyContext; + } + public function getCurrentWebsite(): ?Website { return $this->websiteManager->getCurrentWebsite();