<?php
declare(strict_types=1);
namespace Rhiem\RhiemUserProducts;
use Doctrine\DBAL\Connection;
use Rhiem\RhiemUserProducts\Entities\ProductGroup\ProductGroupCollection;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
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\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
class RhiemUserProducts extends Plugin
{
public const FALLBACK_PRODUCT_GROUP_ID = "0502913fa0a54ee7b24a4128fb2c62bb";
public function install(InstallContext $installContext): void
{
parent::install($installContext);
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
/**
* @var Connection $connection
*/
$connection = $this->container->get(Connection::class);
$connection->executeStatement('ALTER TABLE customer DROP FOREIGN KEY `fk.customer.rhiem_product_group_id`');
$connection->executeStatement('ALTER TABLE customer DROP INDEX `fk.customer.rhiem_product_group_id`');
$connection->executeStatement('DROP TABLE IF EXISTS `rhiem_userproducts_productgroup_product`');
$connection->executeStatement('DROP TABLE IF EXISTS `rhiem_userproducts_productgroup_customer`');
$connection->executeStatement('DROP TABLE IF EXISTS `rhiem_userproducts_productgroup`');
$connection->executeStatement('ALTER TABLE `customer` DROP COLUMN `rhiemProductGroup`');
$connection->executeStatement('ALTER TABLE `customer` DROP COLUMN `rhiem_product_group_id`');
$connection->executeStatement('ALTER TABLE `product` DROP COLUMN `product_groups`');
}
public function activate(ActivateContext $activateContext): void
{
$this->createFallbackProductGroup();
}
public function deactivate(DeactivateContext $deactivateContext): void
{
parent::deactivate($deactivateContext);
}
public function createFallbackProductGroup(): void
{
/** @var EntityRepositoryInterface $productGroupRepository */
$productGroupRepository = $this->container->get('rhiem_userproducts_productgroup.repository');
/**
* @var ProductGroupCollection $result
*/
$result = $productGroupRepository->search(
new Criteria([self::FALLBACK_PRODUCT_GROUP_ID]),
Context::createDefaultContext()
);
if ($result->getElements()) {
return;
}
$productGroupRepository->upsert(
[
[
'id' => self::FALLBACK_PRODUCT_GROUP_ID,
'name' => 'Fallback',
'groupMode' => 'blacklist',
'active' => true,
'fallback' => true
]
],
Context::createDefaultContext()
);
}
public function postUpdate(UpdateContext $updateContext): void
{
if (version_compare($updateContext->getCurrentPluginVersion(), '1.0.7', '<')) {
/**
* @var Connection $connection
*/
$connection = $this->container->get(Connection::class);
try {
$connection->executeStatement('ALTER TABLE customer DROP FOREIGN KEY `fk.customer.rhiem_product_group_id`');
} catch (\Exception $e) {
}
$sql = "ALTER TABLE customer
ADD CONSTRAINT `fk.customer.rhiem_product_group_id` FOREIGN KEY (`rhiem_product_group_id`)
REFERENCES `rhiem_userproducts_productgroup` (`id`) ON DELETE SET NULL ON UPDATE CASCADE";
/**
* @var Connection $connection
*/
$connection->executeStatement($sql);
}
parent::postUpdate($updateContext);
}
}