Skip to content

Commit

Permalink
allow using double-opt-in variables in placeholder processor
Browse files Browse the repository at this point in the history
  • Loading branch information
solverat committed Oct 23, 2024
1 parent 3550768 commit 49405f0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
3 changes: 3 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Upgrade Notes

## 5.1.4
- **[BUGFIX]** Allow using double-opt-in variables in placeholder processor

## 5.1.3
- **[BUGFIX]** Fix DoubleOptInType Service Definition [@dpfaffenbauer](https://github.com/dachcom-digital/pimcore-formbuilder/pull/483)

Expand Down
6 changes: 4 additions & 2 deletions docs/OutputWorkflow/10_EmailChannel.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ There is also a `Default` fallback field. If a requested locale can't be found d
***

## Placeholder

Place your form somewhere on your Website.
If you want to submit the form to the user, you can use your field names as placeholders. Formbuilder automatically will transform the field into the given address.
For Example you can set a placeholder called `%emailaddress%` (where *emailaddress* is the name of your form field) in the *To:* field ("Settings" Tab of your email template).
For example, you can set a placeholder called `%emailaddress%` (where *emailaddress* is the name of your form field) in the *To:* field ("Settings" Tab of your email template).

**Subject, From, ReplyTo**
You also may want to add some placeholder in the subject of your mail template.
To do so, just add a placeholder like `%emailaddress%` to the subject field. They will get transformed automatically.

> [!NOTE]
> If the double-opt-in-feature is enabled, an additional property `double_opt_in_session_email` is available!
***

## Mail Submission Types
Expand Down
63 changes: 53 additions & 10 deletions src/OutputWorkflow/Channel/Email/Parser/MailParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ public function create(Email $mailTemplate, FormInterface $form, array $channelC

$fieldValues = $this->formValuesOutputApplier->applyForChannel($form, $ignoreFields, 'mail', $locale);

$this->parseMailRecipients($mailTemplate, $fieldValues);
$this->parseMailSender($mailTemplate, $fieldValues);
$this->parseReplyTo($mailTemplate, $fieldValues);
$this->parseSubject($mailTemplate, $fieldValues);
$this->setMailPlaceholders($mail, $fieldValues);
$systemFieldValues = $fieldValues;
if ($doubleOptInSession instanceof DoubleOptInSessionInterface) {
$systemFieldValues = array_merge($systemFieldValues, $this->createDoubleOptInSessionValues($doubleOptInSession, ['email'], true));
}

$mail->setParam('double_opt_in_session', $doubleOptInSession);
$this->parseMailRecipients($mailTemplate, $systemFieldValues);
$this->parseMailSender($mailTemplate, $systemFieldValues);
$this->parseReplyTo($mailTemplate, $systemFieldValues);
$this->parseSubject($mailTemplate, $systemFieldValues);
$this->setMailPlaceholders($mail, $fieldValues);

/** @var FormDataInterface $formData */
$formData = $form->getData();
Expand Down Expand Up @@ -146,10 +149,10 @@ protected function setMailBodyPlaceholder(

$doubleOptInSessionValues = [];
if ($doubleOptInSession instanceof DoubleOptInSessionInterface) {
$doubleOptInSessionValues['email'] = $doubleOptInSession->getEmail();
$doubleOptInSessionValues['token'] = $doubleOptInSession->getTokenAsString();
$doubleOptInSessionValues['creation_date'] = $doubleOptInSession->getCreationDate();
$doubleOptInSessionValues['additional_data'] = $doubleOptInSession->getAdditionalData();
$doubleOptInSessionValues = $this->createDoubleOptInSessionValues(
$doubleOptInSession,
['email', 'token', 'creation_date', 'additional_data']
);
}

if ($mailLayout === null) {
Expand Down Expand Up @@ -297,4 +300,44 @@ protected function getMailLayout(array $channelConfiguration, bool $forcePlainTe

return $data === '' ? null : $data;
}

protected function createDoubleOptInSessionValues(?DoubleOptInSessionInterface $doubleOptInSession, array $validFields, bool $createFieldValueScheme = false): array
{
if (!$doubleOptInSession instanceof DoubleOptInSessionInterface) {
return [];
}

$data = [];

foreach ($validFields as $validField) {
$data[] = match ($validField) {
'email' => $this->createDoubleOptInSessionValue('email', $doubleOptInSession->getEmail(), $createFieldValueScheme),
'token' => $this->createDoubleOptInSessionValue('token', $doubleOptInSession->getTokenAsString(), $createFieldValueScheme),
'creation_date' => $this->createDoubleOptInSessionValue('creation_date', $doubleOptInSession->getCreationDate(), $createFieldValueScheme),
'additional_data' => $this->createDoubleOptInSessionValue('additional_data', $doubleOptInSession->getAdditionalData(), $createFieldValueScheme),
default => null,
};
}

return array_merge([], ...$data);
}

protected function createDoubleOptInSessionValue(string $name, mixed $value, bool $createFieldValueScheme = false): mixed
{
if ($createFieldValueScheme === false) {
return [$name => $value];
}

return [
sprintf('double_opt_in_session_%s', $name) => [
'label' => sprintf('double_opt_in_session_%s', $name),
'email_label' => sprintf('double_opt_in_session_%s', $name),
'name' => sprintf('double_opt_in_session_%s', $name),
'value' => $value,
'field_type' => 'simple',
'type' => 'text'
]
];

}
}

0 comments on commit 49405f0

Please sign in to comment.