custom/plugins/CogiRecruiting/src/CogiRecruiting.php line 30

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cogi\CogiRecruiting;
  3. use DateTime;
  4. use Doctrine\DBAL\Connection;
  5. use Doctrine\DBAL\DBALException;
  6. use Shopware\Core\Content\ImportExport\Exception\FileNotFoundException;
  7. use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
  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\Indexing\Indexer\InheritanceIndexer;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  16. use Shopware\Core\Framework\Plugin;
  17. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  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\Language\LanguageEntity;
  24. use Symfony\Component\Config\FileLocator;
  25. use Symfony\Component\DependencyInjection\ContainerBuilder;
  26. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  27. class CogiRecruiting extends Plugin {
  28.     const MAIL_TEMPLATE_ADMIN_NEW_APPLICATION 'cogi_recruiting_admin_new_application';
  29.     const MAIL_TEMPLATE_APPLICATION_CONFIRMATION 'cogi_recruiting_application_confirmation';
  30.     public function build(ContainerBuilder $container): void {
  31.         parent::build($container);
  32.         $loader = new XmlFileLoader($container, new FileLocator(__DIR__ '/Resources/config'));
  33.         $loader->load('services.xml');
  34.     }
  35.     public function activate(ActivateContext $activateContext): void {
  36.     }
  37.     public function update(UpdateContext $updateContext): void {
  38.         if ($updateContext->getCurrentPluginVersion() === "1.1.7" || $updateContext->getUpdatePluginVersion() === "1.1.8") {
  39.             $this->_createMailTemplates($updateContext->getContext());
  40.         }
  41.     }
  42.     public function install(InstallContext $installContext): void {
  43.         $this->_createMailTemplates($installContext->getContext());
  44.     }
  45.     public function uninstall(UninstallContext $context): void {
  46.         if ($context->keepUserData()) {
  47.             parent::uninstall($context);
  48.             return;
  49.         }
  50.         /** @var Connection $connection */
  51.         $connection $this->container->get(Connection::class);
  52.         $connection->executeStatement('DROP TABLE IF EXISTS `cogi_recruiting_application_link`');
  53.         $connection->executeStatement('DROP TABLE IF EXISTS `cogi_recruiting_application_comment`');
  54.         $connection->executeStatement('DROP TABLE IF EXISTS `cogi_recruiting_application`');
  55.         $connection->executeStatement('DROP TABLE IF EXISTS `cogi_recruiting_job_translation`');
  56.         $connection->executeStatement('DROP TABLE IF EXISTS `cogi_recruiting_job_event`');
  57.         $connection->executeStatement('DROP TABLE IF EXISTS `cogi_recruiting_job`');
  58.         $connection->executeStatement('DROP TABLE IF EXISTS `cogi_recruiting_category_translation`');
  59.         $connection->executeStatement('DROP TABLE IF EXISTS `cogi_recruiting_category`');
  60.         $connection->executeStatement("DELETE FROM `seo_url_template` WHERE `entity_name` LIKE '%cogi_recruiting%';");
  61.         $connection->executeStatement("DELETE FROM `seo_url` WHERE `route_name` LIKE '%frontend.cogirecruiting%';");
  62.         // Uninstall email templates
  63.         $this->removeEmailTemplate(self::MAIL_TEMPLATE_ADMIN_NEW_APPLICATION$context->getContext());
  64.         $this->removeEmailTemplate(self::MAIL_TEMPLATE_APPLICATION_CONFIRMATION$context->getContext());
  65.     }
  66.     private function _createMailTemplates(Context $context) {
  67.         /** @var Connection $connection */
  68.         $connection $this->container->get(Connection::class);
  69.         //Admin Mail
  70. //        try {
  71.             $templateTypeData['technicalName'] = self::MAIL_TEMPLATE_ADMIN_NEW_APPLICATION;
  72.             $templateTypeData['availableEntities'] = [
  73.                 'application' => 'cogi_recruiting_application',
  74.                 'job' => 'cogi_recruiting_job',
  75.             ];
  76.             $templateTypeData['enName'] = 'Recruiting Admin - new job application';
  77.             $templateTypeData['deName'] = 'Recruiting Admin - Neue Stellenbewerbung';
  78.             $mailTemplateTypeId $this->createMailTemplateType($connection$templateTypeData$context);
  79.             $templateData['en-GB']['senderName'] = '{{ salesChannel.name }}';
  80.             $templateData['en-GB']['subject'] = 'Recruiting - new job application';
  81.             $templateData['en-GB']['description'] = '';
  82.             $templateData['en-GB']['contentHtml'] = $this->getMailContent('en-GB''admin_new_application''html');
  83.             $templateData['en-GB']['contentPlain'] = $this->getMailContent('en-GB''admin_new_application''txt');
  84.             $templateData['de-DE']['senderName'] = '{{ salesChannel.name }}';
  85.             $templateData['de-DE']['subject'] = 'Recruiting - Neue Stellenbewerbung';
  86.             $templateData['de-DE']['description'] = '';
  87.             $templateData['de-DE']['contentHtml'] = $this->getMailContent('de-DE''admin_new_application''html');
  88.             $templateData['de-DE']['contentPlain'] = $this->getMailContent('de-DE''admin_new_application''txt');
  89.             $this->createMailTemplate($connection$mailTemplateTypeId$templateData$context);
  90. //        } catch (\Exception $e) {
  91. //        }
  92.         //Applicant Mail
  93. //        try {
  94.             $templateTypeData['technicalName'] = self::MAIL_TEMPLATE_APPLICATION_CONFIRMATION;
  95.             $templateTypeData['availableEntities'] = [
  96.                 'application' => 'cogi_recruiting_application',
  97.                 'job' => 'cogi_recruiting_job',
  98.             ];
  99.             $templateTypeData['enName'] = 'Recruiting Applicant - thank you';
  100.             $templateTypeData['deName'] = 'Recruiting Bewerber - vielen Dank';
  101.             $mailTemplateTypeId $this->createMailTemplateType($connection$templateTypeData$context);
  102.             $templateData['en-GB']['senderName'] = '{{ salesChannel.name }}';
  103.             $templateData['en-GB']['subject'] = 'Confirmation of your application';
  104.             $templateData['en-GB']['description'] = '';
  105.             $templateData['en-GB']['contentHtml'] = $this->getMailContent('en-GB''user_new_application''html');
  106.             $templateData['en-GB']['contentPlain'] = $this->getMailContent('en-GB''user_new_application''txt');
  107.             $templateData['de-DE']['senderName'] = '{{ salesChannel.name }}';
  108.             $templateData['de-DE']['subject'] = 'Bestätigung Ihrer Bewerbung';
  109.             $templateData['de-DE']['description'] = '';
  110.             $templateData['de-DE']['contentHtml'] = $this->getMailContent('de-DE''user_new_application''html');
  111.             $templateData['de-DE']['contentPlain'] = $this->getMailContent('de-DE''user_new_application''txt');
  112.             $this->createMailTemplate($connection$mailTemplateTypeId$templateData$context);
  113. //        } catch (\Exception $e) {
  114. //        }
  115.     }
  116.     /**
  117.      * ------------------------
  118.      *
  119.      * Mail helpers below!
  120.      *
  121.      * ------------------------
  122.      */
  123.     /**
  124.      * @param Connection $connection
  125.      * @param array $data
  126.      *
  127.      * @return string
  128.      *
  129.      * @throws DBALException
  130.      * @throws InconsistentCriteriaIdsException
  131.      * @throws InvalidUuidException
  132.      */
  133.     private function createMailTemplateType(Connection $connection, array $dataContext $context): string {
  134.         $mailTemplateTypeId Uuid::randomHex();
  135.         $connection->insert('mail_template_type', [
  136.             'id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  137.             'technical_name' => $data['technicalName'],
  138.             'available_entities' => json_encode($data['availableEntities']),
  139.             'created_at' => (new DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  140.         ]);
  141.         $connection->insert('mail_template_type_translation', [
  142.             'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  143.             'language_id' => Uuid::fromHexToBytes($this->getLanguageIdByLocale('en-GB')),
  144.             'name' => $data['enName'],
  145.             'created_at' => (new DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  146.         ]);
  147.         $connection->insert('mail_template_type_translation', [
  148.             'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  149.             'language_id' => Uuid::fromHexToBytes($this->getLanguageIdByLocale('de-DE')),
  150.             'name' => $data['deName'],
  151.             'created_at' => (new DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  152.         ]);
  153.         // Add fallback
  154.         $defaultLocalCode $this->getDefaultLocaleCode($context);
  155.         if (!in_array($defaultLocalCode, ['de-DE''en-GB'])) {
  156.             $connection->insert('mail_template_type_translation', [
  157.                 'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  158.                 'language_id' => Uuid::fromHexToBytes($this->getLanguageIdByLocale($defaultLocalCode)),
  159.                 'name' => $data['enName'],
  160.                 'created_at' => (new DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  161.             ]);
  162.         }
  163.         return $mailTemplateTypeId;
  164.     }
  165.     /**
  166.      * @param string $locale
  167.      *
  168.      * @return string
  169.      *
  170.      * @throws InconsistentCriteriaIdsException
  171.      */
  172.     private function getLanguageIdByLocale(string $locale): string {
  173.         $context = new Context(new SystemSource());
  174.         /** @var EntityRepository $languageRepository */
  175.         $languageRepository $this->container->get('language.repository');
  176.         $criteria = new Criteria();
  177.         $criteria->addAssociation('locale');
  178.         $criteria->addFilter(new EqualsFilter('locale.code'$locale));
  179.         /** @var LanguageEntity $languageEntity */
  180.         $languageEntity $languageRepository->search($criteria$context)->first();
  181.         return $languageEntity->getId();
  182.     }
  183.     private function getDefaultLocaleCode($context): string {
  184.         /** @var EntityRepository $languageRepository */
  185.         $languageRepository $this->container->get('language.repository');
  186.         $criteria = new Criteria();
  187.         $criteria->addAssociation('locale');
  188.         $criteria->addFilter(new EqualsFilter('id'Defaults::LANGUAGE_SYSTEM));
  189.         /** @var LanguageEntity $languageEntity */
  190.         $languageEntity $languageRepository->search($criteria$context)->get(Defaults::LANGUAGE_SYSTEM);
  191.         return $languageEntity->getLocale()->getCode();
  192.     }
  193.     /**
  194.      * @param string $locale
  195.      * @param string $prefix
  196.      * @param string $type
  197.      *
  198.      * @return string
  199.      */
  200.     private function getMailContent(string $localestring $prefixstring $type): string {
  201.         $path $this->getPath() . '/Resources/email/' $locale '/';
  202.         $file "{$path}{$prefix}.{$type}";
  203.         if (!is_file($file)) {
  204.             throw new FileNotFoundException($file);
  205.         }
  206.         return file_get_contents($file);
  207.     }
  208.     /**
  209.      * @param Connection $connection
  210.      * @param string $mailTemplateTypeId
  211.      * @param array $data
  212.      *
  213.      * @return void
  214.      *
  215.      * @throws DBALException
  216.      * @throws InconsistentCriteriaIdsException
  217.      * @throws InvalidUuidException
  218.      */
  219.     private function createMailTemplate(Connection $connectionstring $mailTemplateTypeId, array $dataContext $context): void {
  220.         $mailTemplateId Uuid::randomHex();
  221.         $connection->insert('mail_template', [
  222.             'id' => Uuid::fromHexToBytes($mailTemplateId),
  223.             'mail_template_type_id' => Uuid::fromHexToBytes($mailTemplateTypeId),
  224.             'system_default' => true,
  225.             'created_at' => (new DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  226.         ]);
  227.         $connection->insert('mail_template_translation', [
  228.             'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
  229.             'language_id' => Uuid::fromHexToBytes($this->getLanguageIdByLocale('en-GB')),
  230.             'sender_name' => $data['en-GB']['senderName'],
  231.             'subject' => $data['en-GB']['subject'],
  232.             'description' => $data['en-GB']['description'],
  233.             'content_html' => $data['en-GB']['contentHtml'],
  234.             'content_plain' => $data['en-GB']['contentPlain'],
  235.             'created_at' => (new DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  236.         ]);
  237.         $connection->insert('mail_template_translation', [
  238.             'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
  239.             'language_id' => Uuid::fromHexToBytes($this->getLanguageIdByLocale('de-DE')),
  240.             'sender_name' => $data['de-DE']['senderName'],
  241.             'subject' => $data['de-DE']['subject'],
  242.             'description' => $data['de-DE']['description'],
  243.             'content_html' => $data['de-DE']['contentHtml'],
  244.             'content_plain' => $data['de-DE']['contentPlain'],
  245.             'created_at' => (new DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  246.         ]);
  247.         // Add fallback
  248.         $defaultLocalCode $this->getDefaultLocaleCode($context);
  249.         if (!in_array($defaultLocalCode, ['de-DE''en-GB'])) {
  250.             $connection->insert('mail_template_translation', [
  251.                 'mail_template_id' => Uuid::fromHexToBytes($mailTemplateId),
  252.                 'language_id' => Uuid::fromHexToBytes($this->getLanguageIdByLocale($defaultLocalCode)),
  253.                 'sender_name' => $data['en-GB']['senderName'],
  254.                 'subject' => $data['en-GB']['subject'],
  255.                 'description' => $data['en-GB']['description'],
  256.                 'content_html' => $data['en-GB']['contentHtml'],
  257.                 'content_plain' => $data['en-GB']['contentPlain'],
  258.                 'created_at' => (new DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT)
  259.             ]);
  260.         }
  261.     }
  262.     /**
  263.      * Removes a specific email template by templateTypeTechnicalName
  264.      * @param $templateTypeTechnicalName
  265.      * @param Context $context
  266.      * @return void
  267.      */
  268.     public function removeEmailTemplate($templateTypeTechnicalNameContext $context) {
  269.         /** @var EntityRepository $mailTemplateTypeRepository */
  270.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  271.         /** @var EntityRepository $mailTemplateRepository */
  272.         $mailTemplateRepository $this->container->get('mail_template.repository');
  273.         /** @var MailTemplateTypeEntity $customMailTemplateType */
  274.         $customMailTemplateType $mailTemplateTypeRepository->search(
  275.             (new Criteria())
  276.                 ->addFilter(new EqualsFilter('technicalName'$templateTypeTechnicalName)),
  277.             $context
  278.         )->first();
  279.         if ($customMailTemplateType) {
  280.             $mailTemplateIds $mailTemplateRepository->searchIds(
  281.                 (new Criteria())
  282.                     ->addFilter(new EqualsFilter('mailTemplateTypeId'$customMailTemplateType->getId())),
  283.                 $context
  284.             )->getIds();
  285.             $ids array_map(static function ($id) {
  286.                 return ['id' => $id];
  287.             }, $mailTemplateIds);
  288.             $mailTemplateRepository->delete($ids$context);
  289.             // Delete the TemplateType which were added by this Plugin
  290.             $mailTemplateTypeRepository->delete([
  291.                 ['id' => $customMailTemplateType->getId()]
  292.             ], $context);
  293.         }
  294.     }
  295. }