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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Emz\ShippingPaymentIcons;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Content\Media\File\FileSaver;
  5. use Shopware\Core\Content\Media\File\MediaFile;
  6. use Shopware\Core\Content\Media\MediaCollection;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\Plugin;
  12. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  14. use Shopware\Core\Framework\Context;
  15. use Shopware\Core\Framework\Uuid\Uuid;
  16. class EmzPlatformShippingPaymentIcons extends Plugin
  17. {
  18.     const BASE_MEDIA_FOLDER_NAME "Standard Icons & Logos für Versand & Zahlung";
  19.     /**
  20.      * {@inheritDoc}
  21.      */
  22.     public function install(InstallContext $context): void
  23.     {
  24.         parent::install($context);
  25.         $this->addBaseIcons();
  26.     }
  27.     /**
  28.      * {@inheritDoc}
  29.      */
  30.     public function uninstall(UninstallContext $context): void
  31.     {
  32.         parent::uninstall($context);
  33.         if ($context->keepUserData()) {
  34.             return;
  35.         }
  36.         $this->removeDatabaseTables();
  37.         $this->removeBaseFolder();
  38.     }
  39.     /**
  40.      * Drops database tables used in this plugin.
  41.      */
  42.     private function removeDatabaseTables()
  43.     {
  44.         $connection $this->container->get(Connection::class);
  45.         $connection->executeStatement('DROP TABLE IF EXISTS `emz_icon_translation`');
  46.         $connection->executeStatement('DROP TABLE IF EXISTS `emz_icon`');
  47.     }
  48.     /**
  49.      * Deletes the base folder and icons. Base icons are removed automatically when folder is deleted.
  50.      */
  51.     private function removeBaseFolder()
  52.     {
  53.         /** @var EntityRepositoryInterface $mediaFolderRepository */
  54.         $mediaFolderRepository $this->container->get('media_folder.repository');
  55.         /** @var Context $context */
  56.         $context Context::createDefaultContext();
  57.         if (!$mediaFolderId $this->getBaseMediaFolder()) {
  58.             return;
  59.         }
  60.         $mediaFolderRepository->delete(
  61.             [['id' => $mediaFolderId]],
  62.             $context
  63.         );
  64.     }
  65.     private function addBaseIcons(): void
  66.     {
  67.         $baseIcons array_filter(glob($this->getBasePath() . '/src/Resources/assets/baseIcons/*'), 'is_file');
  68.         $baseIcons $this->filterNamesInUse($baseIcons);
  69.         if (!$baseIcons) {
  70.             return;
  71.         }
  72.         /** @var FileSaver $fileSaver */
  73.         $fileSaver $this->container->get(FileSaver::class);
  74.         /** @var Context $context */
  75.         $context Context::createDefaultContext();
  76.         /** @var EntityRepositoryInterface $mediaRepository */
  77.         $mediaRepository $this->container->get('media.repository');
  78.         if (!$mediaFolderId $this->getBaseMediaFolder()) {
  79.             return;
  80.         }
  81.         foreach ($baseIcons as $baseIcon) {
  82.             if (!file_exists($baseIcon)) {
  83.                 continue;
  84.             }
  85.             $mediaFile = new MediaFile(
  86.                 $baseIcon,
  87.                 mime_content_type($baseIcon),
  88.                 pathinfo($baseIconPATHINFO_EXTENSION),
  89.                 filesize($baseIcon)
  90.             );
  91.             if (!$mediaFile instanceof MediaFile) {
  92.                 continue;
  93.             }
  94.             $mediaId Uuid::randomHex();
  95.             $mediaRepository->create(
  96.                 [
  97.                     [
  98.                         'id' => $mediaId,
  99.                         'private' => false,
  100.                         'mediaFolderId' => $mediaFolderId
  101.                     ]
  102.                 ],
  103.                 $context
  104.             );
  105.             $fileSaver->persistFileToMedia(
  106.                 $mediaFile,
  107.                 pathinfo($baseIconPATHINFO_FILENAME),
  108.                 $mediaId,
  109.                 $context
  110.             );
  111.         }
  112.     }
  113.     /**
  114.      * @return string
  115.      */
  116.     private function getBaseMediaFolder()
  117.     {
  118.         /** @var EntityRepositoryInterface $mediaFolderRepository */
  119.         $mediaFolderRepository $this->container->get('media_folder.repository');
  120.         /** @var Context $context */
  121.         $context Context::createDefaultContext();
  122.         $result $mediaFolderRepository->search(
  123.             (new Criteria())->addFilter(
  124.                 new EqualsFilter('media_folder.name'self::BASE_MEDIA_FOLDER_NAME)
  125.             ),
  126.             $context
  127.         );
  128.         if ($result->getTotal() > 0) {
  129.             return $result->getEntities()->first()->getUniqueIdentifier();
  130.         }
  131.         $mediaFolderId Uuid::randomHex();
  132.         $mediaFolderRepository->upsert(
  133.             [
  134.                 [
  135.                     'id' => $mediaFolderId,
  136.                     'name' => self::BASE_MEDIA_FOLDER_NAME,
  137.                     'configuration' => [
  138.                         'id' => Uuid::randomHex(),
  139.                         'createThumbnails' => false,
  140.                     ]
  141.                 ]
  142.             ],
  143.             $context
  144.         );
  145.         return $mediaFolderId;
  146.     }
  147.     /**
  148.      * @param array|null $baseIcons
  149.      * @return array
  150.      */
  151.     private function filterNamesInUse($baseIcons null)
  152.     {
  153.         if (!$baseIcons || !is_array($baseIcons)) {
  154.             return [];
  155.         }
  156.         /** @var Context $context */
  157.         $context Context::createDefaultContext();
  158.         /** @var EntityRepositoryInterface $mediaRepository */
  159.         $mediaRepository $this->container->get('media.repository');
  160.         $baseIconNames = [];
  161.         foreach ($baseIcons as $key => $baseIcon) {
  162.             $baseIconNames[$key] = pathinfo($baseIconPATHINFO_FILENAME);
  163.         }
  164.         if (!$baseIconNames) {
  165.             return [];
  166.         }
  167.         $result $mediaRepository->search(
  168.             (new Criteria())->addFilter(
  169.                 new EqualsAnyFilter('media.fileName'$baseIconNames)
  170.             ),
  171.             $context
  172.         );
  173.         /** @var MediaCollection $entities */
  174.         $entities $result->getEntities();
  175.         $namesInUse = [];
  176.         foreach ($entities as $entity) {
  177.             $namesInUse[] = $entity->getFileName();
  178.         }
  179.         //remove base icons which name is already taken
  180.         foreach ($baseIconNames as $key => $baseIconName) {
  181.             if (in_array($baseIconName$namesInUse)) {
  182.                 unset($baseIcons[$key]);
  183.             }
  184.         }
  185.         return $baseIcons;
  186.     }
  187. }