<?php declare(strict_types=1);
namespace MoorlMagazine;
use Doctrine\DBAL\Connection;
use MoorlCreator\MoorlCreator;
use MoorlFoundation\Core\Service\DataService;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class MoorlMagazine extends Plugin
{
public const NAME = 'MoorlMagazine';
public const CMS_PAGE_MAGAZINE_ARTICLE_DEFAULT_ID = 'b054f7fb8664f8f6da162b2387e9c351';
public const CMS_PAGE_MAGAZINE_ARTICLE_TOC_ID = 'b054f7fb8664f8a6da162b2387e9c351';
public const DATA_CREATED_AT = '2003-03-03 03:02:15.000';
public const CMS_PAGE_ID = '2f649b87fd9341623d4595016ea9e72b';
public const MAIN_ENTITY = 'moorl_magazine_article';
public const PLUGIN_TABLES = [
'moorl_magazine_article_tag',
'moorl_magazine_article_product',
'moorl_magazine_comment',
'moorl_magazine_article_category',
'moorl_magazine_category_translation',
'moorl_magazine_category',
'moorl_magazine_article_sales_channel',
'moorl_magazine_article_translation',
'moorl_magazine_article',
'moorl_magazine_author',
'moorl_magazine_author_translation'
];
public const SHOPWARE_TABLES = [
'cms_page',
'cms_page_translation',
'cms_section',
'cms_block',
'cms_slot',
'category',
'category_translation',
'mail_template_type',
'mail_template_type_translation',
'mail_template',
'mail_template_translation',
'event_action',
'custom_field_set',
'media_default_folder',
'seo_url_template',
'moorl_sorting',
'moorl_creator_type',
'moorl_creator',
];
public function build(ContainerBuilder $container): void
{
if (class_exists(MoorlCreator::class)) {
parent::build($container);
}
}
public function install(InstallContext $context): void
{
parent::install($context);
}
public function activate(ActivateContext $activateContext): void
{
parent::activate($activateContext); // TODO: Change the autogenerated stub
/* @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 (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;
}
}
}
}