-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathEmailFinisher.php
More file actions
117 lines (101 loc) · 4.14 KB
/
EmailFinisher.php
File metadata and controls
117 lines (101 loc) · 4.14 KB
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
<?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->parseOption('message');
$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->parseOption('message');
$formValues = $formRuntime->getFormState()->getFormValues();
$message = $this->replaceIfs($message, $formValues);
$fluidEmail->assign('message', $message);
return $fluidEmail;
}
private function replaceIfs($message, $formValues)
{
return preg_replace_callback(
'/{if:([a-z0-9\-_]+):([^:]+):([a-z0-9\-_,]*)}(.*)({endif})/sU',
function($match) use ($formValues) {
#debug($match, 'a match!');
$operandOne = $match[1];
$operation = $match[2];
$operandTwo = $match[3];
list($thenValue, $elseValue) = GeneralUtility::trimExplode('{else}', $match[4], true);
#debug([$thenValue, $elseValue]);
if (isset($formValues[$operandOne])) {
$operandOneValue = $formValues[$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
);
}
}