<?php declare(strict_types=1);
namespace Acris\CmsPreConfig;
use Doctrine\DBAL\Connection;
use Shopware\Core\Content\Media\Aggregate\MediaDefaultFolder\MediaDefaultFolderEntity;
use Shopware\Core\Content\Media\Aggregate\MediaFolder\MediaFolderEntity;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
class AcrisCmsPreConfigCS extends Plugin
{
const DEFAULT_MEDIA_FOLDER_NAME = "Preconfigured cms elements";
const DEFAULT_MEDIA_FOLDER_CUSTOM_FIELD = 'acrisPreConCmsElementsFolder';
public function uninstall(UninstallContext $context): void
{
if ($context->keepUserData()) {
return;
}
$this->cleanupDatabase();
$this->removeMediaUploadFolder($context->getContext());
}
private function removeMediaUploadFolder(Context $context): void
{
$mediaFolderRepository = $this->container->get('media_folder.repository');
$defaultMediaFolderRepository = $this->container->get('media_default_folder.repository');
$defaultMediaUploadFolder = $this->getDefaultMediaUploadFolder($mediaFolderRepository, $context);
$defaultMediaDefaultUploadFolder = $this->getDefaultMediaDefaultUploadFolder($defaultMediaFolderRepository, $context);
if($defaultMediaUploadFolder instanceof MediaFolderEntity) {
if($defaultMediaUploadFolder->getMedia() && $defaultMediaUploadFolder->getMedia()->count() > 0) {
return;
}
if (!$defaultMediaDefaultUploadFolder instanceof MediaDefaultFolderEntity) {
return;
}
$defaultMediaUploadFolderConfiguration = $defaultMediaUploadFolder->getConfiguration();
$deleteConfigurationId = null;
if($defaultMediaUploadFolderConfiguration && $defaultMediaUploadFolderConfiguration->getMediaFolders()) {
if($defaultMediaUploadFolderConfiguration->getMediaFolders()->count() < 2) {
$deleteConfigurationId = $defaultMediaUploadFolderConfiguration->getId();
}
}
$mediaFolderRepository->delete([['id' => $defaultMediaUploadFolder->getId()]], $context);
$defaultMediaFolderRepository->delete([['id' => $defaultMediaDefaultUploadFolder->getId()]], $context);
if($deleteConfigurationId !== null) {
$this->container->get('media_folder_configuration.repository')->delete([['id' => $deleteConfigurationId]], $context);
}
}
}
private function getDefaultMediaUploadFolder(EntityRepositoryInterface $mediaFolderRepository, Context $context): ?MediaFolderEntity
{
return $mediaFolderRepository->search((new Criteria())->addAssociation('media')->addAssociation('configuration')->addAssociation('configuration.mediaFolders')->addFilter(new EqualsFilter('customFields.'.self::DEFAULT_MEDIA_FOLDER_CUSTOM_FIELD, 'true')), $context)->first();
}
private function getDefaultMediaDefaultUploadFolder(EntityRepositoryInterface $mediaDefaultFolderRepository, Context $context): ?MediaDefaultFolderEntity
{
return $mediaDefaultFolderRepository->search((new Criteria())->addAssociation('folder')->addFilter(new EqualsFilter('customFields.'.self::DEFAULT_MEDIA_FOLDER_CUSTOM_FIELD, 'true')), $context)->first();
}
private function cleanupDatabase(): void
{
$connection = $this->container->get(Connection::class);
$connection->executeStatement('DROP TABLE IF EXISTS acris_config_slots_translation');
$connection->executeStatement('DROP TABLE IF EXISTS acris_config_blocks');
$connection->executeStatement('DROP TABLE IF EXISTS acris_config_block_group');
$connection->executeStatement('DROP TABLE IF EXISTS acris_config_slots');
}
}