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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace CioEnergieImport;
  4. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  5. use Shopware\Core\Checkout\Order\OrderStates;
  6. use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  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. use Shopware\Core\System\StateMachine\Aggregation\StateMachineState\StateMachineStateEntity;
  16. use Shopware\Core\System\StateMachine\Aggregation\StateMachineTransition\StateMachineTransitionEntity;
  17. class CioEnergieImport extends Plugin
  18. {
  19.     public function install(InstallContext $installContext): void
  20.     {
  21.         parent::install($installContext);
  22.         $context $installContext;
  23.         // add mail templates
  24.         $mailTemplateTypeId Uuid::randomHex();
  25.         $this->createEmailTypeWithTemplates(
  26.             [
  27.             'id' => $mailTemplateTypeId,
  28.             'name' => 'Passwort vergessen E-Mail für einen neuen Kunden aus Stammdaten-Import ',
  29.             'technicalName' => 'energie_import_master_data_forgot_password',
  30.             'availableEntities' => [
  31.                 'customer' => 'customer',
  32.             ]
  33.         ],
  34.             [
  35.                 [
  36.                     'id' => Uuid::randomHex(),
  37.                     'mailTemplateTypeId' => $mailTemplateTypeId,
  38.                     'subject' => [
  39.                         'en-GB' => 'Ihr Account wurde erstellt. Ändern Sie bitte Ihr Passwort.',
  40.                         'de-DE' => 'Ihr Account wurde erstellt. Ändern Sie bitte Ihr Passwort.'
  41.                     ],
  42.                     'contentPlain' => "Ihr Account wurde erstellt. Damit Sie sich einloggen können, ändern Sie bitte Ihr Passwort unter {{ forgot_password_url }}.",
  43.                     'contentHtml' => "<p>Ihr Account wurde erstellt. Damit Sie sich einloggen können, ändern Sie bitte Ihr Passwort unter <a href=\"{{ forgot_password_url }}\">{{ forgot_password_url }}</a></p>",
  44.                 ]
  45.             ],
  46.             $installContext->getContext()
  47.         );
  48.     }
  49.     public function uninstall(UninstallContext $uninstallContext): void
  50.     {
  51.         parent::uninstall($uninstallContext);
  52.         //Keep UserData? Then do nothing here
  53.         if ($uninstallContext->keepUserData()) {
  54.             return;
  55.         }
  56.         $this->removeEmailTypeWithTemplates('energie_import_master_data_forgot_password'$uninstallContext->getContext());
  57.     }
  58.     protected function createEmailTypeWithTemplates(array $type, array $templatesContext $context)
  59.     {
  60.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  61.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  62.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  63.         $mailTemplateRepository $this->container->get('mail_template.repository');
  64.         try {
  65.             $mailTemplateTypeRepository->create([$type], $context);
  66.             $mailTemplateRepository->create($templates$context);
  67.         } catch (UniqueConstraintViolationException $exception) {
  68.             // No, we've already installed the fields, it's fine.
  69.         }
  70.     }
  71.     protected function removeEmailTypeWithTemplates(string $typeNameContext $context)
  72.     {
  73.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  74.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  75.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  76.         $mailTemplateRepository $this->container->get('mail_template.repository');
  77.         /** @var MailTemplateTypeEntity $myCustomMailTemplateType */
  78.         $myCustomMailTemplateType $mailTemplateTypeRepository->search(
  79.             (new Criteria())->addFilter(new EqualsFilter('technicalName'$typeName)),
  80.             $context
  81.         )->first();
  82.         $ids = [];
  83.         if ($myCustomMailTemplateType instanceof MailTemplateTypeEntity) {
  84.             $mailTemplateIds $mailTemplateRepository->searchIds(
  85.                 (new Criteria())->addFilter(new EqualsFilter('mailTemplateTypeId'$myCustomMailTemplateType->getId())),
  86.                 $context
  87.             )->getIds();
  88.             //Get the Ids from the fetched Entities
  89.             $ids array_map(static function ($id) {
  90.                 return ['id' => $id];
  91.             }, $mailTemplateIds);
  92.         }
  93.         $mailTemplateTypeRepository->delete([
  94.             ['id' => $myCustomMailTemplateType->getId()]
  95.         ], $context);
  96.         $mailTemplateRepository->delete($ids$context);
  97.     }
  98. }