Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAUT-11289 : Segments: New (not)between date filter #347

Merged
merged 12 commits into from
Jun 14, 2024
2 changes: 1 addition & 1 deletion CustomFieldType/DateTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function getEntityClass(): string
public function getOperators(): array
{
$allOperators = parent::getOperators();
$allowedOperators = array_flip(['=', '!=', 'gt', 'gte', 'lt', 'lte', 'empty', '!empty']);
$allowedOperators = array_flip(['=', '!=', 'gt', 'gte', 'lt', 'lte', 'empty', '!empty', 'between', '!between']);

return array_intersect_key($allOperators, $allowedOperators);
}
Expand Down
2 changes: 1 addition & 1 deletion CustomFieldType/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function getEntityClass(): string
public function getOperators(): array
{
$allOperators = parent::getOperators();
$allowedOperators = array_flip(['=', '!=', 'gt', 'gte', 'lt', 'lte', 'empty', '!empty']);
$allowedOperators = array_flip(['=', '!=', 'gt', 'gte', 'lt', 'lte', 'empty', '!empty', 'between', '!between']);

return array_intersect_key($allOperators, $allowedOperators);
}
Expand Down
25 changes: 22 additions & 3 deletions Helper/QueryFilterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public function addCustomFieldValueExpressionFromSegmentFilter(
$tableAlias,
$filter,
$valueParameter,
$filterAlreadyNegated
$filterAlreadyNegated,
$filter->getParameterValue()
);

$this->addOperatorExpression(
Expand Down Expand Up @@ -157,14 +158,17 @@ private function addOperatorExpression(
/**
* Form the logical expression needed to limit the CustomValue's value for given operator.
*
* @param mixed $filterParameterValue
*
* @return CompositeExpression|string
*/
private function getCustomValueValueExpression(
SegmentQueryBuilder $customQuery,
string $tableAlias,
ContactSegmentFilter $filter,
string $valueParameter,
bool $alreadyNegated = false
bool $alreadyNegated = false,
$filterParameterValue = null
) {
$operator = $filter->getOperator();
if ($alreadyNegated) {
Expand Down Expand Up @@ -228,6 +232,19 @@ private function getCustomValueValueExpression(
);

break;
case 'between':
case 'notBetween':
if (is_array($filterParameterValue)) {
$expression = $customQuery->expr()->{$operator}(
$tableAlias.'_value.value',
array_map(function (mixed $val) use ($customQuery): mixed {
return is_numeric($val) && intval($val) === $val ?
$val : $customQuery->expr()->literal($val);
}, array_values($filterParameterValue))
);
break;
}
// no break
dadarya0 marked this conversation as resolved.
Show resolved Hide resolved
default:
$expression = $customQuery->expr()->{$operator}(
$tableAlias.'_value.value',
Expand Down Expand Up @@ -445,7 +462,9 @@ private function getMergeExpression(
$qb,
$alias,
$filter,
$valueParameter
$valueParameter,
false,
$filter->getParameterValue()
);
}

Expand Down
17 changes: 17 additions & 0 deletions Tests/Functional/Helper/QueryFilterHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ public function testGetCustomValueValueExpression(): void
]
);

$this->assertMatchWhere(
"test_value.value BETWEEN '2024-05-15 00:00:00' AND '2024-05-24 23:59:59'",
[
'glue' => 'and',
'field' => 'cmf_'.$this->getFixtureById('custom_object_product')->getId(),
'object' => 'custom_object',
'type' => 'datetime',
'operator' => 'between',
'properties' => [
'filter' => [
'date_from' => 'May 15, 2024',
'date_to' => 'May 24, 2024',
],
],
]
);

$this->assertMatchWhere(
'test_value.value IS NULL',
[
Expand Down
Loading