Skip to content

Commit 8e5cc19

Browse files
authored
Merge pull request #12 from kitzberger/ifs
Feature: ifs within mailtexts
2 parents 9d60df1 + 6ae6a9f commit 8e5cc19

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

Classes/Domain/Finishers/EmailFinisher.php

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Kitzberger\FormMailtext\Domain\Finishers;
44

55
use TYPO3\CMS\Core\Mail\FluidEmail;
6+
use TYPO3\CMS\Core\Utility\GeneralUtility;
67
use TYPO3\CMS\Fluid\View\StandaloneView;
78
use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
89

@@ -18,7 +19,14 @@ protected function initializeStandaloneView(FormRuntime $formRuntime, string $fo
1819
{
1920
$standaloneView = parent::initializeStandaloneView($formRuntime, $format);
2021

21-
$standaloneView->assign('message', $this->parseOption('message'));
22+
$message = $this->parseOption('message');
23+
$formValues = $formRuntime->getFormState()->getFormValues();
24+
#debug($formValues, 'formValues');
25+
#debug($message, 'message (before)');
26+
$message = $this->replaceIfs($message, $formValues);
27+
#debug($message, 'message (after)');
28+
#die();
29+
$standaloneView->assign('message', $message);
2230

2331
return $standaloneView;
2432
}
@@ -27,8 +35,83 @@ protected function initializeFluidEmail(FormRuntime $formRuntime): FluidEmail
2735
{
2836
$fluidEmail = parent::initializeFluidEmail($formRuntime);
2937

30-
$fluidEmail->assign('message', $this->parseOption('message'));
38+
$message = $this->parseOption('message');
39+
$formValues = $formRuntime->getFormState()->getFormValues();
40+
$message = $this->replaceIfs($message, $formValues);
41+
42+
$fluidEmail->assign('message', $message);
3143

3244
return $fluidEmail;
3345
}
46+
47+
private function replaceIfs($message, $formValues)
48+
{
49+
return preg_replace_callback(
50+
'/{if:([a-z0-9\-_]+):([^:]+):([a-z0-9\-_,]*)}(.*)({endif})/sU',
51+
function($match) use ($formValues) {
52+
#debug($match, 'a match!');
53+
54+
$operandOne = $match[1];
55+
$operation = $match[2];
56+
$operandTwo = $match[3];
57+
58+
list($thenValue, $elseValue) = GeneralUtility::trimExplode('{else}', $match[4], true);
59+
#debug([$thenValue, $elseValue]);
60+
61+
if (isset($formValues[$operandOne])) {
62+
$operandOneValue = $formValues[$operandOne];
63+
}
64+
if (isset($formValues[$operandTwo])) {
65+
$operandTwoValue = $formValues[$operandTwo];
66+
} else {
67+
$operandTwoValue = $operandTwo;
68+
}
69+
70+
switch ($operation) {
71+
case '=':
72+
case '==':
73+
case '===':
74+
if ($operandOneValue == $operandTwoValue) {
75+
return $thenValue;
76+
}
77+
return $elseValue ?? '';
78+
case '>':
79+
case '>':
80+
if ($operandOneValue > $operandTwoValue) {
81+
return $thenValue;
82+
}
83+
return $elseValue ?? '';
84+
case '<':
85+
case '&lt;':
86+
if ($operandOneValue < $operandTwoValue) {
87+
return $thenValue;
88+
}
89+
return $elseValue ?? '';
90+
case '!=':
91+
case '<>':
92+
case '&lt;&gt;':
93+
if ($operandOneValue != $operandTwoValue) {
94+
return $thenValue;
95+
}
96+
return $elseValue ?? '';
97+
case 'in':
98+
// example: {if:multicheckbox-1:in:dog,cat}
99+
100+
if (!is_array($operandOneValue)) {
101+
$operandOneValue = [$operandOneValue];
102+
}
103+
$operandTwoValue = GeneralUtility::trimExplode(',', $operandTwoValue, true);
104+
105+
foreach ($operandOneValue as $value) {
106+
if (in_array($value, $operandTwoValue)) {
107+
return $thenValue;
108+
}
109+
}
110+
111+
return $elseValue ?? '';
112+
}
113+
},
114+
$message
115+
);
116+
}
34117
}

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,21 @@ module.tx_form {
2828
}
2929
}
3030
```
31+
32+
## Mail text
33+
34+
Within the mail text you can output any user input using the identifier of the input field, e.g. `{text-1}`.
35+
36+
Additionally there's an **experimental feature** enabling you to use `if` like control flows:
37+
38+
```
39+
Hi {text-1},
40+
41+
you've selected {if:singleselect-1:=:}nothing{endif}{if:singleselect-1:=:1}apple{endif}{if:singleselect-1:=:2}orange{endif}{if:singleselect-1:=:3}banana{endif}.
42+
43+
And your other input is within an array: {if:multiselect-1:in:cat,dog,hamster}it's a pet{endif}
44+
45+
Apparently you like {if:selectsingle-1:=:1}bikes{else}walking{endif}
46+
47+
Good bye.
48+
```

0 commit comments

Comments
 (0)