33namespace Kitzberger \FormMailtext \Domain \Finishers ;
44
55use TYPO3 \CMS \Core \Mail \FluidEmail ;
6+ use TYPO3 \CMS \Core \Utility \GeneralUtility ;
67use TYPO3 \CMS \Fluid \View \StandaloneView ;
78use 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 '< ' :
86+ if ($ operandOneValue < $ operandTwoValue ) {
87+ return $ thenValue ;
88+ }
89+ return $ elseValue ?? '' ;
90+ case '!= ' :
91+ case '<> ' :
92+ case '<> ' :
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}
0 commit comments