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

ModelFilter::handleMultiple()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.2
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\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' => 'Symfony\Bridge\Doctrine\Form\Type\EntityType',
51
            'field_options' => array(),
52
            'operator_type' => 'Sonata\CoreBundle\Form\Type\EqualType',
53
            'operator_options' => array(),
54
        );
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getRenderSettings()
61
    {
62
        return array('Sonata\AdminBundle\Form\Type\Filter\DefaultType', array(
63
            'field_type' => $this->getFieldType(),
64
            'field_options' => $this->getFieldOptions(),
65
            'operator_type' => $this->getOption('operator_type'),
66
            'operator_options' => $this->getOption('operator_options'),
67
            'label' => $this->getLabel(),
68
        ));
69
    }
70
71
    /**
72
     * For the record, the $alias value is provided by the association method (and the entity join method)
73
     *  so the field value is not used here.
74
     *
75
     * @param ProxyQueryInterface|QueryBuilder $queryBuilder
76
     * @param string                           $alias
77
     * @param mixed                            $data
78
     *
79
     * @return mixed
80
     */
81
    protected function handleMultiple(ProxyQueryInterface $queryBuilder, $alias, $data)
82
    {
83
        if (count($data['value']) == 0) {
84
            return;
85
        }
86
87
        $parameterName = $this->getNewParameterName($queryBuilder);
88
89
        if (isset($data['type']) && $data['type'] == EqualType::TYPE_IS_NOT_EQUAL) {
90
            $this->applyWhere($queryBuilder, $queryBuilder->expr()->notIn($alias, ':'.$parameterName));
91
        } else {
92
            $this->applyWhere($queryBuilder, $queryBuilder->expr()->in($alias, ':'.$parameterName));
93
        }
94
95
        $queryBuilder->setParameter($parameterName, $data['value']);
96
    }
97
98
    /**
99
     * @param ProxyQueryInterface|QueryBuilder $queryBuilder
100
     * @param string                           $alias
101
     * @param mixed                            $data
102
     *
103
     * @return mixed
104
     */
105
    protected function handleModel(ProxyQueryInterface $queryBuilder, $alias, $data)
106
    {
107
        if (empty($data['value'])) {
108
            return;
109
        }
110
111
        $parameterName = $this->getNewParameterName($queryBuilder);
112
113
        if (isset($data['type']) && $data['type'] == EqualType::TYPE_IS_NOT_EQUAL) {
114
            $this->applyWhere($queryBuilder, sprintf('%s != :%s', $alias, $parameterName));
115
        } else {
116
            $this->applyWhere($queryBuilder, sprintf('%s = :%s', $alias, $parameterName));
117
        }
118
119
        $queryBuilder->setParameter($parameterName, $data['value']);
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    protected function association(ProxyQueryInterface $queryBuilder, $data)
126
    {
127
        $types = array(
128
            ClassMetadataInfo::ONE_TO_ONE,
129
            ClassMetadataInfo::ONE_TO_MANY,
130
            ClassMetadataInfo::MANY_TO_MANY,
131
            ClassMetadataInfo::MANY_TO_ONE,
132
        );
133
134
        if (!in_array($this->getOption('mapping_type'), $types)) {
135
            throw new \RuntimeException('Invalid mapping type');
136
        }
137
138
        $associationMappings = $this->getParentAssociationMappings();
139
        $associationMappings[] = $this->getAssociationMapping();
140
        $alias = $queryBuilder->entityJoin($associationMappings);
141
142
        return array($alias, false);
143
    }
144
}
145