vendor/store.shopware.com/nenomarketingessentials/src/NenoMarketingEssentials.php line 18

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Neno\MarketingEssentials;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  5. use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
  6. use Shopware\Core\Defaults;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  11. use Shopware\Core\Framework\Plugin;
  12. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  14. use Shopware\Core\Framework\Uuid\Uuid;
  15. class NenoMarketingEssentials extends Plugin
  16. {
  17.     // Newsletter mail
  18.     public const TEMPLATE_TYPE_NAME 'Neno newsletter registration promotion';
  19.     public const TEMPLATE_TYPE_TECHNICAL_NAME 'neno_newsletter_register_promotion';
  20.     // Register mail
  21.     public const REGISTER_PROMOTION_TEMPLATE_TYPE_NAME 'Neno registration promotion';
  22.     public const REGISTER_PROMOTION_TEMPLATE_TYPE_TECHNICAL_NAME 'neno_register_promotion';
  23.     private function fetchMailTemplateType($typeConnection $connection) {
  24.         $result $connection->fetchAllAssociative('SELECT * FROM mail_template_type WHERE technical_name = ?', array($type));
  25.         if ($result && array_key_exists(0$result)) {
  26.             return Uuid::fromBytesToHex($result[0]['id']);
  27.         }
  28.         return null;
  29.     }
  30.     private function checkMailTemplate($id$context) {
  31.         $mailTemplateRepository $this->container->get('mail_template.repository');
  32.         $criteria = new Criteria();
  33.         $criteria->addFilter(new EqualsFilter('mailTemplateTypeId'$id));
  34.         return $mailTemplateRepository->search($criteria$context->getContext())->first() != null;
  35.     }
  36.     public function install(InstallContext $installContext): void
  37.     {
  38.         $connection $this->container->get(Connection::class);
  39.         // Create newsletter mail
  40.         $newsletterMailTemplateTypeId $this->fetchMailTemplateType(self::TEMPLATE_TYPE_TECHNICAL_NAME$connection);
  41.         if (!$newsletterMailTemplateTypeId) {
  42.             $newsletterMailTemplateTypeId Uuid::randomHex();
  43.             $this->createNewsletterMailTemplateType($connection$newsletterMailTemplateTypeId);
  44.         }
  45.         if (!$this->checkMailTemplate($newsletterMailTemplateTypeId$installContext)) {
  46.             try {
  47.                 $this->createNewsletterMailTemplate($connection$newsletterMailTemplateTypeId);
  48.             } catch (UniqueConstraintViolationException $err) {}
  49.         }
  50.         // Create register mail
  51.         $registerMailTemplateTypeId $this->fetchMailTemplateType(self::REGISTER_PROMOTION_TEMPLATE_TYPE_TECHNICAL_NAME$connection);
  52.         if(!$registerMailTemplateTypeId) {
  53.             $registerMailTemplateTypeId Uuid::randomHex();
  54.             $this->createRegisterMailTemplateType($connection$registerMailTemplateTypeId);
  55.         }
  56.         if (!$this->checkMailTemplate($registerMailTemplateTypeId$installContext)) {
  57.             try {
  58.                 $this->createRegisterMailTemplate($connection$registerMailTemplateTypeId);
  59.             } catch (UniqueConstraintViolationException $err) {}
  60.         }
  61.     }
  62.     /* ---- Newsletter mail ---- */
  63.     // create mail template type
  64.     private function createNewsletterMailTemplateType($connection$mailTemplateTypeId)
  65.     {
  66.         $enLangId $this->getLanguageIdByLocale($connection'en-GB');
  67.         $deLangId $this->getLanguageIdByLocale($connection'de-DE');
  68.         $connection->insert('mail_template_type', [
  69.             'id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  70.             'technical_name' => self::TEMPLATE_TYPE_TECHNICAL_NAME,
  71.             'available_entities' => json_encode([
  72.                 'newsletterRecipient' => 'newsletter_recipient',
  73.                 'promotion' => 'promotion',
  74.                 'salesChannel' => 'sales_channel'
  75.             ]),
  76.             'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  77.         ]);
  78.         if ($enLangId) {
  79.             $connection->insert('mail_template_type_translation', [
  80.                 'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  81.                 'language_id' => $enLangId,
  82.                 'name' => self::TEMPLATE_TYPE_NAME,
  83.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  84.             ]);
  85.         }
  86.         if ($deLangId) {
  87.             $connection->insert('mail_template_type_translation', [
  88.                 'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  89.                 'language_id' => $deLangId,
  90.                 'name' => self::TEMPLATE_TYPE_NAME,
  91.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  92.             ]);
  93.         }
  94.         if (
  95.             $deLangId != Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM) &&
  96.             $enLangId != Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM)
  97.         ) {
  98.             $connection->insert('mail_template_type_translation', [
  99.                 'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  100.                 'language_id' => Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM),
  101.                 'name' => self::TEMPLATE_TYPE_NAME,
  102.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  103.             ]);
  104.         }
  105.     }
  106.     // Create mail template
  107.     private function createNewsletterMailTemplate($connection$mailTemplateTypeId)
  108.     {
  109.         $mailTemplateId Uuid::randomHex();
  110.         $enLangId $this->getLanguageIdByLocale($connection'en-GB');
  111.         $deLangId $this->getLanguageIdByLocale($connection'de-DE');
  112.         $connection->insert('mail_template', [
  113.             'id' => Uuid::fromHexToBytes($mailTemplateId),
  114.             'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  115.             'system_default' => true,
  116.             'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  117.         ]);
  118.         if ($enLangId) {
  119.             $connection->insert('mail_template_translation', [
  120.                 'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
  121.                 'language_id' => $enLangId,
  122.                 'sender_name' => '{{ salesChannel.name }}',
  123.                 'subject' => 'Your coupon code. Thank you for registering for the newsletter',
  124.                 'description' => '',
  125.                 'content_html' => $this->getNewsletterContentHtmlEn(),
  126.                 'content_plain' => $this->getNewsletterContentPlainEn(),
  127.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  128.             ]);
  129.         }
  130.         if ($deLangId) {
  131.             $connection->insert('mail_template_translation', [
  132.                 'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
  133.                 'language_id' => $this->getLanguageIdByLocale($connection'de-DE'),
  134.                 'sender_name' => '{{ salesChannel.name }}',
  135.                 'subject' => 'Dein coupon code. Danke für die Registrierung zum Newsletter',
  136.                 'description' => '',
  137.                 'content_html' => $this->getNewsletterContentHtmlDe(),
  138.                 'content_plain' => $this->getNewsletterContentPlainDe(),
  139.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  140.             ]);
  141.         }
  142.         if (
  143.             $deLangId != Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM) &&
  144.             $enLangId != Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM)
  145.         ) {
  146.             $connection->insert('mail_template_translation', [
  147.                 'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
  148.                 'language_id' => Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM),
  149.                 'sender_name' => '{{ salesChannel.name }}',
  150.                 'subject' => 'Your coupon code. Thank you for registering for the newsletter',
  151.                 'description' => '',
  152.                 'content_html' => $this->getNewsletterContentHtmlEn(),
  153.                 'content_plain' => $this->getNewsletterContentPlainEn(),
  154.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  155.             ]);
  156.         }
  157.         $this->addTemplateToSalesChannels($connection$mailTemplateTypeId$mailTemplateId);
  158.     }
  159.     /* ---- Register mail ---- */
  160.     // Create mail template type
  161.     private function createRegisterMailTemplateType($connection$mailTemplateTypeId)
  162.     {
  163.         $enLangId $this->getLanguageIdByLocale($connection'en-GB');
  164.         $deLangId $this->getLanguageIdByLocale($connection'de-DE');
  165.         $connection->insert('mail_template_type', [
  166.             'id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  167.             'technical_name' => self::REGISTER_PROMOTION_TEMPLATE_TYPE_TECHNICAL_NAME,
  168.             'available_entities' => json_encode([
  169.                 'customer' => 'customer',
  170.                 'promotion' => 'promotion',
  171.                 'salesChannel' => 'sales_channel'
  172.             ]),
  173.             'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  174.         ]);
  175.         if ($enLangId) {
  176.             $connection->insert('mail_template_type_translation', [
  177.                 'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  178.                 'language_id' => $enLangId,
  179.                 'name' => self::REGISTER_PROMOTION_TEMPLATE_TYPE_NAME,
  180.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  181.             ]);
  182.         }
  183.         if ($deLangId) {
  184.             $connection->insert('mail_template_type_translation', [
  185.                 'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  186.                 'language_id' => $deLangId,
  187.                 'name' => self::REGISTER_PROMOTION_TEMPLATE_TYPE_NAME,
  188.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  189.             ]);
  190.         }
  191.         if (
  192.             $deLangId != Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM) &&
  193.             $enLangId != Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM)
  194.         ) {
  195.             $connection->insert('mail_template_type_translation', [
  196.                 'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  197.                 'language_id' => Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM),
  198.                 'name' => self::REGISTER_PROMOTION_TEMPLATE_TYPE_NAME,
  199.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  200.             ]);
  201.         }
  202.     }
  203.     // Create mail template
  204.     private function createRegisterMailTemplate($connection$mailTemplateTypeId)
  205.     {
  206.         $mailTemplateId Uuid::randomHex();
  207.         $enLangId $this->getLanguageIdByLocale($connection'en-GB');
  208.         $deLangId $this->getLanguageIdByLocale($connection'de-DE');
  209.         $connection->insert('mail_template', [
  210.             'id' => Uuid::fromHexToBytes($mailTemplateId),
  211.             'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  212.             'system_default' => true,
  213.             'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  214.         ]);
  215.         if ($enLangId) {
  216.             $connection->insert('mail_template_translation', [
  217.                 'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
  218.                 'language_id' => $enLangId,
  219.                 'sender_name' => '{{ salesChannel.name }}',
  220.                 'subject' => 'Your coupon code. Thank you for registering',
  221.                 'description' => '',
  222.                 'content_html' => $this->getRegisterContentHtmlEn(),
  223.                 'content_plain' => $this->getRegisterContentPlainEn(),
  224.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  225.             ]);
  226.         }
  227.         if ($deLangId) {
  228.             $connection->insert('mail_template_translation', [
  229.                 'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
  230.                 'language_id' => $this->getLanguageIdByLocale($connection'de-DE'),
  231.                 'sender_name' => '{{ salesChannel.name }}',
  232.                 'subject' => 'Dein coupon code. Danke für die Registrierung',
  233.                 'description' => '',
  234.                 'content_html' => $this->getRegisterContentHtmlDe(),
  235.                 'content_plain' => $this->getRegisterContentPlainDe(),
  236.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  237.             ]);
  238.         }
  239.         if (
  240.             $deLangId != Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM) &&
  241.             $enLangId != Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM)
  242.         ) {
  243.             $connection->insert('mail_template_translation', [
  244.                 'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
  245.                 'language_id' => Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM),
  246.                 'sender_name' => '{{ salesChannel.name }}',
  247.                 'subject' => 'Your coupon code. Thank you for registering',
  248.                 'description' => '',
  249.                 'content_html' => $this->getRegisterContentHtmlEn(),
  250.                 'content_plain' => $this->getRegisterContentPlainEn(),
  251.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  252.             ]);
  253.         }
  254.         $this->addTemplateToSalesChannels($connection$mailTemplateTypeId$mailTemplateId);
  255.     }
  256.     /* ---- Add templates to sales channels ---- */
  257.     private function addTemplateToSalesChannels(Connection $connectionstring $mailTemplateTypeIdstring $mailTemplateId): void
  258.     {
  259.         $salesChannels $connection->fetchAllAssociative('SELECT `id` FROM `sales_channel` ');
  260.         foreach ($salesChannels as $salesChannel) {
  261.             $mailTemplateSalesChannel = [
  262.                 'id' => Uuid::randomBytes(),
  263.                 'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
  264.                 'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  265.                 'sales_channel_id' => $salesChannel['id'],
  266.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  267.             ];
  268.             $connection->insert('mail_template_sales_channel'$mailTemplateSalesChannel);
  269.         }
  270.     }
  271.     /* ---- Check for languages ---- */
  272.     private function getLanguageIdByLocale(Connection $connectionstring $locale): ?string
  273.     {
  274.         $sql = <<<SQL
  275.         SELECT `language`.`id`
  276.         FROM `language`
  277.         INNER JOIN `locale` ON `locale`.`id` = `language`.`locale_id`
  278.         WHERE `locale`.`code` = :code
  279.         SQL;
  280.         $languageId $connection->executeQuery($sql, ['code' => $locale])->fetchOne();
  281.         return $languageId;
  282.     }
  283.     /* ---- Content for newsletter registration promotion mail ----*/
  284.     // Content html
  285.     private function getNewsletterContentHtmlEn() {
  286.         return <<<MAIL
  287. Hello,<br><br>Your coupon code is {{ promotion.code }}.<br><br>Thank you for registering for the newsletter.
  288. MAIL;
  289.     }
  290.     private function getNewsletterContentHtmlDe() {
  291.         return <<<MAIL
  292. Hallo,<br><br>Dein Coupon Code lautet {{ promotion.code }}.<br><br>Vielen Dank für deine Registrierung zum Newsletter.
  293. MAIL;
  294.     }
  295.     // Content plain
  296.     private function getNewsletterContentPlainEn() {
  297.         return <<<MAIL
  298. Hello,\n\n Your coupon code is {{ promotion.code }}.\n\n Thank you for registering for the newsletter.
  299. MAIL;
  300.     }
  301.     private function getNewsletterContentPlainDe() {
  302.         return <<<MAIL
  303. Hallo,\n\n Dein Coupon Code lautet {{ promotion.code }}.\n\n Vielen Dank für deine Registrierung zum Newsletter.
  304. MAIL;
  305.     }
  306.     /* ---- Content for registration promotion mail ----*/
  307.     // Content html
  308.     private function getRegisterContentHtmlEn() {
  309.         return <<<MAIL
  310. Hello,<br><br>Your coupon code is {{ promotion.code }}.<br><br>Thank you for your registration.
  311. MAIL;
  312.     }
  313.     private function getRegisterContentHtmlDe() {
  314.         return <<<MAIL
  315. Hallo,<br><br>Dein Coupon Code lautet {{ promotion.code }}.<br><br>Vielen Dank für deine Registrierung.
  316. MAIL;
  317.     }
  318.     // Content plain
  319.     private function getRegisterContentPlainEn() {
  320.         return <<<MAIL
  321. Hello,\n\n Your coupon code is {{ promotion.code }}.\n\n Thank you for your registration.
  322. MAIL;
  323.     }
  324.     private function getRegisterContentPlainDe() {
  325.         return <<<MAIL
  326. Hallo,\n\n Dein Coupon Code lautet {{ promotion.code }}.\n\n Vielen Dank für deine Registrierung.
  327. MAIL;
  328.     }
  329.     public function uninstall(UninstallContext $context): void
  330.     {
  331.         parent::uninstall($context);
  332.         if ($context->keepUserData()) {
  333.             return;
  334.         }
  335.         $connection $this->container->get(Connection::class);
  336.         $connection->executeUpdate('DROP TABLE IF EXISTS `neno_marketing_essentials_newsletter_popup_translation`');
  337.         $connection->executeUpdate('DROP TABLE IF EXISTS `neno_marketing_essentials_register_popup_translation`');
  338.         $connection->executeUpdate('DROP TABLE IF EXISTS `neno_marketing_essentials_tabs_translation`');
  339.         $connection->executeUpdate('DROP TABLE IF EXISTS `neno_marketing_essentials_conversion_bar_translation`');
  340.         $connection->executeUpdate('DROP TABLE IF EXISTS `neno_marketing_essentials_newsletter_popup`');
  341.         $connection->executeUpdate('DROP TABLE IF EXISTS `neno_marketing_essentials_register_popup`');
  342.         $connection->executeUpdate('DROP TABLE IF EXISTS `neno_marketing_essentials_tabs`');
  343.         $connection->executeUpdate('DROP TABLE IF EXISTS `neno_marketing_essentials_conversion_bar`');
  344.         //get the Templates and Associations added by this Plugin from the DB
  345.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  346.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  347.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  348.         $mailTemplateRepository $this->container->get('mail_template.repository');
  349.         /** @var MailTemplateTypeEntity $myCustomMailTemplateType */
  350.         $myCustomMailTemplateType $mailTemplateTypeRepository->search(
  351.             (new Criteria())
  352.                 ->addFilter(new EqualsFilter('technicalName'self::TEMPLATE_TYPE_TECHNICAL_NAME)),
  353.             $context
  354.                 ->getContext()
  355.         )->first();
  356.         /** @var MailTemplateTypeEntity $registerMailTemplateType */
  357.         $registerMailTemplateType $mailTemplateTypeRepository->search(
  358.             (new Criteria())
  359.                 ->addFilter(new EqualsFilter('technicalName'self::REGISTER_PROMOTION_TEMPLATE_TYPE_TECHNICAL_NAME)),
  360.             $context
  361.                 ->getContext()
  362.         )->first();
  363.         $mailTemplateIds $mailTemplateRepository->searchIds(
  364.             (new Criteria())
  365.                 ->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR,[
  366.                     new EqualsFilter('mailTemplateTypeId'$myCustomMailTemplateType->getId()),
  367.                     new EqualsFilter('mailTemplateTypeId'$registerMailTemplateType->getId()),
  368.                 ])),
  369.             $context
  370.                 ->getContext()
  371.         )->getIds();
  372.         //Get the Ids from the fetched Entities
  373.         $ids array_map(static function ($id) {
  374.             return ['id' => $id];
  375.         }, $mailTemplateIds);
  376.         //Delete the Templates which were added by this Plugin
  377.         $mailTemplateRepository->delete($ids$context->getContext());
  378.         //Delete the TemplateType which were added by this Plugin
  379.         $mailTemplateTypeRepository->delete([
  380.             ['id' => $myCustomMailTemplateType->getId()],
  381.             ['id' => $registerMailTemplateType->getId()]
  382.         ], $context->getContext());
  383.     }
  384. }