custom/plugins/CrayssnLabsBase/src/CrayssnLabsBase.php line 29

Open in your IDE?
  1. <?php
  2. /** @noinspection PhpUnused */
  3. declare(strict_types=1);
  4. namespace CrayssnLabsBase;
  5. use Exception;
  6. use Shopware\Core\Content\Media\File\FileSaver;
  7. use Shopware\Core\Content\Media\MediaService;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter;
  14. use Symfony\Component\HttpFoundation\File;
  15. use Shopware\Core\Content\Media;
  16. /**
  17.  * Class CrayssnLabsBase
  18.  *
  19.  * @package   CrayssnLabsBase
  20.  *
  21.  * @author    Sebastian Ludwig <dev@cl.team>
  22.  * @copyright Copyright (c) 2021, CrayssnLabs Ludwig Wiegler GbR
  23.  */
  24. class CrayssnLabsBase extends Plugin
  25. {
  26.     const MEDIA_FOLDER 'CrayssnLabs_Base';
  27.     private Context $context;
  28.     /**
  29.      * Function activate
  30.      *
  31.      * @param \Shopware\Core\Framework\Plugin\Context\ActivateContext $activateContext
  32.      */
  33.     public function activate(ActivateContext $activateContext): void
  34.     {
  35.         $this->context $activateContext->getContext();
  36.         $this->addMediaFile(__DIR__ '/Resources/data/image-placeholder.jpg''cms-placeholder');
  37.     }
  38.     /**
  39.      * Function addMediaFile
  40.      *
  41.      * @param string $_filePath
  42.      * @param string $_fileName
  43.      *
  44.      * @return void
  45.      */
  46.     private function addMediaFile(string $_filePathstring $_fileName ''): void
  47.     {
  48.         if (empty($_fileName)) {
  49.             $_fileName basename($_filePath);
  50.         }
  51.         $mediaFolderRepository $this->getMediaFolderRepository();
  52.         $mediaFolderIds $mediaFolderRepository->search(
  53.             (new Criteria())->addFilter(new Filter\EqualsFilter('name''CrayssnLabs')),
  54.             $this->context
  55.         )->getIds();
  56.         $mediaRepository $this->getMediaRepository();
  57.         $mediaIds $mediaRepository->searchIds(
  58.             (new Criteria())->addFilter(new Filter\EqualsFilter('fileName'$_fileName)),
  59.             $this->context
  60.         )->getIds();
  61.         if(!empty($mediaIds))
  62.         {
  63.             $mediaIds array_map(static function (string $id) {
  64.                 return ['id' => $id];
  65.             }, $mediaIds);
  66.             $mediaRepository->delete($mediaIds$this->context);
  67.         }
  68.         if (empty($mediaFolderIds)) {
  69.             $mediaDefaultFolderIds $this->getMediaDefaultFolderRepository()->searchIds(
  70.                 (new Criteria())->addFilter(new Filter\ContainsFilter('entity'"crayssnlabs_")),
  71.                 $this->context
  72.             )->getIds();
  73.             if (!empty($mediaDefaultFolderIds)) {
  74.                 foreach ($mediaDefaultFolderIds as $id) {
  75.                     $this->getMediaDefaultFolderRepository()->delete([
  76.                         ['id' => $id]
  77.                     ], $this->context);
  78.                 }
  79.             }
  80.             $writtenContainerEvent $this->getMediaDefaultFolderRepository()->upsert([
  81.                 [
  82.                     'associationFields' => ["crayssnlabsBase"],
  83.                     'entity'            => self::MEDIA_FOLDER,
  84.                 ]
  85.             ], $this->context);
  86.             $mediaDefaultFolderIds $writtenContainerEvent->getPrimaryKeys('media_default_folder');
  87.             $writtenContainerEvent $this->getMediaFolderRepository()->create([
  88.                 [
  89.                     'name'                   => 'CrayssnLabs',
  90.                     'useParentConfiguration' => false,
  91.                     'configuration'          => [
  92.                         'createThumbnails' => false,
  93.                     ]
  94.                 ]
  95.             ], $this->context);
  96.             $configuratorFolderId $writtenContainerEvent->getPrimaryKeys('media_folder')[0];
  97.             $this->getMediaFolderRepository()->create([
  98.                 [
  99.                     'name' => 'Base',
  100.                     'parentId' => $configuratorFolderId,
  101.                     'defaultFolderId' => $mediaDefaultFolderIds[0],
  102.                     'useParentConfiguration' => false,
  103.                     'configuration' => [
  104.                         'createThumbnails' => false,
  105.                     ]
  106.                 ]
  107.             ], $this->context);
  108.         }
  109.         $file = new File\File($_filePath);
  110.         $mediaFile = new Media\File\MediaFile($file->getRealPath(), $file->getMimeType(), $file->getExtension(), $file->getSize());
  111.         $mediaId $this->getMediaService()->createMediaInFolder(self::MEDIA_FOLDER$this->contextfalse);
  112.         try {
  113.             $this->getFileSaver()->persistFileToMedia(
  114.                 $mediaFile,
  115.                 $_fileName,
  116.                 $mediaId,
  117.                 $this->context
  118.             );
  119.         } catch (Exception $e) {}
  120.     }
  121.     /**
  122.      * Function getMediaRepository
  123.      *
  124.      * @return \Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface
  125.      */
  126.     private function getMediaRepository(): EntityRepositoryInterface
  127.     {
  128.         /** @noinspection PhpIncompatibleReturnTypeInspection */
  129.         return $this->container->get('media.repository');
  130.     }
  131.     /**
  132.      * Function getCustomFieldSetRepository
  133.      *
  134.      * @return \Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface
  135.      */
  136.     private function getMediaFolderRepository(): EntityRepositoryInterface
  137.     {
  138.         /** @noinspection PhpIncompatibleReturnTypeInspection */
  139.         return $this->container->get('media_folder.repository');
  140.     }
  141.     /**
  142.      * Function getCustomFieldSetRepository
  143.      *
  144.      * @return \Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface
  145.      */
  146.     public function getMediaDefaultFolderRepository(): EntityRepositoryInterface
  147.     {
  148.         /** @noinspection PhpIncompatibleReturnTypeInspection */
  149.         return $this->container->get('media_default_folder.repository');
  150.     }
  151.     /**
  152.      * Function getFileSaver
  153.      *
  154.      * @return \Shopware\Core\Content\Media\File\FileSaver
  155.      */
  156.     public function getFileSaver(): FileSaver
  157.     {
  158.         /** @noinspection PhpIncompatibleReturnTypeInspection */
  159.         return $this->container->get('crayssnlabs_base.media_file_saver');
  160.     }
  161.     /**
  162.      * Function getMediaService
  163.      *
  164.      * @return \Shopware\Core\Content\Media\MediaService
  165.      */
  166.     public function getMediaService(): MediaService
  167.     {
  168.         /** @noinspection PhpIncompatibleReturnTypeInspection */
  169.         return $this->container->get('crayssnlabs_base.media_service');
  170.     }
  171. }