-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathEmailFinisher.php
131 lines (113 loc) · 4.51 KB
/
EmailFinisher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
declare(strict_types = 1);
namespace Kitzberger\FormMailtext\Domain\Finishers;
use TYPO3\CMS\Core\Mail\FluidEmail;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
class EmailFinisher extends \TYPO3\CMS\Form\Domain\Finishers\EmailFinisher
{
/**
* @param FormRuntime $formRuntime
* @param string $format
* @return StandaloneView
* @throws FinisherException
*/
protected function initializeStandaloneView(FormRuntime $formRuntime, string $format): StandaloneView
{
$standaloneView = parent::initializeStandaloneView($formRuntime, $format);
$message = $this->parseMessage();
$formValues = $formRuntime->getFormState()->getFormValues();
#debug($formValues, 'formValues');
#debug($message, 'message (before)');
$message = $this->replaceIfs($message, $formValues);
#debug($message, 'message (after)');
#die();
$standaloneView->assign('message', $message);
return $standaloneView;
}
protected function initializeFluidEmail(FormRuntime $formRuntime): FluidEmail
{
$fluidEmail = parent::initializeFluidEmail($formRuntime);
$message = $this->parseMessage();
$formValues = $formRuntime->getFormState()->getFormValues();
$message = $this->replaceIfs($message, $formValues);
$fluidEmail->assign('message', $message);
return $fluidEmail;
}
protected function parseMessage()
{
$message = $this->parseOption('message');
if (empty($message)) {
$message = '{variables}';
}
return $message;
}
private function replaceIfs($message, $formValues)
{
return preg_replace_callback(
'/{if:([a-z0-9\-_]+):([^:]+):([a-z0-9\-_,]*)}(.*)({endif})/isU',
function($match) use ($formValues) {
#debug($match, 'a match!');
$operandOne = $match[1];
$operation = $match[2];
$operandTwo = $match[3];
$thenElse = GeneralUtility::trimExplode('{else}', $match[4], true);
$thenValue = $thenElse[0];
$elseValue = $thenElse[1] ?? null;
if (isset($formValues[$operandOne])) {
$operandOneValue = $formValues[$operandOne];
} else {
#debug($formValues);
throw new \Exception('This field identifier does not exist: ' . $operandOne);
}
if (isset($formValues[$operandTwo])) {
$operandTwoValue = $formValues[$operandTwo];
} else {
$operandTwoValue = $operandTwo;
}
switch ($operation) {
case '=':
case '==':
case '===':
if ($operandOneValue == $operandTwoValue) {
return $thenValue;
}
return $elseValue ?? '';
case '>':
case '>':
if ($operandOneValue > $operandTwoValue) {
return $thenValue;
}
return $elseValue ?? '';
case '<':
case '<':
if ($operandOneValue < $operandTwoValue) {
return $thenValue;
}
return $elseValue ?? '';
case '!=':
case '<>':
case '<>':
if ($operandOneValue != $operandTwoValue) {
return $thenValue;
}
return $elseValue ?? '';
case 'in':
// example: {if:multicheckbox-1:in:dog,cat}
if (!is_array($operandOneValue)) {
$operandOneValue = [$operandOneValue];
}
$operandTwoValue = GeneralUtility::trimExplode(',', $operandTwoValue, true);
foreach ($operandOneValue as $value) {
if (in_array($value, $operandTwoValue)) {
return $thenValue;
}
}
return $elseValue ?? '';
}
},
$message
);
}
}