From 9a3c445b849b8043632baa5024dfae35fcfb1903 Mon Sep 17 00:00:00 2001 From: Lorenzo Ruozzi Date: Tue, 13 Feb 2024 10:17:23 +0100 Subject: [PATCH] WIP: Add import all action on admin section (#153) --- config/admin_routing.php | 17 +++++++++ config/admin_routing.yaml | 4 +++ config/config.yaml | 13 ++++++- src/Controller/ProductImportController.php | 42 ++++++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 config/admin_routing.php diff --git a/config/admin_routing.php b/config/admin_routing.php new file mode 100644 index 00000000..87c1f5b0 --- /dev/null +++ b/config/admin_routing.php @@ -0,0 +1,17 @@ +add('webgriffe_sylius_akeneo_product_import', '/product/{productId}/import') + ->controller(['webgriffe_sylius_akeneo.controller.product_import_controller', 'importAction']) + ; + + $routes->import( + '@WebgriffeSyliusAkeneoPlugin/Resources/config/admin_routing.yml', + 'sylius_admin' + ); + $routes->import('.', 'sylius.resource'); +}; diff --git a/config/admin_routing.yaml b/config/admin_routing.yaml index e191cc61..e49e48eb 100644 --- a/config/admin_routing.yaml +++ b/config/admin_routing.yaml @@ -18,3 +18,7 @@ webgriffe_sylius_akeneo_item_import_result: index: icon: 'cloud download' type: sylius.resource + +webgriffe_sylius_akeneo_products_import: + controller: webgriffe_sylius_akeneo.controller.product_import_controller::importAllAction + path: products/import diff --git a/config/config.yaml b/config/config.yaml index a9f1a329..f1513902 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -59,7 +59,18 @@ sylius_grid: label: webgriffe_sylius_akeneo.ui.successful message: type: string - label: webgriffe_sylius_akeneo.ui.message + label: webgriffe_sylius_akeneo.ui.message + actions: + main: + import_product_variants: + type: importProduct + label: webgriffe_sylius_akeneo.ui.schedule_import + options: + link: + route: webgriffe_sylius_akeneo_products_import + parameters: [] + icon: cloud download + color: violet framework: messenger: diff --git a/src/Controller/ProductImportController.php b/src/Controller/ProductImportController.php index 291ae686..fb00cbbe 100644 --- a/src/Controller/ProductImportController.php +++ b/src/Controller/ProductImportController.php @@ -4,6 +4,8 @@ namespace Webgriffe\SyliusAkeneoPlugin\Controller; +use Akeneo\Pim\ApiClient\AkeneoPimClient; +use DateTime; use Sylius\Component\Core\Model\ProductVariantInterface; use Sylius\Component\Product\Model\ProductInterface; use Sylius\Component\Product\Repository\ProductRepositoryInterface; @@ -13,6 +15,7 @@ use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Contracts\Translation\TranslatorInterface; use Webgriffe\SyliusAkeneoPlugin\Message\ItemImport; +use Webgriffe\SyliusAkeneoPlugin\Product\Importer; use Webgriffe\SyliusAkeneoPlugin\Product\Importer as ProductImporter; use Webgriffe\SyliusAkeneoPlugin\ProductAssociations\Importer as ProductAssociationsImporter; use Webgriffe\SyliusAkeneoPlugin\ProductModel\Importer as ProductModelImporter; @@ -24,7 +27,27 @@ public function __construct( private ProductRepositoryInterface $productRepository, private MessageBusInterface $messageBus, private TranslatorInterface $translator, + private ?Importer $productImporter = null, ) { + if ($this->akeneoPimClient === null) { + trigger_deprecation( + 'webgriffe/sylius-akeneo-plugin', + '1.5.0', + 'Not passing an instance of %s to %s constructor is deprecated. It will be mandatory in version 2.0.0.', + AkeneoPimClient::class, + self::class, + ); + } + + if ($this->productImporter === null) { + trigger_deprecation( + 'webgriffe/sylius-akeneo-plugin', + '1.5.0', + 'Not passing an instance of %s to %s constructor is deprecated. It will be mandatory in version 2.0.0.', + AkeneoPimClient::class, + self::class, + ); + } } public function importAction(int $productId): Response @@ -65,4 +88,23 @@ public function importAction(int $productId): Response return $this->redirectToRoute('webgriffe_sylius_akeneo_admin_item_import_result_index'); } + + public function importAllAction(): Response + { + $identifiers = $this->productImporter->getIdentifiersModifiedSince((new DateTime())->setTimestamp(0)); + + foreach ($identifiers as $identifier) { + $itemImport = new ItemImport( + Importer::AKENEO_ENTITY, + $identifier, + ); + $this->messageBus->dispatch($itemImport); + } + $this->addFlash( + 'success', + $this->translator->trans('webgriffe_sylius_akeneo.ui.products_enqueued_successfully'), + ); + + return $this->redirectToRoute('webgriffe_sylius_akeneo_admin_item_import_result_index'); + } }