custom/plugins/SasBlogModule/src/SasBlogModule.php line 21

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Sas\BlogModule;
  3. use Doctrine\DBAL\Connection;
  4. use Sas\BlogModule\Content\Blog\BlogEntriesDefinition;
  5. use Sas\BlogModule\Util\Lifecycle;
  6. use Sas\BlogModule\Util\Update;
  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\EqualsAnyFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  13. use Shopware\Core\Framework\Plugin;
  14. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  15. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  16. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  17. use Shopware\Core\System\SystemConfig\SystemConfigService;
  18. class SasBlogModule extends Plugin
  19. {
  20.     public const ANONYMOUS_AUTHOR_ID '64f4c60194634128b9b85d9299797c45';
  21.     public function install(InstallContext $installContext): void
  22.     {
  23.         parent::install($installContext);
  24.         $this->createBlogMediaFolder($installContext->getContext());
  25.         $this->getLifeCycle()->install($installContext->getContext());
  26.     }
  27.     public function uninstall(UninstallContext $context): void
  28.     {
  29.         parent::uninstall($context);
  30.         if ($context->keepUserData()) {
  31.             return;
  32.         }
  33.         /*
  34.          * We need to uninstall our default media folder,
  35.          * the media folder and the thumbnail sizes.
  36.          * However, we have to clean this up within a next update :)
  37.          */
  38.         $this->deleteMediaFolder($context->getContext());
  39.         $this->deleteDefaultMediaFolder($context->getContext());
  40.         $this->deleteSeoUrlTemplate($context->getContext());
  41.         $this->checkForThumbnailSizes($context->getContext());
  42.         /**
  43.          * And of course we need to drop our tables
  44.          */
  45.         $connection $this->container->get(Connection::class);
  46.         $connection->executeStatement('SET FOREIGN_KEY_CHECKS=0;');
  47.         $connection->executeStatement('DROP TABLE IF EXISTS `sas_blog_entries`');
  48.         $connection->executeStatement('DROP TABLE IF EXISTS `sas_blog_entries_translation`');
  49.         $connection->executeStatement('DROP TABLE IF EXISTS `sas_blog_blog_category`');
  50.         $connection->executeStatement('DROP TABLE IF EXISTS `sas_blog_category_translation`');
  51.         $connection->executeStatement('DROP TABLE IF EXISTS `sas_blog_category`');
  52.         $connection->executeStatement('DROP TABLE IF EXISTS `sas_blog_author_translation`');
  53.         $connection->executeStatement('DROP TABLE IF EXISTS `sas_blog_author`');
  54.         /** @var EntityRepositoryInterface $cmsBlockRepo */
  55.         $cmsBlockRepo $this->container->get('cms_block.repository');
  56.         $context Context::createDefaultContext();
  57.         $criteria = new Criteria();
  58.         $criteria->addFilter(new EqualsAnyFilter('type', ['blog-detail''blog-listing']));
  59.         $cmsBlocks $cmsBlockRepo->searchIds($criteria$context);
  60.         $cmsBlockRepo->delete(array_values($cmsBlocks->getData()), $context);
  61.         $connection->executeQuery('SET FOREIGN_KEY_CHECKS=1;');
  62.     }
  63.     public function update(UpdateContext $updateContext): void
  64.     {
  65.         parent::update($updateContext);
  66.         (new Update())->update($this->container$updateContext);
  67.         if (version_compare($updateContext->getCurrentPluginVersion(), '1.1.0''<')) {
  68.             $this->createBlogMediaFolder($updateContext->getContext());
  69.         }
  70.     }
  71.     /**
  72.      * We need to create a folder for the blog media with it's,
  73.      * own configuration to generate thumbnails for the teaser image.
  74.      */
  75.     public function createBlogMediaFolder(Context $context): void
  76.     {
  77.         $this->deleteDefaultMediaFolder($context);
  78.         $this->checkForThumbnailSizes($context);
  79.         /** @var EntityRepositoryInterface $mediaFolderRepository */
  80.         $mediaFolderRepository $this->container->get('media_default_folder.repository');
  81.         $mediaFolderRepository->create([
  82.             [
  83.                 'entity' => BlogEntriesDefinition::ENTITY_NAME,
  84.                 'associationFields' => ['media'],
  85.                 'folder' => [
  86.                     'name' => 'Blog Images',
  87.                     'useParentConfiguration' => false,
  88.                     'configuration' => [
  89.                         'createThumbnails' => true,
  90.                         'mediaThumbnailSizes' => [
  91.                             [
  92.                                 'width' => 650,
  93.                                 'height' => 330,
  94.                             ],
  95.                         ],
  96.                     ],
  97.                 ],
  98.             ],
  99.         ], $context);
  100.     }
  101.     private function deleteDefaultMediaFolder(Context $context): void
  102.     {
  103.         $criteria = new Criteria();
  104.         $criteria->addFilter(
  105.             new EqualsAnyFilter('entity', [
  106.                 BlogEntriesDefinition::ENTITY_NAME,
  107.             ])
  108.         );
  109.         /** @var EntityRepositoryInterface $mediaFolderRepository */
  110.         $mediaFolderRepository $this->container->get('media_default_folder.repository');
  111.         $mediaFolderIds $mediaFolderRepository->searchIds($criteria$context)->getIds();
  112.         if (!empty($mediaFolderIds)) {
  113.             $ids array_map(static function ($id) {
  114.                 return ['id' => $id];
  115.             }, $mediaFolderIds);
  116.             $mediaFolderRepository->delete($ids$context);
  117.         }
  118.     }
  119.     private function deleteMediaFolder(Context $context): void
  120.     {
  121.         $criteria = new Criteria();
  122.         $criteria->addFilter(
  123.             new EqualsFilter('name''Blog Images')
  124.         );
  125.         /** @var EntityRepositoryInterface $mediaFolderRepository */
  126.         $mediaFolderRepository $this->container->get('media_folder.repository');
  127.         $mediaFolderRepository->search($criteria$context);
  128.         $mediaFolderIds $mediaFolderRepository->searchIds($criteria$context)->getIds();
  129.         if (!empty($mediaFolderIds)) {
  130.             $ids array_map(static function ($id) {
  131.                 return ['id' => $id];
  132.             }, $mediaFolderIds);
  133.             $mediaFolderRepository->delete($ids$context);
  134.         }
  135.     }
  136.     private function deleteSeoUrlTemplate(Context $context): void
  137.     {
  138.         $criteria = new Criteria();
  139.         $criteria->addFilter(
  140.             new EqualsFilter('entityName''sas_blog_entries')
  141.         );
  142.         /** @var EntityRepositoryInterface $seoUrlTemplateRepository */
  143.         $seoUrlTemplateRepository $this->container->get('seo_url_template.repository');
  144.         $seoUrlTemplateRepository->search($criteria$context);
  145.         $seoUrlTemplateIds $seoUrlTemplateRepository->searchIds($criteria$context)->getIds();
  146.         if (!empty($seoUrlTemplateIds)) {
  147.             $ids array_map(static function ($id) {
  148.                 return ['id' => $id];
  149.             }, $seoUrlTemplateIds);
  150.             $seoUrlTemplateRepository->delete($ids$context);
  151.         }
  152.     }
  153.     private function checkForThumbnailSizes(Context $context): void
  154.     {
  155.         $criteria = new Criteria();
  156.         $criteria->addFilter(
  157.             new MultiFilter(
  158.                 MultiFilter::CONNECTION_AND,
  159.                 [
  160.                     new EqualsFilter('width'650),
  161.                     new EqualsFilter('height'330),
  162.                 ]
  163.             )
  164.         );
  165.         /** @var EntityRepositoryInterface $thumbnailSizeRepository */
  166.         $thumbnailSizeRepository $this->container->get('media_thumbnail_size.repository');
  167.         $thumbnailIds $thumbnailSizeRepository->searchIds($criteria$context)->getIds();
  168.         if (!empty($thumbnailIds)) {
  169.             $ids array_map(static function ($id) {
  170.                 return ['id' => $id];
  171.             }, $thumbnailIds);
  172.             $thumbnailSizeRepository->delete($ids$context);
  173.         }
  174.     }
  175.     private function getLifeCycle(): Lifecycle
  176.     {
  177.         /** @var SystemConfigService $systemConfig */
  178.         $systemConfig $this->container->get(SystemConfigService::class);
  179.         /** @var EntityRepositoryInterface $cmsPageRepository */
  180.         $cmsPageRepository $this->container->get('cms_page.repository');
  181.         return new Lifecycle(
  182.             $systemConfig,
  183.             $cmsPageRepository
  184.         );
  185.     }
  186. }