custom/plugins/MoorlFormBuilder/src/MoorlFormBuilder.php line 12

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlFormBuilder;
  3. use Doctrine\DBAL\Connection;
  4. use MoorlFoundation\Core\Service\DataService;
  5. use Shopware\Core\Framework\Plugin;
  6. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  7. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  8. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  9. class MoorlFormBuilder extends Plugin
  10. {
  11.     public const NAME 'MoorlFormBuilder';
  12.     public const DATA_CREATED_AT '2003-03-03 03:02:01.000';
  13.     public const PLUGIN_TABLES = [
  14.         'moorl_form_rule',
  15.         'moorl_form_product',
  16.         'moorl_form',
  17.         'moorl_form_history'
  18.     ];
  19.     public const SHOPWARE_TABLES = [
  20.         'cms_page',
  21.         'cms_page_translation',
  22.         'cms_section',
  23.         'cms_block',
  24.         'category',
  25.         'category_translation',
  26.         'mail_template_type',
  27.         'mail_template_type_translation',
  28.         'mail_template',
  29.         'mail_template_translation',
  30.         'event_action',
  31.         'custom_field_set'
  32.     ];
  33.     public const MAIL_TEMPLATE_MAIL_SEND_ACTION 'moorl_form_builder.action.mail.send';
  34.     public function activate(ActivateContext $activateContext): void
  35.     {
  36.         parent::activate($activateContext); // TODO: Change the autogenerated stub
  37.         try {
  38.             /* @var $dataService DataService */
  39.             $dataService $this->container->get(DataService::class);
  40.             $dataService->install(self::NAME);
  41.         } catch (\Exception $exception) {
  42.         }
  43.     }
  44.     public function update(UpdateContext $updateContext): void
  45.     {
  46.         parent::update($updateContext); // TODO: Change the autogenerated stub
  47.         try {
  48.             /* @var $dataService DataService */
  49.             $dataService $this->container->get(DataService::class);
  50.             $dataService->install(self::NAME);
  51.         } catch (\Exception $exception) {
  52.         }
  53.     }
  54.     public function uninstall(UninstallContext $context): void
  55.     {
  56.         parent::uninstall($context);
  57.         if ($context->keepUserData()) {
  58.             return;
  59.         }
  60.         $this->removePluginData();
  61.         $this->dropTables();
  62.     }
  63.     private function removePluginData(): void
  64.     {
  65.         $connection $this->container->get(Connection::class);
  66.         foreach (array_reverse(self::SHOPWARE_TABLES) as $table) {
  67.             $sql sprintf("SET FOREIGN_KEY_CHECKS=0; DELETE FROM `%s` WHERE `created_at` = '%s';"$tableself::DATA_CREATED_AT);
  68.             try {
  69.                 $connection->executeUpdate($sql);
  70.             } catch (\Exception $exception) {
  71.                 continue;
  72.             }
  73.         }
  74.         try {
  75.             $connection->executeUpdate('ALTER TABLE `product` DROP COLUMN `forms`');
  76.         } catch (\Exception $exception) {
  77.         }
  78.     }
  79.     private function dropTables(): void
  80.     {
  81.         $connection $this->container->get(Connection::class);
  82.         foreach (self::PLUGIN_TABLES as $table) {
  83.             $sql sprintf('SET FOREIGN_KEY_CHECKS=0; DROP TABLE IF EXISTS `%s`;'$table);
  84.             $connection->executeUpdate($sql);
  85.         }
  86.     }
  87. }