<?php
declare(strict_types=1);
/**
* @copyright 2023 Crehler Sp. z o.o.
*
* https://crehler.com/
* support@crehler.com
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\AdvancedSort;
use Doctrine\DBAL\Connection;
use Shopware\Core\Content\Product\SalesChannel\Sorting\ProductSortingEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
class CrehlerAdvancedSort extends Plugin
{
public const PRODUCT_SORTING_ENTRY = '62a84b8e0fc74db2bbd203f9165ce787';
public function update(UpdateContext $updateContext): void
{
if (version_compare($updateContext->getCurrentPluginVersion(), '1.0.2', '<=')) {
$systemConfigService = $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService');
$systemConfigService->set('CrehlerAdvancedSort.config.basicSorting', null);
$systemConfigService->set('CrehlerAdvancedSort.config.defaultSorting', null);
}
parent::update($updateContext);
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
$connection = $this->container->get(Connection::class);
$connection->executeStatement('DROP TABLE IF EXISTS `crehler_product_position`');
$connection->executeStatement('DROP TABLE IF EXISTS `crehler_category_position_active`');
}
public function activate(ActivateContext $activateContext): void
{
parent::activate($activateContext);
$this->addPositionSort($activateContext);
}
public function deactivate(DeactivateContext $deactivateContext): void
{
parent::deactivate($deactivateContext);
$this->removePositionSort($deactivateContext);
}
private function removePositionSort(DeactivateContext $deactivateContext): void
{
$productSortingRepository = $this->container->get('product_sorting.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('key', 'position'));
$positionSort = $productSortingRepository->search($criteria, $deactivateContext->getContext())->first();
if ($positionSort instanceof ProductSortingEntity) {
$productSortingRepository->delete([['id' => $positionSort->getId()]], $deactivateContext->getContext());
}
}
private function addPositionSort(ActivateContext $activateContext): void
{
/** @var EntityRepository $productSortingRepository */
$productSortingRepository = $this->container->get('product_sorting.repository');
$criteria = new Criteria();
$criteria->addFilter(
new NotFilter(
NotFilter::CONNECTION_OR,
[
new EqualsFilter('id', self::PRODUCT_SORTING_ENTRY),
]
)
);
$criteria->addSorting(new FieldSorting('priority', FieldSorting::DESCENDING));
/** @var ProductSortingEntity $entry */
$entry = $productSortingRepository->search($criteria, $activateContext->getContext())->first();
$crehlerAdvancedSorting = [
'id' => self::PRODUCT_SORTING_ENTRY,
'key' => 'position',
'priority' => $entry->getPriority() + 1,
'active' => true,
'locked' => false,
'fields' => [
[
'field' => 'positions.position',
'order' => 'asc',
'priority' => 1,
'naturalSorting' => 0,
],
],
'label' => 'Positions',
];
$productSortingRepository->upsert([$crehlerAdvancedSorting], $activateContext->getContext());
}
}