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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace LoyxxSeminar;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Checkout\Cart\CartEvents;
  5. use Shopware\Core\Checkout\Order\OrderEvents;
  6. use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
  7. use Shopware\Core\Content\MailTemplate\MailTemplateActions;
  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\Plugin\Context\UpdateContext;
  16. use Shopware\Core\Framework\Uuid\Uuid;
  17. use Shopware\Core\System\CustomField\CustomFieldTypes;
  18. class LoyxxSeminar extends Plugin
  19. {
  20.     public const BUNDLE_NAME 'LoyxxSeminar';
  21.     public const PRODUCT_CUSTOM_FIELD_GROUP 'loyxx_seminar_group';
  22.     public const PRODUCT_CUSTOM_FIELD_SEMINAR 'loyxx_seminar';
  23.     public const MAIL_TEMPLATE_TYPE 'loyxx_seminar_mt'// UUid for loyxx_seminar_mail_template
  24.     public const EVENT_ACTION_ID 'a61d1c00d51c4e85a3589e476fd43cf7';
  25.     public function install(InstallContext $installContext): void
  26.     {
  27.         parent::install($installContext);
  28.         $this->setupSeminarCustomField($installContext->getContext());
  29.         $this->setupSeminarMailTemplate($installContext->getContext());
  30.     }
  31.     public function uninstall(UninstallContext $uninstallContext): void
  32.     {
  33.         parent::uninstall($uninstallContext);
  34.         if ($uninstallContext->keepUserData()) {
  35.             return;
  36.         }
  37.         $connection $this->container->get(Connection::class);
  38.         $connection->executeStatement('SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;');
  39.         $connection->executeStatement("DROP TABLE IF EXISTS `loyxx_order_participant`");
  40.         $connection->executeStatement('SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;');
  41.         $this->removeSeminarCustomField($uninstallContext);
  42.         $this->removeMailTemplate($uninstallContext->getContext());
  43.         $this->removeSeminarEventAction($uninstallContext->getContext());
  44.     }
  45.     private function setupSeminarCustomField(Context $context)
  46.     {
  47.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  48.         //check if the custom field set is defined
  49.         $criteria = new Criteria();
  50.         $criteria->addFilter(new EqualsFilter('name', static::PRODUCT_CUSTOM_FIELD_GROUP));
  51.         $customFieldSetEntity $customFieldSetRepository->searchIds($criteria$context);
  52.         if (!$customFieldSetEntity->getTotal()) {
  53.             $customFieldSetEntity $customFieldSetRepository->create(
  54.                 [
  55.                     [
  56.                         'name' => static::PRODUCT_CUSTOM_FIELD_GROUP,
  57.                         'customFields' => [
  58.                             [
  59.                                 'name' => static::PRODUCT_CUSTOM_FIELD_SEMINAR ,
  60.                                 'type' => CustomFieldTypes::BOOL,
  61.                                 'config' => [
  62.                                     'type' => 'switch',
  63.                                     'label' => [
  64.                                         'en-GB' => 'Activate participant booking',
  65.                                         'de-DE' => 'Teilnehmerbuchung aktivieren'
  66.                                     ],
  67.                                     'componentName' => 'sw-field',
  68.                                     'customFieldType' => 'switch',
  69.                                     'customFieldPosition' => 1,
  70.                                     'translated' => true
  71.                                 ]
  72.                             ]
  73.                         ],
  74.                         'relations' => [
  75.                             ['entityName' => 'product']
  76.                         ],
  77.                         'config' => [
  78.                             'description' => [
  79.                                 'en-GB' => 'Seminar participation',
  80.                                 'de-DE' => 'Teilnahme am Seminar'
  81.                             ],
  82.                             'label' => [
  83.                                 'en-GB' => 'Seminar',
  84.                                 'de-DE' => 'Seminar'
  85.                             ],
  86.                             'translated' => true
  87.                         ]
  88.                     ]
  89.                 ],
  90.                 $context
  91.             );
  92.         }
  93.     }
  94.     private function removeSeminarCustomField(UninstallContext $uninstallContext)
  95.     {
  96.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  97.         $context $uninstallContext->getContext();
  98.         //check if the custom field set is defined
  99.         $criteria = new Criteria();
  100.         $criteria->addFilter(new EqualsFilter('name', static::PRODUCT_CUSTOM_FIELD_GROUP));
  101.         $customFieldSetEntity $customFieldSetRepository->searchIds($criteria$context);
  102.         if ($customFieldSetEntity->getTotal()) {
  103.             $customFieldSetRepository->delete(array_values($customFieldSetEntity->getData()), $context);
  104.         }
  105.     }
  106.     /**
  107.      * @param Context $context
  108.      */
  109.     private function setupSeminarMailTemplate(Context $context)
  110.     {
  111.         $mailTemplateId Uuid::fromBytesToHex(static::MAIL_TEMPLATE_TYPE);
  112.         $mailTemplateRepository $this->container->get('mail_template.repository');
  113.         $mailTemplate $this->mailTemplateExists($mailTemplateRepository$mailTemplateId$context);
  114.         $mailTemplateType $this->getMailTemplateType($context);
  115.         if ($mailTemplateType !== null ){
  116.             if ($mailTemplate === null){
  117.                 $mailTemplateRepository->create([
  118.                     [
  119.                         "id" => $mailTemplateId,
  120.                         "mailTemplateTypeId" => $mailTemplateType->getId(),
  121.                         "senderName" => [
  122.                             "en-GB" => "{{ salesChannel.name }}",
  123.                             "de-DE" => "{{ salesChannel.name }}"
  124.                         ],
  125.                         "subject" => [
  126.                             "en-GB" => "Order confirmation with participant details",
  127.                             "de-DE" => "Auftragsbestätigung mit Teilnehmerangaben"
  128.                         ],
  129.                         "description" => [
  130.                             "en-GB" => "Order confirmation email template with participant details",
  131.                             "de-DE" => "Bestellbestätigungs-E-Mail-Vorlage mit Teilnehmerdetails"
  132.                         ],
  133.                         "contentHtml" => [
  134.                             "en-GB" => $this->getOrderConfirmationHtmlTemplate('en-GB'),
  135.                             "de-DE" => $this->getOrderConfirmationHtmlTemplate('de-DE')
  136.                         ],
  137.                         "contentPlain" => [
  138.                             "en-GB" => $this->getOrderConfirmationPlainTemplate('en-GB'),
  139.                             "de-DE" => $this->getOrderConfirmationPlainTemplate('de-DE')
  140.                         ]
  141.                     ]
  142.                 ], $context);
  143.             }
  144.             $this->eventActionRepository()->upsert([
  145.                 [
  146.                     'id' => static::EVENT_ACTION_ID,
  147.                     'title' => "Order confirmation with participant details",
  148.                     'eventName' => CartEvents::CHECKOUT_ORDER_PLACED,
  149.                     'actionName' => MailTemplateActions::MAIL_TEMPLATE_MAIL_SEND_ACTION,
  150.                     'config' => [
  151.                         'mail_template_id' => $mailTemplateId,
  152.                         'mail_template_type_id' => $mailTemplateType->getId()
  153.                     ]
  154.                 ]
  155.             ], $context);
  156.         }
  157.     }
  158.     /**
  159.      * @param string $type
  160.      * @return string
  161.      */
  162.     private function getOrderConfirmationHtmlTemplate($type 'de-DE'): string
  163.     {
  164.         return file_get_contents(__DIR__."/Resources/stubs/participants.{$type}.stub.html.twig");
  165.     }
  166.     /**
  167.      * @param string $type
  168.      * @return string
  169.      */
  170.     private function getOrderConfirmationPlainTemplate($type 'de-DE'): string
  171.     {
  172.         return file_get_contents(__DIR__."/Resources/stubs/participants.{$type}.stub.txt.twig");
  173.     }
  174.     /**
  175.      * @param object|null $mailTemplateRepository
  176.      * @param string $mailTemplateId
  177.      * @param Context $context
  178.      * @return bool
  179.      */
  180.     private function mailTemplateExists(?object $mailTemplateRepositorystring $mailTemplateIdContext $context)
  181.     {
  182.         $criteria = new Criteria();
  183.         $criteria->addFilter(new EqualsFilter('id'$mailTemplateId));
  184.         return $mailTemplateRepository->search($criteria$context)->first();
  185.     }
  186.     /**
  187.      * @param Context $context
  188.      * @return mixed
  189.      */
  190.     private function getMailTemplateType(Context $context)
  191.     {
  192.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  193.         $criteria = new Criteria();
  194.         $criteria->addFilter(new EqualsFilter('technicalName''order_confirmation_mail'));
  195.         return $mailTemplateTypeRepository->search($criteria$context)->first();
  196.     }
  197.     /**
  198.      * @return object|EntityRepository|null
  199.      */
  200.     private function mailTemplateRepository()
  201.     {
  202.         return $this->container->get('mail_template.repository');
  203.     }
  204.     /**
  205.      * @return object|EntityRepository|null
  206.      */
  207.     private function mailTemplateTypeRepository()
  208.     {
  209.         return $this->container->get('mail_template_type.repository');
  210.     }
  211.     /**
  212.      * @return object|EntityRepository|null
  213.      */
  214.     private function eventActionRepository()
  215.     {
  216.         return $this->container->get('event_action.repository');
  217.     }
  218.     private function removeMailTemplateType(Context $context)
  219.     {
  220.         /** @var MailTemplateTypeEntity $templateType */
  221.         $templateType $this->getMailTemplateType($context);
  222.         if ($templateType){
  223.             $this->mailTemplateTypeRepository()->delete([
  224.                 [
  225.                     'id' => $templateType->getId()
  226.                 ]
  227.             ], $context);
  228.         }
  229.     }
  230.     private function removeMailTemplate(Context $context)
  231.     {
  232.         $this->mailTemplateRepository()->delete([
  233.             [
  234.                 'id' => Uuid::fromBytesToHex(static::MAIL_TEMPLATE_TYPE)
  235.             ]
  236.         ], $context);
  237.     }
  238.     private function removeSeminarEventAction(Context $context)
  239.     {
  240.         $this->eventActionRepository()->delete([
  241.             [
  242.                 'id' => static::EVENT_ACTION_ID
  243.             ]
  244.         ], $context);
  245.     }
  246.     public function update(UpdateContext $updateContext) : void
  247.     {
  248.         parent::update($updateContext);
  249.         $this->setupSeminarMailTemplate($updateContext->getContext());
  250.     }
  251. }