<?php declare(strict_types=1);
namespace Acris\SuppliesUpselling;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
class AcrisSuppliesUpsellingCS extends Plugin
{
public function postUpdate(UpdateContext $updateContext): void
{
if((version_compare($updateContext->getCurrentPluginVersion(), '2.0.0', '<') && version_compare($updateContext->getUpdatePluginVersion(), '2.0.0', '>=')) && $updateContext->getPlugin()->isActive() === true) {
$this->getConfigurationSettings($updateContext->getContext());
}
}
private function getConfigurationSettings(Context $context): void
{
/** @var EntityRepositoryInterface $systemConfigRepository */
$systemConfigRepository = $this->container->get('system_config.repository');
/** @var EntityRepositoryInterface $productCrossSellingRepository */
$productCrossSellingRepository = $this->container->get('product_cross_selling.repository');
/** @var EntityRepositoryInterface $acrisCrossSellingDisplayRepository */
$acrisCrossSellingDisplayRepository = $this->container->get('acris_cross_selling_display.repository');
/** @var EntitySearchResult $systemConfigResults */
$systemConfigResults = $systemConfigRepository->search((new Criteria())->addFilter(new ContainsFilter('configurationKey', 'AcrisSuppliesUpsellingCS.config')), $context);
/** @var EntitySearchResult $productCrossSellingResults */
$productCrossSellingResults = $productCrossSellingRepository->search((new Criteria()), $context);
if ($productCrossSellingResults->getElements()) {
foreach ($productCrossSellingResults->getElements() as $productCrossSelling) {
$data = null;
if ($systemConfigResults->first() && $systemConfigResults->getTotal() > 0) {
$listing = false;
foreach ($systemConfigResults as $systemConfig) {
if ($systemConfig->getConfigurationValue() === "listing") {
$listing = true;
}
if ($listing) {
if ($systemConfig->getConfigurationValue() === "above") {
$data = [
'productCrossSellingId' => $productCrossSelling->getId(),
'crossSellingLayout' => 'layoutList',
'crossSellingDisplay' => 'displayAbovePrice',
'crossSellingFlyoutDisplay' => 'flyoutHide'
];
} else if ($systemConfig->getConfigurationValue() === "tab") {
$data = [
'productCrossSellingId' => $productCrossSelling->getId(),
'crossSellingLayout' => 'layoutList',
'crossSellingDisplay' => 'displaySwStandard',
'crossSellingFlyoutDisplay' => 'flyoutHide'
];
}
}
}
}
if ($data) {
$acrisCrossSellingDisplayRepository->upsert([$data], $context);
}
}
}
}
public
function uninstall(UninstallContext $context): void
{
if ($context->keepUserData()) {
return;
}
$this->cleanupDatabase();
}
private
function cleanupDatabase(): void
{
$connection = $this->container->get(Connection::class);
$connection->executeStatement('DROP TABLE IF EXISTS acris_cross_selling_display');
$connection->executeStatement('DROP TABLE IF EXISTS acris_cross_selling_translation');
$connection->executeStatement('DROP TABLE IF EXISTS acris_cross_selling');
$connection->executeStatement('DROP TABLE IF EXISTS acris_cross_selling');
$this->removeInheritance($connection, 'product_cross_selling', 'acrisCrossSellingDisplay');
$this->removeInheritance($connection, 'product_cross_selling_assigned_products', 'acrisProductCrossSellingAssignedProduct');
}
protected function removeInheritance(Connection $connection, string $entity, string $propertyName): void
{
$sql = str_replace(
['#table#', '#column#'],
[$entity, $propertyName],
'ALTER TABLE `#table#` DROP COLUMN `#column#`'
);
$connection->executeUpdate($sql);
}
}