|
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\Datagrid\ProxyQueryInterface; |
|
15
|
|
|
use Sonata\CoreBundle\Form\Type\BooleanType; |
|
16
|
|
|
|
|
17
|
|
|
class BooleanFilter 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('type', $data) || !array_key_exists('value', $data)) { |
|
25
|
|
|
return; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
if (is_array($data['value'])) { |
|
29
|
|
|
$values = array(); |
|
30
|
|
|
foreach ($data['value'] as $v) { |
|
31
|
|
|
if (!in_array($v, array(BooleanType::TYPE_NO, BooleanType::TYPE_YES))) { |
|
32
|
|
|
continue; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$values[] = ($v == BooleanType::TYPE_YES) ? 1 : 0; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if (count($values) == 0) { |
|
39
|
|
|
return; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$this->applyWhere($queryBuilder, $queryBuilder->expr()->in(sprintf('%s.%s', $alias, $field), $values)); |
|
43
|
|
|
} else { |
|
44
|
|
|
|
|
45
|
|
|
if (!in_array($data['value'], array(BooleanType::TYPE_NO, BooleanType::TYPE_YES))) { |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$parameterName = $this->getNewParameterName($queryBuilder); |
|
50
|
|
|
$this->applyWhere($queryBuilder, sprintf('%s.%s = :%s', $alias, $field, $parameterName)); |
|
51
|
|
|
$queryBuilder->setParameter($parameterName, ($data['value'] == BooleanType::TYPE_YES) ? 1 : 0); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getDefaultOptions() |
|
59
|
|
|
{ |
|
60
|
|
|
return array( |
|
61
|
|
|
'field_type' => 'sonata_type_boolean' |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getRenderSettings() |
|
69
|
|
|
{ |
|
70
|
|
|
return array('sonata_type_filter_default', array( |
|
71
|
|
|
'field_type' => $this->getFieldType(), |
|
72
|
|
|
'field_options' => $this->getFieldOptions(), |
|
73
|
|
|
'operator_type' => 'hidden', |
|
74
|
|
|
'operator_options' => array(), |
|
75
|
|
|
'label' => $this->getLabel() |
|
76
|
|
|
)); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|