<?php declare(strict_types=1);
namespace FTBPlugin;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\OrFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class FTBPlugin extends Plugin
{
public const NEW_CUSTOMER_TEMPLATE_TYPE_NAME = 'NeuerKunde';
public const NEW_CUSTOMER_TEMPLATE_TYPE_TECHNICAL_NAME = 'ftb_new_customer';
public const SEARCH_SUMMARY_TEMPLATE_TYPE_NAME = 'ZusammenfassungSuche';
public const SEARCH_SUMMARY_TEMPLATE_TYPE_TECHNICAL_NAME = 'ftb_summary_search_nfound';
public function build(ContainerBuilder $container): void
{
$container->setParameter('ftb.plugin', 'FTB');
parent::build($container);
}
public function install(Plugin\Context\InstallContext $installContext): void
{
$this->addMailTemplates($installContext);
}
public function uninstall(UninstallContext $uninstallContext): void
{
//Keep UserData? Then do nothing here
if ($uninstallContext->keepUserData()) {
return;
}
parent::uninstall($uninstallContext);
//todo
// $this->removeMailTemplates($uninstallContext);
}
/**
* E-Mail Templates anlegen
* @param $installContext
*/
private function addMailTemplates($installContext)
{
/** @var EntityRepositoryInterface $mailTemplateTypeRepository */
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
/** @var EntityRepositoryInterface $mailTemplateRepository */
$mailTemplateRepository = $this->container->get('mail_template.repository');
$newCustomerMailTemplateTypeId = Uuid::randomHex();
$searchSummaryMailTemplateTypeId = Uuid::randomHex();
$mailTemplateType = [
[
'id' => $newCustomerMailTemplateTypeId,
'name' => self::NEW_CUSTOMER_TEMPLATE_TYPE_NAME,
'technicalName' => self::NEW_CUSTOMER_TEMPLATE_TYPE_TECHNICAL_NAME,
],
[
'id' => $searchSummaryMailTemplateTypeId,
'name' => self::SEARCH_SUMMARY_TEMPLATE_TYPE_NAME,
'technicalName' => self::SEARCH_SUMMARY_TEMPLATE_TYPE_TECHNICAL_NAME,
],
];
$mailTemplate = [
[
'id' => Uuid::randomHex(),
'mailTemplateTypeId' => $newCustomerMailTemplateTypeId,
'subject' => [
'de-DE' => 'Neue Kundenregistrierung',
'en-GB' => 'Neue Kundenregistrierung'
],
'senderName' => '{{ salesChannel.name }}',
'contentPlain' => "Hallo, es gab eine neue Kundenregistrierung:
Ansprechpartner:
Vorname des Ansprechpartners: {{ customer.ansprechpartnerVorname }}
Nachname des Ansprechpartners: {{ customer.ansprechpartnerNachname }}
Telefonnummer des Ansprechpartners: {{ customer.ansprechpartnerTelefonnummer }}
Rechnungsadresse:
Firmenzeile 1: {{customer.firstName}}
Firmenzeile 2: {{customer.lastName }}
Firmenzeile 3: {{ customer.addresses|first.company }}
E-Mail-Adresse: {{customer.email}}
Umsatzsteuer-ID: {{ customer.vatId ? customer.vatId : '-' }}
Straße und Hausnummer: {{ customer.addresses|first.street }}
Postleitzahl: {{ customer.addresses|first.zipcode }}
Ort: {{ customer.addresses|first.city }}
Land: {{ customer.addresses|first.country }}
{% if customer.addresses|length > 1 %}
Abweichende Lieferadresse:
Firmenzeile 1: {{customer.addresses|last.firstName}}
Firmenzeile 2: {{customer.addresses|last.lastName}}
Firmenzeile 3: {{ customer.addresses|last.company }}
Straße und Hausnummer: {{customer.addresses|last.street}}
PLZ: {{customer.addresses|last.zipcode}}
Ort: {{customer.addresses|last.city}}
Land: {{ customer.addresses|last.country }}
{% endif %}",
'contentHtml' => "Hallo, es gab eine neue Kundenregistrierung:<br>
<br>
<strong>Ansprechpartner:</strong>
Vorname des Ansprechpartners: {{ customer.ansprechpartnerVorname }}<br>
Nachname des Ansprechpartners: {{ customer.ansprechpartnerNachname }}<br>
Telefonnummer des Ansprechpartners: {{ customer.ansprechpartnerTelefonnummer }}<br>
<strong>Rechnungsadresse:</strong><br>
Firmenzeile 1: {{customer.firstName}}<br>
Firmenzeile 2: {{customer.lastName }}<br>
Firmenzeile 3: {{ customer.addresses|first.company }}<br>
E-Mail-Adresse: {{customer.email}}<br>
Umsatzsteuer-ID: {{ customer.vatId ? customer.vatId : '-' }}<br>
Straße und Hausnummer: {{ customer.addresses|first.street }}<br>
Postleitzahl: {{ customer.addresses|first.zipcode }}<br>
Ort: {{ customer.addresses|first.city }}<br>
Land: {{ customer.addresses|first.country }}<br>
{% if customer.addresses|length > 1 %}
<br>
<strong>Abweichende Lieferadresse:</strong><br>
Firmenzeile 1: {{customer.addresses|last.firstName}}<br>
Firmenzeile 2: {{customer.addresses|last.lastName}}<br>
Firmenzeile 3: {{ customer.addresses|last.company }}<br>
Straße und Hausnummer: {{customer.addresses|last.street}}<br>
PLZ: {{customer.addresses|last.zipcode}}<br>
Ort: {{customer.addresses|last.city}}<br>
Land: {{ customer.addresses|last.country }}
{% endif %}",
],
[
'id' => Uuid::randomHex(),
'mailTemplateTypeId' => $searchSummaryMailTemplateTypeId,
'subject' => [
'de-DE' => 'Zusammenfassung: Suche nicht gefunden',
'en-GB' => 'Zusammenfassung: Suche nicht gefunden',
],
'senderName' => 'FTB - Dentalfiltershop',
'contentPlain' => "Sehr geehrte Damen und Herren,
hier fnden Sie die {{ when }} Zusammenfassung der Suchanfragen, welche nicht gefunden worden sind.
Hier finden sich die Suchanfragen ab dem {{ fromDate|date(\"d.m.Y\") }} bis jetzt.
Suchanfragen:
{% for searchTerm,count in searchResults %}
{{searchTerm}}
{{count}}
{% endfor %}",
'contentHtml' => '<div style="font-family:arial; font-size:12px;">
<p>
Sehr geehrte Damen und Herren,<br>
hier fnden Sie die {{ when }} Zusammenfassung der Suchanfragen, welche nicht gefunden worden sind.<br><br>
Hier finden sich die Suchanfragen ab dem {{ fromDate|date("d.m.Y") }} bis jetzt.<br><br>
Suchanfragen:<br><br>
</p>
<table>
<tr>
<th>Suchanfrage:</th>
<th>Anzahl:</th>
</tr>
{% for searchTerm,count in searchResults %}
<tr>
<td>{{searchTerm}}</td>
<td>{{count}}</td>
</tr>
{% endfor %}
</table>
</div>',
]
];
try {
$mailTemplateTypeRepository->create($mailTemplateType, $installContext->getContext());
$mailTemplateRepository->create($mailTemplate, $installContext->getContext());
} catch (UniqueConstraintViolationException $exception) {
// No, we've already installed the fields, it's fine.
}
}
protected function createCustomFieldSet(): void
{
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
// Kunden Zusatzfelder
$customFieldSetRepository->create([[
'name' => 'FTBCustomer',
'config' => [
'label' => [
'de-DE' => 'FTB Kunden',
],
],
'customFields' => [
[
'name' => 'AnsprechpartnerVorname',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 1,
'label' => [
'de-DE' => 'AnsprechpartnerVorname',
],
],
],
[
'name' => 'AnsprechpartnerNachname',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 2,
'label' => [
'de-DE' => 'AnsprechpartnerNachname',
],
],
],
[
'name' => 'AnsprechpartnerTelefonnummer',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 2,
'label' => [
'de-DE' => 'AnsprechpartnerTelefonnummer',
],
],
],
],
'relations' => [
[
'entityName' => 'customer',
],
],
]], Context::createDefaultContext());
}
/** Beim Aktivieren des Plugins wird das Installationsdatum auf einen festen Wert, der weit in der
* Vergangenheit liegt gesetzt. Dies ist notwendig, damit dass FTB-Plugin immer als erstes geladen wird und andere
* Plugins das FTB-Plugin nicht überschreiben.
* @param ActivateContext $activateContext
* @return void
*/
public function activate(ActivateContext $activateContext): void
{
//aktiviere das Plugin
parent::activate($activateContext);
$criteria = new Criteria();
// suche nach dem Namen des Plugins in der Spalte 'name'
$criteria->addFilter(new EqualsFilter('name', 'FTBPlugin'));
// hole das plugin.repository
$pluginRepo = $this->container->get('plugin.repository');
$result = $pluginRepo->search($criteria, $activateContext->getContext());
$result = $result->getEntities()->first();
// setze das Datum auf einen frühen Wert
$date = new \DateTime('2000-01-01 11:00:00.000');
// Update die Plugin-Tabelle mit dem frühen Datum
$pluginRepo->update([[
'id' => $result->getId(),
'installedAt' => $date->format(Defaults::STORAGE_DATE_TIME_FORMAT),
]], $activateContext->getContext());
}
/**
*
* @param $uninstallContext
*/
private function removeMailTemplates($uninstallContext)
{
// E-Mail Template
//get the Templates and Associations added by this Plugin from the DB
/** @var EntityRepositoryInterface $mailTemplateTypeRepository */
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
/** @var EntityRepositoryInterface $mailTemplateRepository */
$mailTemplateRepository = $this->container->get('mail_template.repository');
// todo alle mail templates gleichzeitig löschen !
/** @var MailTemplateTypeEntity $myCustomMailTemplateType */
$myCustomMailTemplateType = $mailTemplateTypeRepository->search(
(new Criteria())
->addFilter((new OrFilter([
new EqualsFilter('technicalName', self::NEW_CUSTOMER_TEMPLATE_TYPE_TECHNICAL_NAME),
// new EqualsFilter('technicalName', self::SEARCH_SUMMARY_TEMPLATE_TYPE_TECHNICAL_NAME),
]))),
$uninstallContext->getContext()
)->first();
if ($myCustomMailTemplateType === null) {
return;
}
$mailTemplateIds = $mailTemplateRepository->searchIds(
(new Criteria())
->addFilter(new EqualsFilter('mailTemplateTypeId', $myCustomMailTemplateType->getId())),
$uninstallContext
->getContext()
)->getIds();
//Get the Ids from the fetched Entities
$ids = array_map(static function ($id) {
return ['id' => $id];
}, $mailTemplateIds);
//Delete the Templates which were added by this Plugin
$mailTemplateRepository->delete($ids, $uninstallContext->getContext());
//Delete the TemplateType which were added by this Plugin
$mailTemplateTypeRepository->delete([
['id' => $myCustomMailTemplateType->getId()]
], $uninstallContext->getContext());
}
}