<?php declare(strict_types=1);
namespace LoyxxSeminar;
use Doctrine\DBAL\Connection;
use Shopware\Core\Checkout\Cart\CartEvents;
use Shopware\Core\Checkout\Order\OrderEvents;
use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
use Shopware\Core\Content\MailTemplate\MailTemplateActions;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class LoyxxSeminar extends Plugin
{
public const BUNDLE_NAME = 'LoyxxSeminar';
public const PRODUCT_CUSTOM_FIELD_GROUP = 'loyxx_seminar_group';
public const PRODUCT_CUSTOM_FIELD_SEMINAR = 'loyxx_seminar';
public const MAIL_TEMPLATE_TYPE = 'loyxx_seminar_mt'; // UUid for loyxx_seminar_mail_template
public const EVENT_ACTION_ID = 'a61d1c00d51c4e85a3589e476fd43cf7';
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->setupSeminarCustomField($installContext->getContext());
$this->setupSeminarMailTemplate($installContext->getContext());
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
$connection = $this->container->get(Connection::class);
$connection->executeStatement('SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;');
$connection->executeStatement("DROP TABLE IF EXISTS `loyxx_order_participant`");
$connection->executeStatement('SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;');
$this->removeSeminarCustomField($uninstallContext);
$this->removeMailTemplate($uninstallContext->getContext());
$this->removeSeminarEventAction($uninstallContext->getContext());
}
private function setupSeminarCustomField(Context $context)
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
//check if the custom field set is defined
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', static::PRODUCT_CUSTOM_FIELD_GROUP));
$customFieldSetEntity = $customFieldSetRepository->searchIds($criteria, $context);
if (!$customFieldSetEntity->getTotal()) {
$customFieldSetEntity = $customFieldSetRepository->create(
[
[
'name' => static::PRODUCT_CUSTOM_FIELD_GROUP,
'customFields' => [
[
'name' => static::PRODUCT_CUSTOM_FIELD_SEMINAR ,
'type' => CustomFieldTypes::BOOL,
'config' => [
'type' => 'switch',
'label' => [
'en-GB' => 'Activate participant booking',
'de-DE' => 'Teilnehmerbuchung aktivieren'
],
'componentName' => 'sw-field',
'customFieldType' => 'switch',
'customFieldPosition' => 1,
'translated' => true
]
]
],
'relations' => [
['entityName' => 'product']
],
'config' => [
'description' => [
'en-GB' => 'Seminar participation',
'de-DE' => 'Teilnahme am Seminar'
],
'label' => [
'en-GB' => 'Seminar',
'de-DE' => 'Seminar'
],
'translated' => true
]
]
],
$context
);
}
}
private function removeSeminarCustomField(UninstallContext $uninstallContext)
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$context = $uninstallContext->getContext();
//check if the custom field set is defined
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', static::PRODUCT_CUSTOM_FIELD_GROUP));
$customFieldSetEntity = $customFieldSetRepository->searchIds($criteria, $context);
if ($customFieldSetEntity->getTotal()) {
$customFieldSetRepository->delete(array_values($customFieldSetEntity->getData()), $context);
}
}
/**
* @param Context $context
*/
private function setupSeminarMailTemplate(Context $context)
{
$mailTemplateId = Uuid::fromBytesToHex(static::MAIL_TEMPLATE_TYPE);
$mailTemplateRepository = $this->container->get('mail_template.repository');
$mailTemplate = $this->mailTemplateExists($mailTemplateRepository, $mailTemplateId, $context);
$mailTemplateType = $this->getMailTemplateType($context);
if ($mailTemplateType !== null ){
if ($mailTemplate === null){
$mailTemplateRepository->create([
[
"id" => $mailTemplateId,
"mailTemplateTypeId" => $mailTemplateType->getId(),
"senderName" => [
"en-GB" => "{{ salesChannel.name }}",
"de-DE" => "{{ salesChannel.name }}"
],
"subject" => [
"en-GB" => "Order confirmation with participant details",
"de-DE" => "Auftragsbestätigung mit Teilnehmerangaben"
],
"description" => [
"en-GB" => "Order confirmation email template with participant details",
"de-DE" => "Bestellbestätigungs-E-Mail-Vorlage mit Teilnehmerdetails"
],
"contentHtml" => [
"en-GB" => $this->getOrderConfirmationHtmlTemplate('en-GB'),
"de-DE" => $this->getOrderConfirmationHtmlTemplate('de-DE')
],
"contentPlain" => [
"en-GB" => $this->getOrderConfirmationPlainTemplate('en-GB'),
"de-DE" => $this->getOrderConfirmationPlainTemplate('de-DE')
]
]
], $context);
}
$this->eventActionRepository()->upsert([
[
'id' => static::EVENT_ACTION_ID,
'title' => "Order confirmation with participant details",
'eventName' => CartEvents::CHECKOUT_ORDER_PLACED,
'actionName' => MailTemplateActions::MAIL_TEMPLATE_MAIL_SEND_ACTION,
'config' => [
'mail_template_id' => $mailTemplateId,
'mail_template_type_id' => $mailTemplateType->getId()
]
]
], $context);
}
}
/**
* @param string $type
* @return string
*/
private function getOrderConfirmationHtmlTemplate($type = 'de-DE'): string
{
return file_get_contents(__DIR__."/Resources/stubs/participants.{$type}.stub.html.twig");
}
/**
* @param string $type
* @return string
*/
private function getOrderConfirmationPlainTemplate($type = 'de-DE'): string
{
return file_get_contents(__DIR__."/Resources/stubs/participants.{$type}.stub.txt.twig");
}
/**
* @param object|null $mailTemplateRepository
* @param string $mailTemplateId
* @param Context $context
* @return bool
*/
private function mailTemplateExists(?object $mailTemplateRepository, string $mailTemplateId, Context $context)
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('id', $mailTemplateId));
return $mailTemplateRepository->search($criteria, $context)->first();
}
/**
* @param Context $context
* @return mixed
*/
private function getMailTemplateType(Context $context)
{
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('technicalName', 'order_confirmation_mail'));
return $mailTemplateTypeRepository->search($criteria, $context)->first();
}
/**
* @return object|EntityRepository|null
*/
private function mailTemplateRepository()
{
return $this->container->get('mail_template.repository');
}
/**
* @return object|EntityRepository|null
*/
private function mailTemplateTypeRepository()
{
return $this->container->get('mail_template_type.repository');
}
/**
* @return object|EntityRepository|null
*/
private function eventActionRepository()
{
return $this->container->get('event_action.repository');
}
private function removeMailTemplateType(Context $context)
{
/** @var MailTemplateTypeEntity $templateType */
$templateType = $this->getMailTemplateType($context);
if ($templateType){
$this->mailTemplateTypeRepository()->delete([
[
'id' => $templateType->getId()
]
], $context);
}
}
private function removeMailTemplate(Context $context)
{
$this->mailTemplateRepository()->delete([
[
'id' => Uuid::fromBytesToHex(static::MAIL_TEMPLATE_TYPE)
]
], $context);
}
private function removeSeminarEventAction(Context $context)
{
$this->eventActionRepository()->delete([
[
'id' => static::EVENT_ACTION_ID
]
], $context);
}
public function update(UpdateContext $updateContext) : void
{
parent::update($updateContext);
$this->setupSeminarMailTemplate($updateContext->getContext());
}
}