<?php declare(strict_types=1);
namespace MoorlProductPromo;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Migration\InheritanceUpdaterTrait;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
class MoorlProductPromo extends Plugin
{
use InheritanceUpdaterTrait;
public const NAME = 'MoorlProductPromo';
public const DATA_CREATED_AT = '2003-03-03 03:13:04.000';
public const CMS_PAGE = 'moorl_pp';
public const CMS_PAGE_ID = 'ab1da2eddd6886c1fc72ea0f3f948f35';
public const PLUGIN_TABLES = [
'moorl_pp',
'moorl_pp_translation',
'moorl_pp_promotion',
'moorl_pp_promotion_translation',
'moorl_pp_stock',
'moorl_pp_promotion_sales_channel',
'moorl_pp_promotion_customer',
'moorl_pp_promotion_customer_group'
];
public const SHOPWARE_TABLES = [
'cms_page',
'cms_page_translation',
'cms_section',
'cms_block',
'category',
'category_translation',
'product',
'product_translation',
'product_category',
'product_visibility',
'moorl_sorting'
];
public const INHERITANCES = [
'product' => ['ppPromotions', 'ppStocks'],
'sales_channel' => ['ppPromotions'],
'order_line_item' => ['ppStock'],
'customer_group' => ['ppPromotions'],
'customer' => ['ppPromotions'],
'media' => ['ppPromotion']
];
public function activate(ActivateContext $activateContext): void
{
parent::activate($activateContext);
$connection = $this->container->get(Connection::class);
foreach (self::INHERITANCES as $table => $propertyNames) {
foreach ($propertyNames as $propertyName) {
try {
$this->updateInheritance($connection, $table, $propertyName);
} catch (\Exception $exception) {
continue;
}
}
}
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
$this->uninstallTrait();
}
private function uninstallTrait(): void
{
$connection = $this->container->get(Connection::class);
foreach (self::PLUGIN_TABLES as $table) {
$sql = sprintf('SET FOREIGN_KEY_CHECKS=0; DROP TABLE IF EXISTS `%s`;', $table);
$connection->executeStatement($sql);
}
foreach (array_reverse(self::SHOPWARE_TABLES) as $table) {
$sql = sprintf("SET FOREIGN_KEY_CHECKS=0; DELETE FROM `%s` WHERE `created_at` = '%s';", $table, self::DATA_CREATED_AT);
try {
$connection->executeStatement($sql);
} catch (\Exception $exception) {
continue;
}
}
foreach (self::INHERITANCES as $table => $propertyNames) {
foreach ($propertyNames as $propertyName) {
$sql = sprintf("ALTER TABLE `%s` DROP `%s`;", $table, $propertyName);
try {
$connection->executeStatement($sql);
} catch (\Exception $exception) {
continue;
}
}
}
}
}