<?php declare(strict_types=1);
namespace MoorlForms;
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 MoorlForms extends Plugin
{
public const NAME = 'MoorlForms';
public const DATA_CREATED_AT = '2003-03-03 03:02:21.000';
public const PLUGIN_TABLES = [
'moorl_fb_form',
'moorl_fb_form_translation',
'moorl_fb_element',
'moorl_fb_element_translation'
];
public const SHOPWARE_TABLES = [
'media_default_folder',
'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 INHERITANCES = [
'product' => ['moorl_fb_forms']
];
public const MAIL_TEMPLATE_MAIL_SEND_ACTION = 'moorl_fb.action.mail.send';
public const MSG_CODE = 58667;
public const DANGER = 'danger';
public const SUCCESS = 'success';
public const WARNING = 'warning';
public const REPEATER_INDICATOR = '?';
public const CHECKED_INDICATOR = 'x';
public const FORM_ID_KEY = '_fb_form_id';
public const ENTITY_ID_KEY = '_fb_entity_id';
public const ENTITY_NAME_KEY = '_fb_entity_name';
public const ENTITY_FIELD_KEY = '_fb_entity_field';
public const CUSTOM_FIELD_HTML_KEY = '_moorl_fb_html';
public const CUSTOM_FIELD_PLAIN_KEY = '_moorl_fb_plain';
public const CUSTOM_FIELD_MEDIA_IDS_KEY = '_moorl_fb_media_ids';
public const PAYLOAD_KEY = '_moorl_fb';
public function activate(ActivateContext $activateContext): void
{
parent::activate($activateContext);
/* @var $dataService DataService */
$dataService = $this->container->get(DataService::class);
$dataService->install(self::NAME);
}
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext);
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->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 (array_reverse(self::SHOPWARE_TABLES) as $table) {
$sql = sprintf("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;
}
}
}
$sql = "DELETE FROM `snippet` WHERE `translation_key` LIKE 'customFields.moorl_fb_%';";
$connection->executeStatement($sql);
}
}