Completed
Pull Request — 3.x (#650)
by
unknown
01:53
created

ModelFilter::handleMultiple()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 24
rs 8.6845
cc 4
eloc 13
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\Mapping\ClassMetadataInfo;
16
use Doctrine\ORM\QueryBuilder;
17
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
18
use Sonata\CoreBundle\Form\Type\EqualType;
19
20
class ModelFilter extends Filter
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
26
    {
27
        if (!$data || !is_array($data) || !array_key_exists('value', $data)) {
28
            return;
29
        }
30
31
        if ($data['value'] instanceof Collection) {
32
            $data['value'] = $data['value']->toArray();
33
        }
34
35
        if (is_array($data['value'])) {
36
            $this->handleMultiple($queryBuilder, $alias, $data);
37
        } else {
38
            $this->handleModel($queryBuilder, $alias, $data);
39
        }
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getDefaultOptions()
46
    {
47
        return array(
48
            'mapping_type' => false,
49
            'field_name' => false,
50
            'field_type' => method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
51
                ? 'Symfony\Bridge\Doctrine\Form\Type\EntityType'
52
                : 'entity', // NEXT_MAJOR: Remove ternary (when requirement of Symfony is >= 2.8)
53
            'field_options' => array(),
54
            'operator_type' => method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
55
                ? 'Sonata\CoreBundle\Form\Type\EqualType'
56
                : 'sonata_type_equal', // NEXT_MAJOR: Remove ternary (when requirement of Symfony is >= 2.8)
57
            'operator_options' => array(),
58
        );
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getRenderSettings()
65
    {
66
        // NEXT_MAJOR: Remove this line when drop Symfony <2.8 support
67
        $type = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
68
            ? 'Sonata\AdminBundle\Form\Type\Filter\DefaultType'
69
            : 'sonata_type_filter_default';
70
71
        return array($type, array(
72
            'field_type' => $this->getFieldType(),
73
            'field_options' => $this->getFieldOptions(),
74
            'operator_type' => $this->getOption('operator_type'),
75
            'operator_options' => $this->getOption('operator_options'),
76
            'label' => $this->getLabel(),
77
        ));
78
    }
79
80
    /**
81
     * For the record, the $alias value is provided by the association method (and the entity join method)
82
     *  so the field value is not used here.
83
     *
84
     * @param ProxyQueryInterface|QueryBuilder $queryBuilder
85
     * @param string                           $alias
86
     * @param mixed                            $data
87
     *
88
     * @return mixed
89
     */
90
    protected function handleMultiple(ProxyQueryInterface $queryBuilder, $alias, $data)
91
    {
92
        if (count($data['value']) == 0) {
93
            return;
94
        }
95
96
        $parameterName = $this->getNewParameterName($queryBuilder);
97
98
        if (isset($data['type']) && $data['type'] == EqualType::TYPE_IS_NOT_EQUAL) {
99
            $or = $queryBuilder->expr()->orX();
100
101
            $or->add($queryBuilder->expr()->notIn($alias, ':'.$parameterName));
102
103
            $or->add($queryBuilder->expr()->isNull(
104
                sprintf('IDENTITY(%s.%s)', $queryBuilder->getRootAlias(), $this->getFieldName())
105
            ));
106
107
            $this->applyWhere($queryBuilder, $or);
108
        } else {
109
            $this->applyWhere($queryBuilder, $queryBuilder->expr()->in($alias, ':'.$parameterName));
110
        }
111
112
        $queryBuilder->setParameter($parameterName, $data['value']);
113
    }
114
115
    /**
116
     * @param ProxyQueryInterface|QueryBuilder $queryBuilder
117
     * @param string                           $alias
118
     * @param mixed                            $data
119
     *
120
     * @return mixed
121
     */
122
    protected function handleModel(ProxyQueryInterface $queryBuilder, $alias, $data)
123
    {
124
        if (empty($data['value'])) {
125
            return;
126
        }
127
128
        $parameterName = $this->getNewParameterName($queryBuilder);
129
130
        if (isset($data['type']) && $data['type'] == EqualType::TYPE_IS_NOT_EQUAL) {
131
            $or = $queryBuilder->expr()->orX();
132
133
            $or->add($queryBuilder->expr()->neq($alias, ':'.$parameterName));
134
135
            $or->add($queryBuilder->expr()->isNull(
136
                sprintf('IDENTITY(%s.%s)', $queryBuilder->getRootAlias(), $this->getFieldName())
137
            ));
138
139
            $this->applyWhere($queryBuilder, $or);
140
        } else {
141
            $this->applyWhere($queryBuilder, sprintf('%s = :%s', $alias, $parameterName));
142
        }
143
144
        $queryBuilder->setParameter($parameterName, $data['value']);
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    protected function association(ProxyQueryInterface $queryBuilder, $data)
151
    {
152
        $types = array(
153
            ClassMetadataInfo::ONE_TO_ONE,
154
            ClassMetadataInfo::ONE_TO_MANY,
155
            ClassMetadataInfo::MANY_TO_MANY,
156
            ClassMetadataInfo::MANY_TO_ONE,
157
        );
158
159
        if (!in_array($this->getOption('mapping_type'), $types)) {
160
            throw new \RuntimeException('Invalid mapping type');
161
        }
162
163
        $associationMappings = $this->getParentAssociationMappings();
164
        $associationMappings[] = $this->getAssociationMapping();
165
        $alias = $queryBuilder->entityJoin($associationMappings);
166
167
        return array($alias, false);
168
    }
169
}
170