Completed
Pull Request — 3.x (#683)
by
unknown
02:06
created

ModelFilter::getDefaultOptions()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 0
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) || empty($data['value'])) {
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
            $data['value'] = array($data['value']);
37
        }
38
39
        $this->handleMultiple($queryBuilder, $alias, $data);
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)', $this->getParentAlias($queryBuilder, $alias), $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
     * {@inheritdoc}
117
     */
118
    protected function association(ProxyQueryInterface $queryBuilder, $data)
119
    {
120
        $types = array(
121
            ClassMetadataInfo::ONE_TO_ONE,
122
            ClassMetadataInfo::ONE_TO_MANY,
123
            ClassMetadataInfo::MANY_TO_MANY,
124
            ClassMetadataInfo::MANY_TO_ONE,
125
        );
126
127
        if (!in_array($this->getOption('mapping_type'), $types)) {
128
            throw new \RuntimeException('Invalid mapping type');
129
        }
130
131
        $associationMappings = $this->getParentAssociationMappings();
132
        $associationMappings[] = $this->getAssociationMapping();
133
        $alias = $queryBuilder->entityJoin($associationMappings);
134
135
        return array($alias, false);
136
    }
137
138
    /**
139
     * Retrieve the parent alias for given alias.
140
     * Root alias for direct association or entity joined alias for association depth >= 2.
141
     *
142
     * @param ProxyQueryInterface $queryBuilder
143
     * @param string              $alias
144
     *
145
     * @return string
146
     */
147
    private function getParentAlias(ProxyQueryInterface $queryBuilder, $alias)
148
    {
149
        $parentAlias = $rootAlias = current($queryBuilder->getRootAliases());
150
        $joins = $queryBuilder->getDQLPart('join');
151
        if (isset($joins[$rootAlias])) {
152
            foreach ($joins[$rootAlias] as $join) {
153
                if ($join->getAlias() == $alias) {
154
                    $parts = explode('.', $join->getJoin());
155
                    $parentAlias = $parts[0];
156
                    break;
157
                }
158
            }
159
        }
160
161
        return $parentAlias;
162
    }
163
}
164