Skip to content

Commit

Permalink
Fix: Problem with Notification grid column filter (#16968)
Browse files Browse the repository at this point in the history
  • Loading branch information
blankse committed Apr 26, 2024
1 parent 6b40dbb commit 07a760f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 26 deletions.
2 changes: 1 addition & 1 deletion models/Notification/Listing.php
Expand Up @@ -35,7 +35,7 @@ public function isValidOrderKey(string $key): bool
*
* @return Model\Notification[]
*/
public function getItems(int $offset, int $limit): array
public function getItems(int $offset, ?int $limit): array
{
$this->setOffset($offset);
$this->setLimit($limit);
Expand Down
33 changes: 27 additions & 6 deletions models/Notification/Service/NotificationService.php
Expand Up @@ -166,25 +166,46 @@ public function findAndMarkAsRead(int $id, ?int $recipientId = null): Notificati
return $notification;
}

/**
* @param array<string, mixed> $filter
* @param array{offset?: int|string, limit?: int|string|null} $options
*
* @return array{total: int, data: Notification[]}
*/
public function findAll(array $filter = [], array $options = []): array
{
$listing = new Listing();

if (!empty($filter)) {
if ($filter) {
$conditions = [];
$conditionVariables = [];
foreach ($filter as $key => $value) {
$conditions[] = $key . ' = :' . $key;
if (isset($value['condition'])) {
$conditions[] = $value['condition'];
$conditionVariables[] = $value['conditionVariables'] ?? [];
} else {
$conditions[] = $key . ' = :' . $key;
$conditionVariables[] = [$key => $value];
}
}

$condition = implode(' AND ', $conditions);
$listing->setCondition($condition, $filter);
$listing->setCondition($condition, array_merge(...$conditionVariables));
}

$listing->setOrderKey('creationDate');
$listing->setOrder('DESC');
$options += ['offset' => 0, 'limit' => 0];
$offset = (int) $options['offset'];
$limit = (int) $options['limit'];
$offset = $options['offset'] ?? 0;
$limit = $options['limit'] ?? null;

if (is_string($offset)) {
//TODO: Trigger deprecation
$offset = (int) $offset;
}
if (is_string($limit)) {
//TODO: Trigger deprecation
$limit = (int) $limit;
}

$this->beginTransaction();

Expand Down
70 changes: 51 additions & 19 deletions models/Notification/Service/NotificationServiceFilterParser.php
Expand Up @@ -17,6 +17,7 @@

namespace Pimcore\Model\Notification\Service;

use Carbon\Carbon;
use Symfony\Component\HttpFoundation\Request;

/**
Expand Down Expand Up @@ -48,39 +49,43 @@ class NotificationServiceFilterParser

private Request $request;

private array $properties;
private array $properties = [
'title' => 'title',
'timestamp' => 'creationDate',
];

/**
* ExtJSFilterParser constructor.
*
*/
public function __construct(Request $request)
{
$this->request = $request;
$this->properties = [
'title' => 'title',
'date' => 'creationDate',
];
}

/**
* @return array<string, array{condition: string, conditionVariables: array<string, mixed>}>
*/
public function parse(): array
{
$result = [];
$filter = $this->request->get(self::KEY_FILTER, '[]');
$filter = $this->request->request->get(self::KEY_FILTER, '[]');
$items = json_decode($filter, true);

foreach ($items as $item) {
$type = $item[self::KEY_TYPE];

switch ($type) {
case self::TYPE_STRING:
[$key, $value] = $this->parseString($item);
$result[$key] = $value;
[$key, $condition, $conditionVariables] = $this->parseString($item);
$result[$key] = [
'condition' => $condition,
'conditionVariables' => $conditionVariables,
];

break;
case self::TYPE_DATE:
[$key, $value] = $this->parseDate($item);
$result[$key] = $value;
[$key, $condition, $conditionVariables] = $this->parseDate($item);
$result[$key] = [
'condition' => $condition,
'conditionVariables' => $conditionVariables,
];

break;
}
Expand All @@ -90,6 +95,8 @@ public function parse(): array
}

/**
* @return array{0: string, 1: string, 2: array<string, mixed>}
*
* @throws \Exception
*/
private function parseString(array $item): array
Expand All @@ -100,7 +107,12 @@ private function parseString(array $item): array

switch ($item[self::KEY_OPERATOR]) {
case self::OPERATOR_LIKE:
$result = ["{$property} LIKE ?", "%{$value}%"];
$key = $property . '_like';
$result = [
$key,
"{$property} LIKE :{$key}",
[$key => "%{$value}%"],
];

break;
}
Expand All @@ -113,25 +125,45 @@ private function parseString(array $item): array
}

/**
* @return array{0: string, 1: string, 2: array<string, mixed>}
*
* @throws \Exception
*/
private function parseDate(array $item): array
{
$result = null;
$property = $this->getDbProperty($item);
$value = strtotime($item[self::KEY_VALUE]);
$value = new Carbon($item[self::KEY_VALUE]);

switch ($item[self::KEY_OPERATOR]) {
case self::OPERATOR_EQ:
$result = ["{$property} BETWEEN ? AND ?", [$value, $value + (86400 - 1)]];
$key = $property . '_eq';
$result = [
$key,
"{$property} BETWEEN :{$key}_start AND :{$key}_end",
[
$key . '_start' => $value->toDateTimeString(),
$key . '_end' => $value->addDay()->subSecond()->toDateTimeString(),
],
];

break;
case self::OPERATOR_GT:
$result = ["{$property} > ?", [$value]];
$key = $property . '_gt';
$result = [
$key,
"{$property} > :{$key}",
[$key => $value->toDateTimeString()],
];

break;
case self::OPERATOR_LT:
$result = ["{$property} < ?", [$value]];
$key = $property . '_lt';
$result = [
$key,
"{$property} < :{$key}",
[$key => $value->addDay()->subSecond()->toDateTimeString()],
];

break;
}
Expand Down

0 comments on commit 07a760f

Please sign in to comment.