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\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 = []; |
30
|
|
|
foreach ($data['value'] as $v) { |
31
|
|
|
if (!in_array($v, [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
|
|
|
if (!in_array($data['value'], [BooleanType::TYPE_NO, BooleanType::TYPE_YES])) { |
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$parameterName = $this->getNewParameterName($queryBuilder); |
49
|
|
|
$this->applyWhere($queryBuilder, sprintf('%s.%s = :%s', $alias, $field, $parameterName)); |
50
|
|
|
$queryBuilder->setParameter($parameterName, ($data['value'] == BooleanType::TYPE_YES) ? 1 : 0); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function getDefaultOptions() |
58
|
|
|
{ |
59
|
|
|
return ['field_type' => 'Sonata\CoreBundle\Form\Type\BooleanType']; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function getRenderSettings() |
66
|
|
|
{ |
67
|
|
|
<<<<<<< HEAD |
|
|
|
|
68
|
|
|
return ['Sonata\AdminBundle\Form\Type\Filter\DefaultType', [ |
69
|
|
|
'field_type' => $this->getFieldType(), |
70
|
|
|
'field_options' => $this->getFieldOptions(), |
71
|
|
|
'operator_type' => 'Symfony\Component\Form\Extension\Core\Type\HiddenType', |
72
|
|
|
======= |
73
|
|
|
// NEXT_MAJOR: Remove this line when drop Symfony <2.8 support |
74
|
|
|
$type = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') |
75
|
|
|
? 'Sonata\AdminBundle\Form\Type\Filter\DefaultType' |
76
|
|
|
: 'sonata_type_filter_default'; |
77
|
|
|
|
78
|
|
|
return [$type, [ |
79
|
|
|
'field_type' => $this->getFieldType(), |
80
|
|
|
'field_options' => $this->getFieldOptions(), |
81
|
|
|
'operator_type' => method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') |
82
|
|
|
? 'Symfony\Component\Form\Extension\Core\Type\HiddenType' |
83
|
|
|
: 'hidden', // NEXT_MAJOR: Remove ternary (when requirement of Symfony is >= 2.8) |
84
|
|
|
>>>>>>> origin/3.x |
85
|
|
|
'operator_options' => [], |
86
|
|
|
'label' => $this->getLabel(), |
87
|
|
|
]]; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.