-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSavedSearchConfigurationRepository.php
More file actions
151 lines (127 loc) · 4.77 KB
/
Copy pathSavedSearchConfigurationRepository.php
File metadata and controls
151 lines (127 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
declare(strict_types=1);
/**
* This source file is available under the terms of the
* Pimcore Open Core License (POCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
* @license Pimcore Open Core License (POCL)
*/
namespace Pimcore\Bundle\StudioBackendBundle\Search\Repository;
use Doctrine\ORM\EntityManagerInterface;
use Pimcore\Bundle\StudioBackendBundle\Entity\Search\SavedSearchConfiguration;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\OwnershipManagement\Service\Filter\EntityCollectionFilterInterface;
/**
* @internal
*/
final readonly class SavedSearchConfigurationRepository implements SavedSearchConfigurationRepositoryInterface
{
public function __construct(
private EntityManagerInterface $entityManager,
private EntityCollectionFilterInterface $entityCollectionFilter,
) {
}
/**
* @throws NotFoundException
*/
public function getById(int $id): SavedSearchConfiguration
{
$configuration = $this->entityManager->find(SavedSearchConfiguration::class, $id);
if (!$configuration instanceof SavedSearchConfiguration) {
throw new NotFoundException('Saved Search Configuration', $id);
}
return $configuration;
}
public function getList(?string $searchTerm, ?string $sortBy = null, ?string $sortOrder = null): array
{
// Whitelist the sortable fields to keep the ORDER BY safe; default to newest first.
$sortField = match ($sortBy) {
'name' => 'c.name',
'modificationDate' => 'c.modificationDate',
default => 'c.modificationDate',
};
$direction = strtoupper((string) $sortOrder) === 'ASC' ? 'ASC' : 'DESC';
$queryBuilder = $this->entityManager->createQueryBuilder()
->select('c')
->from(SavedSearchConfiguration::class, 'c')
->orderBy($sortField, $direction);
$searchTerm = $searchTerm !== null ? trim($searchTerm) : null;
if ($searchTerm !== null && $searchTerm !== '') {
$queryBuilder
->where('LOWER(c.name) LIKE LOWER(:searchTerm)')
->setParameter('searchTerm', '%' . $searchTerm . '%');
}
return $queryBuilder->getQuery()->getResult();
}
public function getMenuShortcutList(): array
{
return $this->entityManager->createQueryBuilder()
->select('c')
->from(SavedSearchConfiguration::class, 'c')
->where('c.createMenuShortcut = :createMenuShortcut')
->setParameter('createMenuShortcut', true)
->orderBy('c.modificationDate', 'DESC')
->getQuery()
->getResult();
}
public function create(SavedSearchConfiguration $configuration): SavedSearchConfiguration
{
$configuration->setCreated();
$this->entityManager->persist($configuration);
$this->entityManager->flush();
return $configuration;
}
public function update(SavedSearchConfiguration $configuration): SavedSearchConfiguration
{
$configuration->setModified();
$this->entityManager->persist($configuration);
$this->entityManager->flush();
return $configuration;
}
public function clearShares(SavedSearchConfiguration $configuration): SavedSearchConfiguration
{
$configuration->clearShares();
$this->entityManager->persist($configuration);
$this->entityManager->flush();
return $configuration;
}
public function findAllPaginated(
int $offset,
int $limit,
?string $searchTerm = null,
array $ownerIds = [],
array $excludeOwnerIds = [],
array $sortBy = [],
): array {
return $this->entityCollectionFilter->findAllPaginated(
SavedSearchConfiguration::class,
$offset,
$limit,
$searchTerm,
$ownerIds,
$excludeOwnerIds,
$sortBy,
);
}
public function countAll(?string $searchTerm = null, array $ownerIds = [], array $excludeOwnerIds = []): int
{
return $this->entityCollectionFilter->countAll(
SavedSearchConfiguration::class,
$searchTerm,
$ownerIds,
$excludeOwnerIds,
);
}
public function getDistinctOwnerIds(): array
{
return $this->entityCollectionFilter->getDistinctOwnerIds(SavedSearchConfiguration::class);
}
public function delete(SavedSearchConfiguration $configuration): void
{
$this->entityManager->remove($configuration);
$this->entityManager->flush();
}
}