Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
vendor/
.idea/
*.log
.vite/
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "frosh/mjml",
"description": "Compiles MJML email templates to HTML for Shopware mail sending",
"version": "1.0.3",
"version": "1.1.0",
"type": "shopware-platform-plugin",
"license": "MIT",
"authors": [
Expand Down
3 changes: 1 addition & 2 deletions src/Entity/MailTemplate/FroshMjmlMailTemplateDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Shopware\Core\Content\MailTemplate\MailTemplateDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Field\BoolField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\FkField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\ApiAware;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\PrimaryKey;
Expand Down Expand Up @@ -39,7 +38,7 @@ protected function defineFields(): FieldCollection
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new PrimaryKey(), new Required(), new ApiAware()),
(new FkField('mail_template_id', 'mailTemplateId', MailTemplateDefinition::class))->addFlags(new Required(), new ApiAware()),
(new BoolField('enabled', 'enabled'))->addFlags(new ApiAware()),
(new TranslatedField('enabled'))->addFlags(new ApiAware()),
(new TranslatedField('mjmlContent'))->addFlags(new ApiAware()),
(new TranslationsAssociationField(FroshMjmlMailTemplateTranslationDefinition::class, 'frosh_mjml_mail_template_id'))->addFlags(new ApiAware(), new Required()),
(new OneToOneAssociationField('mailTemplate', 'mail_template_id', 'id', MailTemplateDefinition::class, false))->addFlags(new ApiAware()),
Expand Down
6 changes: 3 additions & 3 deletions src/Entity/MailTemplate/FroshMjmlMailTemplateEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FroshMjmlMailTemplateEntity extends Entity

protected string $mailTemplateId;

protected bool $enabled = false;
protected ?bool $enabled = null;

protected ?string $mjmlContent = null;

Expand All @@ -30,12 +30,12 @@ public function setMailTemplateId(string $mailTemplateId): void
$this->mailTemplateId = $mailTemplateId;
}

public function isEnabled(): bool
public function getEnabled(): ?bool
{
return $this->enabled;
}

public function setEnabled(bool $enabled): void
public function setEnabled(?bool $enabled): void
{
$this->enabled = $enabled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Frosh\Mjml\Entity\MailTemplate;

use Shopware\Core\Framework\DataAbstractionLayer\EntityTranslationDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Field\BoolField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\AllowHtml;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\ApiAware;
use Shopware\Core\Framework\DataAbstractionLayer\Field\LongTextField;
Expand Down Expand Up @@ -37,6 +38,7 @@ protected function getParentDefinitionClass(): string
protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new BoolField('enabled', 'enabled'))->addFlags(new ApiAware()),
(new LongTextField('mjml_content', 'mjmlContent'))->addFlags(new ApiAware(), new AllowHtml(false)),
]);
}
Expand Down
12 changes: 12 additions & 0 deletions src/Entity/MailTemplate/FroshMjmlMailTemplateTranslationEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class FroshMjmlMailTemplateTranslationEntity extends TranslationEntity

protected ?FroshMjmlMailTemplateEntity $froshMjmlMailTemplate = null;

protected ?bool $enabled = null;

protected ?string $mjmlContent = null;

public function getFroshMjmlMailTemplateId(): string
Expand All @@ -32,6 +34,16 @@ public function setFroshMjmlMailTemplate(?FroshMjmlMailTemplateEntity $froshMjml
$this->froshMjmlMailTemplate = $froshMjmlMailTemplate;
}

public function getEnabled(): ?bool
{
return $this->enabled;
}

public function setEnabled(?bool $enabled): void
{
$this->enabled = $enabled;
}

public function getMjmlContent(): ?string
{
return $this->mjmlContent;
Expand Down
2 changes: 1 addition & 1 deletion src/Listener/MailBeforeValidateListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function onMailBeforeValidate(MailBeforeValidateEvent $event): void

/** @var FroshMjmlMailTemplateEntity|null $config */
$config = $mailTemplate?->getExtension('froshMjml');
if ($config === null || !$config->isEnabled()) {
if ($config === null || $config->getTranslation('enabled') !== true) {
return;
}

Expand Down
43 changes: 43 additions & 0 deletions src/Migration/Migration1784619412TranslatableEnabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

namespace Frosh\Mjml\Migration;

use Doctrine\DBAL\Connection;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Migration\MigrationStep;
use Shopware\Core\Framework\Uuid\Uuid;

class Migration1784619412TranslatableEnabled extends MigrationStep
{
public function getCreationTimestamp(): int
{
return 1784619412;
}

public function update(Connection $connection): void
{
$connection->executeStatement('
ALTER TABLE `frosh_mjml_mail_template_translation`
ADD COLUMN `enabled` TINYINT(1) NULL AFTER `language_id`
');

$connection->executeStatement('
INSERT INTO `frosh_mjml_mail_template_translation`
(`frosh_mjml_mail_template_id`, `language_id`, `enabled`, `created_at`)
SELECT `id`, :languageId, `enabled`, NOW(3)
FROM `frosh_mjml_mail_template`
WHERE `enabled` = 1
ON DUPLICATE KEY UPDATE `enabled` = VALUES(`enabled`)
', ['languageId' => Uuid::fromHexToBytes(Defaults::LANGUAGE_SYSTEM)]);
}

public function updateDestructive(Connection $connection): void
{
$columns = $connection->fetchAllAssociative('SHOW COLUMNS FROM `frosh_mjml_mail_template` LIKE \'enabled\'');
if ($columns === []) {
return;
}

$connection->executeStatement('ALTER TABLE `frosh_mjml_mail_template` DROP COLUMN `enabled`');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,65 @@ const { Criteria } = Shopware.Data;
Component.override('sw-mail-template-detail', {
template,

data() {
return {
froshMjmlHasParentLanguage: false,
froshMjmlInheritedEnabled: null,
};
},

computed: {
froshMjmlConfig() {
return this.mailTemplate?.extensions?.froshMjml ?? null;
},

froshMjmlEnabled() {
const config = this.froshMjmlConfig;
if (!config) {
return false;
}

if (config.enabled !== null && config.enabled !== undefined) {
return config.enabled;
}

return (
this.froshMjmlInheritedEnabled ??
config.translated?.enabled ??
false
);
},
},

watch: {
'mailTemplate.id': {
mailTemplate: {
immediate: true,
handler() {
if (
this.mailTemplate &&
!this.mailTemplate.extensions.froshMjml
) {
if (!this.mailTemplate?.id) {
return;
}

if (!this.mailTemplate.extensions.froshMjml) {
const config = this.repositoryFactory
.create('frosh_mjml_mail_template')
.create(Shopware.Context.api);
config.enabled = false;
config.mjmlContent = '';
this.mailTemplate.extensions.froshMjml = config;
}

this.loadFroshMjmlInheritance();
},
},

async 'mailTemplate.extensions.froshMjml.enabled'(enabled) {
const config = this.mailTemplate?.extensions?.froshMjml;
const config = this.froshMjmlConfig;

if (!enabled || !config || config.mjmlContent) {
if (
!enabled ||
!config ||
config.mjmlContent ||
config.translated?.mjmlContent
) {
return;
}

Expand All @@ -41,11 +78,37 @@ Component.override('sw-mail-template-detail', {
},

methods: {
onMjmlToggle(value) {
const config = this.mailTemplate?.extensions?.froshMjml;
if (config) {
config.enabled = value;
async loadFroshMjmlInheritance() {
const { languageId, systemLanguageId } = Shopware.Context.api;
this.froshMjmlHasParentLanguage = languageId !== systemLanguageId;

if (!this.froshMjmlHasParentLanguage) {
this.froshMjmlInheritedEnabled = null;
return;
}

const language = await this.repositoryFactory
.create('language')
.get(languageId, Shopware.Context.api);

const criteria = new Criteria(1, 1);
criteria.addFilter(
Criteria.equals('mailTemplateId', this.mailTemplate.id)
);

const inheritedLanguageContext = {
...Shopware.Context.api,
languageId: language?.parentId ?? systemLanguageId,
};

const config = (
await this.repositoryFactory
.create('frosh_mjml_mail_template')
.search(criteria, inheritedLanguageContext)
).first();

this.froshMjmlInheritedEnabled =
config?.translated?.enabled ?? config?.enabled ?? false;
},

async buildInitialMjmlContent() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
{% block sw_mail_template_mail_text_form_content_html_field %}
<mt-switch
<sw-inherit-wrapper
v-if="mailTemplate.extensions.froshMjml"
v-model:value="mailTemplate.extensions.froshMjml.enabled"
class="sw-mail-template-detail__mjml-toggle"
:label="$tc('frosh-mjml-component.editor.useMjml')"
:checked="mailTemplate.extensions.froshMjml.enabled"
:disabled="!acl.can('mail_templates.editor')"
@change="onMjmlToggle"
/>
:has-parent="froshMjmlHasParentLanguage"
:inherited-value="froshMjmlInheritedEnabled"
>
<template #content="props">
<mt-switch
:label="$tc('frosh-mjml-component.editor.useMjml')"
:is-inheritance-field="props.isInheritField"
:is-inherited="props.isInherited"
:inherited-value="!!froshMjmlInheritedEnabled"
:disabled="!acl.can('mail_templates.editor')"
:model-value="!!props.currentValue"
@update:model-value="props.updateCurrentValue"
@inheritance-restore="props.restoreInheritance"
@inheritance-remove="props.removeInheritance"
/>
</template>
</sw-inherit-wrapper>

<frosh-mjml-editor
v-if="mailTemplate.extensions.froshMjml && mailTemplate.extensions.froshMjml.enabled"
v-if="mailTemplate.extensions.froshMjml && froshMjmlEnabled"
:key="`${mailTemplate.mailTemplateTypeId}mjml`"
v-model:value="mailTemplate.extensions.froshMjml.mjmlContent"
class="sw-mail-template-detail__html-editor"
Expand All @@ -20,9 +33,7 @@
:editor-config="editorConfig"
/>

<div
v-show="!mailTemplate.extensions.froshMjml || !mailTemplate.extensions.froshMjml.enabled"
>
<div v-show="!mailTemplate.extensions.froshMjml || !froshMjmlEnabled">
{% parent() %}
</div>
{% endblock %}
Loading