custom/plugins/CogiSurvey/src/CogiSurvey.php line 21

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cogi\CogiSurvey;
  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\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\Framework\Plugin;
  14. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  15. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  16. use Shopware\Core\Framework\Uuid\Uuid;
  17. use Shopware\Core\System\Language\LanguageEntity;
  18. class CogiSurvey extends Plugin
  19. {
  20.     public const TEMPLATE_TYPE_TECHNICAL_NAME 'survey_type';
  21.     public const MAIL_TEMPLATE_NAME "SurveyMailTemplate";
  22.     public function install(InstallContext $installContext): void
  23.     {
  24.         /** @var Connection $connection */
  25.         $connection $this->container->get(Connection::class);
  26.         //Customer Emails
  27.         $context = new Context(new SystemSource());
  28.         $languageTypeRepository $this->container->get('mail_template_type.repository');
  29.         $criteria = new Criteria();
  30.         $criteria->setLimit(1);
  31.         $criteria->addFilter(new EqualsFilter('technicalName'self::TEMPLATE_TYPE_TECHNICAL_NAME));
  32.         /** @var MailTemplateTypeEntity $languageTypeEntity */
  33.         $languageTypeEntity $languageTypeRepository->search($criteria$context)->first();
  34.         if(isset($languageTypeEntity)){
  35.             $typeName $languageTypeEntity->getTechnicalName();
  36.         }
  37.         else {
  38.             $typeName " ";
  39.         }
  40.         if ($typeName != self::TEMPLATE_TYPE_TECHNICAL_NAME) {
  41.             $templateTypeData['technicalName'] = self::TEMPLATE_TYPE_TECHNICAL_NAME;
  42.             $templateTypeData['availableEntities'] = [
  43.                 'order' => 'order',
  44.                 'salesChannel' => 'sales_channel',
  45.                 'customer' => 'customer'
  46.             ];
  47.             $templateTypeData['enName'] = 'Survey';
  48.             $templateTypeData['deName'] = 'Umfrage';
  49.             $mailTemplateTypeId $this->createMailTemplateType($connection$templateTypeData);
  50.             $templateData['en-GB']['senderName'] = '{{ salesChannel.translated.name }}';
  51.             $templateData['en-GB']['subject'] = 'Survey of your order from {{ salesChannel.translated.name }}';
  52.             $templateData['en-GB']['description'] = '';
  53.             $templateData['en-GB']['contentHtml'] = $this->getMailContent('en-GB''survey''html');
  54.             $templateData['en-GB']['contentPlain'] = $this->getMailContent('en-GB''survey''plain');
  55.             $templateData['de-DE']['senderName'] = '{{ salesChannel.translated.name }}';
  56.             $templateData['de-DE']['subject'] = 'Umfrage Ihrer Bestellung von {{ salesChannel.translated.name }}';
  57.             $templateData['de-DE']['description'] = '';
  58.             $templateData['de-DE']['contentHtml'] = $this->getMailContent('de-DE''survey''html');
  59.             $templateData['de-DE']['contentPlain'] = $this->getMailContent('de-DE''survey''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_survey_answer_translation`');
  100.         $connection->exec('DROP TABLE IF EXISTS `cogi_survey_translation`');
  101.         $connection->exec('DROP TABLE IF EXISTS `cogi_survey_answer`');
  102.         $connection->exec('DROP TABLE IF EXISTS `cogi_survey`');
  103.     }
  104.     private function createMailTemplateType(Connection $connection, array $data): string
  105.     {
  106.         $mailTemplateTypeId Uuid::randomHex();
  107.         $defaultLangId $this->getLanguageIdByLocale($connection'en-GB');
  108.         $deLangId $this->getLanguageIdByLocale($connection,'de-DE');
  109.         $defaultSystemLanguage Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM);
  110.         $connection->insert('mail_template_type', [
  111.             'id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  112.             'technical_name' => $data['technicalName'],
  113.             'available_entities' => json_encode($data['availableEntities']),
  114.             'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  115.         ]);
  116.         if ($defaultLangId !== $deLangId) {
  117.             $connection->insert('mail_template_type_translation', [
  118.                 'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  119.                 'language_id' => $defaultLangId,
  120.                 'name' => $data['enName'],
  121.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  122.             ]);
  123.         }
  124.         if ($defaultLangId !== $defaultSystemLanguage && $deLangId !== $defaultSystemLanguage){
  125.             $connection->insert('mail_template_type_translation', [
  126.                 'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  127.                 'language_id' => $defaultSystemLanguage,
  128.                 'name' => $data['enName'],
  129.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  130.             ]);
  131.         }
  132.         if ($deLangId) {
  133.             $connection->insert('mail_template_type_translation', [
  134.                 'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  135.                 'language_id' => $deLangId,
  136.                 'name' => $data['deName'],
  137.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  138.             ]);
  139.         }
  140.         return $mailTemplateTypeId;
  141.     }
  142.     private function getMailContent(string $localestring $prefixstring $type): string {
  143.         $path $this->getPath() . '/Resources/email/' $locale '/';
  144.         switch ($type) {
  145.             case 'html':
  146.                 $ext 'html';
  147.                 break;
  148.             case 'plain':
  149.                 $ext 'txt';
  150.                 break;
  151.             default:
  152.                 $ext 'txt';
  153.         }
  154.         $file $path $prefix '-' $type '.' $ext;
  155.         if (!is_file($file)) {
  156.             throw new FileNotFoundException($file);
  157.         }
  158.         return file_get_contents($file);
  159.     }
  160.     private function createMailTemplate(Connection $connectionstring $mailTemplateTypeId, array $data): void {
  161.         $mailTemplateId Uuid::randomHex();
  162.         $defaultLangId $this->getLanguageIdByLocale($connection,'en-GB');
  163.         $deLangId $this->getLanguageIdByLocale($connection,'de-DE');
  164.         $defaultSystemLanguage Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM);
  165.         $connection->insert('mail_template', [
  166.             'id' => Uuid::fromHexToBytes($mailTemplateId),
  167.             'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  168.             'system_default' => true,
  169.             'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  170.         ]);
  171.         if ($defaultLangId !== $deLangId) {
  172.             $connection->insert('mail_template_translation', [
  173.                 'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
  174.                 'language_id' => $defaultLangId,
  175.                 'sender_name' => $data['en-GB']['senderName'],
  176.                 'subject' => $data['en-GB']['subject'],
  177.                 'description' => $data['en-GB']['description'],
  178.                 'content_html' => $data['en-GB']['contentHtml'],
  179.                 'content_plain' => $data['en-GB']['contentPlain'],
  180.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  181.             ]);
  182.         }
  183.         if ($defaultLangId !== $defaultSystemLanguage && $deLangId !== $defaultSystemLanguage){
  184.             $connection->insert('mail_template_translation', [
  185.                 'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
  186.                 'language_id' => $defaultLangId,
  187.                 'sender_name' => $data['en-GB']['senderName'],
  188.                 'subject' => $data['en-GB']['subject'],
  189.                 'description' => $data['en-GB']['description'],
  190.                 'content_html' => $data['en-GB']['contentHtml'],
  191.                 'content_plain' => $data['en-GB']['contentPlain'],
  192.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  193.             ]);
  194.         }
  195.         if ($deLangId) {
  196.             $connection->insert('mail_template_translation', [
  197.                 'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
  198.                 'language_id' => $deLangId,
  199.                 'sender_name' => $data['de-DE']['senderName'],
  200.                 'subject' => $data['de-DE']['subject'],
  201.                 'description' => $data['de-DE']['description'],
  202.                 'content_html' => $data['de-DE']['contentHtml'],
  203.                 'content_plain' => $data['de-DE']['contentPlain'],
  204.                 'created_at' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  205.             ]);
  206.         }
  207.     }
  208.     private function getLanguageIdByLocale(Connection $connectionstring $locale): ?string
  209.     {
  210.         $sql = <<<'SQL'
  211. SELECT `language`.`id`
  212. FROM `language`
  213. INNER JOIN `locale` ON `locale`.`id` = `language`.`locale_id`
  214. WHERE `locale`.`code` = :code
  215. SQL;
  216.         $languageId $connection->executeQuery($sql, ['code' => $locale])->fetchColumn();
  217.         if (!$languageId && $locale !== 'en-GB') {
  218.             return null;
  219.         }
  220.         if (!$languageId) {
  221.             return Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM);
  222.         }
  223.         return $languageId;
  224.     }
  225. }