custom/plugins/NetzpBlog6/src/NetzpBlog6.php line 13

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace NetzpBlog6;
  3. use NetzpBlog6\Core\Content\Blog\BlogDefinition;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\Plugin;
  6. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  7. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  8. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  9. use Doctrine\DBAL\Connection;
  10. class NetzpBlog6 extends Plugin
  11. {
  12.     public const BLOG_MEDIAFOLDER_NAME 'Blog Media';
  13.     public const BLOG_MEDIAFOLDER_ID '59D4F5B90E944D44B997ED0A60804034';
  14.     public function install(InstallContext $context): void
  15.     {
  16.         $this->createMediaFolder($context->getContext());
  17.     }
  18.     public function update(UpdateContext $context): void
  19.     {
  20.         if (version_compare($context->getCurrentPluginVersion(), '1.1.0''<')) {
  21.             $this->createMediaFolder($context->getContext());
  22.         }
  23.         // NE 29.11.2021 - set `associationFields` to 'netzpBlogMedia' to ensure correct media deletion via bin/console media:delete-unused
  24.         $this->fixMediaFolder($context->getContext());
  25.     }
  26.     public function uninstall(UninstallContext $context): void
  27.     {
  28.         parent::uninstall($context);
  29.         if ($context->keepUserData()) {
  30.             return;
  31.         }
  32.         $this->removeMediaFolder($context->getContext());
  33.         $this->removeMigrations();
  34.         $connection $this->container->get(Connection::class);
  35.         $connection->executeStatement('DROP TABLE IF EXISTS `s_plugin_netzp_blog_categories`');
  36.         $connection->executeStatement('DROP TABLE IF EXISTS `s_plugin_netzp_blog_product`');
  37.         $connection->executeStatement('DROP TABLE IF EXISTS `s_plugin_netzp_blog_category_translation`');
  38.         $connection->executeStatement('DROP TABLE IF EXISTS `s_plugin_netzp_blog_category`');
  39.         $connection->executeStatement('DROP TABLE IF EXISTS `s_plugin_netzp_blog_author_translation`');
  40.         $connection->executeStatement('DROP TABLE IF EXISTS `s_plugin_netzp_blog_author`');
  41.         $connection->executeStatement('DROP TABLE IF EXISTS `s_plugin_netzp_blog_tag`');
  42.         $connection->executeStatement('DROP TABLE IF EXISTS `s_plugin_netzp_blog_item_translation`');
  43.         $connection->executeStatement('DROP TABLE IF EXISTS `s_plugin_netzp_blog_item`');
  44.         $connection->executeStatement('DROP TABLE IF EXISTS `s_plugin_netzp_blog_media`');
  45.         $connection->executeStatement('DROP TABLE IF EXISTS `s_plugin_netzp_blog_translation`');
  46.         $connection->executeStatement('DROP TABLE IF EXISTS `s_plugin_netzp_blog`');
  47.         try {
  48.             $connection->executeStatement('ALTER TABLE `product` DROP COLUMN `blogs`');
  49.         }
  50.         catch (\Exception $ex) { }
  51.         try {
  52.             $connection->executeStatement('DELETE FROM `seo_url_template` WHERE route_name = "frontend.blog.post"');
  53.         }
  54.         catch (\Exception $ex) {
  55.             //
  56.         }
  57.     }
  58.     private function removeMediaFolder(Context $context): void
  59.     {
  60.         $connection $this->container->get(Connection::class);
  61.         try {
  62.             $defaultFolderId $connection->fetchColumn('SELECT HEX(id) FROM media_default_folder WHERE HEX(id) = ?', [
  63.                 strtolower(self::BLOG_MEDIAFOLDER_ID)
  64.             ]);
  65.             if( ! $defaultFolderId) return;
  66.             $defaultConfigurationId $connection->fetchColumn('SELECT HEX(media_folder_configuration_id) FROM media_folder WHERE HEX(default_folder_id) = ?', [
  67.                 $defaultFolderId
  68.             ]);
  69.             if( ! $defaultConfigurationId) return;
  70.             $connection->executeStatement('DELETE FROM `media_folder_configuration` WHERE HEX(id) = ?', [
  71.                 $defaultConfigurationId
  72.             ]);
  73.             $connection->executeStatement('DELETE FROM `media_folder` WHERE HEX(default_folder_id) = ?', [
  74.                 $defaultFolderId
  75.             ]);
  76.             $connection->executeStatement('DELETE FROM `media_default_folder` WHERE HEX(id) = ?', [
  77.                 strtolower(self::BLOG_MEDIAFOLDER_ID)
  78.             ]);
  79.         }
  80.         catch (\Exception $ex) {
  81.             //
  82.         }
  83.     }
  84.     public function createMediaFolder(Context $context): void
  85.     {
  86.         try {
  87.             $connection $this->container->get(Connection::class);
  88.             $thumbnailIds $connection->fetchAll('SELECT LOWER(HEX(id)) AS id from `media_thumbnail_size` WHERE width in (400, 800, 1920)');
  89.             $repo $this->container->get('media_default_folder.repository');
  90.             $repo->upsert([
  91.                 [
  92.                     'id'                => strtolower(self::BLOG_MEDIAFOLDER_ID),
  93.                     'entity'            => BlogDefinition::ENTITY_NAME,
  94.                     'associationFields' => ['netzpBlogMedia'],
  95.                     'folder'            => [
  96.                         'name'                   => self::BLOG_MEDIAFOLDER_NAME,
  97.                         'useParentConfiguration' => false,
  98.                         'configuration'          => [
  99.                             'createThumbnails'    => true,
  100.                             'mediaThumbnailSizes' => $thumbnailIds
  101.                         ]
  102.                     ]
  103.                 ]
  104.             ], $context);
  105.         }
  106.         catch (\Exception $ex) {
  107.             //
  108.         }
  109.     }
  110.     public function fixMediaFolder(Context $context): void
  111.     {
  112.         try {
  113.             $repo $this->container->get('media_default_folder.repository');
  114.             $repo->upsert([
  115.                 [
  116.                     'id'                => strtolower(self::BLOG_MEDIAFOLDER_ID),
  117.                     'associationFields' => ['netzpBlogMedia']
  118.                 ]
  119.             ], $context);
  120.         }
  121.         catch (\Exception $ex) {
  122.             //
  123.         }
  124.     }
  125. }