Completed
Push — master ( 6ca861...e4be68 )
by
unknown
02:04
created

ModelFilter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 3
dl 0
loc 109
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultOptions() 0 11 1
A getRenderSettings() 0 10 1
B filter() 0 16 7
B handleMultiple() 0 24 4
A association() 0 19 2
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' => '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
            $or = $queryBuilder->expr()->orX();
91
92
            $or->add($queryBuilder->expr()->notIn($alias, ':'.$parameterName));
93
94
            $or->add($queryBuilder->expr()->isNull(
95
                sprintf('IDENTITY(%s.%s)', current(($queryBuilder->getRootAliases())), $this->getFieldName())
96
            ));
97
98
            $this->applyWhere($queryBuilder, $or);
99
        } else {
100
            $this->applyWhere($queryBuilder, $queryBuilder->expr()->in($alias, ':'.$parameterName));
101
        }
102
103
        $queryBuilder->setParameter($parameterName, $data['value']);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    protected function association(ProxyQueryInterface $queryBuilder, $data)
110
    {
111
        $types = array(
112
            ClassMetadataInfo::ONE_TO_ONE,
113
            ClassMetadataInfo::ONE_TO_MANY,
114
            ClassMetadataInfo::MANY_TO_MANY,
115
            ClassMetadataInfo::MANY_TO_ONE,
116
        );
117
118
        if (!in_array($this->getOption('mapping_type'), $types)) {
119
            throw new \RuntimeException('Invalid mapping type');
120
        }
121
122
        $associationMappings = $this->getParentAssociationMappings();
123
        $associationMappings[] = $this->getAssociationMapping();
124
        $alias = $queryBuilder->entityJoin($associationMappings);
125
126
        return array($alias, false);
127
    }
128
}
129