Passed
Push — master ( c9bd7c...617e81 )
by Dāvis
02:28
created

ORMTranslationAdmin::joinTranslations()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
c 0
b 0
f 0
nc 6
nop 3
dl 0
loc 19
rs 8.8571
1
<?php
2
3
namespace Sludio\HelperBundle\Lexik\Admin;
4
5
use Doctrine\ORM\Query;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Query was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Doctrine\ORM\QueryBuilder;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\QueryBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Sonata\AdminBundle\Datagrid\DatagridMapper;
8
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
0 ignored issues
show
Bug introduced by
The type Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
class ORMTranslationAdmin extends TranslationAdmin
11
{
12
    protected function configureDatagridFilters(DatagridMapper $filter)
13
    {
14
        /** @var \Doctrine\ORM\EntityManager $em */
15
        $em = $this->getContainer()->get('doctrine')->getManagerForClass('Lexik\Bundle\TranslationBundle\Entity\File');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $em. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
16
17
        $domains = [];
18
        $domainsQueryResult = $em->createQueryBuilder()
19
            ->select('DISTINCT t.domain')
20
            ->from('\Lexik\Bundle\TranslationBundle\Entity\File', 't')
21
            ->getQuery()
22
            ->getResult(Query::HYDRATE_ARRAY)
23
        ;
24
25
        array_walk_recursive($domainsQueryResult, function($domain) use (&$domains) {
26
            $domains[$domain] = $domain;
27
        });
28
        ksort($domains);
29
30
        $filter->add('locale', 'doctrine_orm_callback', [
31
            'callback' => function(ProxyQuery $queryBuilder, $alias, $field, $options) {
32
                /* @var $queryBuilder \Doctrine\ORM\QueryBuilder */
33
                if (!isset($options['value']) || empty($options['value'])) {
34
                    return;
35
                }
36
                // use on to filter locales
37
                $this->joinTranslations($queryBuilder, $alias, $options['value']);
38
            },
39
            'field_options' => [
40
                'choices' => $this->formatLocales($this->managedLocales),
41
                'required' => false,
42
                'multiple' => true,
43
                'expanded' => false,
44
            ],
45
            'field_type' => 'choice',
46
        ])->add('show_non_translated_only', 'doctrine_orm_callback', [
47
            'callback' => function(ProxyQuery $queryBuilder, $alias, $field, $options) {
48
                /* @var $queryBuilder \Doctrine\ORM\QueryBuilder */
49
                if (!isset($options['value']) || empty($options['value']) || false === $options['value']) {
50
                    return;
51
                }
52
                $this->joinTranslations($queryBuilder, $alias);
53
54
                foreach ($this->getEmptyFieldPrefixes() as $prefix) {
55
                    if (empty($prefix)) {
56
                        $queryBuilder->orWhere('translations.content IS NULL');
57
                    } else {
58
                        $queryBuilder->orWhere('translations.content LIKE :content')
59
                            ->setParameter('content', $prefix.'%')
60
                        ;
61
                    }
62
63
                }
64
            },
65
            'field_options' => [
66
                'required' => true,
67
                'value' => $this->getNonTranslatedOnly(),
68
            ],
69
            'field_type' => 'checkbox',
70
        ])->add('key', 'doctrine_orm_string')->add('domain', 'doctrine_orm_choice', [
71
            'field_options' => [
72
                'choices' => $domains,
73
                'required' => true,
74
                'multiple' => false,
75
                'expanded' => false,
76
                'empty_data' => 'all',
77
            ],
78
            'field_type' => 'choice',
79
        ])->add('content', 'doctrine_orm_callback', [
80
            'callback' => function(ProxyQuery $queryBuilder, $alias, $field, $options) {
81
                /* @var $queryBuilder \Doctrine\ORM\QueryBuilder */
82
                if (!isset($options['value']) || empty($options['value'])) {
83
                    return;
84
                }
85
                $this->joinTranslations($queryBuilder, $alias);
86
                $queryBuilder->andWhere('translations.content LIKE :content')
87
                    ->setParameter('content', '%'.$options['value'].'%')
88
                ;
89
            },
90
            'field_type' => 'text',
91
            'label' => 'content',
92
        ])
93
        ;
94
    }
95
96
    /**
97
     * @param ProxyQuery $queryBuilder
98
     * @param String     $alias
99
     */
100
    private function joinTranslations(ProxyQuery $queryBuilder, $alias, array $locales = null)
101
    {
102
        $alreadyJoined = false;
103
        $joins = $queryBuilder->getDQLPart('join');
104
        if (array_key_exists($alias, $joins)) {
105
            $joins = $joins[$alias];
106
            foreach ($joins as $join) {
107
                if (strpos($join->__toString(), "$alias.translations ")) {
108
                    $alreadyJoined = true;
109
                }
110
            }
111
        }
112
        if (!$alreadyJoined) {
113
            /** @var QueryBuilder $queryBuilder */
114
            if ($locales) {
115
                $queryBuilder->leftJoin(sprintf('%s.translations', $alias), 'translations', 'WITH', 'translations.locale in (:locales)');
116
                $queryBuilder->setParameter('locales', $locales);
117
            } else {
118
                $queryBuilder->leftJoin(sprintf('%s.translations', $alias), 'translations');
119
            }
120
        }
121
    }
122
123
    /**
124
     * @return array
125
     */
126
    private function formatLocales(array $locales)
127
    {
128
        $formattedLocales = [];
129
        array_walk_recursive($locales, function($language) use (&$formattedLocales) {
130
            $formattedLocales[$language] = $language;
131
        });
132
133
        return $formattedLocales;
134
    }
135
}