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.         'moorl_form_appointment'
  19.     ];
  20.     public const SHOPWARE_TABLES = [
  21.         'cms_page',
  22.         'cms_page_translation',
  23.         'cms_section',
  24.         'cms_block',
  25.         'category',
  26.         'category_translation',
  27.         'mail_template_type',
  28.         'mail_template_type_translation',
  29.         'mail_template',
  30.         'mail_template_translation',
  31.         'event_action',
  32.         'custom_field_set'
  33.     ];
  34.     public const MAIL_TEMPLATE_MAIL_SEND_ACTION 'moorl_form_builder.action.mail.send';
  35.     public function activate(ActivateContext $activateContext): void
  36.     {
  37.         parent::activate($activateContext); // TODO: Change the autogenerated stub
  38.         try {
  39.             /* @var $dataService DataService */
  40.             $dataService $this->container->get(DataService::class);
  41.             $dataService->install(self::NAME);
  42.         } catch (\Exception $exception) {
  43.         }
  44.     }
  45.     public function update(UpdateContext $updateContext): void
  46.     {
  47.         parent::update($updateContext); // TODO: Change the autogenerated stub
  48.         try {
  49.             /* @var $dataService DataService */
  50.             $dataService $this->container->get(DataService::class);
  51.             $dataService->install(self::NAME);
  52.         } catch (\Exception $exception) {
  53.         }
  54.     }
  55.     public function uninstall(UninstallContext $context): void
  56.     {
  57.         parent::uninstall($context);
  58.         if ($context->keepUserData()) {
  59.             return;
  60.         }
  61.         $this->removePluginData();
  62.         $this->dropTables();
  63.     }
  64.     private function removePluginData(): void
  65.     {
  66.         $connection $this->container->get(Connection::class);
  67.         foreach (array_reverse(self::SHOPWARE_TABLES) as $table) {
  68.             $sql sprintf("SET FOREIGN_KEY_CHECKS=0; DELETE FROM `%s` WHERE `created_at` = '%s';"$tableself::DATA_CREATED_AT);
  69.             try {
  70.                 $connection->executeUpdate($sql);
  71.             } catch (\Exception $exception) {
  72.                 continue;
  73.             }
  74.         }
  75.         try {
  76.             $connection->executeUpdate('ALTER TABLE `product` DROP COLUMN `forms`');
  77.         } catch (\Exception $exception) {
  78.         }
  79.     }
  80.     private function dropTables(): void
  81.     {
  82.         $connection $this->container->get(Connection::class);
  83.         foreach (self::PLUGIN_TABLES as $table) {
  84.             $sql sprintf('SET FOREIGN_KEY_CHECKS=0; DROP TABLE IF EXISTS `%s`;'$table);
  85.             $connection->executeUpdate($sql);
  86.         }
  87.     }
  88. }