Skip to content

Commit

Permalink
[Improvement]: Add the possibility to filter on date and number types…
Browse files Browse the repository at this point in the history
… on calculated values (#16144)

* Add the possibility to filter on date and number types on calculated values

* fix filters when expressions are used instead of class

* Use addDay function indtead of adding seconds

Co-authored-by: Niklas <niklas.brunberg@cag.se>

* refactor fix filters when expressions are used instead of class

---------

Co-authored-by: Niklas <niklas.brunberg@cag.se>
  • Loading branch information
SamyMP and NiklasBr committed Nov 13, 2023
1 parent e9a6d42 commit a44da1e
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions models/DataObject/ClassDefinition/Data.php
Expand Up @@ -397,6 +397,7 @@ public function getFilterConditionExt(mixed $value, string $operator, array $par
$db = \Pimcore\Db::get();
$name = $params['name'] ?: $this->name;
$key = $db->quoteIdentifier($name);
$isNumeric = false;
if (!empty($params['brickPrefix'])) {
$key = $params['brickPrefix'].$key;
}
Expand All @@ -407,17 +408,50 @@ public function getFilterConditionExt(mixed $value, string $operator, array $par
return $key . ' ' . $operator . ' (' . $formattedValues . ')';
}

if ($value === 'NULL') {
if ($operator === '=') {
$operator = 'IS';
} elseif ($operator === '!=') {
$operator = 'IS NOT';
if ($this instanceof \Pimcore\Model\DataObject\ClassDefinition\Data\CalculatedValue) {
if ($this->elementType === 'date') {
$dateFormat = 'Y-m-d H:i:s';
$startDate = new \Carbon\Carbon($value);
if ($operator === '=') {
$maxTime = $startDate->addDay();
$endDate = new \Carbon\Carbon($maxTime);
$operator = ' BETWEEN ' . $db->quote($startDate->format($dateFormat));
$operator .= ' AND ' . $db->quote($endDate->format($dateFormat));

return $key . ' ' . $operator;
} else {
return $key . ' ' . $operator . ' ' . $db->quote($startDate->format($dateFormat));
}
}
} elseif (!is_array($value) && !is_object($value)) {
if ($operator === 'LIKE') {
$value = $db->quote('%' . $value . '%');
} else {
$value = $db->quote($value);

if ($this->elementType === 'boolean') {
if ($this->calculatorType === 'class') {
$bool = $value === 1 ? 1 : 0;
} else {
$bool = $value === 1 ? $db->quote('true') : $db->quote('false');
}

return $key . ' ' . $operator . ' ' . $bool;
}

if ($this->elementType === 'numeric') {
$isNumeric = true;
}
}

if (!$isNumeric) {
if ($value === 'NULL') {
if ($operator === '=') {
$operator = 'IS';
} elseif ($operator === '!=') {
$operator = 'IS NOT';
}
} elseif (!is_array($value) && !is_object($value)) {
if ($operator === 'LIKE') {
$value = $db->quote('%' . $value . '%');
} else {
$value = $db->quote($value);
}
}
}

Expand Down

0 comments on commit a44da1e

Please sign in to comment.