<?php declare(strict_types=1);
namespace HuebertAddOrderAttributes;
use \Exception;
use Doctrine\DBAL\Connection;
use HuebertAddOrderAttributes\Core\Content\Attribute\AttributeDefinition;
use HuebertAddOrderAttributes\Template\AttributesMailTemplateContent;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexerRegistry;
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\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\Framework\Migration\InheritanceUpdaterTrait;
class HuebertAddOrderAttributes extends Plugin
{
use InheritanceUpdaterTrait;
/**
* @var string
*/
public const mailTemplateTypeId = '0a63447aa36b086e2eaeff36bfbeb779';
/**
* @var string
*/
public const mailTemplateId = '8e990c3ba1d46e729f59d98b9db9c9dd';
/**
* @param InstallContext $installContext
* @return void
*/
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->createHuebertMediaFolder($installContext->getContext());
$this->createNotificationEmail($installContext->getContext());
}
/**
* @param UpdateContext $updateContext
* @return void
*/
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext);
$this->createNotificationEmail($updateContext->getContext());
}
/**
* @param ActivateContext $activateContext
* @return void
*/
public function activate(ActivateContext $activateContext): void
{
$registry = $this->container->get(EntityIndexerRegistry::class);
$registry->sendIndexingMessage(['product.indexer']);
$registry->sendIndexingMessage(['product_stream.indexer']);
$registry->sendIndexingMessage(['category.indexer']);
}
/**
* @param InstallContext $installContext
* @return void
*/
public function postInstall(InstallContext $installContext): void
{
parent::postInstall($installContext); // TODO: Change the autogenerated stub
$connection = $this->container->get(Connection::class);
$connection->executeUpdate('UPDATE product SET attributes = id WHERE attributes IS NULL');
$connection->executeUpdate('UPDATE category SET attributes = id WHERE attributes IS NULL');
$connection->executeUpdate('UPDATE product_stream SET attributes = id WHERE attributes IS NULL');
}
/**
* @param UninstallContext $uninstallContext
* @return void
*/
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if($uninstallContext->keepUserData()) {
return;
}
$connection = $this->container->get(Connection::class);
$connection->executeUpdate('DROP TRIGGER IF EXISTS `update_product_attributes`');
$connection->executeUpdate('DROP TRIGGER IF EXISTS `update_category_attributes`');
$productAttributesCheck = $connection->executeQuery("
SHOW COLUMNS FROM `product` LIKE 'attributes'
");
$productAttributesExists = (bool)$productAttributesCheck->rowCount();
if($productAttributesExists) {
$connection->executeUpdate('ALTER TABLE `product` DROP COLUMN `attributes`');
}
$categoryAttributesCheck = $connection->executeQuery("
SHOW COLUMNS FROM `category` LIKE 'attributes'
");
$categoryAttributesExists = (bool)$categoryAttributesCheck->rowCount();
if($categoryAttributesExists) {
$connection->executeUpdate('ALTER TABLE `category` DROP COLUMN `attributes`');
}
$productStreamAttributesCheck = $connection->executeQuery("
SHOW COLUMNS FROM `product_stream` LIKE 'attributes'
");
$productStreamAttributesExists = (bool)$productStreamAttributesCheck->rowCount();
if($productStreamAttributesExists) {
$connection->executeUpdate('ALTER TABLE `product_stream` DROP COLUMN `attributes`');
}
$connection->executeUpdate('DROP TABLE IF EXISTS `sysea_attribute_field_translation`');
$connection->executeUpdate('DROP TABLE IF EXISTS `sysea_attribute_product_stream`');
$connection->executeUpdate('DROP TABLE IF EXISTS `sysea_attribute_category`');
$connection->executeUpdate('DROP TABLE IF EXISTS `sysea_attribute_product`');
$connection->executeUpdate('DROP TABLE IF EXISTS `sysea_attribute_field`');
$connection->executeUpdate('DROP TABLE IF EXISTS `sysea_attribute_dependency`');
$connection->executeUpdate('DELETE FROM `media_folder` WHERE name = "Huebert Imported Media"');
$templateTypeRepository = $this->container->get('mail_template_type.repository');
$templateTypeRepository->delete([['id' => self::mailTemplateTypeId]], $uninstallContext->getContext());
$mailTemplateRepository = $this->container->get('mail_template.repository');
$mailTemplateRepository->delete([['id'=>self::mailTemplateId]], $uninstallContext->getContext());
}
/**
* @param UpdateContext $updateContext
* @return void
*/
private function migrateProductStream(UpdateContext $updateContext)
{
$updateContext->getContext()->setConsiderInheritance(true);
$updateContext->setAutoMigrate(true);
}
/**
* @return void
* @throws \Doctrine\DBAL\Exception
*/
private function migrateOptionsField()
{
/**
* @var Connection $connection
*/
$connection = $this->container->get(Connection::class);
$optionsCheck = $connection->executeQuery("
SHOW COLUMNS FROM `sysea_attribute_field` LIKE 'options'
");
$optionsExists = (bool)$optionsCheck->rowCount();
if(!$optionsExists) {
$connection->executeUpdate('
ALTER TABLE sysea_attribute_field
ADD options TEXT NULL
');
}
$optionsCheck = $connection->executeQuery("
SHOW COLUMNS FROM `sysea_attribute_field_translation` LIKE 'options'
");
$optionsExists = (bool)$optionsCheck->rowCount();
if(!$optionsExists) {
$connection->executeUpdate('
ALTER TABLE sysea_attribute_field_translation
ADD options TEXT NULL
');
}
}
/**
* @param Context $context
* @return void
*/
public function createHuebertMediaFolder(Context $context): void
{
$this->deleteDefaultMediaFolder($context);
$this->checkForThumbnailSizes($context);
/** @var EntityRepositoryInterface $mediaFolderRepository */
$mediaFolderRepository = $this->container->get('media_default_folder.repository');
$mediaFolderRepository->upsert([
[
'entity' => AttributeDefinition::ENTITY_NAME,
'associationFields' => ['media'],
'folder' => [
'name' => 'Huebert Imported Media',
'useParentConfiguration' => false,
'configuration' => [
'createThumbnails' => true,
'mediaThumbnailSizes' => [
[
'width' => 350,
'height' => 350,
],
],
],
],
],
], $context);
}
/**
* @param Context $context
* @return void
*/
private function checkForThumbnailSizes(Context $context): void
{
$criteria = new Criteria();
$criteria->addFilter(
new MultiFilter(
MultiFilter::CONNECTION_AND,
[
new EqualsFilter('width', 350),
new EqualsFilter('height', 350),
]
)
);
/** @var EntityRepositoryInterface $thumbnailSizeRepository */
$thumbnailSizeRepository = $this->container->get('media_thumbnail_size.repository');
$thumbnailIds = $thumbnailSizeRepository->searchIds($criteria, $context)->getIds();
if (!empty($thumbnailIds)) {
$ids = array_map(static function ($id) {
return ['id' => $id];
}, $thumbnailIds);
$thumbnailSizeRepository->delete($ids, $context);
}
}
/**
* @param Context $context
* @return void
*/
private function deleteDefaultMediaFolder(Context $context): void
{
$criteria = new Criteria();
$criteria->addFilter(
new EqualsAnyFilter('entity', [
AttributeDefinition::ENTITY_NAME,
])
);
/** @var EntityRepositoryInterface $mediaFolderRepository */
$mediaFolderRepository = $this->container->get('media_default_folder.repository');
$mediaFolderIds = $mediaFolderRepository->searchIds($criteria, $context)->getIds();
if (!empty($mediaFolderIds)) {
$ids = array_map(static function ($id) {
return ['id' => $id];
}, $mediaFolderIds);
$mediaFolderRepository->delete($ids, $context);
}
}
/**
* @param Context $context
* @return void
*/
private function createNotificationEmail(Context $context): void
{
$templateTypeRepository = $this->container->get('mail_template_type.repository');
$mailTemplateType = [
[
'id' => self::mailTemplateTypeId,
'name' => 'Bestellbestätigung (Artikel Konfigurator)',
'technicalName' => 'huebert.attributes',
'availableEntities' => [
'salesChannel' => 'sales_channel'
]
]
];
/** @var EntityRepositoryInterface $mailTemplateRepository */
$mailTemplateRepository = $this->container->get('mail_template.repository');
$mailData = new AttributesMailTemplateContent();
$mailTemplate = [
[
'id' => self::mailTemplateId,
'mailTemplateTypeId' => self::mailTemplateTypeId,
'senderName' => [
Defaults::LANGUAGE_SYSTEM => '{{ salesChannel.name }}',
'en-GB' => '{{ salesChannel.name }}',
'de-DE' => '{{ salesChannel.name }}',
],
'subject' => [
Defaults::LANGUAGE_SYSTEM => 'Bestellbestätigung',
'en-GB' => 'Order confirmation',
'de-DE' => 'Bestellbestätigung '
],
'description' => [
Defaults::LANGUAGE_SYSTEM => 'Berücksichtigt die zusätzlichen Felder in der E-Mail',
'en-GB' => 'Takes into account the additional fields in the e-mail',
'de-DE' => 'Berücksichtigt die zusätzlichen Felder in der E-Mail',
],
'contentHtml' => [
'en-GB' => $mailData->mailTemplateData()['en-GB']['html'],
'de-DE' => $mailData->mailTemplateData()['de-DE']['html'],
Defaults::LANGUAGE_SYSTEM => $mailData->mailTemplateData()['de-DE']['html'],
],
'contentPlain' => [
'en-GB' => $mailData->mailTemplateData()['en-GB']['plain'],
'de-DE' => $mailData->mailTemplateData()['de-DE']['plain'],
Defaults::LANGUAGE_SYSTEM => $mailData->mailTemplateData()['de-DE']['plain'],
],
],
];
try {
$templateTypeRepository->upsert($mailTemplateType, $context);
$mailTemplateRepository->upsert($mailTemplate, $context);
} catch (Exception $exception) {
error_log(print_r(array('uslo: ', $exception->getMessage()), true) . "\n", 3, '../error.log');
}
}
}