<?php declare(strict_types=1);
namespace MoorlFormBuilder;
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;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
class MoorlFormBuilder extends Plugin
{
public const NAME = 'MoorlFormBuilder';
public const DATA_CREATED_AT = '2003-03-03 03:02:01.000';
public const PLUGIN_TABLES = [
'moorl_form_rule',
'moorl_form_product',
'moorl_form',
'moorl_form_history',
'moorl_form_appointment'
];
public const SHOPWARE_TABLES = [
'cms_page',
'cms_page_translation',
'cms_section',
'cms_block',
'category',
'category_translation',
'mail_template_type',
'mail_template_type_translation',
'mail_template',
'mail_template_translation',
'event_action',
'custom_field_set'
];
public const MAIL_TEMPLATE_MAIL_SEND_ACTION = 'moorl_form_builder.action.mail.send';
public function activate(ActivateContext $activateContext): void
{
parent::activate($activateContext); // TODO: Change the autogenerated stub
try {
/* @var $dataService DataService */
$dataService = $this->container->get(DataService::class);
$dataService->install(self::NAME);
} catch (\Exception $exception) {
}
}
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext); // TODO: Change the autogenerated stub
try {
/* @var $dataService DataService */
$dataService = $this->container->get(DataService::class);
$dataService->install(self::NAME);
} catch (\Exception $exception) {
}
}
public function uninstall(UninstallContext $context): void
{
parent::uninstall($context);
if ($context->keepUserData()) {
return;
}
$this->removePluginData();
$this->dropTables();
}
private function removePluginData(): void
{
$connection = $this->container->get(Connection::class);
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->executeUpdate($sql);
} catch (\Exception $exception) {
continue;
}
}
try {
$connection->executeUpdate('ALTER TABLE `product` DROP COLUMN `forms`');
} catch (\Exception $exception) {
}
}
private function dropTables(): 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->executeUpdate($sql);
}
}
}