custom/plugins/MoorlProductPromo/src/MoorlProductPromo.php line 11

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlProductPromo;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\Migration\InheritanceUpdaterTrait;
  5. use Shopware\Core\Framework\Plugin;
  6. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  7. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  8. class MoorlProductPromo extends Plugin
  9. {
  10.     use InheritanceUpdaterTrait;
  11.     public const NAME 'MoorlProductPromo';
  12.     public const DATA_CREATED_AT '2003-03-03 03:13:04.000';
  13.     public const CMS_PAGE 'moorl_pp';
  14.     public const CMS_PAGE_ID 'ab1da2eddd6886c1fc72ea0f3f948f35';
  15.     public const PLUGIN_TABLES = [
  16.         'moorl_pp',
  17.         'moorl_pp_translation',
  18.         'moorl_pp_promotion',
  19.         'moorl_pp_promotion_translation',
  20.         'moorl_pp_stock',
  21.         'moorl_pp_promotion_sales_channel',
  22.         'moorl_pp_promotion_customer',
  23.         'moorl_pp_promotion_customer_group'
  24.     ];
  25.     public const SHOPWARE_TABLES = [
  26.         'cms_page',
  27.         'cms_page_translation',
  28.         'cms_section',
  29.         'cms_block',
  30.         'category',
  31.         'category_translation',
  32.         'product',
  33.         'product_translation',
  34.         'product_category',
  35.         'product_visibility',
  36.         'moorl_sorting'
  37.     ];
  38.     public const INHERITANCES = [
  39.         'product' => ['ppPromotions''ppStocks'],
  40.         'sales_channel' => ['ppPromotions'],
  41.         'order_line_item' => ['ppStock'],
  42.         'customer_group' => ['ppPromotions'],
  43.         'customer' => ['ppPromotions'],
  44.         'media' => ['ppPromotion']
  45.     ];
  46.     public function activate(ActivateContext $activateContext): void
  47.     {
  48.         parent::activate($activateContext);
  49.         $connection $this->container->get(Connection::class);
  50.         foreach (self::INHERITANCES as $table => $propertyNames) {
  51.             foreach ($propertyNames as $propertyName) {
  52.                 try {
  53.                     $this->updateInheritance($connection$table$propertyName);
  54.                 } catch (\Exception $exception) {
  55.                     continue;
  56.                 }
  57.             }
  58.         }
  59.     }
  60.     public function uninstall(UninstallContext $uninstallContext): void
  61.     {
  62.         parent::uninstall($uninstallContext);
  63.         if ($uninstallContext->keepUserData()) {
  64.             return;
  65.         }
  66.         $this->uninstallTrait();
  67.     }
  68.     private function uninstallTrait(): void
  69.     {
  70.         $connection $this->container->get(Connection::class);
  71.         foreach (self::PLUGIN_TABLES as $table) {
  72.             $sql sprintf('SET FOREIGN_KEY_CHECKS=0; DROP TABLE IF EXISTS `%s`;'$table);
  73.             $connection->executeStatement($sql);
  74.         }
  75.         foreach (array_reverse(self::SHOPWARE_TABLES) as $table) {
  76.             $sql sprintf("SET FOREIGN_KEY_CHECKS=0; DELETE FROM `%s` WHERE `created_at` = '%s';"$tableself::DATA_CREATED_AT);
  77.             try {
  78.                 $connection->executeStatement($sql);
  79.             } catch (\Exception $exception) {
  80.                 continue;
  81.             }
  82.         }
  83.         foreach (self::INHERITANCES as $table => $propertyNames) {
  84.             foreach ($propertyNames as $propertyName) {
  85.                 $sql sprintf("ALTER TABLE `%s` DROP `%s`;"$table$propertyName);
  86.                 try {
  87.                     $connection->executeStatement($sql);
  88.                 } catch (\Exception $exception) {
  89.                     continue;
  90.                 }
  91.             }
  92.         }
  93.     }
  94. }