Skip to content

Commit 5ad9c44

Browse files
committed
Implement 'in array' comparison
1 parent 7393b53 commit 5ad9c44

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

Classes/Domain/Finishers/EmailFinisher.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
declare(strict_types = 1);
33
namespace Kitzberger\FormMailtext\Domain\Finishers;
44

5+
use TYPO3\CMS\Core\Utility\GeneralUtility;
56
use TYPO3\CMS\Fluid\View\StandaloneView;
67
use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
78

@@ -32,7 +33,7 @@ protected function initializeStandaloneView(FormRuntime $formRuntime, string $fo
3233
private function replaceIfs($message, $formValues)
3334
{
3435
return preg_replace_callback(
35-
'/{if:([a-z0-9\-_]+):(.+):([a-z0-9\-_]*)}(.*)({endif})/sU',
36+
'/{if:([a-z0-9\-_]+):([^:]+):([a-z0-9\-_,]*)}(.*)({endif})/sU',
3637
function($match) use ($formValues) {
3738
#debug($match, 'a match!');
3839

@@ -55,31 +56,41 @@ function($match) use ($formValues) {
5556
case '===':
5657
if ($operandOneValue == $operandTwoValue) {
5758
return $match[4];
58-
} else {
59-
return '';
6059
}
60+
return '';
6161
case '>':
6262
case '>':
6363
if ($operandOneValue > $operandTwoValue) {
6464
return $match[4];
65-
} else {
66-
return '';
6765
}
66+
return '';
6867
case '<':
6968
case '&lt;':
7069
if ($operandOneValue < $operandTwoValue) {
7170
return $match[4];
72-
} else {
73-
return '';
7471
}
72+
return '';
7573
case '!=':
7674
case '<>':
7775
case '&lt;&gt;':
7876
if ($operandOneValue != $operandTwoValue) {
7977
return $match[4];
80-
} else {
81-
return '';
8278
}
79+
return '';
80+
case 'in':
81+
// example: {if:multicheckbox-1:in:dog,cat}
82+
83+
if (!is_array($operandOneValue)) {
84+
$operandOneValue = [$operandOneValue];
85+
}
86+
$operandTwoValue = GeneralUtility::trimExplode(',', $operandTwoValue, true);
87+
88+
foreach ($operandOneValue as $value) {
89+
if (in_array($value, $operandTwoValue)) {
90+
return $match[4];
91+
}
92+
}
93+
return '';
8394
}
8495
},
8596
$message

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ Additionally there's an **experimental feature** enabling you to use `if` like c
3838
```
3939
Hi {text-1},
4040
41-
you've selected {if:singleselect-1:=:1}apple{endif}{if:singleselect-1:=:2}orange{endif}{if:singleselect-1:=:3}banana{endif}.
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}
4244
4345
Good bye.
4446
```

0 commit comments

Comments
 (0)