<?php
/** @noinspection PhpUnused */
declare(strict_types=1);
namespace CrayssnLabsBase;
use Exception;
use Shopware\Core\Content\Media\File\FileSaver;
use Shopware\Core\Content\Media\MediaService;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter;
use Symfony\Component\HttpFoundation\File;
use Shopware\Core\Content\Media;
/**
* Class CrayssnLabsBase
*
* @package CrayssnLabsBase
*
* @author Sebastian Ludwig <dev@cl.team>
* @copyright Copyright (c) 2021, CrayssnLabs Ludwig Wiegler GbR
*/
class CrayssnLabsBase extends Plugin
{
const MEDIA_FOLDER = 'CrayssnLabs_Base';
private Context $context;
/**
* Function activate
*
* @param \Shopware\Core\Framework\Plugin\Context\ActivateContext $activateContext
*/
public function activate(ActivateContext $activateContext): void
{
$this->context = $activateContext->getContext();
$this->addMediaFile(__DIR__ . '/Resources/data/image-placeholder.jpg', 'cms-placeholder');
}
/**
* Function addMediaFile
*
* @param string $_filePath
* @param string $_fileName
*
* @return void
*/
private function addMediaFile(string $_filePath, string $_fileName = ''): void
{
if (empty($_fileName)) {
$_fileName = basename($_filePath);
}
$mediaFolderRepository = $this->getMediaFolderRepository();
$mediaFolderIds = $mediaFolderRepository->search(
(new Criteria())->addFilter(new Filter\EqualsFilter('name', 'CrayssnLabs')),
$this->context
)->getIds();
$mediaRepository = $this->getMediaRepository();
$mediaIds = $mediaRepository->searchIds(
(new Criteria())->addFilter(new Filter\EqualsFilter('fileName', $_fileName)),
$this->context
)->getIds();
if(!empty($mediaIds))
{
$mediaIds = array_map(static function (string $id) {
return ['id' => $id];
}, $mediaIds);
$mediaRepository->delete($mediaIds, $this->context);
}
if (empty($mediaFolderIds)) {
$mediaDefaultFolderIds = $this->getMediaDefaultFolderRepository()->searchIds(
(new Criteria())->addFilter(new Filter\ContainsFilter('entity', "crayssnlabs_")),
$this->context
)->getIds();
if (!empty($mediaDefaultFolderIds)) {
foreach ($mediaDefaultFolderIds as $id) {
$this->getMediaDefaultFolderRepository()->delete([
['id' => $id]
], $this->context);
}
}
$writtenContainerEvent = $this->getMediaDefaultFolderRepository()->upsert([
[
'associationFields' => ["crayssnlabsBase"],
'entity' => self::MEDIA_FOLDER,
]
], $this->context);
$mediaDefaultFolderIds = $writtenContainerEvent->getPrimaryKeys('media_default_folder');
$writtenContainerEvent = $this->getMediaFolderRepository()->create([
[
'name' => 'CrayssnLabs',
'useParentConfiguration' => false,
'configuration' => [
'createThumbnails' => false,
]
]
], $this->context);
$configuratorFolderId = $writtenContainerEvent->getPrimaryKeys('media_folder')[0];
$this->getMediaFolderRepository()->create([
[
'name' => 'Base',
'parentId' => $configuratorFolderId,
'defaultFolderId' => $mediaDefaultFolderIds[0],
'useParentConfiguration' => false,
'configuration' => [
'createThumbnails' => false,
]
]
], $this->context);
}
$file = new File\File($_filePath);
$mediaFile = new Media\File\MediaFile($file->getRealPath(), $file->getMimeType(), $file->getExtension(), $file->getSize());
$mediaId = $this->getMediaService()->createMediaInFolder(self::MEDIA_FOLDER, $this->context, false);
try {
$this->getFileSaver()->persistFileToMedia(
$mediaFile,
$_fileName,
$mediaId,
$this->context
);
} catch (Exception $e) {}
}
/**
* Function getMediaRepository
*
* @return \Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface
*/
private function getMediaRepository(): EntityRepositoryInterface
{
/** @noinspection PhpIncompatibleReturnTypeInspection */
return $this->container->get('media.repository');
}
/**
* Function getCustomFieldSetRepository
*
* @return \Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface
*/
private function getMediaFolderRepository(): EntityRepositoryInterface
{
/** @noinspection PhpIncompatibleReturnTypeInspection */
return $this->container->get('media_folder.repository');
}
/**
* Function getCustomFieldSetRepository
*
* @return \Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface
*/
public function getMediaDefaultFolderRepository(): EntityRepositoryInterface
{
/** @noinspection PhpIncompatibleReturnTypeInspection */
return $this->container->get('media_default_folder.repository');
}
/**
* Function getFileSaver
*
* @return \Shopware\Core\Content\Media\File\FileSaver
*/
public function getFileSaver(): FileSaver
{
/** @noinspection PhpIncompatibleReturnTypeInspection */
return $this->container->get('crayssnlabs_base.media_file_saver');
}
/**
* Function getMediaService
*
* @return \Shopware\Core\Content\Media\MediaService
*/
public function getMediaService(): MediaService
{
/** @noinspection PhpIncompatibleReturnTypeInspection */
return $this->container->get('crayssnlabs_base.media_service');
}
}