<?php declare(strict_types=1);
namespace HTC\Testimonials;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Uuid\Uuid;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Context;
use HTC\Testimonials\Core\Content\Testimonials\TestimonialDefinition;
class HTCTestimonials extends Plugin
{
public const HTC_TESTIMONIAL_MEDIAFOLDER_NAME = 'HTC Testimonial 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 `htc_testimonial`');
$connection->delete('cms_block', ['type' => "htc-testimonial"]);
$connection->delete('cms_block', ['type' => "htc-cms-testimonial-list"]);
$connection->delete('cms_block', ['type' => "htc-testimonial-form"]);
$connection->delete('cms_block', ['type' => "htc-testimonial-grid"]);
$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 = ?', [
TestimonialDefinition::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 = ?', [
TestimonialDefinition::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');
$thumbnailId = $connection->fetchAll('SELECT LOWER(HEX(id)) AS id from `media_thumbnail_size` WHERE width in (170)');
if (!$thumbnailId) {
$repo->upsert([
[
'id' => $mediaThumbnailSizeId,
'width' => 170,
'height' => 170
]
], $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 (170)');
$repo = $this->container->get('media_default_folder.repository');
$repo->upsert([
[
'id' => $mediaFolderId,
'entity' => TestimonialDefinition::ENTITY_NAME,
'associationFields' => ['media'],
'folder' => [
'name' => self::HTC_TESTIMONIAL_MEDIAFOLDER_NAME,
'useParentConfiguration' => false,
'configuration' => [
'createThumbnails' => true,
'mediaThumbnailSizes' => $thumbnailIds
]
]
]
], $context);
} catch (\Exception $e) {
echo $e->getMessage();
}
}
}