|
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 Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
|
19
|
|
|
|
|
20
|
|
|
final class EmptyFieldFilter extends Filter |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Field is null. |
|
24
|
|
|
*/ |
|
25
|
|
|
public const TYPE_EMPTY = 1; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Field is not null. |
|
29
|
|
|
*/ |
|
30
|
|
|
public const TYPE_NOT_EMPTY = 2; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $alias |
|
34
|
|
|
* @param string $field |
|
35
|
|
|
* @param array|string|null $data |
|
36
|
|
|
*/ |
|
37
|
|
|
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void |
|
38
|
|
|
{ |
|
39
|
|
|
if (null === $data || !\is_array($data) || !\array_key_exists('value', $data)) { |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if ((int) $data['value'] === static::TYPE_NOT_EMPTY) { |
|
44
|
|
|
$queryBuilder->andWhere(sprintf('%s.%s IS null', $alias, $field)); |
|
45
|
|
|
} elseif ((int) $data['value'] === static::TYPE_EMPTY) { |
|
46
|
|
|
$queryBuilder->andWhere(sprintf('%s.%s IS NOT null', $alias, $field)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->active = null !== $data['value']; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getDefaultOptions() |
|
53
|
|
|
{ |
|
54
|
|
|
return []; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function getFieldType() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->getOption('field_type', ChoiceType::class); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getFieldOptions() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->getOption('choices', [ |
|
65
|
|
|
'choices' => [ |
|
66
|
|
|
'label_type_yes' => static::TYPE_EMPTY, |
|
67
|
|
|
'label_type_no' => static::TYPE_NOT_EMPTY, |
|
68
|
|
|
], |
|
69
|
|
|
'choice_translation_domain' => 'Core23AdminBundle', |
|
70
|
|
|
]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getRenderSettings() |
|
74
|
|
|
{ |
|
75
|
|
|
return [DefaultType::class, [ |
|
76
|
|
|
'field_type' => $this->getFieldType(), |
|
77
|
|
|
'field_options' => $this->getFieldOptions(), |
|
78
|
|
|
'label' => $this->getLabel(), |
|
79
|
|
|
]]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getParentAssociationMappings() |
|
83
|
|
|
{ |
|
84
|
|
|
$mappings = $this->getOption('parent_association_mappings', []); |
|
85
|
|
|
|
|
86
|
|
|
$fields = explode('.', $this->getFieldName()); |
|
87
|
|
|
for ($i = 0; $i < \count($fields) - 1; ++$i) { |
|
88
|
|
|
$mappings[] = ['fieldName' => $fields[$i]]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $mappings; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param mixed|null $data |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function association(ProxyQueryInterface $queryBuilder, $data): array |
|
98
|
|
|
{ |
|
99
|
|
|
$alias = $queryBuilder->entityJoin($this->getParentAssociationMappings()); |
|
100
|
|
|
$part = strrchr('.'.$this->getFieldName(), '.'); |
|
101
|
|
|
$fieldName = substr(false === $part ? $this->getFieldType() : $part, 1); |
|
102
|
|
|
|
|
103
|
|
|
return [$alias, $fieldName]; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|