Completed
Pull Request — 3.x (#761)
by Jordi Sala
01:34
created

ModelAutocompleteFilter::handleModel()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 3
nop 3
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 Doctrine\Common\Collections\Collection;
15
use Doctrine\ORM\QueryBuilder;
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\AdminBundle\Form\Type\Filter\DefaultType;
18
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
19
use Sonata\CoreBundle\Form\Type\EqualType;
20
use Sonata\CoreBundle\Form\Type\EqualType;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Cannot use Sonata\CoreBundle\Form\Type\EqualType as EqualType because the name is already in use
Loading history...
21
22
class ModelAutocompleteFilter extends Filter
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
28
    {
29
        if (!$data || !is_array($data) || !array_key_exists('value', $data)) {
30
            return;
31
        }
32
33
        if ($data['value'] instanceof Collection) {
34
            $data['value'] = $data['value']->toArray();
35
        }
36
37
        if (is_array($data['value'])) {
38
            $this->handleMultiple($queryBuilder, $alias, $data);
39
        } else {
40
            $this->handleModel($queryBuilder, $alias, $data);
41
        }
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getDefaultOptions()
48
    {
49
        return [
50
            'field_name' => false,
51
            'field_type' => ModelAutocompleteType::class,
52
            'field_options' => [],
53
            'operator_type' => EqualType::class,
54
            'operator_options' => [],
55
        ];
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getRenderSettings()
62
    {
63
        return [DefaultType::class, [
64
            'field_type' => $this->getFieldType(),
65
            'field_options' => $this->getFieldOptions(),
66
            'operator_type' => $this->getOption('operator_type'),
67
            'operator_options' => $this->getOption('operator_options'),
68
            'label' => $this->getLabel(),
69
        ]];
70
    }
71
72
    /**
73
     * For the record, the $alias value is provided by the association method (and the entity join method)
74
     *  so the field value is not used here.
75
     *
76
     * @param ProxyQueryInterface|QueryBuilder $queryBuilder
77
     * @param string                           $alias
78
     * @param mixed                            $data
79
     *
80
     * @return mixed
81
     */
82
    protected function handleMultiple(ProxyQueryInterface $queryBuilder, $alias, $data)
83
    {
84
        if (0 == count($data['value'])) {
85
            return;
86
        }
87
88
        $parameterName = $this->getNewParameterName($queryBuilder);
89
90
        if (isset($data['type']) && EqualType::TYPE_IS_NOT_EQUAL == $data['type']) {
91
            $this->applyWhere($queryBuilder, $queryBuilder->expr()->notIn($alias, ':'.$parameterName));
92
        } else {
93
            $this->applyWhere($queryBuilder, $queryBuilder->expr()->in($alias, ':'.$parameterName));
94
        }
95
96
        $queryBuilder->setParameter($parameterName, $data['value']);
97
    }
98
99
    /**
100
     * @param ProxyQueryInterface|QueryBuilder $queryBuilder
101
     * @param string                           $alias
102
     * @param mixed                            $data
103
     *
104
     * @return mixed
105
     */
106
    protected function handleModel(ProxyQueryInterface $queryBuilder, $alias, $data)
107
    {
108
        if (empty($data['value'])) {
109
            return;
110
        }
111
112
        $parameterName = $this->getNewParameterName($queryBuilder);
113
114
        if (isset($data['type']) && EqualType::TYPE_IS_NOT_EQUAL == $data['type']) {
115
            $this->applyWhere($queryBuilder, sprintf('%s != :%s', $alias, $parameterName));
116
        } else {
117
            $this->applyWhere($queryBuilder, sprintf('%s = :%s', $alias, $parameterName));
118
        }
119
120
        $queryBuilder->setParameter($parameterName, $data['value']);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    protected function association(ProxyQueryInterface $queryBuilder, $data)
127
    {
128
        $associationMappings = $this->getParentAssociationMappings();
129
        $associationMappings[] = $this->getAssociationMapping();
130
        $alias = $queryBuilder->entityJoin($associationMappings);
131
132
        return [$alias, false];
133
    }
134
}
135