<?php
declare(strict_types=1);
namespace Wbfk\ProductDownloads;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Wbfk\ProductDownloads\Entity\Download\ProductDownloadDefinition;
class WbfkProductDownloads extends Plugin
{
public const DOWNLOAD_FOLDER_NAME = 'Product Files';
public const DOWNLOAD_FOLDER_ID = '4074b33d34af4cb0abbced336854a0ff';
public const DOWNLOAD_DEFAULT_FOLDER_ID = '8e8c4e2b909648ddbd5833518db7accf';
public const DOWNLOAD_FOLDER_CONFIGURATION_ID = '66b8fe55d1794441b6a72ddc9bcbcdd9';
public function install(InstallContext $installContext): void
{
$this->createMediaFolder($installContext->getContext());
}
public function update(UpdateContext $updateContext): void
{
$this->createMediaFolder($updateContext->getContext());
}
public function createMediaFolder(Context $context): void
{
$mediaDefaultFolderRepository = $this->container->get('media_default_folder.repository');
$mediaDefaultFolderRepository->upsert([
[
'id' => self::DOWNLOAD_DEFAULT_FOLDER_ID,
'entity' => ProductDownloadDefinition::ENTITY_NAME,
'associationFields' => ['wbfkProductDownload'],
'folder' => [
'id' => self::DOWNLOAD_FOLDER_ID,
'name' => self::DOWNLOAD_FOLDER_NAME,
'useParentConfiguration' => false,
'configuration' => [
'id' => self::DOWNLOAD_FOLDER_CONFIGURATION_ID,
'createThumbnails' => false,
'noAssociation' => null
]
]
]
], $context);
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
// todo: Enable after release, maybe. This is a dangerous thing...
return;
$this->removeMediaFolder($uninstallContext->getContext());
$this->removeMigrations();
/** @var Connection $connection */
$connection = $this->container->get(Connection::class);
try {
$connection->executeStatement('DROP TABLE IF EXISTS `wbfk_product_download`');
} catch (Exception $e) {
}
}
private function removeMediaFolder(Context $context): void
{
$mediaDefaultFolderRepository = $this->container->get('media_default_folder.repository');
$mediaFolderRepository = $this->container->get('media_folder.repository');
$mediaFolderConfigurationRepository = $this->container->get('media_folder_configuration.repository');
$mediaDefaultFolderRepository->delete([['id' => self::DOWNLOAD_DEFAULT_FOLDER_ID]], $context);
$mediaFolderRepository->delete([['id' => self::DOWNLOAD_FOLDER_ID]], $context);
$mediaFolderConfigurationRepository->delete([['id' => self::DOWNLOAD_FOLDER_CONFIGURATION_ID]],
$context);
}
}