Skip to content

Commit

Permalink
Import category position
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGrimmChester committed Nov 3, 2023
1 parent c4917ac commit b6ad918
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function getConfigTreeBuilder()
->arrayNode('excluded_category_codes')
->scalarPrototype()->defaultValue([])->end()
->end()
->booleanNode('use_akeneo_positions')->defaultFalse()->end()
->end()
->end()

Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/SynoliaSyliusAkeneoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 17 additions & 2 deletions src/Model/Configuration/CategoryConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
8 changes: 6 additions & 2 deletions src/Model/Configuration/CategoryConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class CategoryConfigurationProvider implements CategoryConfigurationProviderInte
public function __construct(
private array $categoryCodesToImport,
private array $categoryCodesToExclude,
private bool $useAkeneoPositions,
) {
}

Expand All @@ -26,6 +27,7 @@ public function get(): CategoryConfigurationInterface
return $this->configuration = new CategoryConfiguration(
$this->categoryCodesToImport,
$this->categoryCodesToExclude,
$this->useAkeneoPositions,
);
}
}
29 changes: 29 additions & 0 deletions src/Task/Category/CreateUpdateEntityTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -61,6 +63,7 @@ public function __construct(
private FactoryInterface $taxonAttributeValueFactory,
private TaxonAttributeTypeMatcher $taxonAttributeTypeMatcher,
private TaxonAttributeValueBuilder $taxonAttributeValueBuilder,
private CategoryConfigurationProviderInterface $categoryConfigurationProvider,
) {
}

Expand All @@ -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);

Expand Down Expand Up @@ -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()) {
Expand Down
2 changes: 2 additions & 0 deletions src/Task/Category/RetrieveCategoriesTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down

0 comments on commit b6ad918

Please sign in to comment.