Passed
Push — master ( 7ce289...8aaaed )
by Dāvis
04:53
created

ORMTranslationAdmin::getContentOptions()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Lexik\Admin;
4
5
use Doctrine\ORM\Query;
6
use Sonata\AdminBundle\Datagrid\DatagridMapper;
0 ignored issues
show
Bug introduced by
The type Sonata\AdminBundle\Datagrid\DatagridMapper 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\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...
8
9
class ORMTranslationAdmin extends TranslationAdmin
10
{
11
    private function getLocaleOptions()
12
    {
13
        return [
14
            'callback' => function (ProxyQuery $queryBuilder, $alias, $field, $options) {
15
                if (!isset($options['value']) || empty($options['value'])) {
16
                    return;
17
                }
18
                // use on to filter locales
19
                $this->joinTranslations($queryBuilder, $alias, $options['value']);
20
            },
21
            'field_options' => [
22
                'choices' => $this->formatLocales($this->managedLocales),
23
                'required' => false,
24
                'multiple' => true,
25
                'expanded' => false,
26
            ],
27
            'field_type' => 'choice',
28
        ];
29
    }
30
31
    private function getNonOptions()
32
    {
33
        return [
34
            'callback' => function (ProxyQuery $queryBuilder, $alias, $field, $options) {
35
                if (!isset($options['value']) || empty($options['value']) || false === $options['value']) {
36
                    return;
37
                }
38
                $this->joinTranslations($queryBuilder, $alias);
39
40
                foreach ($this->getEmptyFieldPrefixes() as $prefix) {
41
                    if (empty($prefix)) {
42
                        $queryBuilder->orWhere('translations.content IS NULL');
43
                    } else {
44
                        $queryBuilder->orWhere('translations.content LIKE :content')->setParameter('content', $prefix.'%');
45
                    }
46
                }
47
            },
48
            'field_options' => [
49
                'required' => true,
50
                'value' => $this->getNonTranslatedOnly(),
51
            ],
52
            'field_type' => 'checkbox',
53
        ];
54
    }
55
56
    private function getKeyOptions($domains)
57
    {
58
        return [
59
            'field_options' => [
60
                'choices' => $domains,
61
                'required' => true,
62
                'multiple' => false,
63
                'expanded' => false,
64
                'empty_data' => 'all',
65
            ],
66
            'field_type' => 'choice',
67
        ];
68
    }
69
70
    private function getContentOptions()
71
    {
72
        return [
73
            'callback' => function (ProxyQuery $queryBuilder, $alias, $field, $options) {
74
                if (!isset($options['value']) || empty($options['value'])) {
75
                    return;
76
                }
77
                $this->joinTranslations($queryBuilder, $alias);
78
                $queryBuilder->andWhere('translations.content LIKE :content')->setParameter('content', '%'.$options['value'].'%');
79
            },
80
            'field_type' => 'text',
81
            'label' => 'content',
82
        ];
83
    }
84
85
    protected function configureDatagridFilters(DatagridMapper $filter)
86
    {
87
        /** @var \Doctrine\ORM\EntityManager $entityManager */
88
        $entityManager = $this->getContainer()->get('doctrine')->getManagerForClass('Lexik\Bundle\TranslationBundle\Entity\File');
89
90
        $domains = [];
91
        $domainsQueryResult = $entityManager->createQueryBuilder()->select('DISTINCT t.domain')->from('\Lexik\Bundle\TranslationBundle\Entity\File', 't')->getQuery()->getResult(Query::HYDRATE_ARRAY);
92
93
        array_walk_recursive($domainsQueryResult, function ($domain) use (&$domains) {
94
            $domains[$domain] = $domain;
95
        });
96
        ksort($domains);
97
98
        // @formatter:off
99
        $filter
100
            ->add('locale', 'doctrine_orm_callback', $this->getLocaleOptions())
101
            ->add('show_non_translated_only', 'doctrine_orm_callback', $this->getNonOptions())
102
            ->add('key', 'doctrine_orm_string')->add('domain', 'doctrine_orm_choice', $this->getKeyOptions($domains))
103
            ->add('content', 'doctrine_orm_callback', $this->getContentOptions());
104
        // @formatter:on
105
    }
106
107
    /**
108
     * @param ProxyQuery $queryBuilder
109
     * @param String     $alias
110
     * @param array|null $locales
111
     */
112
    private function joinTranslations(ProxyQuery $queryBuilder, $alias, array $locales = null)
113
    {
114
        $alreadyJoined = false;
115
        $joins = $queryBuilder->getDQLPart('join');
116
        if (array_key_exists($alias, $joins)) {
117
            $joins = $joins[$alias];
118
            /** @var $joins array */
119
            foreach ($joins as $join) {
120
                if (strpos($join->__toString(), "$alias.translations ")) {
121
                    $alreadyJoined = true;
122
                }
123
            }
124
        }
125
        if (!$alreadyJoined) {
126
            if ($locales) {
127
                $queryBuilder->leftJoin(sprintf('%s.translations', $alias), 'translations', 'WITH', 'translations.locale in (:locales)');
128
                $queryBuilder->setParameter('locales', $locales);
129
            } else {
130
                $queryBuilder->leftJoin(sprintf('%s.translations', $alias), 'translations');
131
            }
132
        }
133
    }
134
135
    /**
136
     * @param array $locales
137
     *
138
     * @return array
139
     */
140
    private function formatLocales(array $locales)
141
    {
142
        $formattedLocales = [];
143
        array_walk_recursive($locales, function ($language) use (&$formattedLocales) {
144
            $formattedLocales[$language] = $language;
145
        });
146
147
        return $formattedLocales;
148
    }
149
}
150