diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index f69f7eac..83406e82 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -62,6 +62,7 @@ public function getConfigTreeBuilder() ->arrayNode('excluded_category_codes') ->scalarPrototype()->defaultValue([])->end() ->end() + ->booleanNode('use_akeneo_positions')->defaultFalse()->end() ->end() ->end() diff --git a/src/DependencyInjection/SynoliaSyliusAkeneoExtension.php b/src/DependencyInjection/SynoliaSyliusAkeneoExtension.php index 6457d4e6..6e97e757 100644 --- a/src/DependencyInjection/SynoliaSyliusAkeneoExtension.php +++ b/src/DependencyInjection/SynoliaSyliusAkeneoExtension.php @@ -126,6 +126,7 @@ private function processCategoryConfiguration(ContainerBuilder $container, array $categoryConfigurationProviderDefinition ->setArgument('$categoryCodesToImport', $config['category_configuration']['root_category_codes']) ->setArgument('$categoryCodesToExclude', $config['category_configuration']['excluded_category_codes']) + ->setArgument('$categoryCodesToExclude', $config['category_configuration']['use_akeneo_positions']) ; $container->setAlias(CategoryConfigurationProviderInterface::class, CategoryConfigurationProvider::class); diff --git a/src/Model/Configuration/CategoryConfiguration.php b/src/Model/Configuration/CategoryConfiguration.php index 4e84c426..c54298e3 100644 --- a/src/Model/Configuration/CategoryConfiguration.php +++ b/src/Model/Configuration/CategoryConfiguration.php @@ -6,8 +6,11 @@ class CategoryConfiguration implements CategoryConfigurationInterface { - public function __construct(private array $categoryCodesToImport, private array $categoryCodesToExclude) - { + public function __construct( + private array $categoryCodesToImport, + private array $categoryCodesToExclude, + private bool $useAkeneoPositions, + ) { } public function getCategoryCodesToImport(): array @@ -33,4 +36,16 @@ public function setCategoryCodesToExclude(array $categoryCodesToExclude): self return $this; } + + public function isUseAkeneoPositions(): bool + { + return $this->useAkeneoPositions; + } + + public function setUseAkeneoPositions(bool $useAkeneoPositions): self + { + $this->useAkeneoPositions = $useAkeneoPositions; + + return $this; + } } diff --git a/src/Model/Configuration/CategoryConfigurationInterface.php b/src/Model/Configuration/CategoryConfigurationInterface.php index f8f3e2a3..3ba73110 100644 --- a/src/Model/Configuration/CategoryConfigurationInterface.php +++ b/src/Model/Configuration/CategoryConfigurationInterface.php @@ -10,7 +10,11 @@ public function getCategoryCodesToImport(): array; public function getCategoryCodesToExclude(): array; - public function setCategoryCodesToImport(array $categoryCodesToImport): CategoryConfiguration; + public function setCategoryCodesToImport(array $categoryCodesToImport): self; - public function setCategoryCodesToExclude(array $categoryCodesToExclude): CategoryConfiguration; + public function setCategoryCodesToExclude(array $categoryCodesToExclude): self; + + public function isUseAkeneoPositions(): bool; + + public function setUseAkeneoPositions(bool $useAkeneoPositions): self; } diff --git a/src/Provider/Configuration/Api/CategoryConfigurationProvider.php b/src/Provider/Configuration/Api/CategoryConfigurationProvider.php index e6964d6f..ea80a498 100644 --- a/src/Provider/Configuration/Api/CategoryConfigurationProvider.php +++ b/src/Provider/Configuration/Api/CategoryConfigurationProvider.php @@ -14,6 +14,7 @@ class CategoryConfigurationProvider implements CategoryConfigurationProviderInte public function __construct( private array $categoryCodesToImport, private array $categoryCodesToExclude, + private bool $useAkeneoPositions, ) { } @@ -26,6 +27,7 @@ public function get(): CategoryConfigurationInterface return $this->configuration = new CategoryConfiguration( $this->categoryCodesToImport, $this->categoryCodesToExclude, + $this->useAkeneoPositions, ); } } diff --git a/src/Task/Category/CreateUpdateEntityTask.php b/src/Task/Category/CreateUpdateEntityTask.php index f6cd9395..668f6584 100644 --- a/src/Task/Category/CreateUpdateEntityTask.php +++ b/src/Task/Category/CreateUpdateEntityTask.php @@ -6,6 +6,7 @@ use Behat\Transliterator\Transliterator; use Doctrine\ORM\EntityManagerInterface; +use Gedmo\Sortable\SortableListener; use Psr\Log\LoggerInterface; use Sylius\Component\Core\Model\TaxonInterface; use Sylius\Component\Resource\Factory\FactoryInterface; @@ -24,6 +25,7 @@ use Synolia\SyliusAkeneoPlugin\Logger\Messages; use Synolia\SyliusAkeneoPlugin\Payload\Category\CategoryPayload; use Synolia\SyliusAkeneoPlugin\Payload\PipelinePayloadInterface; +use Synolia\SyliusAkeneoPlugin\Provider\Configuration\Api\CategoryConfigurationProviderInterface; use Synolia\SyliusAkeneoPlugin\Provider\SyliusAkeneoLocaleCodeProvider; use Synolia\SyliusAkeneoPlugin\Repository\TaxonRepository; use Synolia\SyliusAkeneoPlugin\Task\AkeneoTaskInterface; @@ -61,6 +63,7 @@ public function __construct( private FactoryInterface $taxonAttributeValueFactory, private TaxonAttributeTypeMatcher $taxonAttributeTypeMatcher, private TaxonAttributeValueBuilder $taxonAttributeValueBuilder, + private CategoryConfigurationProviderInterface $categoryConfigurationProvider, ) { } @@ -87,6 +90,12 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte $this->assignParent($taxon, $taxons, $resource); + $this->logger->warning('SetPosition', [ + 'taxon_code' => $taxon->getCode(), + 'position' => $resource['position'], + 'positiond' => $taxon->getPosition(), + ]); + foreach ($this->syliusAkeneoLocaleCodeProvider->getUsedLocalesOnBothPlatforms() as $syliusLocale) { $akeneoLocale = $this->syliusAkeneoLocaleCodeProvider->getAkeneoLocale($syliusLocale); @@ -144,6 +153,26 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte $this->dispatcher->dispatch(new AfterProcessingTaxonEvent($resource, $taxon)); + if ($this->categoryConfigurationProvider->get()->isUseAkeneoPositions()) { + $taxon->setPosition($resource['position']); + + foreach ($this->entityManager->getEventManager()->getAllListeners() as $listenerTypes) { + foreach ($listenerTypes as $listener) { + if (get_class($listener) === SortableListener::class) { + $this->entityManager->getEventManager()->removeEventListener([ + 'onFlush', + 'loadClassMetadata', + 'prePersist', + 'postPersist', + 'preUpdate', + 'postRemove', + 'postFlush', + ], $listener); + } + } + } + } + $this->entityManager->flush(); if ($this->entityManager->getConnection()->isTransactionActive()) { diff --git a/src/Task/Category/RetrieveCategoriesTask.php b/src/Task/Category/RetrieveCategoriesTask.php index 52746805..a3b0c858 100644 --- a/src/Task/Category/RetrieveCategoriesTask.php +++ b/src/Task/Category/RetrieveCategoriesTask.php @@ -45,7 +45,9 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte } catch (CommandContextIsNullException) { $queryParameters = []; } + $queryParameters['with_enriched_attributes'] = true; + $queryParameters['with_position'] = true; $queryParameters = \array_merge_recursive($queryParameters, $payload->getCustomFilters());