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