|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sonata\TranslationBundle\Filter; |
|
13
|
|
|
|
|
14
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
|
15
|
|
|
use Sonata\DoctrineORMAdminBundle\Filter\Filter; |
|
16
|
|
|
|
|
17
|
|
|
final class TranslationFieldFilter extends Filter |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritdoc} |
|
21
|
|
|
*/ |
|
22
|
|
|
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data) |
|
23
|
|
|
{ |
|
24
|
|
|
if (!$data || !is_array($data) || !array_key_exists('value', $data)) { |
|
25
|
|
|
return; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$data['value'] = trim($data['value']); |
|
29
|
|
|
|
|
30
|
|
|
if (strlen($data['value']) == 0) { |
|
31
|
|
|
return; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$joinAlias = 'tff'; |
|
35
|
|
|
|
|
36
|
|
|
// verify if the join is not already done |
|
37
|
|
|
$aliasAlreadyExists = false; |
|
38
|
|
|
foreach ($queryBuilder->getDQLParts()['join'] as $joins) { |
|
39
|
|
|
foreach ($joins as $join) { |
|
40
|
|
|
if ($join->getAlias() === $joinAlias) { |
|
41
|
|
|
$aliasAlreadyExists = true; |
|
42
|
|
|
break 2; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if (!$aliasAlreadyExists) { |
|
48
|
|
|
$queryBuilder->leftJoin($alias.'.translations', $joinAlias); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// search on translation OR on normal field |
|
52
|
|
|
$queryBuilder->andWhere($queryBuilder->expr()->orX( |
|
53
|
|
|
$queryBuilder->expr()->andX( |
|
54
|
|
|
$queryBuilder->expr()->eq($joinAlias.'.field', $queryBuilder->expr()->literal($field)), |
|
55
|
|
|
$queryBuilder->expr()->like( |
|
56
|
|
|
$joinAlias.'.content', |
|
57
|
|
|
$queryBuilder->expr()->literal('%'.$data['value'].'%') |
|
58
|
|
|
) |
|
59
|
|
|
), |
|
60
|
|
|
$queryBuilder->expr()->like( |
|
61
|
|
|
sprintf('%s.%s', $alias, $field), |
|
62
|
|
|
$queryBuilder->expr()->literal('%'.$data['value'].'%') |
|
63
|
|
|
) |
|
64
|
|
|
)); |
|
65
|
|
|
|
|
66
|
|
|
$this->active = true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getDefaultOptions() |
|
73
|
|
|
{ |
|
74
|
|
|
return array( |
|
75
|
|
|
'field_type' => method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') |
|
76
|
|
|
? 'Symfony\Component\Form\Extension\Core\Type\TextType' |
|
77
|
|
|
: 'text', // NEXT_MAJOR: Remove ternary (when requirement of Symfony is >= 2.8) |
|
78
|
|
|
'operator_type' => method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') |
|
79
|
|
|
? 'Symfony\Component\Form\Extension\Core\Type\HiddenType' |
|
80
|
|
|
: 'hidden', // NEXT_MAJOR: Remove ternary (when requirement of Symfony is >= 2.8) |
|
81
|
|
|
'operator_options' => array(), |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getRenderSettings() |
|
89
|
|
|
{ |
|
90
|
|
|
// NEXT_MAJOR: Remove this line when drop Symfony <2.8 support |
|
91
|
|
|
$type = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') |
|
92
|
|
|
? 'Sonata\AdminBundle\Form\Type\Filter\DefaultType' |
|
93
|
|
|
: 'sonata_type_filter_default'; |
|
94
|
|
|
|
|
95
|
|
|
return array($type, array( |
|
96
|
|
|
'field_type' => $this->getFieldType(), |
|
97
|
|
|
'field_options' => $this->getFieldOptions(), |
|
98
|
|
|
'operator_type' => $this->getOption('operator_type'), |
|
99
|
|
|
'operator_options' => $this->getOption('operator_options'), |
|
100
|
|
|
'label' => $this->getLabel(), |
|
101
|
|
|
)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function association(ProxyQueryInterface $queryBuilder, $data) |
|
108
|
|
|
{ |
|
109
|
|
|
$alias = $queryBuilder->entityJoin($this->getParentAssociationMappings()); |
|
110
|
|
|
|
|
111
|
|
|
return array($this->getOption('alias', $alias), $this->getFieldName()); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|