Skip to content

Commit 5dff9a0

Browse files
committed
IBX-9060: Fixed notification query parameters to use arrays instead of strings
1 parent 55a5f0d commit 5dff9a0

File tree

4 files changed

+101
-22
lines changed

4 files changed

+101
-22
lines changed

src/bundle/Controller/NotificationController.php

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Ibexa\Bundle\AdminUi\Controller;
1010

11+
use DateTimeInterface;
1112
use Exception;
1213
use Ibexa\AdminUi\Form\Data\Notification\NotificationRemoveData;
1314
use Ibexa\AdminUi\Form\Factory\FormFactory;
@@ -79,16 +80,27 @@ public function getNotificationsAction(Request $request, int $offset, int $limit
7980

8081
public function renderNotificationsPageAction(Request $request, int $page): Response
8182
{
82-
$searchForm = $this->createSearchForm();
83+
$notificationTypes = array_unique(
84+
array_map(
85+
fn($notification) => $notification->type,
86+
$this->notificationService->loadNotifications(0, PHP_INT_MAX)->items
87+
)
88+
);
89+
sort($notificationTypes);
90+
91+
$searchForm = $this->createForm(SearchType::class, null, [
92+
'notification_types' => $notificationTypes,
93+
]);
8394
$searchForm->handleRequest($request);
8495

96+
$query = [];
8597
if ($searchForm->isSubmitted() && $searchForm->isValid()) {
8698
$data = $searchForm->getData();
8799
$query = $this->buildQuery($data);
88100
}
89101

90102
$pagerfanta = new Pagerfanta(
91-
new NotificationAdapter($this->notificationService, $query ?? null)
103+
new NotificationAdapter($this->notificationService, $query)
92104
);
93105
$pagerfanta->setMaxPerPage($this->configResolver->getParameter('pagination.notification_limit'));
94106
$pagerfanta->setCurrentPage(min($page, $pagerfanta->getNbPages()));
@@ -116,16 +128,38 @@ public function renderNotificationsPageAction(Request $request, int $page): Resp
116128
]);
117129
}
118130

119-
private function buildQuery(SearchQueryData $data): ?string
131+
private function buildQuery(SearchQueryData $data): array
120132
{
121-
return $data->getQuery();
122-
}
133+
$query = [];
123134

124-
private function createSearchForm(): FormInterface
125-
{
126-
return $this->createForm(SearchType::class);
135+
if ($data->getType()) {
136+
$query['type'] = $data->getType();
137+
}
138+
139+
if (!empty($data->getStatuses())) {
140+
$query['status'] = [];
141+
if (in_array('read', $data->getStatuses(), true)) {
142+
$query['status'][] = 'read';
143+
}
144+
if (in_array('unread', $data->getStatuses(), true)) {
145+
$query['status'][] = 'unread';
146+
}
147+
}
148+
149+
$range = $data->getCreatedRange();
150+
if ($range !== null) {
151+
if ($range->getMin() instanceof DateTimeInterface) {
152+
$query['created_from'] = $range->getMin()->getTimestamp();
153+
}
154+
if ($range->getMax() instanceof DateTimeInterface) {
155+
$query['created_to'] = $range->getMax()->getTimestamp();
156+
}
157+
}
158+
159+
return $query;
127160
}
128161

162+
129163
private function createNotificationRemoveData(Pagerfanta $pagerfanta): NotificationRemoveData
130164
{
131165
$notificationIds = [];

src/bundle/Form/Data/SearchQueryData.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,43 @@
88

99
namespace Ibexa\Bundle\AdminUi\Form\Data;
1010

11+
use Ibexa\AdminUi\Form\Data\DateRangeData;
12+
1113
final class SearchQueryData
1214
{
13-
private ?string $query = null;
15+
private array $statuses = [];
16+
private ?string $type = null;
17+
18+
private ?DateRangeData $createdRange = null;
19+
20+
21+
public function getStatuses(): array
22+
{
23+
return $this->statuses;
24+
}
25+
26+
public function setStatuses(array $statuses): void
27+
{
28+
$this->statuses = $statuses;
29+
}
30+
31+
public function getType(): ?string
32+
{
33+
return $this->type;
34+
}
35+
36+
public function setType(?string $type): void
37+
{
38+
$this->type = $type;
39+
}
1440

15-
public function getQuery(): ?string
41+
public function getCreatedRange(): ?DateRangeData
1642
{
17-
return $this->query;
43+
return $this->createdRange;
1844
}
1945

20-
public function setQuery(?string $query): void
46+
public function setCreatedRange(?DateRangeData $createdRange): void
2147
{
22-
$this->query = $query;
48+
$this->createdRange = $createdRange;
2349
}
2450
}

src/bundle/Form/Type/SearchType.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,38 @@
88

99
namespace Ibexa\Bundle\AdminUi\Form\Type;
1010

11+
use Ibexa\AdminUi\Form\Type\DateRangeType;
1112
use Ibexa\Bundle\AdminUi\Form\Data\SearchQueryData;
1213
use Symfony\Component\Form\AbstractType;
13-
use Symfony\Component\Form\Extension\Core\Type\TextType;
14+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
1415
use Symfony\Component\Form\FormBuilderInterface;
1516
use Symfony\Component\OptionsResolver\OptionsResolver;
1617

1718
final class SearchType extends AbstractType
1819
{
1920
public function buildForm(FormBuilderInterface $builder, array $options): void
2021
{
21-
$builder->add('query', TextType::class, [
22-
'required' => false,
23-
]);
22+
$builder
23+
->add('type', ChoiceType::class, [
24+
'required' => false,
25+
'choices' => array_combine($options['notification_types'], $options['notification_types']),
26+
'placeholder' => 'All types',
27+
'label' => 'Type',
28+
])
29+
->add('statuses', ChoiceType::class, [
30+
'choices' => [
31+
'Read' => 'read',
32+
'Unread' => 'unread',
33+
],
34+
'expanded' => true,
35+
'multiple' => true,
36+
'required' => false,
37+
'label' => 'Status',
38+
])
39+
->add('createdRange', DateRangeType::class, [
40+
'required' => false,
41+
'label' => 'Date and time',
42+
]);
2443
}
2544

2645
public function configureOptions(OptionsResolver $resolver): void
@@ -29,6 +48,7 @@ public function configureOptions(OptionsResolver $resolver): void
2948
'method' => 'GET',
3049
'csrf_protection' => false,
3150
'data_class' => SearchQueryData::class,
51+
'notification_types' => [],
3252
]);
3353
}
3454
}

src/lib/Pagination/Pagerfanta/NotificationAdapter.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ class NotificationAdapter implements AdapterInterface
1919
{
2020
private NotificationService $notificationService;
2121

22-
private ?string $query;
22+
private array $query;
2323

2424
private int $nbResults;
2525

2626
public function __construct(
2727
NotificationService $notificationService,
28-
?string $query = null
28+
array $query = []
2929
) {
3030
$this->notificationService = $notificationService;
3131
$this->query = $query;
@@ -53,9 +53,8 @@ public function getSlice($offset, $length): NotificationList
5353
{
5454
$notifications = $this->notificationService->loadNotifications($offset, $length, $this->query);
5555

56-
if (null === $this->nbResults) {
57-
$this->nbResults = $notifications->totalCount;
58-
}
56+
$this->nbResults ??= $notifications->totalCount;
57+
5958

6059
return $notifications;
6160
}

0 commit comments

Comments
 (0)