custom/plugins/FTBPlugin/src/FTBPlugin.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace FTBPlugin;
  3. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  4. use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
  5. use Shopware\Core\Defaults;
  6. use Shopware\Core\Framework\Context;
  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\OrFilter;
  11. use Shopware\Core\Framework\Plugin;
  12. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  13. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  14. use Shopware\Core\Framework\Uuid\Uuid;
  15. use Shopware\Core\System\CustomField\CustomFieldTypes;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. class FTBPlugin extends Plugin
  18. {
  19.     public const NEW_CUSTOMER_TEMPLATE_TYPE_NAME 'NeuerKunde';
  20.     public const NEW_CUSTOMER_TEMPLATE_TYPE_TECHNICAL_NAME 'ftb_new_customer';
  21.     public const SEARCH_SUMMARY_TEMPLATE_TYPE_NAME 'ZusammenfassungSuche';
  22.     public const SEARCH_SUMMARY_TEMPLATE_TYPE_TECHNICAL_NAME 'ftb_summary_search_nfound';
  23.     public function build(ContainerBuilder $container): void
  24.     {
  25.         $container->setParameter('ftb.plugin''FTB');
  26.         parent::build($container);
  27.     }
  28.     public function install(Plugin\Context\InstallContext $installContext): void
  29.     {
  30.         $this->addMailTemplates($installContext);
  31.     }
  32.     public function uninstall(UninstallContext $uninstallContext): void
  33.     {
  34.         //Keep UserData? Then do nothing here
  35.         if ($uninstallContext->keepUserData()) {
  36.             return;
  37.         }
  38.         parent::uninstall($uninstallContext);
  39.         //todo
  40. //        $this->removeMailTemplates($uninstallContext);
  41.     }
  42.     /**
  43.      * E-Mail Templates anlegen
  44.      * @param $installContext
  45.      */
  46.     private function addMailTemplates($installContext)
  47.     {
  48.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  49.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  50.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  51.         $mailTemplateRepository $this->container->get('mail_template.repository');
  52.         $newCustomerMailTemplateTypeId Uuid::randomHex();
  53.         $searchSummaryMailTemplateTypeId Uuid::randomHex();
  54.         $mailTemplateType = [
  55.             [
  56.                 'id' => $newCustomerMailTemplateTypeId,
  57.                 'name' => self::NEW_CUSTOMER_TEMPLATE_TYPE_NAME,
  58.                 'technicalName' => self::NEW_CUSTOMER_TEMPLATE_TYPE_TECHNICAL_NAME,
  59.             ],
  60.             [
  61.                 'id' => $searchSummaryMailTemplateTypeId,
  62.                 'name' => self::SEARCH_SUMMARY_TEMPLATE_TYPE_NAME,
  63.                 'technicalName' => self::SEARCH_SUMMARY_TEMPLATE_TYPE_TECHNICAL_NAME,
  64.             ],
  65.         ];
  66.         $mailTemplate = [
  67.             [
  68.                 'id' => Uuid::randomHex(),
  69.                 'mailTemplateTypeId' => $newCustomerMailTemplateTypeId,
  70.                 'subject' => [
  71.                     'de-DE' => 'Neue Kundenregistrierung',
  72.                     'en-GB' => 'Neue Kundenregistrierung'
  73.                 ],
  74.                 'senderName' => '{{ salesChannel.name }}',
  75.                 'contentPlain' => "Hallo, es gab eine neue Kundenregistrierung:
  76. Ansprechpartner:
  77. Vorname des Ansprechpartners: {{ customer.ansprechpartnerVorname }}
  78. Nachname des Ansprechpartners: {{ customer.ansprechpartnerNachname }}
  79. Telefonnummer des Ansprechpartners: {{ customer.ansprechpartnerTelefonnummer }}
  80. Rechnungsadresse:
  81. Firmenzeile 1: {{customer.firstName}}
  82. Firmenzeile 2: {{customer.lastName }}
  83. Firmenzeile 3: {{ customer.addresses|first.company }}
  84. E-Mail-Adresse: {{customer.email}}
  85. Umsatzsteuer-ID: {{ customer.vatId ? customer.vatId : '-' }}
  86. Straße und Hausnummer: {{ customer.addresses|first.street }}
  87. Postleitzahl: {{ customer.addresses|first.zipcode }}
  88. Ort: {{ customer.addresses|first.city }}
  89. Land: {{ customer.addresses|first.country }}
  90. {% if customer.addresses|length > 1 %}
  91. Abweichende Lieferadresse:
  92. Firmenzeile 1: {{customer.addresses|last.firstName}}
  93. Firmenzeile 2: {{customer.addresses|last.lastName}}
  94. Firmenzeile 3: {{ customer.addresses|last.company }}
  95. Straße und Hausnummer: {{customer.addresses|last.street}}
  96. PLZ: {{customer.addresses|last.zipcode}}
  97. Ort: {{customer.addresses|last.city}}
  98. Land: {{ customer.addresses|last.country }}
  99. {% endif %}",
  100.                 'contentHtml' => "Hallo, es gab eine neue Kundenregistrierung:<br>
  101. <br>
  102. <strong>Ansprechpartner:</strong>
  103. Vorname des Ansprechpartners: {{ customer.ansprechpartnerVorname }}<br>
  104. Nachname des Ansprechpartners: {{ customer.ansprechpartnerNachname }}<br>
  105. Telefonnummer des Ansprechpartners: {{ customer.ansprechpartnerTelefonnummer }}<br>
  106. <strong>Rechnungsadresse:</strong><br>
  107. Firmenzeile 1: {{customer.firstName}}<br>
  108. Firmenzeile 2: {{customer.lastName }}<br>
  109. Firmenzeile 3: {{ customer.addresses|first.company }}<br>
  110. E-Mail-Adresse: {{customer.email}}<br>
  111. Umsatzsteuer-ID: {{ customer.vatId ? customer.vatId : '-' }}<br>
  112. Straße und Hausnummer: {{ customer.addresses|first.street }}<br>
  113. Postleitzahl: {{ customer.addresses|first.zipcode }}<br>
  114. Ort: {{ customer.addresses|first.city }}<br>
  115. Land: {{ customer.addresses|first.country }}<br>
  116. {% if customer.addresses|length > 1 %}
  117. <br>
  118. <strong>Abweichende Lieferadresse:</strong><br>
  119. Firmenzeile 1: {{customer.addresses|last.firstName}}<br>
  120. Firmenzeile 2: {{customer.addresses|last.lastName}}<br>
  121. Firmenzeile 3: {{ customer.addresses|last.company }}<br>
  122. Straße und Hausnummer: {{customer.addresses|last.street}}<br>
  123. PLZ: {{customer.addresses|last.zipcode}}<br>
  124. Ort: {{customer.addresses|last.city}}<br>
  125. Land: {{ customer.addresses|last.country }}
  126. {% endif %}",
  127.             ],
  128.             [
  129.                 'id' => Uuid::randomHex(),
  130.                 'mailTemplateTypeId' => $searchSummaryMailTemplateTypeId,
  131.                 'subject' => [
  132.                     'de-DE' => 'Zusammenfassung: Suche nicht gefunden',
  133.                     'en-GB' => 'Zusammenfassung: Suche nicht gefunden',
  134.                 ],
  135.                 'senderName' => 'FTB - Dentalfiltershop',
  136.                 'contentPlain' => "Sehr geehrte Damen und Herren,
  137. hier fnden Sie die {{ when }} Zusammenfassung der Suchanfragen, welche nicht gefunden worden sind.
  138. Hier finden sich die Suchanfragen ab dem {{ fromDate|date(\"d.m.Y\") }} bis jetzt.
  139. Suchanfragen:
  140. {% for searchTerm,count in searchResults %}
  141. {{searchTerm}}
  142. {{count}}
  143. {% endfor %}",
  144.                 'contentHtml' => '<div style="font-family:arial; font-size:12px;">
  145.     <p>
  146.         Sehr geehrte Damen und Herren,<br>
  147. hier fnden Sie die {{ when }} Zusammenfassung der Suchanfragen, welche nicht gefunden worden sind.<br><br>
  148. Hier finden sich die Suchanfragen ab dem {{ fromDate|date("d.m.Y") }} bis jetzt.<br><br>
  149. Suchanfragen:<br><br>
  150.     </p>
  151.     <table>
  152.   <tr>
  153.     <th>Suchanfrage:</th>
  154.     <th>Anzahl:</th>
  155.   </tr>
  156. {% for searchTerm,count in searchResults %}
  157.       <tr>
  158.     <td>{{searchTerm}}</td>
  159.     <td>{{count}}</td>
  160.   </tr>
  161. {% endfor %}
  162. </table>
  163. </div>',
  164.             ]
  165.         ];
  166.         try {
  167.             $mailTemplateTypeRepository->create($mailTemplateType$installContext->getContext());
  168.             $mailTemplateRepository->create($mailTemplate$installContext->getContext());
  169.         } catch (UniqueConstraintViolationException $exception) {
  170.             // No, we've already installed the fields, it's fine.
  171.         }
  172.     }
  173.     protected function createCustomFieldSet(): void
  174.     {
  175.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  176.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  177.         // Kunden Zusatzfelder
  178.         $customFieldSetRepository->create([[
  179.             'name' => 'FTBCustomer',
  180.             'config' => [
  181.                 'label' => [
  182.                     'de-DE' => 'FTB Kunden',
  183.                 ],
  184.             ],
  185.             'customFields' => [
  186.                 [
  187.                     'name' => 'AnsprechpartnerVorname',
  188.                     'type' => CustomFieldTypes::TEXT,
  189.                     'config' => [
  190.                         'componentName' => 'sw-field',
  191.                         'customFieldType' => 'text',
  192.                         'customFieldPosition' => 1,
  193.                         'label' => [
  194.                             'de-DE' => 'AnsprechpartnerVorname',
  195.                         ],
  196.                     ],
  197.                 ],
  198.                 [
  199.                     'name' => 'AnsprechpartnerNachname',
  200.                     'type' => CustomFieldTypes::TEXT,
  201.                     'config' => [
  202.                         'componentName' => 'sw-field',
  203.                         'customFieldType' => 'text',
  204.                         'customFieldPosition' => 2,
  205.                         'label' => [
  206.                             'de-DE' => 'AnsprechpartnerNachname',
  207.                         ],
  208.                     ],
  209.                 ],
  210.                 [
  211.                     'name' => 'AnsprechpartnerTelefonnummer',
  212.                     'type' => CustomFieldTypes::TEXT,
  213.                     'config' => [
  214.                         'componentName' => 'sw-field',
  215.                         'customFieldType' => 'text',
  216.                         'customFieldPosition' => 2,
  217.                         'label' => [
  218.                             'de-DE' => 'AnsprechpartnerTelefonnummer',
  219.                         ],
  220.                     ],
  221.                 ],
  222.             ],
  223.             'relations' => [
  224.                 [
  225.                     'entityName' => 'customer',
  226.                 ],
  227.             ],
  228.         ]], Context::createDefaultContext());
  229.     }
  230.     /** Beim Aktivieren des Plugins wird das Installationsdatum auf einen festen Wert, der weit in der
  231.      * Vergangenheit liegt gesetzt. Dies ist notwendig, damit dass FTB-Plugin immer als erstes geladen wird und andere
  232.      * Plugins das FTB-Plugin nicht überschreiben.
  233.      * @param ActivateContext $activateContext
  234.      * @return void
  235.      */
  236.     public function activate(ActivateContext $activateContext): void
  237.     {
  238.         //aktiviere das Plugin
  239.         parent::activate($activateContext);
  240.         $criteria = new Criteria();
  241.         // suche nach dem Namen des Plugins in der Spalte 'name'
  242.         $criteria->addFilter(new EqualsFilter('name''FTBPlugin'));
  243.         // hole das plugin.repository
  244.         $pluginRepo $this->container->get('plugin.repository');
  245.         $result $pluginRepo->search($criteria$activateContext->getContext());
  246.         $result $result->getEntities()->first();
  247.         // setze das Datum auf einen frühen Wert
  248.         $date = new \DateTime('2000-01-01 11:00:00.000');
  249.         // Update die Plugin-Tabelle mit dem frühen Datum
  250.         $pluginRepo->update([[
  251.             'id' => $result->getId(),
  252.             'installedAt' => $date->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  253.         ]], $activateContext->getContext());
  254.     }
  255.     /**
  256.      *
  257.      * @param $uninstallContext
  258.      */
  259.     private function removeMailTemplates($uninstallContext)
  260.     {
  261.         // E-Mail Template
  262.         //get the Templates and Associations added by this Plugin from the DB
  263.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  264.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  265.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  266.         $mailTemplateRepository $this->container->get('mail_template.repository');
  267.         // todo alle mail templates gleichzeitig löschen !
  268.         /** @var MailTemplateTypeEntity $myCustomMailTemplateType */
  269.         $myCustomMailTemplateType $mailTemplateTypeRepository->search(
  270.             (new Criteria())
  271.                 ->addFilter((new OrFilter([
  272.                     new EqualsFilter('technicalName'self::NEW_CUSTOMER_TEMPLATE_TYPE_TECHNICAL_NAME),
  273. //                    new EqualsFilter('technicalName', self::SEARCH_SUMMARY_TEMPLATE_TYPE_TECHNICAL_NAME),
  274.                 ]))),
  275.             $uninstallContext->getContext()
  276.         )->first();
  277.         if ($myCustomMailTemplateType === null) {
  278.             return;
  279.         }
  280.         $mailTemplateIds $mailTemplateRepository->searchIds(
  281.             (new Criteria())
  282.                 ->addFilter(new EqualsFilter('mailTemplateTypeId'$myCustomMailTemplateType->getId())),
  283.             $uninstallContext
  284.                 ->getContext()
  285.         )->getIds();
  286.         //Get the Ids from the fetched Entities
  287.         $ids array_map(static function ($id) {
  288.             return ['id' => $id];
  289.         }, $mailTemplateIds);
  290.         //Delete the Templates which were added by this Plugin
  291.         $mailTemplateRepository->delete($ids$uninstallContext->getContext());
  292.         //Delete the TemplateType which were added by this Plugin
  293.         $mailTemplateTypeRepository->delete([
  294.             ['id' => $myCustomMailTemplateType->getId()]
  295.         ], $uninstallContext->getContext());
  296.     }
  297. }