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\DoctrineORMAdminBundle\Filter; |
13
|
|
|
|
14
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
15
|
|
|
use Sonata\AdminBundle\Form\Type\Filter\ChoiceType; |
16
|
|
|
|
17
|
|
|
class StringFilter 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
|
|
|
$data['type'] = !isset($data['type']) ? ChoiceType::TYPE_CONTAINS : $data['type']; |
35
|
|
|
|
36
|
|
|
$operator = $this->getOperator((int) $data['type']); |
37
|
|
|
|
38
|
|
|
if (!$operator) { |
|
|
|
|
39
|
|
|
$operator = 'LIKE'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// c.name > '1' => c.name OPERATOR :FIELDNAME |
43
|
|
|
$parameterName = $this->getNewParameterName($queryBuilder); |
44
|
|
|
|
45
|
|
|
$or = $queryBuilder->expr()->orX(); |
46
|
|
|
|
47
|
|
|
$or->add(sprintf('%s.%s %s :%s', $alias, $field, $operator, $parameterName)); |
48
|
|
|
|
49
|
|
|
if ($data['type'] == ChoiceType::TYPE_NOT_CONTAINS) { |
50
|
|
|
$or->add($queryBuilder->expr()->isNull(sprintf('%s.%s', $alias, $field))); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->applyWhere($queryBuilder, $or); |
54
|
|
|
|
55
|
|
|
if ($data['type'] == ChoiceType::TYPE_EQUAL) { |
56
|
|
|
$queryBuilder->setParameter($parameterName, $data['value']); |
57
|
|
|
} else { |
58
|
|
|
$queryBuilder->setParameter($parameterName, sprintf($this->getOption('format'), $data['value'])); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function getDefaultOptions() |
66
|
|
|
{ |
67
|
|
|
return array( |
68
|
|
|
'format' => '%%%s%%', |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function getRenderSettings() |
76
|
|
|
{ |
77
|
|
|
// NEXT_MAJOR: Remove this line when drop Symfony <2.8 support |
78
|
|
|
$type = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') |
79
|
|
|
? 'Sonata\AdminBundle\Form\Type\Filter\ChoiceType' |
80
|
|
|
: 'sonata_type_filter_choice'; |
81
|
|
|
|
82
|
|
|
return array($type, array( |
83
|
|
|
'field_type' => $this->getFieldType(), |
84
|
|
|
'field_options' => $this->getFieldOptions(), |
85
|
|
|
'label' => $this->getLabel(), |
86
|
|
|
)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $type |
91
|
|
|
* |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
private function getOperator($type) |
95
|
|
|
{ |
96
|
|
|
$choices = array( |
97
|
|
|
ChoiceType::TYPE_CONTAINS => 'LIKE', |
98
|
|
|
ChoiceType::TYPE_NOT_CONTAINS => 'NOT LIKE', |
99
|
|
|
ChoiceType::TYPE_EQUAL => '=', |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
return isset($choices[$type]) ? $choices[$type] : false; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: