<?php declare(strict_types=1);
namespace MoorlCreator;
use Doctrine\DBAL\Connection;
use MoorlFoundation\Core\Service\DataService;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
class MoorlCreator extends Plugin
{
public const NAME = 'MoorlCreator';
public const CMS_PAGE_CREATOR_DEFAULT_ID = 'a054f7fb8664f8f6da162b2387e9c351';
public const CREATOR_TYPE_DEFAULT_ID = 'a054f7fb8664f8f6da222b2387e9c351';
public const DATA_CREATED_AT = '2012-03-26 00:00:00.000';
public const PLUGIN_TABLES = [
'moorl_creator_type',
'moorl_creator_type_translation',
'moorl_creator',
'moorl_creator_translation',
'moorl_creator_customer',
'moorl_creator_product',
'moorl_creator_follower'
];
public const SHOPWARE_TABLES = [
'custom_field_set',
'unit',
'shipping_method',
'shipping_method_translation',
'cms_page',
'cms_page_translation',
'cms_section',
'cms_block',
'category',
'category_translation',
'product',
'product_translation',
'product_category',
'product_visibility',
'seo_url_template',
'media_default_folder',
'custom_field_set',
'moorl_sorting'
];
public const INHERITANCES = [
'product' => ['creators']
];
public function activate(ActivateContext $activateContext): void
{
parent::activate($activateContext);
/* @var $dataService DataService */
$dataService = $this->container->get(DataService::class);
$dataService->install(self::NAME);
}
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 (array_reverse(self::PLUGIN_TABLES) as $table) {
$sql = sprintf('DROP TABLE IF EXISTS `%s`;', $table);
$connection->executeStatement($sql);
}
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;
}
}
}
}
}