|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\DoctrineORMAdminBundle\Filter; |
|
15
|
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
|
17
|
|
|
use Sonata\AdminBundle\Form\Type\Filter\DefaultType; |
|
18
|
|
|
use Sonata\Form\Type\BooleanType; |
|
19
|
|
|
use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
|
20
|
|
|
|
|
21
|
|
|
class BooleanFilter extends Filter |
|
22
|
|
|
{ |
|
23
|
|
|
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void |
|
24
|
|
|
{ |
|
25
|
|
|
if (!$data || !\is_array($data) || !\array_key_exists('type', $data) || !\array_key_exists('value', $data)) { |
|
|
|
|
|
|
26
|
|
|
return; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if (\is_array($data['value'])) { |
|
30
|
|
|
$values = []; |
|
31
|
|
|
foreach ($data['value'] as $v) { |
|
32
|
|
|
if (!\in_array($v, [BooleanType::TYPE_NO, BooleanType::TYPE_YES], true)) { |
|
33
|
|
|
continue; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$values[] = (BooleanType::TYPE_YES === $v) ? 1 : 0; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if (0 === \count($values)) { |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$this->applyWhere($queryBuilder, $queryBuilder->expr()->in(sprintf('%s.%s', $alias, $field), $values)); |
|
44
|
|
|
} else { |
|
45
|
|
|
if (!\in_array($data['value'], [BooleanType::TYPE_NO, BooleanType::TYPE_YES], true)) { |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$parameterName = $this->getNewParameterName($queryBuilder); |
|
50
|
|
|
$this->applyWhere($queryBuilder, sprintf('%s.%s = :%s', $alias, $field, $parameterName)); |
|
51
|
|
|
$queryBuilder->setParameter($parameterName, (BooleanType::TYPE_YES === $data['value']) ? 1 : 0); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getDefaultOptions() |
|
56
|
|
|
{ |
|
57
|
|
|
return [ |
|
58
|
|
|
'field_type' => BooleanType::class, |
|
59
|
|
|
'operator_type' => HiddenType::class, |
|
60
|
|
|
'operator_options' => [], |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getRenderSettings() |
|
65
|
|
|
{ |
|
66
|
|
|
return [DefaultType::class, [ |
|
67
|
|
|
'field_type' => $this->getFieldType(), |
|
68
|
|
|
'field_options' => $this->getFieldOptions(), |
|
69
|
|
|
'operator_type' => $this->getOption('operator_type'), |
|
70
|
|
|
'operator_options' => $this->getOption('operator_options'), |
|
71
|
|
|
'label' => $this->getLabel(), |
|
72
|
|
|
]]; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.