Skip to content

Commit

Permalink
Import category position from Akeneo
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGrimmChester committed Nov 6, 2023
1 parent b6ad918 commit cb03582
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 20 deletions.
3 changes: 3 additions & 0 deletions docs/CONFIGURE_DETAIL.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ The category import configuration contains two configurations.

`excluded_category_codes` allows you to choose the categories that you want to exclude from the import.

`use_akeneo_positions` import category position from Akeneo, this will bypass the default sortable event.

**Selecting a parent will exclude the parent and its children**.

```yaml
Expand All @@ -66,6 +68,7 @@ synolia_sylius_akeneo:
- led_tvs
- audio_video
- mp3_players
use_akeneo_positions: true
```


Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/SynoliaSyliusAkeneoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +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'])
->setArgument('$useAkeneoPositions', $config['category_configuration']['use_akeneo_positions'])
;

$container->setAlias(CategoryConfigurationProviderInterface::class, CategoryConfigurationProvider::class);
Expand Down
16 changes: 16 additions & 0 deletions src/Entity/CategoryConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class CategoryConfiguration implements ResourceInterface
#[ORM\Column(type: Types::ARRAY)]
private array $rootCategories = [];

/** @ORM\Column(type="boolean") */
#[ORM\Column(type: Types::BOOLEAN)]
private bool $useAkeneoPositions = false;

public function getId(): int
{
return $this->id;
Expand Down Expand Up @@ -88,4 +92,16 @@ public function setRootCategories(array $rootCategories): self

return $this;
}

public function useAkeneoPositions(): bool
{
return $this->useAkeneoPositions;
}

public function setUseAkeneoPositions(bool $useAkeneoPositions): self
{
$this->useAkeneoPositions = $useAkeneoPositions;

return $this;
}
}
4 changes: 4 additions & 0 deletions src/Fixture/CategoryConfigurationFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function load(array $options): void
$categoryConfiguration = $this->categoriesConfigurationFactory->createNew();
$categoryConfiguration->setRootCategories($options['root_categories_to_import']);
$categoryConfiguration->setNotImportCategories($options['categories_to_exclude']);
$categoryConfiguration->setUseAkeneoPositions($options['use_akeneo_positions']);

$this->entityManager->persist($categoryConfiguration);
$this->entityManager->flush();
Expand All @@ -46,6 +47,9 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
->arrayNode('categories_to_exclude')
->scalarPrototype()->defaultValue([])->end()
->end()
->arrayNode('use_akeneo_positions')
->scalarPrototype()->defaultFalse()->end()
->end()
->end()
;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Form/Type/CategoriesConfigurationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Synolia\SyliusAkeneoPlugin\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;

Expand All @@ -26,6 +27,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'required' => false,
'multiple' => true,
])
->add('use_akeneo_positions', CheckboxType::class, [
'label' => 'sylius.ui.admin.akeneo.categories.use_akeneo_positions',
'required' => false,
])
->add('submit', SubmitType::class, [
'attr' => ['class' => 'ui icon primary button'],
'label' => 'sylius.ui.admin.akeneo.save',
Expand Down
40 changes: 40 additions & 0 deletions src/Manager/Doctrine/DoctrineSortableManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Synolia\SyliusAkeneoPlugin\Manager\Doctrine;

use Doctrine\ORM\EntityManagerInterface;
use Gedmo\Sortable\SortableListener;

class DoctrineSortableManager
{
private array $originalEventListeners = [];

public function __construct(private EntityManagerInterface $entityManager)
{
}

public function disableSortableEventListener(): void
{
foreach ($this->entityManager->getEventManager()->getListeners() as $eventName => $listeners) {
foreach ($listeners as $listener) {
if ($listener instanceof SortableListener) {
$this->originalEventListeners[$eventName] = $listener;
$this->entityManager->getEventManager()->removeEventListener($eventName, $listener);
}
}
}
}

public function enableSortableEventListener(): void
{
if ($this->originalEventListeners === []) {
return;
}

foreach ($this->originalEventListeners as $eventName => $listener) {
$this->entityManager->getEventManager()->addEventListener($eventName, $listener);
}
}
}
29 changes: 29 additions & 0 deletions src/Migrations/Version20231106080715.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Synolia\SyliusAkeneoPlugin\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20231106080715 extends AbstractMigration
{
public function getDescription(): string
{
return 'Added useAkeneoPositions column';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE akeneo_api_configuration_categories ADD useAkeneoPositions TINYINT(1) NOT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE akeneo_api_configuration_categories DROP useAkeneoPositions');
}
}
2 changes: 1 addition & 1 deletion src/Model/Configuration/CategoryConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function setCategoryCodesToExclude(array $categoryCodesToExclude): self
return $this;
}

public function isUseAkeneoPositions(): bool
public function useAkeneoPositions(): bool
{
return $this->useAkeneoPositions;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Configuration/CategoryConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function setCategoryCodesToImport(array $categoryCodesToImport): self;

public function setCategoryCodesToExclude(array $categoryCodesToExclude): self;

public function isUseAkeneoPositions(): bool;
public function useAkeneoPositions(): bool;

public function setUseAkeneoPositions(bool $useAkeneoPositions): self;
}
1 change: 1 addition & 0 deletions src/Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ sylius:
main_category: Main category
root_categories: Sylius Root categories
empty_local_replace_by: Replace empty local by
use_akeneo_positions: Import category position from Akeneo
products:
title: Products
subtitle: Products configuration
Expand Down
1 change: 1 addition & 0 deletions src/Resources/translations/messages.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ sylius:
main_category: Catégorie principale
root_categories: Catégories racines de Sylius
empty_local_replace_by: Remplacer la locale vide par
use_akeneo_positions: Importer la position des catégories d'Akeneo
products:
title: Produits
subtitle: Configuration des produits
Expand Down
22 changes: 5 additions & 17 deletions src/Task/Category/CreateUpdateEntityTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

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 @@ -23,6 +22,7 @@
use Synolia\SyliusAkeneoPlugin\Event\Category\BeforeProcessingTaxonEvent;
use Synolia\SyliusAkeneoPlugin\Exceptions\UnsupportedAttributeTypeException;
use Synolia\SyliusAkeneoPlugin\Logger\Messages;
use Synolia\SyliusAkeneoPlugin\Manager\Doctrine\DoctrineSortableManager;
use Synolia\SyliusAkeneoPlugin\Payload\Category\CategoryPayload;
use Synolia\SyliusAkeneoPlugin\Payload\PipelinePayloadInterface;
use Synolia\SyliusAkeneoPlugin\Provider\Configuration\Api\CategoryConfigurationProviderInterface;
Expand Down Expand Up @@ -64,6 +64,7 @@ public function __construct(
private TaxonAttributeTypeMatcher $taxonAttributeTypeMatcher,
private TaxonAttributeValueBuilder $taxonAttributeValueBuilder,
private CategoryConfigurationProviderInterface $categoryConfigurationProvider,
private DoctrineSortableManager $sortableManager,
) {
}

Expand Down Expand Up @@ -153,27 +154,14 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte

$this->dispatcher->dispatch(new AfterProcessingTaxonEvent($resource, $taxon));

if ($this->categoryConfigurationProvider->get()->isUseAkeneoPositions()) {
if ($this->categoryConfigurationProvider->get()->useAkeneoPositions()) {
$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->sortableManager->disableSortableEventListener();
}

$this->entityManager->flush();
$this->sortableManager->enableSortableEventListener();

if ($this->entityManager->getConnection()->isTransactionActive()) {
$this->entityManager->commit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function transform(CategoryConfiguration $configuration): CategoryConfigu
return new \Synolia\SyliusAkeneoPlugin\Model\Configuration\CategoryConfiguration(
$configuration->getRootCategories(),
$configuration->getNotImportCategories(),
$configuration->useAkeneoPositions(),
);
}
}

0 comments on commit cb03582

Please sign in to comment.