custom/plugins/WbfkProductDownloads/src/WbfkProductDownloads.php line 16

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wbfk\ProductDownloads;
  4. use Doctrine\DBAL\Connection;
  5. use Doctrine\DBAL\Exception;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\Plugin;
  8. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  9. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  11. use Wbfk\ProductDownloads\Entity\Download\ProductDownloadDefinition;
  12. class WbfkProductDownloads extends Plugin
  13. {
  14.     public const DOWNLOAD_FOLDER_NAME 'Product Files';
  15.     public const DOWNLOAD_FOLDER_ID '4074b33d34af4cb0abbced336854a0ff';
  16.     public const DOWNLOAD_DEFAULT_FOLDER_ID '8e8c4e2b909648ddbd5833518db7accf';
  17.     public const DOWNLOAD_FOLDER_CONFIGURATION_ID '66b8fe55d1794441b6a72ddc9bcbcdd9';
  18.     public function install(InstallContext $installContext): void
  19.     {
  20.         $this->createMediaFolder($installContext->getContext());
  21.     }
  22.     public function update(UpdateContext $updateContext): void
  23.     {
  24.         $this->createMediaFolder($updateContext->getContext());
  25.     }
  26.     public function createMediaFolder(Context $context): void
  27.     {
  28.         $mediaDefaultFolderRepository $this->container->get('media_default_folder.repository');
  29.         $mediaDefaultFolderRepository->upsert([
  30.             [
  31.                 'id' => self::DOWNLOAD_DEFAULT_FOLDER_ID,
  32.                 'entity' => ProductDownloadDefinition::ENTITY_NAME,
  33.                 'associationFields' => ['wbfkProductDownload'],
  34.                 'folder' => [
  35.                     'id' => self::DOWNLOAD_FOLDER_ID,
  36.                     'name' => self::DOWNLOAD_FOLDER_NAME,
  37.                     'useParentConfiguration' => false,
  38.                     'configuration' => [
  39.                         'id' => self::DOWNLOAD_FOLDER_CONFIGURATION_ID,
  40.                         'createThumbnails' => false,
  41.                         'noAssociation' => null
  42.                     ]
  43.                 ]
  44.             ]
  45.         ], $context);
  46.     }
  47.     public function uninstall(UninstallContext $uninstallContext): void
  48.     {
  49.         parent::uninstall($uninstallContext);
  50.         if ($uninstallContext->keepUserData()) {
  51.             return;
  52.         }
  53.         // todo: Enable after release, maybe. This is a dangerous thing...
  54.         return;
  55.         $this->removeMediaFolder($uninstallContext->getContext());
  56.         $this->removeMigrations();
  57.         /** @var Connection $connection */
  58.         $connection $this->container->get(Connection::class);
  59.         try {
  60.             $connection->executeStatement('DROP TABLE IF EXISTS `wbfk_product_download`');
  61.         } catch (Exception $e) {
  62.         }
  63.     }
  64.     private function removeMediaFolder(Context $context): void
  65.     {
  66.         $mediaDefaultFolderRepository $this->container->get('media_default_folder.repository');
  67.         $mediaFolderRepository $this->container->get('media_folder.repository');
  68.         $mediaFolderConfigurationRepository $this->container->get('media_folder_configuration.repository');
  69.         $mediaDefaultFolderRepository->delete([['id' => self::DOWNLOAD_DEFAULT_FOLDER_ID]], $context);
  70.         $mediaFolderRepository->delete([['id' => self::DOWNLOAD_FOLDER_ID]], $context);
  71.         $mediaFolderConfigurationRepository->delete([['id' => self::DOWNLOAD_FOLDER_CONFIGURATION_ID]],
  72.             $context);
  73.     }
  74. }