vendor/store.shopware.com/cogicontest/src/CogiContest.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cogi\CogiContest;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Content\ImportExport\Exception\FileNotFoundException;
  5. use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
  6. use Shopware\Core\Defaults;
  7. use Shopware\Core\Framework\Api\Context\SystemSource;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\Plugin;
  13. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  14. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  15. use Shopware\Core\Framework\Uuid\Uuid;
  16. use Shopware\Core\System\Language\LanguageEntity;
  17. class CogiContest extends Plugin
  18. {
  19.     public const TEMPLATE_TYPE_TECHNICAL_NAME 'contest_type';
  20.     public const MAIL_TEMPLATE_NAMTE "ContestMailTemplate";
  21.     public function install(InstallContext $installContext): void
  22.     {
  23.         /** @var Connection $connection */
  24.         $connection $this->container->get(Connection::class);
  25.         //Customer Emails
  26. //        $context = new Context(new SystemSource());
  27.         $languageTypeRepository $this->container->get('mail_template_type.repository');
  28.         $criteria = new Criteria();
  29.         $criteria->setLimit(1);
  30.         $criteria->addFilter(new EqualsFilter('technicalName'self::TEMPLATE_TYPE_TECHNICAL_NAME));
  31.         /** @var MailTemplateTypeEntity $languageTypeEntity */
  32.         $languageTypeEntity $languageTypeRepository->search($criteria$installContext->getContext())->first();
  33.         if(isset($languageTypeEntity)){
  34.             $typeName $languageTypeEntity->getTechnicalName();
  35.         }
  36.         else {
  37.             $typeName " ";
  38.         }
  39.         if ($typeName != self::TEMPLATE_TYPE_TECHNICAL_NAME) {
  40.             $templateTypeData['technicalName'] = self::TEMPLATE_TYPE_TECHNICAL_NAME;
  41.             $templateTypeData['availableEntities'] = [
  42.                 'order' => 'order',
  43.                 'salesChannel' => 'sales_channel',
  44.                 'customer' => 'customer',
  45.                 'mailHeaderFooter' => 'mail_header_footer'
  46.             ];
  47.             $templateTypeData['enName'] = 'Contest';
  48.             $templateTypeData['deName'] = 'Gewinnspiel';
  49.             $mailTemplateTypeId $this->createMailTemplateType($connection$templateTypeData);
  50.             $templateData['en-GB']['senderName'] = '{{ salesChannel.translated.name }}';
  51.             $templateData['en-GB']['subject'] = 'Congratulations, you have won! {{ salesChannel.translated.name }}';
  52.             $templateData['en-GB']['description'] = '';
  53.             $templateData['en-GB']['contentHtml'] = $this->getMailContent('en-GB''contest''html');
  54.             $templateData['en-GB']['contentPlain'] = $this->getMailContent('en-GB''contest''plain');
  55.             $templateData['de-DE']['senderName'] = '{{ salesChannel.translated.name }}';
  56.             $templateData['de-DE']['subject'] = 'Du hast gewonnen! {{ salesChannel.translated.name }}';
  57.             $templateData['de-DE']['description'] = '';
  58.             $templateData['de-DE']['contentHtml'] = $this->getMailContent('de-DE''contest''html');
  59.             $templateData['de-DE']['contentPlain'] = $this->getMailContent('de-DE''contest''plain');
  60.             $this->createMailTemplate($connection$mailTemplateTypeId$templateData);
  61.         }
  62.         parent::install($installContext);
  63.     }
  64.     public function uninstall(UninstallContext $uninstallContext): void
  65.     {
  66.         parent::uninstall($uninstallContext);
  67.         if ($uninstallContext->keepUserData()) {
  68.             return;
  69.         }
  70.         //get the Templates and Associations added by this Plugin from the DB
  71.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  72.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  73.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  74.         $mailTemplateRepository $this->container->get('mail_template.repository');
  75.         /** @var MailTemplateTypeEntity $myCustomMailTemplateType */
  76.         $myCustomMailTemplateType $mailTemplateTypeRepository->search(
  77.             (new Criteria())
  78.                 ->addFilter(new EqualsFilter('technicalName'self::TEMPLATE_TYPE_TECHNICAL_NAME)),
  79.             $uninstallContext
  80.                 ->getContext()
  81.         )->first();
  82.         $mailTemplateIds $mailTemplateRepository->searchIds(
  83.             (new Criteria())
  84.                 ->addFilter(new EqualsFilter('mailTemplateTypeId'$myCustomMailTemplateType->getId())),
  85.             $uninstallContext
  86.                 ->getContext()
  87.         )->getIds();
  88.         //Get the Ids from the fetched Entities
  89.         $ids array_map(static function ($id) {
  90.             return ['id' => $id];
  91.         }, $mailTemplateIds);
  92.         //Delete the Templates which were added by this Plugin
  93.         $mailTemplateRepository->delete($ids$uninstallContext->getContext());
  94.         //Delete the TemplateType which were added by this Plugin
  95.         $mailTemplateTypeRepository->delete([
  96.             ['id' => $myCustomMailTemplateType->getId()]
  97.         ], $uninstallContext->getContext());
  98.         $connection $this->container->get(Connection::class);
  99.         $connection->exec('DROP TABLE IF EXISTS `cogi_contest_member`');
  100.         $connection->exec('DROP TABLE IF EXISTS `cogi_contest_translation`');
  101.         $connection->exec('DROP TABLE IF EXISTS `cogi_contest`');
  102.     }
  103.     private function createMailTemplateType(Connection $connection, array $data): string
  104.     {
  105.         $mailTemplateTypeId Uuid::randomHex();
  106.         $defaultLangId $this->getLanguageIdByLocale($connection'en-GB');
  107.         $deLangId $this->getLanguageIdByLocale($connection'de-DE');
  108.         $defaultSystemLanguage Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM);
  109.         $connection->insert('mail_template_type', [
  110.             'id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  111.             'technical_name' => $data['technicalName'],
  112.             'available_entities' => json_encode($data['availableEntities']),
  113.             'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  114.         ]);
  115.         if ($defaultLangId !== $deLangId) {
  116.             $connection->insert('mail_template_type_translation', [
  117.                 'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  118.                 'language_id' => $defaultLangId,
  119.                 'name' => $data['enName'],
  120.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  121.             ]);
  122.         }
  123.         if ($defaultLangId !== $defaultSystemLanguage && $deLangId !== $defaultSystemLanguage) {
  124.             $connection->insert('mail_template_type_translation', [
  125.                 'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  126.                 'language_id' => $defaultSystemLanguage,
  127.                 'name' => $data['enName'],
  128.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  129.             ]);
  130.         }
  131.         if ($deLangId) {
  132.             $connection->insert('mail_template_type_translation', [
  133.                 'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  134.                 'language_id' => $deLangId,
  135.                 'name' => $data['deName'],
  136.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  137.             ]);
  138.         }
  139.         return $mailTemplateTypeId;
  140.     }
  141.     private function getMailContent(string $localestring $prefixstring $type): string {
  142.         $path $this->getPath() . '/Resources/email/' $locale '/';
  143.         switch ($type) {
  144.             case 'html':
  145.                 $ext 'html';
  146.                 break;
  147.             case 'plain':
  148.                 $ext 'txt';
  149.                 break;
  150.             default:
  151.                 $ext 'txt';
  152.         }
  153.         $file $path $prefix '-' $type '.' $ext;
  154.         if (!is_file($file)) {
  155.             throw new FileNotFoundException($file);
  156.         }
  157.         return file_get_contents($file);
  158.     }
  159.     private function createMailTemplate(Connection $connectionstring $mailTemplateTypeId, array $data): void {
  160.         $mailTemplateId Uuid::randomHex();
  161.         $defaultLangId $this->getLanguageIdByLocale($connection'en-GB');
  162.         $deLangId $this->getLanguageIdByLocale($connection'de-DE');
  163.         $defaultSystemLanguage Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM);
  164.         $connection->insert('mail_template', [
  165.             'id' => Uuid::fromHexToBytes($mailTemplateId),
  166.             'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  167.             'system_default' => true,
  168.             'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  169.         ]);
  170.         if ($defaultLangId !== $deLangId) {
  171.             $connection->insert('mail_template_translation', [
  172.                 'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
  173.                 'language_id' => $defaultLangId,
  174.                 'sender_name' => $data['en-GB']['senderName'],
  175.                 'subject' => $data['en-GB']['subject'],
  176.                 'description' => $data['en-GB']['description'],
  177.                 'content_html' => $data['en-GB']['contentHtml'],
  178.                 'content_plain' => $data['en-GB']['contentPlain'],
  179.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  180.             ]);
  181.         }
  182.         if ($defaultLangId !== $defaultSystemLanguage && $deLangId !== $defaultSystemLanguage) {
  183.             $connection->insert('mail_template_translation', [
  184.                 'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
  185.                 'language_id' => $defaultSystemLanguage,
  186.                 'sender_name' => $data['en-GB']['senderName'],
  187.                 'subject' => $data['en-GB']['subject'],
  188.                 'description' => $data['en-GB']['description'],
  189.                 'content_html' => $data['en-GB']['contentHtml'],
  190.                 'content_plain' => $data['en-GB']['contentPlain'],
  191.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  192.             ]);
  193.         }
  194.         if ($deLangId) {
  195.             $connection->insert('mail_template_translation', [
  196.                 'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
  197.                 'language_id' => $deLangId,
  198.                 'sender_name' => $data['de-DE']['senderName'],
  199.                 'subject' => $data['de-DE']['subject'],
  200.                 'description' => $data['de-DE']['description'],
  201.                 'content_html' => $data['de-DE']['contentHtml'],
  202.                 'content_plain' => $data['de-DE']['contentPlain'],
  203.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  204.             ]);
  205.         }
  206.     }
  207.     private function getLanguageIdByLocale(Connection $connectionstring $locale): ?string
  208.     {
  209.         $sql = <<<'SQL'
  210. SELECT `language`.`id`
  211. FROM `language`
  212. INNER JOIN `locale` ON `locale`.`id` = `language`.`locale_id`
  213. WHERE `locale`.`code` = :code
  214. SQL;
  215.         $languageId $connection->executeQuery($sql, ['code' => $locale])->fetchColumn();
  216.         if (!$languageId && $locale !== 'en-GB') {
  217.             return null;
  218.         }
  219.         if (!$languageId) {
  220.             return Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM);
  221.         }
  222.         return $languageId;
  223.     }
  224. }