|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata 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\Form\Type\Filter\NumberType; |
|
15
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
|
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
|
|
|
* @param string $type |
|
44
|
|
|
* |
|
45
|
|
|
* @return bool |
|
46
|
|
|
*/ |
|
47
|
|
|
private function getOperator($type) |
|
48
|
|
|
{ |
|
49
|
|
|
$choices = array( |
|
50
|
|
|
NumberType::TYPE_EQUAL => '=', |
|
51
|
|
|
NumberType::TYPE_GREATER_EQUAL => '>=', |
|
52
|
|
|
NumberType::TYPE_GREATER_THAN => '>', |
|
53
|
|
|
NumberType::TYPE_LESS_EQUAL => '<=', |
|
54
|
|
|
NumberType::TYPE_LESS_THAN => '<', |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
return isset($choices[$type]) ? $choices[$type] : false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getDefaultOptions() |
|
64
|
|
|
{ |
|
65
|
|
|
return array(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getRenderSettings() |
|
72
|
|
|
{ |
|
73
|
|
|
return array('sonata_type_filter_number', array( |
|
74
|
|
|
'field_type' => $this->getFieldType(), |
|
75
|
|
|
'field_options' => $this->getFieldOptions(), |
|
76
|
|
|
'label' => $this->getLabel() |
|
77
|
|
|
)); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: