TranslationAdmin   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 239
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 239
rs 10
c 0
b 0
f 0
wmc 29

19 Methods

Rating   Name   Duplication   Size   Complexity  
A setEditableOptions() 0 3 1
A setTransUnitManager() 0 3 1
A setEmptyPrefixes() 0 3 1
A setManagedLocales() 0 3 1
A getEmptyFieldPrefixes() 0 3 1
A setDefaultSelections() 0 3 1
A getContainer() 0 3 1
A getDefaultSelections() 0 3 1
A initialize() 0 4 1
A getOriginalTemplate() 0 3 1
A getDefaultDomain() 0 3 1
A getNonTranslatedOnly() 0 3 2
A configureRoutes() 0 3 1
A getTemplate() 0 7 2
A buildDatagrid() 0 14 5
A getBatchActions() 0 10 1
A configureFormFields() 0 9 2
A configureListFields() 0 14 3
A getFilterParameters() 0 13 2
1
<?php
2
3
namespace Sludio\HelperBundle\Lexik\Admin;
4
5
use Lexik\Bundle\TranslationBundle\Manager\TransUnitManagerInterface;
6
use Sonata\AdminBundle\Admin\AbstractAdmin;
7
use Sonata\AdminBundle\Datagrid\ListMapper;
8
use Sonata\AdminBundle\Form\FormMapper;
9
use Sonata\AdminBundle\Route\RouteCollection;
10
use Psr\Container\ContainerInterface;
11
use Symfony\Component\Form\Extension\Core\Type;
12
13
abstract class TranslationAdmin extends AbstractAdmin
14
{
15
    /**
16
     * @var TransUnitManagerInterface
17
     */
18
    protected $transUnitManager;
19
    /**
20
     * @var array
21
     */
22
    protected $editableOptions;
23
24
    /**
25
     * @var array
26
     */
27
    protected $defaultSelections = [];
28
29
    /**
30
     * @var array
31
     */
32
    protected $emptyFieldPrefixes = [];
33
34
    /**
35
     * @var array
36
     */
37
    protected $filterLocales = [];
38
39
    /**
40
     * @var array
41
     */
42
    protected $managedLocales = [];
43
44
    /**
45
     * @param array $options
46
     */
47
    public function setEditableOptions(array $options)
48
    {
49
        $this->editableOptions = $options;
50
    }
51
52
    /**
53
     * @param TransUnitManagerInterface $translationManager
54
     */
55
    public function setTransUnitManager(TransUnitManagerInterface $translationManager)
56
    {
57
        $this->transUnitManager = $translationManager;
58
    }
59
60
    /**
61
     * @param array $managedLocales
62
     */
63
    public function setManagedLocales(array $managedLocales)
64
    {
65
        $this->managedLocales = $managedLocales;
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    public function getEmptyFieldPrefixes()
72
    {
73
        return $this->emptyFieldPrefixes;
74
    }
75
76
    /**
77
     * @return bool
78
     */
79
    public function getNonTranslatedOnly()
80
    {
81
        return array_key_exists('non_translated_only', $this->getDefaultSelections()) && (bool)$this->defaultSelections['nonTranslatedOnly'];
82
    }
83
84
    /**
85
     * @return array
86
     */
87
    public function getDefaultSelections()
88
    {
89
        return $this->defaultSelections;
90
    }
91
92
    /**
93
     * @param array $selections
94
     */
95
    public function setDefaultSelections(array $selections)
96
    {
97
        $this->defaultSelections = $selections;
98
    }
99
100
    /**
101
     * @param array $prefixes
102
     */
103
    public function setEmptyPrefixes(array $prefixes)
104
    {
105
        $this->emptyFieldPrefixes = $prefixes;
106
    }
107
108
    /**
109
     * @param string $name
110
     *
111
     * @return array|NULL|string
112
     */
113
    public function getTemplate($name)
114
    {
115
        if ($name === 'list') {
116
            return 'SludioHelperBundle:Lexik:CRUD\list.html.twig';
117
        }
118
119
        return parent::getTemplate($name);
0 ignored issues
show
Deprecated Code introduced by
The function Sonata\AdminBundle\Admin...actAdmin::getTemplate() has been deprecated: since 3.34, will be dropped in 4.0. Use TemplateRegistry services instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

119
        return /** @scrutinizer ignore-deprecated */ parent::getTemplate($name);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
120
    }
121
122
    /**
123
     * @param string $name
124
     *
125
     * @return string
126
     */
127
    public function getOriginalTemplate($name)
128
    {
129
        return parent::getTemplate($name);
0 ignored issues
show
Deprecated Code introduced by
The function Sonata\AdminBundle\Admin...actAdmin::getTemplate() has been deprecated: since 3.34, will be dropped in 4.0. Use TemplateRegistry services instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

129
        return /** @scrutinizer ignore-deprecated */ parent::getTemplate($name);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function buildDatagrid()
136
    {
137
        if ($this->datagrid) {
138
            return;
139
        }
140
141
        $filterParameters = $this->getFilterParameters();
142
143
        // transform _sort_by from a string to a FieldDescriptionInterface for the datagrid.
144
        if (isset($filterParameters['locale']) && \is_array($filterParameters['locale'])) {
145
            $this->filterLocales = array_key_exists('value', $filterParameters['locale']) ? $filterParameters['locale']['value'] : $this->managedLocales;
146
        }
147
148
        parent::buildDatagrid();
149
    }
150
151
    /**
152
     * @return array
153
     * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
154
     */
155
    public function getFilterParameters()
156
    {
157
        if ($this->getDefaultDomain()) {
158
            $this->datagridValues = array_merge([
159
                'domain' => [
160
                    'value' => $this->getDefaultDomain(),
161
                ],
162
            ], $this->datagridValues
163
164
            );
165
        }
166
167
        return parent::getFilterParameters();
168
    }
169
170
    /**
171
     * @return string
172
     * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
173
     */
174
    protected function getDefaultDomain()
175
    {
176
        return $this->getContainer()->getParameter('sludio_helper.lexik.default_domain');
177
    }
178
179
    /**
180
     * @return ContainerInterface
181
     */
182
    protected function getContainer()
183
    {
184
        return $this->getConfigurationPool()->getContainer();
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    public function getBatchActions()
191
    {
192
        $actions = parent::getBatchActions();
193
        $actions['download'] = [
194
            'label' => $this->trans($this->getLabelTranslatorStrategy()
195
                ->getLabel('download', 'batch', 'SludioHelperBundle')),
196
            'ask_confirmation' => false,
197
        ];
198
199
        return $actions;
200
    }
201
202
    public function initialize()
203
    {
204
        parent::initialize();
205
        $this->managedLocales = $this->getContainer()->getParameter('lexik_translation.managed_locales');
206
    }
207
208
    /**
209
     * @param RouteCollection $collection
210
     */
211
    protected function configureRoutes(RouteCollection $collection)
212
    {
213
        $collection->add('clear_cache')->add('create_trans_unit');
214
    }
215
216
    /**
217
     * @param ListMapper $list
218
     *
219
     * @throws \RuntimeException
220
     */
221
    protected function configureListFields(ListMapper $list)
222
    {
223
        $list->add('id', Type\IntegerType::class)
224
            ->add('key', Type\TextType::class)
225
            ->add('domain', Type\TextType::class);
226
227
        $localesToShow = \count($this->filterLocales) > 0 ? $this->filterLocales : $this->managedLocales;
228
229
        foreach ($localesToShow as $locale) {
230
            $fieldDescription = $this->modelManager->getNewFieldDescriptionInstance($this->getClass(), $locale);
231
            $fieldDescription->setTemplate('SludioHelperBundle:Lexik:CRUD/base_inline_translation_field.html.twig');
232
            $fieldDescription->setOption('locale', $locale);
233
            $fieldDescription->setOption('editable', $this->editableOptions);
234
            $list->add($fieldDescription);
0 ignored issues
show
Bug introduced by
$fieldDescription of type Sonata\AdminBundle\Admin\FieldDescriptionInterface is incompatible with the type string expected by parameter $name of Sonata\AdminBundle\Datagrid\ListMapper::add(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

234
            $list->add(/** @scrutinizer ignore-type */ $fieldDescription);
Loading history...
235
        }
236
    }
237
238
    /**
239
     * @param FormMapper $form
240
     *
241
     * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
242
     */
243
    protected function configureFormFields(FormMapper $form)
244
    {
245
        $subject = $this->getSubject();
246
247
        if (null === $subject->getId()) {
248
            $subject->setDomain($this->getDefaultDomain());
249
        }
250
251
        $form->add('key', Type\TextareaType::class)->add('domain', Type\TextareaType::class);
252
    }
253
}
254