<?php declare(strict_types=1);
namespace Cogi\CogiLicenseManager;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Shopware\Core\Content\ImportExport\Exception\FileNotFoundException;
use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Api\Context\SystemSource;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
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\Uuid\Exception\InvalidUuidException;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\System\Language\LanguageEntity;
class CogiLicenseManager extends Plugin {
const MAIL_TEMPLATE_CUSTOMER = 'cogi_licensemanager_customer_license';
const MAIL_TEMPLATE_ADMIN = 'cogi_licensemanager_admin_mail';
const MAIL_TEMPLATE_ADMIN_EMPTY_POOL = 'cogi_licensemanager_empty_pool';
public function install(InstallContext $context): void {
$this->installMailTemplates();
parent::install($context);
}
public function installMailTemplates(): void {
/** @var Connection $connection */
$connection = $this->container->get('Doctrine\DBAL\Connection');
//Customer Emails
try {
$templateTypeData['technicalName'] = self::MAIL_TEMPLATE_CUSTOMER;
$templateTypeData['availableEntities'] = [
'order' => 'order',
'previousState' => 'state_machine_state',
'newState' => 'state_machine_state',
'salesChannel' => 'sales_channel'
];
$templateTypeData['enName'] = 'LicenseManager License Key';
$templateTypeData['deName'] = 'LicenseManager Lizense Key';
$mailTemplateTypeId = $this->createMailTemplateType($connection, $templateTypeData);
$templateData['en-GB']['senderName'] = '{{ salesChannel.name }}';
$templateData['en-GB']['subject'] = 'Your License Key';
$templateData['en-GB']['description'] = '';
$templateData['en-GB']['contentHtml'] = $this->getMailContent('en-GB', 'licensekey', 'html');
$templateData['en-GB']['contentPlain'] = $this->getMailContent('en-GB', 'licensekey', 'plain');
$templateData['de-DE']['senderName'] = '{{ salesChannel.name }}';
$templateData['de-DE']['subject'] = 'Dein Lizenzschlüssel';
$templateData['de-DE']['description'] = '';
$templateData['de-DE']['contentHtml'] = $this->getMailContent('de-DE', 'licensekey', 'html');
$templateData['de-DE']['contentPlain'] = $this->getMailContent('de-DE', 'licensekey', 'plain');
$this->createMailTemplate($connection, $mailTemplateTypeId, $templateData);
} catch (\Exception $e) {
}
//ADMIN Emails
try {
$templateTypeData['technicalName'] = self::MAIL_TEMPLATE_ADMIN;
$templateTypeData['availableEntities'] = [
'order' => 'order',
'previousState' => 'state_machine_state',
'newState' => 'state_machine_state',
'salesChannel' => 'sales_channel'
];
$templateTypeData['enName'] = 'LicenseManager Sold Keys';
$templateTypeData['deName'] = 'LicenseManager Lizenzen Verkauft';
$mailTemplateTypeId = $this->createMailTemplateType($connection, $templateTypeData);
$templateData['en-GB']['senderName'] = '{{ salesChannel.name }}';
$templateData['en-GB']['subject'] = 'Keys Sold';
$templateData['en-GB']['description'] = '';
$templateData['en-GB']['contentHtml'] = $this->getMailContent('en-GB', 'admin', 'html');
$templateData['en-GB']['contentPlain'] = $this->getMailContent('en-GB', 'admin', 'plain');
$templateData['de-DE']['senderName'] = '{{ salesChannel.name }}';
$templateData['de-DE']['subject'] = 'Lizenzen Verkauft';
$templateData['de-DE']['description'] = '';
$templateData['de-DE']['contentHtml'] = $this->getMailContent('de-DE', 'admin', 'html');
$templateData['de-DE']['contentPlain'] = $this->getMailContent('de-DE', 'admin', 'plain');
$this->createMailTemplate($connection, $mailTemplateTypeId, $templateData);
} catch (\Exception $e) {
}
//ADMIN Emails EMPTY Pool
try {
$templateTypeData['technicalName'] = self::MAIL_TEMPLATE_ADMIN_EMPTY_POOL;
$templateTypeData['availableEntities'] = [
'order' => 'order',
'previousState' => 'state_machine_state',
'newState' => 'state_machine_state',
'salesChannel' => 'sales_channel'
];
$templateTypeData['enName'] = 'LicenseManager Empty Pool';
$templateTypeData['deName'] = 'LicenseManager Pool ist Leer';
$mailTemplateTypeId = $this->createMailTemplateType($connection, $templateTypeData);
$templateData['en-GB']['senderName'] = '{{ salesChannel.name }}';
$templateData['en-GB']['subject'] = 'Empty Pool';
$templateData['en-GB']['description'] = '';
$templateData['en-GB']['contentHtml'] = $this->getMailContent('en-GB', 'empty', 'html');
$templateData['en-GB']['contentPlain'] = $this->getMailContent('en-GB', 'empty', 'plain');
$templateData['de-DE']['senderName'] = '{{ salesChannel.name }}';
$templateData['de-DE']['subject'] = 'Leerer Pool';
$templateData['de-DE']['description'] = '';
$templateData['de-DE']['contentHtml'] = $this->getMailContent('de-DE', 'empty', 'html');
$templateData['de-DE']['contentPlain'] = $this->getMailContent('de-DE', 'empty', 'plain');
$this->createMailTemplate($connection, $mailTemplateTypeId, $templateData);
} catch (\Exception $e) {
}
}
public function update(UpdateContext $context): void {
$this->installMailTemplates();
parent::update($context);
}
public function activate(ActivateContext $context): void {
$this->installMailTemplates();
parent::activate($context);
}
public function deactivate(DeactivateContext $context): void {
// your code you need to run while your plugin gets deactivated
}
public function uninstall(UninstallContext $uninstallContext): void {
$context = new Context(new SystemSource());
try {
$this->deleteMailTemplate($context, self::MAIL_TEMPLATE_CUSTOMER);
$this->deleteMailTemplateType($context, self::MAIL_TEMPLATE_CUSTOMER);
$this->deleteMailTemplateType($context, self::MAIL_TEMPLATE_ADMIN);
$this->deleteMailTemplateType($context, self::MAIL_TEMPLATE_ADMIN_EMPTY_POOL);
} catch (\Exception $e) {
}
if ($uninstallContext->keepUserData()) {
return;
}
$connection = $this->container->get(Connection::class);
$connection->executeStatement('SET foreign_key_checks = 0;');
$connection->executeStatement('DROP TABLE `cogi_licensemanager_category`, `cogi_licensemanager_multikey_log`, `cogi_licensemanager_customer`, `cogi_licensemanager_license`, `cogi_licensemanager_pool`, `cogi_licensemanager_product`, `cogi_licensemanager_media`, `cogi_licensemanager_url`;');
$connection->executeStatement('SET foreign_key_checks = 1;');
}
/**
* @param Connection $connection
* @param array $data
*
* @return string
*
* @throws DBALException
* @throws InconsistentCriteriaIdsException
* @throws InvalidUuidException
*/
private function createMailTemplateType(Connection $connection, array $data): string {
$mailTemplateTypeId = Uuid::randomHex();
$connection->insert('mail_template_type', [
'id' => Uuid::fromHexToBytes($mailTemplateTypeId),
'technical_name' => $data['technicalName'],
'available_entities' => json_encode($data['availableEntities']),
'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
]);
$connection->insert('mail_template_type_translation', [
'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
'language_id' => Uuid::fromHexToBytes($this->getLanguageIdByLocale('en-GB')),
'name' => $data['enName'],
'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
]);
$connection->insert('mail_template_type_translation', [
'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
'language_id' => Uuid::fromHexToBytes($this->getLanguageIdByLocale('de-DE')),
'name' => $data['deName'],
'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
]);
return $mailTemplateTypeId;
}
/**
* @param Connection $connection
* @param string $mailTemplateTypeId
* @param array $data
*
* @return void
*
* @throws DBALException
* @throws InconsistentCriteriaIdsException
* @throws InvalidUuidException
*/
private function createMailTemplate(Connection $connection, string $mailTemplateTypeId, array $data): void {
$mailTemplateId = Uuid::randomHex();
$connection->insert('mail_template', [
'id' => Uuid::fromHexToBytes($mailTemplateId),
'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
'system_default' => true,
'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
]);
$connection->insert('mail_template_translation', [
'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
'language_id' => Uuid::fromHexToBytes($this->getLanguageIdByLocale('en-GB')),
'sender_name' => $data['en-GB']['senderName'],
'subject' => $data['en-GB']['subject'],
'description' => $data['en-GB']['description'],
'content_html' => $data['en-GB']['contentHtml'],
'content_plain' => $data['en-GB']['contentPlain'],
'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
]);
$connection->insert('mail_template_translation', [
'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
'language_id' => Uuid::fromHexToBytes($this->getLanguageIdByLocale('de-DE')),
'sender_name' => $data['de-DE']['senderName'],
'subject' => $data['de-DE']['subject'],
'description' => $data['de-DE']['description'],
'content_html' => $data['de-DE']['contentHtml'],
'content_plain' => $data['de-DE']['contentPlain'],
'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
]);
}
/**
* @param string $locale
*
* @return string
*
* @throws InconsistentCriteriaIdsException
*/
private function getLanguageIdByLocale(string $locale): string {
$context = new Context(new SystemSource());
/** @var EntityRepository $languageRepository */
$languageRepository = $this->container->get('language.repository');
$criteria = new Criteria();
$criteria->addAssociation('locale');
$criteria->addFilter(new EqualsFilter('locale.code', $locale));
/** @var LanguageEntity $languageEntity */
$languageEntity = $languageRepository->search($criteria, $context)->first();
return $languageEntity->getId();
}
/**
* @param string $locale
* @param string $prefix
* @param string $type
*
* @return string
*/
private function getMailContent(string $locale, string $prefix, string $type): string {
$path = $this->getPath() . '/Resources/email/' . $locale . '/';
switch ($type) {
case 'html':
$ext = 'html';
break;
case 'plain':
$ext = 'txt';
break;
default:
$ext = 'txt';
}
$file = $path . $prefix . '-' . $type . '.' . $ext;
if (!is_file($file)) {
throw new FileNotFoundException($file);
}
return file_get_contents($file);
}
/**
* @param string $mailTemplateId
*
* @throws DBALException
*/
private function deleteMailTemplateFromSalesChannels(string $mailTemplateId): void {
/** @var Connection $connection */
$connection = $this->container->get('Doctrine\DBAL\Connection');
$connection->executeQuery('DELETE FROM `mail_template_sales_channel` WHERE `mail_template_id` = :id', ['id' => $mailTemplateId]);
}
/**
* @param Context $context
* @param string $technicalName
*
* @throws InconsistentCriteriaIdsException
* @throws DBALException
*/
private function deleteMailTemplate(Context $context, string $technicalName): void {
$mailTemplateTypeId = $this->getMailTemplateTypeId($context, $technicalName);
$mailTemplateId = $this->getMailTemplateId($context, $mailTemplateTypeId);
$this->deleteMailTemplateFromSalesChannels($mailTemplateId);
/** @var EntityRepository $mailTemplateRepository */
$mailTemplateRepository = $this->container->get('mail_template.repository');
$mailTemplateRepository->delete([
[
'id' => $mailTemplateId
]
], $context);
}
/**
* @param Context $context
* @param string $mailTemplateTypeId
*
* @return string
*
*/
private function getMailTemplateId(Context $context, string $mailTemplateTypeId): string {
/** @var EntityRepository $mailTemplateRepository */
$mailTemplateRepository = $this->container->get('mail_template.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('mailTemplateTypeId', $mailTemplateTypeId));
/** @var MailTemplateEntity $mailTemplateEntity */
$mailTemplateEntity = $mailTemplateRepository->search($criteria, $context)->first();
return $mailTemplateEntity->getId();
}
/**
* @param Context $context
* @param string $technicalName
*
* @throws InconsistentCriteriaIdsException
*/
private function deleteMailTemplateType(Context $context, string $technicalName): void {
$mailTemplateTypeId = $this->getMailTemplateTypeId($context, $technicalName);
/** @var EntityRepository $mailTemplateTypeRepository */
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
$mailTemplateTypeRepository->delete([
[
'id' => $mailTemplateTypeId
]
], $context);
}
/**
* @param Context $context
* @param string $technicalName
*
* @return string
*
*/
private function getMailTemplateTypeId(Context $context, string $technicalName): string {
/** @var EntityRepository $mailTemplateTypeRepository */
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('technicalName', $technicalName));
/** @var MailTemplateTypeEntity $mailTemplateTypeEntity */
$mailTemplateTypeEntity = $mailTemplateTypeRepository->search($criteria, $context)->first();
if ($mailTemplateTypeEntity !== null) return $mailTemplateTypeEntity->getId(); else
return "";
}
}