-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSearchHelperService.php
More file actions
97 lines (82 loc) · 3.71 KB
/
SearchHelperService.php
File metadata and controls
97 lines (82 loc) · 3.71 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
<?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\ClassificationStore\Service;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\UserNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Translation\Repository\TranslationRepositoryInterface;
use Pimcore\Model\DataObject\Classificationstore\CollectionConfig\Listing as CollectionConfigListing;
use Pimcore\Model\DataObject\Classificationstore\GroupConfig\Dao as GroupConfigDao;
use Pimcore\Model\DataObject\Classificationstore\GroupConfig\Listing as GroupConfigListing;
use Pimcore\Model\DataObject\Classificationstore\KeyConfig\Dao as KeyConfigDao;
use Pimcore\Model\DataObject\Classificationstore\KeyConfig\Listing as KeyConfigListing;
use Pimcore\Model\DataObject\Classificationstore\KeyGroupRelation\Listing as KeyGroupRelationListing;
/**
* @internal
*/
final readonly class SearchHelperService implements SearchHelperServiceInterface
{
public function __construct(
private TranslationRepositoryInterface $translationRepository,
private SecurityServiceInterface $securityService
) {
}
public function applySearchTermFilter(GroupConfigListing|CollectionConfigListing $list, string $searchTerm): void
{
$searchTerms = $this->getTranslatedSearchFilterTerms($searchTerm);
$conditions = [];
$preparedSearchTerms = [];
foreach ($searchTerms as $term) {
$conditions[] = 'name LIKE ? OR description LIKE ?';
$preparedSearchTerms[] = '%' . $term . '%';
$preparedSearchTerms[] = '%' . $term . '%';
}
$list->addConditionParam('(' . implode(' OR ', $conditions) . ')', $preparedSearchTerms);
}
public function applyKeySearchFilter(KeyConfigListing $listing, string $searchTerm): void
{
$listing->addConditionParam(
'(name LIKE ? OR description LIKE ?)',
['%' . $searchTerm . '%', '%' . $searchTerm . '%']
);
}
public function applyKeyGroupRelationSearchFilter(
KeyGroupRelationListing $listing,
string $searchTerm,
): void {
$searchTerms = $this->getTranslatedSearchFilterTerms($searchTerm);
$searchFilterConditions = [];
foreach ($searchTerms as $term) {
$searchFilterConditions[] =
KeyConfigDao::TABLE_NAME_KEYS . '.name LIKE ' . $listing->quote('%' . $term . '%')
. ' OR '
. GroupConfigDao::TABLE_NAME_GROUPS . '.name LIKE ' . $listing->quote('%' . $term . '%')
. ' OR '
. KeyConfigDao::TABLE_NAME_KEYS . '.description LIKE ' . $listing->quote('%' . $term . '%');
}
$listing->setResolveGroupName(true);
$listing->addConditionParam('(' . implode(' OR ', $searchFilterConditions) . ')');
}
public function getTranslatedSearchFilterTerms(string $searchTerm): array
{
try {
$user = $this->securityService->getCurrentUser();
$translatedSearchKeys = $this->translationRepository->getTranslationKeysWithTextFilter(
$searchTerm,
$user->getLanguage()
);
$searchTerms = array_merge([$searchTerm], $translatedSearchKeys);
} catch (UserNotFoundException) {
$searchTerms = [$searchTerm];
}
return $searchTerms;
}
}