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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CmsPreConfig;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Content\Media\Aggregate\MediaDefaultFolder\MediaDefaultFolderEntity;
  5. use Shopware\Core\Content\Media\Aggregate\MediaFolder\MediaFolderEntity;
  6. use Shopware\Core\Defaults;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  12. use Shopware\Core\Framework\Plugin;
  13. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  14. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  15. class AcrisCmsPreConfigCS extends Plugin
  16. {
  17.     const DEFAULT_MEDIA_FOLDER_NAME "Preconfigured cms elements";
  18.     const DEFAULT_MEDIA_FOLDER_CUSTOM_FIELD 'acrisPreConCmsElementsFolder';
  19.     public function uninstall(UninstallContext $context): void
  20.     {
  21.         if ($context->keepUserData()) {
  22.             return;
  23.         }
  24.         $this->cleanupDatabase();
  25.         $this->removeMediaUploadFolder($context->getContext());
  26.     }
  27.     private function removeMediaUploadFolder(Context $context): void
  28.     {
  29.         $mediaFolderRepository $this->container->get('media_folder.repository');
  30.         $defaultMediaFolderRepository $this->container->get('media_default_folder.repository');
  31.         $defaultMediaUploadFolder $this->getDefaultMediaUploadFolder($mediaFolderRepository$context);
  32.         $defaultMediaDefaultUploadFolder $this->getDefaultMediaDefaultUploadFolder($defaultMediaFolderRepository$context);
  33.         if($defaultMediaUploadFolder instanceof MediaFolderEntity) {
  34.             if($defaultMediaUploadFolder->getMedia() && $defaultMediaUploadFolder->getMedia()->count() > 0) {
  35.                 return;
  36.             }
  37.             if (!$defaultMediaDefaultUploadFolder instanceof MediaDefaultFolderEntity) {
  38.                 return;
  39.             }
  40.             $defaultMediaUploadFolderConfiguration $defaultMediaUploadFolder->getConfiguration();
  41.             $deleteConfigurationId null;
  42.             if($defaultMediaUploadFolderConfiguration && $defaultMediaUploadFolderConfiguration->getMediaFolders()) {
  43.                 if($defaultMediaUploadFolderConfiguration->getMediaFolders()->count() < 2) {
  44.                     $deleteConfigurationId $defaultMediaUploadFolderConfiguration->getId();
  45.                 }
  46.             }
  47.             $mediaFolderRepository->delete([['id' => $defaultMediaUploadFolder->getId()]], $context);
  48.             $defaultMediaFolderRepository->delete([['id' => $defaultMediaDefaultUploadFolder->getId()]], $context);
  49.             if($deleteConfigurationId !== null) {
  50.                 $this->container->get('media_folder_configuration.repository')->delete([['id' => $deleteConfigurationId]], $context);
  51.             }
  52.         }
  53.     }
  54.     private function getDefaultMediaUploadFolder(EntityRepositoryInterface $mediaFolderRepositoryContext $context): ?MediaFolderEntity
  55.     {
  56.         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();
  57.     }
  58.     private function getDefaultMediaDefaultUploadFolder(EntityRepositoryInterface $mediaDefaultFolderRepositoryContext $context): ?MediaDefaultFolderEntity
  59.     {
  60.         return $mediaDefaultFolderRepository->search((new Criteria())->addAssociation('folder')->addFilter(new EqualsFilter('customFields.'.self::DEFAULT_MEDIA_FOLDER_CUSTOM_FIELD'true')), $context)->first();
  61.     }
  62.     private function cleanupDatabase(): void
  63.     {
  64.         $connection $this->container->get(Connection::class);
  65.         $connection->executeStatement('DROP TABLE IF EXISTS acris_config_slots_translation');
  66.         $connection->executeStatement('DROP TABLE IF EXISTS acris_config_blocks');
  67.         $connection->executeStatement('DROP TABLE IF EXISTS acris_config_block_group');
  68.         $connection->executeStatement('DROP TABLE IF EXISTS acris_config_slots');
  69.     }
  70. }