|
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 Doctrine\ORM\QueryBuilder; |
|
17
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
|
18
|
|
|
use Sonata\AdminBundle\Filter\Filter as BaseFilter; |
|
19
|
|
|
|
|
20
|
|
|
abstract class Filter extends BaseFilter |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var bool |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $active = false; |
|
26
|
|
|
|
|
27
|
|
|
public function apply($queryBuilder, $value): void |
|
28
|
|
|
{ |
|
29
|
|
|
$this->value = $value; |
|
30
|
|
|
if (\is_array($value) && \array_key_exists('value', $value)) { |
|
31
|
|
|
list($alias, $field) = $this->association($queryBuilder, $value); |
|
32
|
|
|
|
|
33
|
|
|
$this->filter($queryBuilder, $alias, $field, $value); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function isActive() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->active; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function association(ProxyQueryInterface $queryBuilder, $value) |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
$alias = $queryBuilder->entityJoin($this->getParentAssociationMappings()); |
|
45
|
|
|
|
|
46
|
|
|
return [$alias, $this->getFieldName()]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param ProxyQueryInterface|QueryBuilder $queryBuilder |
|
51
|
|
|
* @param mixed $parameter |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function applyWhere(ProxyQueryInterface $queryBuilder, $parameter): void |
|
54
|
|
|
{ |
|
55
|
|
|
if (self::CONDITION_OR === $this->getCondition()) { |
|
56
|
|
|
$queryBuilder->orWhere($parameter); |
|
57
|
|
|
} else { |
|
58
|
|
|
$queryBuilder->andWhere($parameter); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// filter is active since it's added to the queryBuilder |
|
62
|
|
|
$this->active = true; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function getNewParameterName(ProxyQueryInterface $queryBuilder) |
|
69
|
|
|
{ |
|
70
|
|
|
// dots are not accepted in a DQL identifier so replace them |
|
71
|
|
|
// by underscores. |
|
72
|
|
|
return str_replace('.', '_', $this->getName()).'_'.$queryBuilder->getUniqueParameterId(); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.