<?php declare(strict_types=1);
namespace Ic\IcCustomQueryResults;
use Shopware\Core\Framework\Plugin;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
class IcCustomQueryResults extends Plugin
{
/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container): void
{
parent::build($container);
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/DependencyInjection'));
$loader->load('util.xml');
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if(!$uninstallContext->keepUserData()) {
$connection = $this->container->get(Connection::class);
$connection->executeUpdate('DROP TABLE IF EXISTS `into_query_list`');
$this->removeConfiguration($uninstallContext->getContext());
}
}
private function removeConfiguration(Context $context): void
{
/** @var EntityRepositoryInterface $systemConfigRepository */
$systemConfigRepository = $this->container->get('system_config.repository');
$criteria = (new Criteria())
->addFilter(new ContainsFilter('configurationKey', 'IcCustomQueryResults.'));
$idSearchResult = $systemConfigRepository->searchIds($criteria, $context);
if (!$idSearchResult->getTotal()) {
return;
}
$ids = array_map(static function ($id) {
return ['id' => $id];
}, $idSearchResult->getIds());
$systemConfigRepository->delete($ids, $context);
}
public function update(UpdateContext $updateContext): void
{
$connection = $this->container->get(Connection::class);
$columnCheck = $connection->executeQuery("
SHOW COLUMNS FROM `into_query_list` LIKE 'query_data'
");
$resultExists = (bool)$columnCheck->rowCount();
if($resultExists) {
$connection->executeUpdate("ALTER TABLE `into_query_list` CHANGE `query_data` `query_data` longtext COLLATE 'utf8mb4_unicode_ci' NULL");
}
parent::update($updateContext);
}
}