Skip to content

Commit

Permalink
Fix SourceField sync with Gally enabled by Website
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreGauthier committed Nov 28, 2024
1 parent 961ec3b commit 5e1ef72
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 32 deletions.
12 changes: 7 additions & 5 deletions src/Decorator/ProductIndexFieldsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}

Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Decorator/SavePriceFilterUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion src/Indexer/IndexDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,6 +47,7 @@ public function __construct(
HtmlTagHelper $htmlTagHelper,
PlaceholderHelper $placeholderHelper,
private WebsiteContextManager $websiteContextManager,
private ContextProvider $contextProvider,
private array $attributeMapping,
private iterable $normalizers,
) {
Expand All @@ -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);
Expand All @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Indexer/Normalizer/BrandDataNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
5 changes: 3 additions & 2 deletions src/Indexer/Normalizer/SelectDataNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions src/Provider/SearchMappingProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Gally to newer versions in the future.
*
* @package Gally
* @author Gally Team <[email protected]>
* @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;
}
}
18 changes: 18 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
9 changes: 5 additions & 4 deletions src/Resources/config/services/indexer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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'
Expand Down
22 changes: 8 additions & 14 deletions src/Resources/config/services/search.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,22 @@ 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:
- '%gally_config.attribute_mapping%'

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'
Expand Down Expand Up @@ -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']]
Expand All @@ -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 }

Expand All @@ -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 }

Expand All @@ -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 }
9 changes: 9 additions & 0 deletions src/Search/Autocomplete/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));

Expand Down
2 changes: 1 addition & 1 deletion src/Search/Autocomplete/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
1 change: 1 addition & 0 deletions src/Search/EngineParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Search/Extension/GallyDataGridExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Search/GallyRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion src/Search/SearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];
Expand Down
Loading

0 comments on commit 5e1ef72

Please sign in to comment.