<?php declare(strict_types=1);
namespace Ks\Attachments;
use Shopware\Core\Framework\Plugin;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Ks\Attachments\Core\Content\Attachments\AttachmentsDefinition;
use Shopware\Core\Framework\Uuid\Uuid;
class KsAttachments extends Plugin
{
public const ATTACHMENT_MEDIAFOLDER_NAME = 'Product Attachment Media';
public function install(InstallContext $context): void
{
$this->createMediaThumbnailSize($context->getContext());
$this->createMediaFolder($context->getContext());
}
public function uninstall(UninstallContext $context): void
{
parent::uninstall($context);
if ($context->keepUserData()) {
return;
}
$connection = $this->container->get(Connection::class);
$connection->executeUpdate('DROP TABLE IF EXISTS `ks_attachment_customer`');
$connection->executeUpdate('DROP TABLE IF EXISTS `ks_attachment_translation`');
$connection->executeUpdate('DROP TABLE IF EXISTS `ks_attachment`');
$this->removeMediaFolder($context->getContext());
}
private function removeMediaFolder(Context $context): void
{
$connection = $this->container->get(Connection::class);
try {
$defaultFolderId = $connection->fetchColumn('SELECT HEX(id) FROM media_default_folder WHERE entity = ?', [
AttachmentsDefinition::ENTITY_NAME
]);
if(!$defaultFolderId) return;
$defaultConfigurationId = $connection->fetchColumn('SELECT HEX(media_folder_configuration_id) FROM media_folder WHERE HEX(default_folder_id) = ?', [
$defaultFolderId
]);
if(!$defaultConfigurationId) return;
$connection->executeUpdate('DELETE FROM `media_folder_configuration` WHERE HEX(id) = ?', [
$defaultConfigurationId
]);
$connection->executeUpdate('DELETE FROM `media_folder` WHERE HEX(default_folder_id) = ?', [
$defaultFolderId
]);
$connection->executeUpdate('DELETE FROM `media_default_folder` WHERE entity = ?', [
AttachmentsDefinition::ENTITY_NAME
]);
}
catch (\Exception $e) {
echo $e->getMessage();
}
}
private function createMediaThumbnailSize(Context $context): void
{
try {
$connection = $this->container->get(Connection::class);
$mediaThumbnailSizeId = Uuid::randomHex();
$repo = $this->container->get('media_thumbnail_size.repository');
$repo->upsert([
[
'id' => $mediaThumbnailSizeId,
'width' => 30,
'height' => 30
]
], $context);
} catch (\Exception $e) {
echo $e->getMessage();
}
}
private function createMediaFolder(Context $context): void
{
try {
$connection = $this->container->get(Connection::class);
$mediaFolderId = Uuid::randomHex();
$thumbnailIds = $connection->fetchAll('SELECT LOWER(HEX(id)) AS id from `media_thumbnail_size` WHERE width in (30)');
$repo = $this->container->get('media_default_folder.repository');
$repo->upsert([
[
'id' => $mediaFolderId,
'entity' => AttachmentsDefinition::ENTITY_NAME,
'associationFields' => ['media'],
'folder' => [
'name' => self::ATTACHMENT_MEDIAFOLDER_NAME,
'useParentConfiguration' => false,
'configuration' => [
'createThumbnails' => true,
'mediaThumbnailSizes' => $thumbnailIds
]
]
]
], $context);
} catch (\Exception $e) {
echo $e->getMessage();
}
}
}