custom/plugins/HTCTestimonials/src/HTCTestimonials.php line 14

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace HTC\Testimonials;
  3. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  4. use Shopware\Core\Framework\Plugin;
  5. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  6. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  7. use Shopware\Core\Framework\Uuid\Uuid;
  8. use Doctrine\DBAL\Connection;
  9. use Shopware\Core\Framework\Context;
  10. use HTC\Testimonials\Core\Content\Testimonials\TestimonialDefinition;
  11. class HTCTestimonials extends Plugin
  12. {
  13.     public const HTC_TESTIMONIAL_MEDIAFOLDER_NAME 'HTC Testimonial Media';
  14.     public function install(InstallContext $context): void
  15.     {
  16.         $this->createMediaThumbnailSize($context->getContext());
  17.         $this->createMediaFolder($context->getContext());
  18.     }
  19.     public function uninstall(UninstallContext $context): void
  20.     {
  21.         parent::uninstall($context);
  22.         if ($context->keepUserData()) {
  23.             return;
  24.         }
  25.         $connection $this->container->get(Connection::class);
  26.         $connection->executeUpdate('DROP TABLE IF EXISTS `htc_testimonial`');
  27.         $connection->delete('cms_block', ['type' => "htc-testimonial"]);
  28.         $connection->delete('cms_block', ['type' => "htc-cms-testimonial-list"]);
  29.         $connection->delete('cms_block', ['type' => "htc-testimonial-form"]);
  30.         $connection->delete('cms_block', ['type' => "htc-testimonial-grid"]);
  31.         $this->removeMediaFolder($context->getContext());
  32.     }
  33.     private function removeMediaFolder(Context $context): void
  34.     {
  35.         $connection $this->container->get(Connection::class);
  36.         try {
  37.             $defaultFolderId $connection->fetchColumn('SELECT HEX(id) FROM media_default_folder WHERE entity = ?', [
  38.                 TestimonialDefinition::ENTITY_NAME
  39.             ]);
  40.             if(!$defaultFolderId) return;
  41.             $defaultConfigurationId $connection->fetchColumn('SELECT HEX(media_folder_configuration_id) FROM media_folder WHERE HEX(default_folder_id) = ?', [
  42.                 $defaultFolderId
  43.             ]);
  44.             if(!$defaultConfigurationId) return;
  45.             $connection->executeUpdate('DELETE FROM `media_folder_configuration` WHERE HEX(id) = ?', [
  46.                 $defaultConfigurationId
  47.             ]);
  48.             $connection->executeUpdate('DELETE FROM `media_folder` WHERE HEX(default_folder_id) = ?', [
  49.                 $defaultFolderId
  50.             ]);
  51.             $connection->executeUpdate('DELETE FROM `media_default_folder` WHERE entity = ?', [
  52.                 TestimonialDefinition::ENTITY_NAME
  53.             ]);
  54.         } catch (\Exception $e) {
  55.             echo $e->getMessage();
  56.         }
  57.     }
  58.     private function createMediaThumbnailSize(Context $context): void
  59.     {
  60.         try {
  61.             $connection $this->container->get(Connection::class);
  62.             $mediaThumbnailSizeId Uuid::randomHex();
  63.             $repo $this->container->get('media_thumbnail_size.repository');
  64.             $thumbnailId $connection->fetchAll('SELECT LOWER(HEX(id)) AS id from `media_thumbnail_size` WHERE width in (170)');
  65.             if (!$thumbnailId) {
  66.                 $repo->upsert([
  67.                     [
  68.                         'id'               => $mediaThumbnailSizeId,
  69.                         'width'            => 170,
  70.                         'height'           => 170
  71.                     ]
  72.                 ], $context);
  73.             }
  74.             
  75.         } catch (\Exception $e) {
  76.             echo $e->getMessage();
  77.         }
  78.     }
  79.     private function createMediaFolder(Context $context): void
  80.     {
  81.         try {
  82.             $connection $this->container->get(Connection::class);
  83.             $mediaFolderId Uuid::randomHex();
  84.             $thumbnailIds $connection->fetchAll('SELECT LOWER(HEX(id)) AS id from `media_thumbnail_size` WHERE width in (170)');
  85.             $repo $this->container->get('media_default_folder.repository');
  86.             $repo->upsert([
  87.                 [
  88.                     'id'                => $mediaFolderId,
  89.                     'entity'            => TestimonialDefinition::ENTITY_NAME,
  90.                     'associationFields' => ['media'],
  91.                     'folder'            => [
  92.                         'name'                   => self::HTC_TESTIMONIAL_MEDIAFOLDER_NAME,
  93.                         'useParentConfiguration' => false,
  94.                         'configuration'          => [
  95.                             'createThumbnails'    => true,
  96.                             'mediaThumbnailSizes' => $thumbnailIds
  97.                         ]
  98.                     ]
  99.                 ]
  100.             ], $context);
  101.         } catch (\Exception $e) {
  102.             echo $e->getMessage();
  103.         }
  104.     }
  105. }