<?php declare(strict_types=1);
namespace Emz\ShippingPaymentIcons;
use Doctrine\DBAL\Connection;
use Shopware\Core\Content\Media\File\FileSaver;
use Shopware\Core\Content\Media\File\MediaFile;
use Shopware\Core\Content\Media\MediaCollection;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Uuid\Uuid;
class EmzPlatformShippingPaymentIcons extends Plugin
{
const BASE_MEDIA_FOLDER_NAME = "Standard Icons & Logos für Versand & Zahlung";
/**
* {@inheritDoc}
*/
public function install(InstallContext $context): void
{
parent::install($context);
$this->addBaseIcons();
}
/**
* {@inheritDoc}
*/
public function uninstall(UninstallContext $context): void
{
parent::uninstall($context);
if ($context->keepUserData()) {
return;
}
$this->removeDatabaseTables();
$this->removeBaseFolder();
}
/**
* Drops database tables used in this plugin.
*/
private function removeDatabaseTables()
{
$connection = $this->container->get(Connection::class);
$connection->executeStatement('DROP TABLE IF EXISTS `emz_icon_translation`');
$connection->executeStatement('DROP TABLE IF EXISTS `emz_icon`');
}
/**
* Deletes the base folder and icons. Base icons are removed automatically when folder is deleted.
*/
private function removeBaseFolder()
{
/** @var EntityRepositoryInterface $mediaFolderRepository */
$mediaFolderRepository = $this->container->get('media_folder.repository');
/** @var Context $context */
$context = Context::createDefaultContext();
if (!$mediaFolderId = $this->getBaseMediaFolder()) {
return;
}
$mediaFolderRepository->delete(
[['id' => $mediaFolderId]],
$context
);
}
private function addBaseIcons(): void
{
$baseIcons = array_filter(glob($this->getBasePath() . '/src/Resources/assets/baseIcons/*'), 'is_file');
$baseIcons = $this->filterNamesInUse($baseIcons);
if (!$baseIcons) {
return;
}
/** @var FileSaver $fileSaver */
$fileSaver = $this->container->get(FileSaver::class);
/** @var Context $context */
$context = Context::createDefaultContext();
/** @var EntityRepositoryInterface $mediaRepository */
$mediaRepository = $this->container->get('media.repository');
if (!$mediaFolderId = $this->getBaseMediaFolder()) {
return;
}
foreach ($baseIcons as $baseIcon) {
if (!file_exists($baseIcon)) {
continue;
}
$mediaFile = new MediaFile(
$baseIcon,
mime_content_type($baseIcon),
pathinfo($baseIcon, PATHINFO_EXTENSION),
filesize($baseIcon)
);
if (!$mediaFile instanceof MediaFile) {
continue;
}
$mediaId = Uuid::randomHex();
$mediaRepository->create(
[
[
'id' => $mediaId,
'private' => false,
'mediaFolderId' => $mediaFolderId
]
],
$context
);
$fileSaver->persistFileToMedia(
$mediaFile,
pathinfo($baseIcon, PATHINFO_FILENAME),
$mediaId,
$context
);
}
}
/**
* @return string
*/
private function getBaseMediaFolder()
{
/** @var EntityRepositoryInterface $mediaFolderRepository */
$mediaFolderRepository = $this->container->get('media_folder.repository');
/** @var Context $context */
$context = Context::createDefaultContext();
$result = $mediaFolderRepository->search(
(new Criteria())->addFilter(
new EqualsFilter('media_folder.name', self::BASE_MEDIA_FOLDER_NAME)
),
$context
);
if ($result->getTotal() > 0) {
return $result->getEntities()->first()->getUniqueIdentifier();
}
$mediaFolderId = Uuid::randomHex();
$mediaFolderRepository->upsert(
[
[
'id' => $mediaFolderId,
'name' => self::BASE_MEDIA_FOLDER_NAME,
'configuration' => [
'id' => Uuid::randomHex(),
'createThumbnails' => false,
]
]
],
$context
);
return $mediaFolderId;
}
/**
* @param array|null $baseIcons
* @return array
*/
private function filterNamesInUse($baseIcons = null)
{
if (!$baseIcons || !is_array($baseIcons)) {
return [];
}
/** @var Context $context */
$context = Context::createDefaultContext();
/** @var EntityRepositoryInterface $mediaRepository */
$mediaRepository = $this->container->get('media.repository');
$baseIconNames = [];
foreach ($baseIcons as $key => $baseIcon) {
$baseIconNames[$key] = pathinfo($baseIcon, PATHINFO_FILENAME);
}
if (!$baseIconNames) {
return [];
}
$result = $mediaRepository->search(
(new Criteria())->addFilter(
new EqualsAnyFilter('media.fileName', $baseIconNames)
),
$context
);
/** @var MediaCollection $entities */
$entities = $result->getEntities();
$namesInUse = [];
foreach ($entities as $entity) {
$namesInUse[] = $entity->getFileName();
}
//remove base icons which name is already taken
foreach ($baseIconNames as $key => $baseIconName) {
if (in_array($baseIconName, $namesInUse)) {
unset($baseIcons[$key]);
}
}
return $baseIcons;
}
}