Completed
Push — master ( 78a4ca...d31c6f )
by
unknown
04:58
created

TranslationFieldFilter::association()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 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\TranslationBundle\Filter;
13
14
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
15
use Sonata\DoctrineORMAdminBundle\Filter\Filter;
16
17
final 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
        $joinAlias = 'tff';
35
36
        // verify if the join is not already done
37
        $aliasAlreadyExists = false;
38
        foreach ($queryBuilder->getDQLParts()['join'] as $joins) {
39
            foreach ($joins as $join) {
40
                if ($join->getAlias() === $joinAlias) {
41
                    $aliasAlreadyExists = true;
42
                    break 2;
43
                }
44
            }
45
        }
46
47
        if (!$aliasAlreadyExists) {
48
            $queryBuilder->leftJoin($alias.'.translations', $joinAlias);
49
        }
50
51
        // search on translation OR on normal field
52
        $queryBuilder->andWhere($queryBuilder->expr()->orX(
53
            $queryBuilder->expr()->andX(
54
                $queryBuilder->expr()->eq($joinAlias.'.field', $queryBuilder->expr()->literal($field)),
55
                $queryBuilder->expr()->like(
56
                    $joinAlias.'.content',
57
                    $queryBuilder->expr()->literal('%'.$data['value'].'%')
58
                )
59
            ),
60
            $queryBuilder->expr()->like(
61
                sprintf('%s.%s', $alias, $field),
62
                $queryBuilder->expr()->literal('%'.$data['value'].'%')
63
            )
64
        ));
65
66
        $this->active = true;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getDefaultOptions()
73
    {
74
        return array(
75
            'field_type' => method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
76
                ? 'Symfony\Component\Form\Extension\Core\Type\TextType'
77
                : 'text', // NEXT_MAJOR: Remove ternary (when requirement of Symfony is >= 2.8)
78
            'operator_type' => method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
79
                ? 'Symfony\Component\Form\Extension\Core\Type\HiddenType'
80
                : 'hidden', // NEXT_MAJOR: Remove ternary (when requirement of Symfony is >= 2.8)
81
            'operator_options' => array(),
82
        );
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getRenderSettings()
89
    {
90
        // NEXT_MAJOR: Remove this line when drop Symfony <2.8 support
91
        $type = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
92
            ? 'Sonata\AdminBundle\Form\Type\Filter\DefaultType'
93
            : 'sonata_type_filter_default';
94
95
        return array($type, array(
96
            'field_type' => $this->getFieldType(),
97
            'field_options' => $this->getFieldOptions(),
98
            'operator_type' => $this->getOption('operator_type'),
99
            'operator_options' => $this->getOption('operator_options'),
100
            'label' => $this->getLabel(),
101
        ));
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    protected function association(ProxyQueryInterface $queryBuilder, $data)
108
    {
109
        $alias = $queryBuilder->entityJoin($this->getParentAssociationMappings());
110
111
        return array($this->getOption('alias', $alias), $this->getFieldName());
112
    }
113
}
114