Completed
Push — 2.x ( 359f6e )
by Sullivan
16:25 queued 14:10
created

ModelFilter::handleModel()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

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