custom/plugins/RhiemUserProducts/src/RhiemUserProducts.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Rhiem\RhiemUserProducts;
  4. use Doctrine\DBAL\Connection;
  5. use Rhiem\RhiemUserProducts\Entities\ProductGroup\ProductGroupCollection;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  11. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  12. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  14. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  15. class RhiemUserProducts extends Plugin
  16. {
  17.     public const FALLBACK_PRODUCT_GROUP_ID "0502913fa0a54ee7b24a4128fb2c62bb";
  18.     public function install(InstallContext $installContext): void
  19.     {
  20.         parent::install($installContext);
  21.     }
  22.     public function uninstall(UninstallContext $uninstallContext): void
  23.     {
  24.         parent::uninstall($uninstallContext);
  25.         if ($uninstallContext->keepUserData()) {
  26.             return;
  27.         }
  28.         /**
  29.          * @var Connection $connection
  30.          */
  31.         $connection $this->container->get(Connection::class);
  32.         $connection->executeStatement('ALTER TABLE customer DROP FOREIGN KEY `fk.customer.rhiem_product_group_id`');
  33.         $connection->executeStatement('ALTER TABLE customer DROP INDEX `fk.customer.rhiem_product_group_id`');
  34.         $connection->executeStatement('DROP TABLE IF EXISTS `rhiem_userproducts_productgroup_product`');
  35.         $connection->executeStatement('DROP TABLE IF EXISTS `rhiem_userproducts_productgroup_customer`');
  36.         $connection->executeStatement('DROP TABLE IF EXISTS `rhiem_userproducts_productgroup`');
  37.         $connection->executeStatement('ALTER TABLE `customer` DROP COLUMN `rhiemProductGroup`');
  38.         $connection->executeStatement('ALTER TABLE `customer` DROP COLUMN `rhiem_product_group_id`');
  39.         $connection->executeStatement('ALTER TABLE `product` DROP COLUMN `product_groups`');
  40.     }
  41.     public function activate(ActivateContext $activateContext): void
  42.     {
  43.         $this->createFallbackProductGroup();
  44.     }
  45.     public function deactivate(DeactivateContext $deactivateContext): void
  46.     {
  47.         parent::deactivate($deactivateContext);
  48.     }
  49.     public function createFallbackProductGroup(): void
  50.     {
  51.         /** @var EntityRepositoryInterface $productGroupRepository */
  52.         $productGroupRepository $this->container->get('rhiem_userproducts_productgroup.repository');
  53.         /**
  54.          * @var ProductGroupCollection $result
  55.          */
  56.         $result $productGroupRepository->search(
  57.             new Criteria([self::FALLBACK_PRODUCT_GROUP_ID]),
  58.             Context::createDefaultContext()
  59.         );
  60.         if ($result->getElements()) {
  61.             return;
  62.         }
  63.         $productGroupRepository->upsert(
  64.             [
  65.                 [
  66.                     'id' => self::FALLBACK_PRODUCT_GROUP_ID,
  67.                     'name' => 'Fallback',
  68.                     'groupMode' => 'blacklist',
  69.                     'active' => true,
  70.                     'fallback' => true
  71.                 ]
  72.             ],
  73.             Context::createDefaultContext()
  74.         );
  75.     }
  76.     public function postUpdate(UpdateContext $updateContext): void
  77.     {
  78.         if (version_compare($updateContext->getCurrentPluginVersion(), '1.0.7''<')) {
  79.             /**
  80.              * @var Connection $connection
  81.              */
  82.             $connection $this->container->get(Connection::class);
  83.             try {
  84.                 $connection->executeStatement('ALTER TABLE customer DROP FOREIGN KEY `fk.customer.rhiem_product_group_id`');
  85.             } catch (\Exception $e) {
  86.             }
  87.             $sql "ALTER TABLE customer
  88.   ADD CONSTRAINT `fk.customer.rhiem_product_group_id` FOREIGN KEY (`rhiem_product_group_id`)
  89.   REFERENCES `rhiem_userproducts_productgroup` (`id`) ON DELETE SET NULL ON UPDATE CASCADE";
  90.             /**
  91.              * @var Connection $connection
  92.              */
  93.             $connection->executeStatement($sql);
  94.         }
  95.         parent::postUpdate($updateContext);
  96.     }
  97. }