<?php declare(strict_types=1);
namespace RhiemExtendedRegistration;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
class RhiemExtendedRegistration extends Plugin
{
private array $tables = [
'rhiem_ar_so_translation',
'rhiem_ar_so',
'rhiem_ar_attribute_translation',
'rhiem_ar_attribute',
'rhiem_ar_group_translation',
'rhiem_ar_group',
];
/**
* @param InstallContext $installContext
*/
public function install(InstallContext $installContext): void
{
parent::install($installContext);
}
/**
* @param UpdateContext $updateContext
*/
public function update(UpdateContext $updateContext): void
{
$updateContext->setAutoMigrate(false);
$migrationCollection = $updateContext->getMigrationCollection();
if (\version_compare($updateContext->getCurrentPluginVersion(), '2.0.0', '<')) {
$migrationCollection->migrateInPlace(1623051960);
$migrationCollection->migrateDestructiveInPlace(1623051960);
}
if (\version_compare($updateContext->getCurrentPluginVersion(), '2.0.0', 'ge') &&
\version_compare($updateContext->getCurrentPluginVersion(), '2.0.2', '<')) {
$migrationCollection->migrateInPlace(1624023259);
$migrationCollection->migrateDestructiveInPlace(1624023259);
}
}
/**
* @param UninstallContext $uninstallContext
*
* @throws \Doctrine\DBAL\Exception
*/
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
/** @var Connection $connection */
$connection = $this->container->get(Connection::class);
$this->deleteCustomFields($connection);
foreach($this->tables as $tableName) {
$connection->executeStatement('DROP TABLE IF EXISTS `' . $tableName . '`');
}
}
/**
* @param Connection $connection
*
* @throws \Doctrine\DBAL\Exception
*/
private function deleteCustomFields(Connection $connection): void
{
$sql = "UPDATE customer
SET custom_fields = JSON_REMOVE(custom_fields, '$.Rhiem_Additional_Registration_Fields_Personal')
WHERE custom_fields IS NOT NULL";
$connection->executeStatement($sql);
}
}