vendor/crehler/advanced-sort/src/CrehlerAdvancedSort.php line 28

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @copyright 2023 Crehler Sp. z o.o.
  5.  *
  6.  * https://crehler.com/
  7.  * support@crehler.com
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Crehler\AdvancedSort;
  13. use Doctrine\DBAL\Connection;
  14. use Shopware\Core\Content\Product\SalesChannel\Sorting\ProductSortingEntity;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  20. use Shopware\Core\Framework\Plugin;
  21. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  22. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  23. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  24. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  25. class CrehlerAdvancedSort extends Plugin
  26. {
  27.     public const PRODUCT_SORTING_ENTRY '62a84b8e0fc74db2bbd203f9165ce787';
  28.     public function update(UpdateContext $updateContext): void
  29.     {
  30.         if (version_compare($updateContext->getCurrentPluginVersion(), '1.0.2''<=')) {
  31.             $systemConfigService $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService');
  32.             $systemConfigService->set('CrehlerAdvancedSort.config.basicSorting'null);
  33.             $systemConfigService->set('CrehlerAdvancedSort.config.defaultSorting'null);
  34.         }
  35.         parent::update($updateContext);
  36.     }
  37.     public function uninstall(UninstallContext $uninstallContext): void
  38.     {
  39.         parent::uninstall($uninstallContext);
  40.         if ($uninstallContext->keepUserData()) {
  41.             return;
  42.         }
  43.         $connection $this->container->get(Connection::class);
  44.         $connection->executeStatement('DROP TABLE IF EXISTS `crehler_product_position`');
  45.         $connection->executeStatement('DROP TABLE IF EXISTS `crehler_category_position_active`');
  46.     }
  47.     public function activate(ActivateContext $activateContext): void
  48.     {
  49.         parent::activate($activateContext);
  50.         $this->addPositionSort($activateContext);
  51.     }
  52.     public function deactivate(DeactivateContext $deactivateContext): void
  53.     {
  54.         parent::deactivate($deactivateContext);
  55.         $this->removePositionSort($deactivateContext);
  56.     }
  57.     private function removePositionSort(DeactivateContext $deactivateContext): void
  58.     {
  59.         $productSortingRepository $this->container->get('product_sorting.repository');
  60.         $criteria = new Criteria();
  61.         $criteria->addFilter(new EqualsFilter('key''position'));
  62.         $positionSort $productSortingRepository->search($criteria$deactivateContext->getContext())->first();
  63.         if ($positionSort instanceof ProductSortingEntity) {
  64.             $productSortingRepository->delete([['id' => $positionSort->getId()]], $deactivateContext->getContext());
  65.         }
  66.     }
  67.     private function addPositionSort(ActivateContext $activateContext): void
  68.     {
  69.         /** @var EntityRepository $productSortingRepository */
  70.         $productSortingRepository $this->container->get('product_sorting.repository');
  71.         $criteria = new Criteria();
  72.         $criteria->addFilter(
  73.             new NotFilter(
  74.                 NotFilter::CONNECTION_OR,
  75.                 [
  76.                     new EqualsFilter('id'self::PRODUCT_SORTING_ENTRY),
  77.                 ]
  78.             )
  79.         );
  80.         $criteria->addSorting(new FieldSorting('priority'FieldSorting::DESCENDING));
  81.         /** @var ProductSortingEntity $entry */
  82.         $entry $productSortingRepository->search($criteria$activateContext->getContext())->first();
  83.         $crehlerAdvancedSorting = [
  84.             'id' => self::PRODUCT_SORTING_ENTRY,
  85.             'key' => 'position',
  86.             'priority' => $entry->getPriority() + 1,
  87.             'active' => true,
  88.             'locked' => false,
  89.             'fields' => [
  90.                 [
  91.                     'field' => 'positions.position',
  92.                     'order' => 'asc',
  93.                     'priority' => 1,
  94.                     'naturalSorting' => 0,
  95.                 ],
  96.             ],
  97.             'label' => 'Positions',
  98.         ];
  99.         $productSortingRepository->upsert([$crehlerAdvancedSorting], $activateContext->getContext());
  100.     }
  101. }