custom/plugins/CogiLicenseManager/src/CogiLicenseManager.php line 29

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cogi\CogiLicenseManager;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\DBALException;
  5. use Shopware\Core\Content\ImportExport\Exception\FileNotFoundException;
  6. use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
  7. use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
  8. use Shopware\Core\Defaults;
  9. use Shopware\Core\Framework\Api\Context\SystemSource;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  15. use Shopware\Core\Framework\Plugin;
  16. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  17. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  18. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  19. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  20. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  21. use Shopware\Core\Framework\Uuid\Exception\InvalidUuidException;
  22. use Shopware\Core\Framework\Uuid\Uuid;
  23. use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity;
  24. use Shopware\Core\System\CustomField\CustomFieldTypes;
  25. use Shopware\Core\System\Language\LanguageEntity;
  26. class CogiLicenseManager extends Plugin {
  27.     const MAIL_TEMPLATE_CUSTOMER 'cogi_licensemanager_customer_license';
  28.     const MAIL_TEMPLATE_ADMIN 'cogi_licensemanager_admin_mail';
  29.     const MAIL_TEMPLATE_ADMIN_EMPTY_POOL 'cogi_licensemanager_empty_pool';
  30.     public function install(InstallContext $context): void {
  31.         $this->installMailTemplates();
  32.         parent::install($context);
  33.     }
  34.     public function installMailTemplates(): void {
  35.         /** @var Connection $connection */
  36.         $connection $this->container->get('Doctrine\DBAL\Connection');
  37.         //Customer Emails
  38.         try {
  39.             $templateTypeData['technicalName'] = self::MAIL_TEMPLATE_CUSTOMER;
  40.             $templateTypeData['availableEntities'] = [
  41.                 'order' => 'order',
  42.                 'previousState' => 'state_machine_state',
  43.                 'newState' => 'state_machine_state',
  44.                 'salesChannel' => 'sales_channel'
  45.             ];
  46.             $templateTypeData['enName'] = 'LicenseManager License Key';
  47.             $templateTypeData['deName'] = 'LicenseManager Lizense Key';
  48.             $mailTemplateTypeId $this->createMailTemplateType($connection$templateTypeData);
  49.             $templateData['en-GB']['senderName'] = '{{ salesChannel.name }}';
  50.             $templateData['en-GB']['subject'] = 'Your License Key';
  51.             $templateData['en-GB']['description'] = '';
  52.             $templateData['en-GB']['contentHtml'] = $this->getMailContent('en-GB''licensekey''html');
  53.             $templateData['en-GB']['contentPlain'] = $this->getMailContent('en-GB''licensekey''plain');
  54.             $templateData['de-DE']['senderName'] = '{{ salesChannel.name }}';
  55.             $templateData['de-DE']['subject'] = 'Dein Lizenzschlüssel';
  56.             $templateData['de-DE']['description'] = '';
  57.             $templateData['de-DE']['contentHtml'] = $this->getMailContent('de-DE''licensekey''html');
  58.             $templateData['de-DE']['contentPlain'] = $this->getMailContent('de-DE''licensekey''plain');
  59.             $this->createMailTemplate($connection$mailTemplateTypeId$templateData);
  60.         } catch (\Exception $e) {
  61.         }
  62.         //ADMIN Emails
  63.         try {
  64.             $templateTypeData['technicalName'] = self::MAIL_TEMPLATE_ADMIN;
  65.             $templateTypeData['availableEntities'] = [
  66.                 'order' => 'order',
  67.                 'previousState' => 'state_machine_state',
  68.                 'newState' => 'state_machine_state',
  69.                 'salesChannel' => 'sales_channel'
  70.             ];
  71.             $templateTypeData['enName'] = 'LicenseManager Sold Keys';
  72.             $templateTypeData['deName'] = 'LicenseManager Lizenzen Verkauft';
  73.             $mailTemplateTypeId $this->createMailTemplateType($connection$templateTypeData);
  74.             $templateData['en-GB']['senderName'] = '{{ salesChannel.name }}';
  75.             $templateData['en-GB']['subject'] = 'Keys Sold';
  76.             $templateData['en-GB']['description'] = '';
  77.             $templateData['en-GB']['contentHtml'] = $this->getMailContent('en-GB''admin''html');
  78.             $templateData['en-GB']['contentPlain'] = $this->getMailContent('en-GB''admin''plain');
  79.             $templateData['de-DE']['senderName'] = '{{ salesChannel.name }}';
  80.             $templateData['de-DE']['subject'] = 'Lizenzen Verkauft';
  81.             $templateData['de-DE']['description'] = '';
  82.             $templateData['de-DE']['contentHtml'] = $this->getMailContent('de-DE''admin''html');
  83.             $templateData['de-DE']['contentPlain'] = $this->getMailContent('de-DE''admin''plain');
  84.             $this->createMailTemplate($connection$mailTemplateTypeId$templateData);
  85.         } catch (\Exception $e) {
  86.         }
  87.         //ADMIN Emails EMPTY Pool
  88.         try {
  89.             $templateTypeData['technicalName'] = self::MAIL_TEMPLATE_ADMIN_EMPTY_POOL;
  90.             $templateTypeData['availableEntities'] = [
  91.                 'order' => 'order',
  92.                 'previousState' => 'state_machine_state',
  93.                 'newState' => 'state_machine_state',
  94.                 'salesChannel' => 'sales_channel'
  95.             ];
  96.             $templateTypeData['enName'] = 'LicenseManager Empty Pool';
  97.             $templateTypeData['deName'] = 'LicenseManager Pool ist Leer';
  98.             $mailTemplateTypeId $this->createMailTemplateType($connection$templateTypeData);
  99.             $templateData['en-GB']['senderName'] = '{{ salesChannel.name }}';
  100.             $templateData['en-GB']['subject'] = 'Empty Pool';
  101.             $templateData['en-GB']['description'] = '';
  102.             $templateData['en-GB']['contentHtml'] = $this->getMailContent('en-GB''empty''html');
  103.             $templateData['en-GB']['contentPlain'] = $this->getMailContent('en-GB''empty''plain');
  104.             $templateData['de-DE']['senderName'] = '{{ salesChannel.name }}';
  105.             $templateData['de-DE']['subject'] = 'Leerer Pool';
  106.             $templateData['de-DE']['description'] = '';
  107.             $templateData['de-DE']['contentHtml'] = $this->getMailContent('de-DE''empty''html');
  108.             $templateData['de-DE']['contentPlain'] = $this->getMailContent('de-DE''empty''plain');
  109.             $this->createMailTemplate($connection$mailTemplateTypeId$templateData);
  110.         } catch (\Exception $e) {
  111.         }
  112.     }
  113.     public function update(UpdateContext $context): void {
  114.         $this->installMailTemplates();
  115.         parent::update($context);
  116.     }
  117.     public function activate(ActivateContext $context): void {
  118.         $this->installMailTemplates();
  119.         parent::activate($context);
  120.     }
  121.     public function deactivate(DeactivateContext $context): void {
  122.         // your code you need to run while your plugin gets deactivated
  123.     }
  124.     public function uninstall(UninstallContext $uninstallContext): void {
  125.         $context = new Context(new SystemSource());
  126.         try {
  127.             $this->deleteMailTemplate($contextself::MAIL_TEMPLATE_CUSTOMER);
  128.             $this->deleteMailTemplateType($contextself::MAIL_TEMPLATE_CUSTOMER);
  129.             $this->deleteMailTemplateType($contextself::MAIL_TEMPLATE_ADMIN);
  130.             $this->deleteMailTemplateType($contextself::MAIL_TEMPLATE_ADMIN_EMPTY_POOL);
  131.         } catch (\Exception $e) {
  132.         }
  133.         if ($uninstallContext->keepUserData()) {
  134.             return;
  135.         }
  136.         $connection $this->container->get(Connection::class);
  137.         $connection->executeStatement('SET foreign_key_checks = 0;');
  138.         $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`;');
  139.         $connection->executeStatement('SET foreign_key_checks = 1;');
  140.     }
  141.     /**
  142.      * @param Connection $connection
  143.      * @param array $data
  144.      *
  145.      * @return string
  146.      *
  147.      * @throws DBALException
  148.      * @throws InconsistentCriteriaIdsException
  149.      * @throws InvalidUuidException
  150.      */
  151.     private function createMailTemplateType(Connection $connection, array $data): string {
  152.         $mailTemplateTypeId Uuid::randomHex();
  153.         $connection->insert('mail_template_type', [
  154.             'id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  155.             'technical_name' => $data['technicalName'],
  156.             'available_entities' => json_encode($data['availableEntities']),
  157.             'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  158.         ]);
  159.         $connection->insert('mail_template_type_translation', [
  160.             'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  161.             'language_id' => Uuid::fromHexToBytes($this->getLanguageIdByLocale('en-GB')),
  162.             'name' => $data['enName'],
  163.             'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  164.         ]);
  165.         $connection->insert('mail_template_type_translation', [
  166.             'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  167.             'language_id' => Uuid::fromHexToBytes($this->getLanguageIdByLocale('de-DE')),
  168.             'name' => $data['deName'],
  169.             'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  170.         ]);
  171.         return $mailTemplateTypeId;
  172.     }
  173.     /**
  174.      * @param Connection $connection
  175.      * @param string $mailTemplateTypeId
  176.      * @param array $data
  177.      *
  178.      * @return void
  179.      *
  180.      * @throws DBALException
  181.      * @throws InconsistentCriteriaIdsException
  182.      * @throws InvalidUuidException
  183.      */
  184.     private function createMailTemplate(Connection $connectionstring $mailTemplateTypeId, array $data): void {
  185.         $mailTemplateId Uuid::randomHex();
  186.         $connection->insert('mail_template', [
  187.             'id' => Uuid::fromHexToBytes($mailTemplateId),
  188.             'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  189.             'system_default' => true,
  190.             'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  191.         ]);
  192.         $connection->insert('mail_template_translation', [
  193.             'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
  194.             'language_id' => Uuid::fromHexToBytes($this->getLanguageIdByLocale('en-GB')),
  195.             'sender_name' => $data['en-GB']['senderName'],
  196.             'subject' => $data['en-GB']['subject'],
  197.             'description' => $data['en-GB']['description'],
  198.             'content_html' => $data['en-GB']['contentHtml'],
  199.             'content_plain' => $data['en-GB']['contentPlain'],
  200.             'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  201.         ]);
  202.         $connection->insert('mail_template_translation', [
  203.             'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
  204.             'language_id' => Uuid::fromHexToBytes($this->getLanguageIdByLocale('de-DE')),
  205.             'sender_name' => $data['de-DE']['senderName'],
  206.             'subject' => $data['de-DE']['subject'],
  207.             'description' => $data['de-DE']['description'],
  208.             'content_html' => $data['de-DE']['contentHtml'],
  209.             'content_plain' => $data['de-DE']['contentPlain'],
  210.             'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  211.         ]);
  212.     }
  213.     /**
  214.      * @param string $locale
  215.      *
  216.      * @return string
  217.      *
  218.      * @throws InconsistentCriteriaIdsException
  219.      */
  220.     private function getLanguageIdByLocale(string $locale): string {
  221.         $context = new Context(new SystemSource());
  222.         /** @var EntityRepository $languageRepository */
  223.         $languageRepository $this->container->get('language.repository');
  224.         $criteria = new Criteria();
  225.         $criteria->addAssociation('locale');
  226.         $criteria->addFilter(new EqualsFilter('locale.code'$locale));
  227.         /** @var LanguageEntity $languageEntity */
  228.         $languageEntity $languageRepository->search($criteria$context)->first();
  229.         return $languageEntity->getId();
  230.     }
  231.     /**
  232.      * @param string $locale
  233.      * @param string $prefix
  234.      * @param string $type
  235.      *
  236.      * @return string
  237.      */
  238.     private function getMailContent(string $localestring $prefixstring $type): string {
  239.         $path $this->getPath() . '/Resources/email/' $locale '/';
  240.         switch ($type) {
  241.             case 'html':
  242.                 $ext 'html';
  243.                 break;
  244.             case 'plain':
  245.                 $ext 'txt';
  246.                 break;
  247.             default:
  248.                 $ext 'txt';
  249.         }
  250.         $file $path $prefix '-' $type '.' $ext;
  251.         if (!is_file($file)) {
  252.             throw new FileNotFoundException($file);
  253.         }
  254.         return file_get_contents($file);
  255.     }
  256.     /**
  257.      * @param string $mailTemplateId
  258.      *
  259.      * @throws DBALException
  260.      */
  261.     private function deleteMailTemplateFromSalesChannels(string $mailTemplateId): void {
  262.         /** @var Connection $connection */
  263.         $connection $this->container->get('Doctrine\DBAL\Connection');
  264.         $connection->executeQuery('DELETE FROM `mail_template_sales_channel` WHERE `mail_template_id` = :id', ['id' => $mailTemplateId]);
  265.     }
  266.     /**
  267.      * @param Context $context
  268.      * @param string $technicalName
  269.      *
  270.      * @throws InconsistentCriteriaIdsException
  271.      * @throws DBALException
  272.      */
  273.     private function deleteMailTemplate(Context $contextstring $technicalName): void {
  274.         $mailTemplateTypeId $this->getMailTemplateTypeId($context$technicalName);
  275.         $mailTemplateId $this->getMailTemplateId($context$mailTemplateTypeId);
  276.         $this->deleteMailTemplateFromSalesChannels($mailTemplateId);
  277.         /** @var EntityRepository $mailTemplateRepository */
  278.         $mailTemplateRepository $this->container->get('mail_template.repository');
  279.         $mailTemplateRepository->delete([
  280.             [
  281.                 'id' => $mailTemplateId
  282.             ]
  283.         ], $context);
  284.     }
  285.     /**
  286.      * @param Context $context
  287.      * @param string $mailTemplateTypeId
  288.      *
  289.      * @return string
  290.      *
  291.      */
  292.     private function getMailTemplateId(Context $contextstring $mailTemplateTypeId): string {
  293.         /** @var EntityRepository $mailTemplateRepository */
  294.         $mailTemplateRepository $this->container->get('mail_template.repository');
  295.         $criteria = new Criteria();
  296.         $criteria->addFilter(new EqualsFilter('mailTemplateTypeId'$mailTemplateTypeId));
  297.         /** @var MailTemplateEntity $mailTemplateEntity */
  298.         $mailTemplateEntity $mailTemplateRepository->search($criteria$context)->first();
  299.         return $mailTemplateEntity->getId();
  300.     }
  301.     /**
  302.      * @param Context $context
  303.      * @param string $technicalName
  304.      *
  305.      * @throws InconsistentCriteriaIdsException
  306.      */
  307.     private function deleteMailTemplateType(Context $contextstring $technicalName): void {
  308.         $mailTemplateTypeId $this->getMailTemplateTypeId($context$technicalName);
  309.         /** @var EntityRepository $mailTemplateTypeRepository */
  310.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  311.         $mailTemplateTypeRepository->delete([
  312.             [
  313.                 'id' => $mailTemplateTypeId
  314.             ]
  315.         ], $context);
  316.     }
  317.     /**
  318.      * @param Context $context
  319.      * @param string $technicalName
  320.      *
  321.      * @return string
  322.      *
  323.      */
  324.     private function getMailTemplateTypeId(Context $contextstring $technicalName): string {
  325.         /** @var EntityRepository $mailTemplateTypeRepository */
  326.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  327.         $criteria = new Criteria();
  328.         $criteria->addFilter(new EqualsFilter('technicalName'$technicalName));
  329.         /** @var MailTemplateTypeEntity $mailTemplateTypeEntity */
  330.         $mailTemplateTypeEntity $mailTemplateTypeRepository->search($criteria$context)->first();
  331.         if ($mailTemplateTypeEntity !== null) return $mailTemplateTypeEntity->getId(); else
  332.             return "";
  333.     }
  334. }