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\NumberType; |
16
|
|
|
|
17
|
|
|
class NumberFilter 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) || !is_numeric($data['value'])) { |
25
|
|
|
return; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$type = isset($data['type']) ? $data['type'] : false; |
29
|
|
|
|
30
|
|
|
$operator = $this->getOperator($type); |
31
|
|
|
|
32
|
|
|
if (!$operator) { |
|
|
|
|
33
|
|
|
$operator = '='; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// c.name > '1' => c.name OPERATOR :FIELDNAME |
37
|
|
|
$parameterName = $this->getNewParameterName($queryBuilder); |
38
|
|
|
$this->applyWhere($queryBuilder, sprintf('%s.%s %s :%s', $alias, $field, $operator, $parameterName)); |
39
|
|
|
$queryBuilder->setParameter($parameterName, $data['value']); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function getDefaultOptions() |
46
|
|
|
{ |
47
|
|
|
return array(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function getRenderSettings() |
54
|
|
|
{ |
55
|
|
|
return array('sonata_type_filter_number', array( |
56
|
|
|
'field_type' => $this->getFieldType(), |
57
|
|
|
'field_options' => $this->getFieldOptions(), |
58
|
|
|
'label' => $this->getLabel(), |
59
|
|
|
)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $type |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
private function getOperator($type) |
68
|
|
|
{ |
69
|
|
|
$choices = array( |
70
|
|
|
NumberType::TYPE_EQUAL => '=', |
71
|
|
|
NumberType::TYPE_GREATER_EQUAL => '>=', |
72
|
|
|
NumberType::TYPE_GREATER_THAN => '>', |
73
|
|
|
NumberType::TYPE_LESS_EQUAL => '<=', |
74
|
|
|
NumberType::TYPE_LESS_THAN => '<', |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
return isset($choices[$type]) ? $choices[$type] : false; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
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: