|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\TranslationBundle\Filter; |
|
15
|
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
|
17
|
|
|
use Sonata\AdminBundle\Form\Type\Filter\DefaultType; |
|
18
|
|
|
use Sonata\DoctrineORMAdminBundle\Filter\Filter; |
|
19
|
|
|
use Sonata\TranslationBundle\Enum\TranslationFilterMode; |
|
20
|
|
|
use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
|
21
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
|
22
|
|
|
|
|
23
|
|
|
final class TranslationFieldFilter extends Filter |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $filterMode; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(string $filterMode = TranslationFilterMode::GEDMO) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->filterMode = $filterMode; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void |
|
39
|
|
|
{ |
|
40
|
|
|
if (!$data || !\is_array($data) || !\array_key_exists('value', $data) || null === $data['value']) { |
|
|
|
|
|
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$data['value'] = trim((string) $data['value']); |
|
45
|
|
|
|
|
46
|
|
|
if (0 === \strlen($data['value'])) { |
|
47
|
|
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
$joinAlias = 'tff'; |
|
50
|
|
|
$filterMode = $this->getOption('filter_mode'); |
|
51
|
|
|
|
|
52
|
|
|
// verify if the join is not already done |
|
53
|
|
|
$aliasAlreadyExists = false; |
|
54
|
|
|
foreach ($queryBuilder->getDQLParts()['join'] as $joins) { |
|
55
|
|
|
foreach ($joins as $join) { |
|
56
|
|
|
if ($join->getAlias() === $joinAlias) { |
|
57
|
|
|
$aliasAlreadyExists = true; |
|
58
|
|
|
|
|
59
|
|
|
break 2; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (!$aliasAlreadyExists) { |
|
65
|
|
|
$queryBuilder->leftJoin($alias.'.translations', $joinAlias); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (TranslationFilterMode::GEDMO === $filterMode) { |
|
69
|
|
|
// search on translation OR on normal field when using Gedmo |
|
70
|
|
|
$this->applyGedmoFilters($queryBuilder, $joinAlias, $alias, $field, $data); |
|
71
|
|
|
|
|
72
|
|
|
$this->active = true; |
|
73
|
|
|
} elseif (TranslationFilterMode::KNPLABS === $filterMode) { |
|
74
|
|
|
// search on translation OR on normal field when using Knp |
|
75
|
|
|
$this->applyKnplabsFilters($queryBuilder, $joinAlias, $field, $data); |
|
76
|
|
|
|
|
77
|
|
|
$this->active = true; |
|
78
|
|
|
} else { |
|
79
|
|
|
throw new \LogicException(sprintf('Invalid filter mode given: "%s"', $filterMode)); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getDefaultOptions() |
|
87
|
|
|
{ |
|
88
|
|
|
return [ |
|
89
|
|
|
'field_type' => TextType::class, |
|
90
|
|
|
'operator_type' => HiddenType::class, |
|
91
|
|
|
'filter_mode' => $this->filterMode, |
|
92
|
|
|
'operator_options' => [], |
|
93
|
|
|
]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getRenderSettings() |
|
100
|
|
|
{ |
|
101
|
|
|
return [DefaultType::class, [ |
|
102
|
|
|
'field_type' => $this->getFieldType(), |
|
103
|
|
|
'field_options' => $this->getFieldOptions(), |
|
104
|
|
|
'operator_type' => $this->getOption('operator_type'), |
|
105
|
|
|
'operator_options' => $this->getOption('operator_options'), |
|
106
|
|
|
'label' => $this->getLabel(), |
|
107
|
|
|
]]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function association(ProxyQueryInterface $queryBuilder, $data) |
|
114
|
|
|
{ |
|
115
|
|
|
$alias = $queryBuilder->entityJoin($this->getParentAssociationMappings()); |
|
116
|
|
|
|
|
117
|
|
|
return [$this->getOption('alias', $alias), $this->getFieldName()]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param mixed[] $data |
|
122
|
|
|
*/ |
|
123
|
|
|
private function applyGedmoFilters(ProxyQueryInterface $queryBuilder, string $joinAlias, string $alias, string $field, $data): void |
|
124
|
|
|
{ |
|
125
|
|
|
$this->applyWhere($queryBuilder, $queryBuilder->expr()->orX( |
|
126
|
|
|
$queryBuilder->expr()->andX( |
|
127
|
|
|
$queryBuilder->expr()->eq($joinAlias.'.field', $queryBuilder->expr()->literal($field)), |
|
128
|
|
|
$queryBuilder->expr()->like( |
|
129
|
|
|
$joinAlias.'.content', |
|
130
|
|
|
$queryBuilder->expr()->literal('%'.$data['value'].'%') |
|
131
|
|
|
) |
|
132
|
|
|
), |
|
133
|
|
|
$queryBuilder->expr()->like( |
|
134
|
|
|
sprintf('%s.%s', $alias, $field), |
|
135
|
|
|
$queryBuilder->expr()->literal('%'.$data['value'].'%') |
|
136
|
|
|
) |
|
137
|
|
|
)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param mixed[] $data |
|
142
|
|
|
*/ |
|
143
|
|
|
private function applyKnplabsFilters(ProxyQueryInterface $queryBuilder, string $joinAlias, string $field, $data): void |
|
144
|
|
|
{ |
|
145
|
|
|
$this->applyWhere( |
|
146
|
|
|
$queryBuilder, |
|
147
|
|
|
$queryBuilder->expr()->andX( |
|
148
|
|
|
$queryBuilder->expr()->like( |
|
149
|
|
|
$joinAlias.".$field", |
|
150
|
|
|
$queryBuilder->expr()->literal('%'.$data['value'].'%') |
|
151
|
|
|
) |
|
152
|
|
|
) |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.