Completed
Pull Request — 2.x (#143)
by
unknown
01:42
created

TranslationFieldFilter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 92
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
D filter() 0 41 9
A getDefaultOptions() 0 12 3
A getRenderSettings() 0 15 2
A association() 0 6 1
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\TranslationBundle\Filter;
13
14
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
15
use Sonata\DoctrineORMAdminBundle\Filter\Filter;
16
17
class TranslationFieldFilter extends Filter
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
23
    {
24
        if (!$data || !is_array($data) || !array_key_exists('value', $data)) {
25
            return;
26
        }
27
28
        $data['value'] = trim($data['value']);
29
30
        if (strlen($data['value']) == 0) {
31
            return;
32
        }
33
34
        // verify if the join is not already done
35
        $aliasAlreadyExists = false;
36
        foreach ($queryBuilder->getDQLParts()['join'] as $joins) {
37
            foreach ($joins as $join) {
38
                if ($join->getAlias() === 't') {
39
                    $aliasAlreadyExists = true;
40
                    break 2;
41
                }
42
            }
43
        }
44
45
        if (!$aliasAlreadyExists) {
46
            $queryBuilder->leftJoin($alias.'.translations', 't');
47
        }
48
49
        // search on translation OR on normal field
50
        $queryBuilder->andWhere($queryBuilder->expr()->orX(
51
            $queryBuilder->expr()->andX(
52
                $queryBuilder->expr()->eq('t.field', $queryBuilder->expr()->literal($field)),
53
                $queryBuilder->expr()->like('t.content', $queryBuilder->expr()->literal('%'.$data['value'].'%'))
54
            ),
55
            $queryBuilder->expr()->like(
56
                sprintf('%s.%s', $alias, $field),
57
                $queryBuilder->expr()->literal('%'.$data['value'].'%')
58
            )
59
        ));
60
61
        $this->active = true;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getDefaultOptions()
68
    {
69
        return array(
70
            'field_type' => method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
71
                ? 'Symfony\Component\Form\Extension\Core\Type\TextType'
72
                : 'text', // NEXT_MAJOR: Remove ternary (when requirement of Symfony is >= 2.8)
73
            'operator_type' => method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
74
                ? 'Symfony\Component\Form\Extension\Core\Type\HiddenType'
75
                : 'hidden', // NEXT_MAJOR: Remove ternary (when requirement of Symfony is >= 2.8)
76
            'operator_options' => array(),
77
        );
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getRenderSettings()
84
    {
85
        // NEXT_MAJOR: Remove this line when drop Symfony <2.8 support
86
        $type = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
87
            ? 'Sonata\AdminBundle\Form\Type\Filter\DefaultType'
88
            : 'sonata_type_filter_default';
89
90
        return array($type, array(
91
            'field_type' => $this->getFieldType(),
92
            'field_options' => $this->getFieldOptions(),
93
            'operator_type' => $this->getOption('operator_type'),
94
            'operator_options' => $this->getOption('operator_options'),
95
            'label' => $this->getLabel(),
96
        ));
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    protected function association(ProxyQueryInterface $queryBuilder, $data)
103
    {
104
        $alias = $queryBuilder->entityJoin($this->getParentAssociationMappings());
105
106
        return array($this->getOption('alias', $alias), $this->getFieldName());
107
    }
108
}
109