custom/plugins/GrimmSorting/src/Subscriber/ProductCategoryTree.php line 62

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace GrimmSorting\Subscriber;
  3. use Enqueue\Util\UUID;
  4. use GrimmSorting\Service\SortingPreserver;
  5. use Shopware\Core\Content\Product\Aggregate\ProductCategory\ProductCategoryDefinition;
  6. use Shopware\Core\Content\Product\ProductEvents;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\InsertCommand;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\ChangeSetAware;
  14. use Doctrine\DBAL\Connection;
  15. class ProductCategoryTree implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var SortingPreserver
  19.      */
  20.     private SortingPreserver $sortingPreserver;
  21.     /**
  22.      * @var Connection
  23.      */
  24.     private Connection $connection;
  25.     private $productCategoryTables = [
  26.         'product_category',
  27.         'product_category_tree'
  28.     ];
  29.     public function __construct(Connection $connectionSortingPreserver $sortingPreserver)
  30.     {
  31.         $this->sortingPreserver $sortingPreserver;
  32.         $this->connection $connection;
  33.     }
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return [
  37.             PreWriteValidationEvent::class => 'beforeDelete',
  38.         ];
  39.     }
  40.     /**
  41.      * Before the product_category AND product_category_tree entries are deleted load the entries
  42.      * and store them in the SortingPreserver to keep the positions
  43.      *
  44.      * @param PreWriteValidationEvent $event
  45.      *
  46.      * @throws \Doctrine\DBAL\Driver\Exception
  47.      * @throws \Doctrine\DBAL\Exception
  48.      */
  49.     public function beforeDelete(PreWriteValidationEvent $event): void
  50.     {
  51.         foreach ($event->getCommands() as $command) {
  52.             if ($command instanceof DeleteCommand || $command instanceof InsertCommand) {
  53.                 if ($command->getDefinition() instanceof ProductCategoryDefinition) {
  54.                     $existence $command->getEntityExistence()->getPrimaryKey();
  55.                     $productId false;
  56.                     if (!array_key_exists('product_id'$existence)) {
  57.                         $primaryKey $command->getPrimaryKey();
  58.                         if (array_key_exists('product_id'$primaryKey)) {
  59.                             $productId \Shopware\Core\Framework\Uuid\Uuid::fromBytesToHex($primaryKey['product_id']);
  60.                         }
  61.                     } else {
  62.                         $productId $existence['product_id'];
  63.                     }
  64.                     $statement $this->connection->prepare('SELECT HEX(product_id) AS product_id, HEX(category_id) AS category_id, swpa_sorting FROM product_category WHERE product_id = UNHEX(:product_id)');
  65.                     $statement->bindParam('product_id'$productId);
  66.                     if ($statement->execute()) {
  67.                         $sortings $statement->fetchAllAssociative();
  68.                         $preservable = [];
  69.                         foreach ($sortings as $sorting) {
  70.                             if ((int)$sorting['swpa_sorting'] !== 99999) {
  71.                                 $preservable[] = $sorting;
  72.                             }
  73.                         }
  74.                         $this->sortingPreserver->setSorting($preservable);
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.     }
  80. }