Passed
Branch master (c65ffc)
by Dāvis
03:08
created

TranslationAdmin   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 234
rs 10
wmc 29

19 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultSelections() 0 3 1
A setEditableOptions() 0 3 1
A setTransUnitManager() 0 3 1
A setEmptyPrefixes() 0 3 1
A setManagedLocales() 0 3 1
A getNonTranslatedOnly() 0 3 2
A getEmptyFieldPrefixes() 0 3 1
A getFilterParameters() 0 13 2
A setDefaultSelections() 0 3 1
A getContainer() 0 3 1
A getTemplate() 0 7 2
A initialize() 0 4 1
B buildDatagrid() 0 14 5
A getBatchActions() 0 10 1
A configureFormFields() 0 9 2
A getOriginalTemplate() 0 3 1
A getDefaultDomain() 0 3 1
A configureListFields() 0 15 3
A configureRoutes() 0 3 1
1
<?php
2
3
namespace Sludio\HelperBundle\Lexik\Admin;
4
5
use Sonata\AdminBundle\Route\RouteCollection;
6
use Lexik\Bundle\TranslationBundle\Manager\TransUnitManagerInterface;
7
use Sonata\AdminBundle\Model\ModelManagerInterface;
8
use Sonata\AdminBundle\Datagrid\ListMapper;
9
use Sonata\AdminBundle\Form\FormMapper;
10
use Sonata\AdminBundle\Admin\AbstractAdmin;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Symfony\Component\Form\Extension\Core\Type;
13
14
abstract class TranslationAdmin extends AbstractAdmin
15
{
16
    /**
17
     * @var TransUnitManagerInterface
18
     */
19
    protected $transUnitManager;
20
    /**
21
     * @var array
22
     */
23
    protected $editableOptions;
24
25
    /**
26
     * @var array
27
     */
28
    protected $defaultSelections = [];
29
30
    /**
31
     * @var array
32
     */
33
    protected $emptyFieldPrefixes = [];
34
35
    /**
36
     * @var array
37
     */
38
    protected $filterLocales = [];
39
40
    /**
41
     * @var array
42
     */
43
    protected $managedLocales = [];
44
45
    /**
46
     * @param array $options
47
     */
48
    public function setEditableOptions(array $options)
49
    {
50
        $this->editableOptions = $options;
51
    }
52
53
    /**
54
     * @param TransUnitManagerInterface $translationManager
55
     */
56
    public function setTransUnitManager(TransUnitManagerInterface $translationManager)
57
    {
58
        $this->transUnitManager = $translationManager;
59
    }
60
61
    /**
62
     * @param array $managedLocales
63
     */
64
    public function setManagedLocales(array $managedLocales)
65
    {
66
        $this->managedLocales = $managedLocales;
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    public function getEmptyFieldPrefixes()
73
    {
74
        return $this->emptyFieldPrefixes;
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    public function getDefaultSelections()
81
    {
82
        return $this->defaultSelections;
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    public function getNonTranslatedOnly()
89
    {
90
        return array_key_exists('non_translated_only', $this->getDefaultSelections()) && (bool)$this->defaultSelections['nonTranslatedOnly'];
91
    }
92
93
    /**
94
     * @param array $selections
95
     */
96
    public function setDefaultSelections(array $selections)
97
    {
98
        $this->defaultSelections = $selections;
99
    }
100
101
    /**
102
     * @param array $prefixes
103
     */
104
    public function setEmptyPrefixes(array $prefixes)
105
    {
106
        $this->emptyFieldPrefixes = $prefixes;
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    public function getFilterParameters()
113
    {
114
        if ($this->getDefaultDomain()) {
115
            $this->datagridValues = array_merge([
116
                'domain' => [
117
                    'value' => $this->getDefaultDomain(),
118
                ],
119
            ], $this->datagridValues
120
121
            );
122
        }
123
124
        return parent::getFilterParameters();
125
    }
126
127
    /**
128
     * @param string $name
129
     *
130
     * @return array|NULL
131
     */
132
    public function getTemplate($name)
133
    {
134
        if ($name === 'list') {
135
            return 'SludioHelperBundle:Lexik:CRUD\list.html.twig';
136
        }
137
138
        return parent::getTemplate($name);
139
    }
140
141
    /**
142
     * @param string $name
143
     *
144
     * @return string
145
     */
146
    public function getOriginalTemplate($name)
147
    {
148
        return parent::getTemplate($name);
149
    }
150
151
    /**
152
     * @param RouteCollection $collection
153
     */
154
    protected function configureRoutes(RouteCollection $collection)
155
    {
156
        $collection->add('clear_cache')->add('create_trans_unit');
157
    }
158
159
    /**
160
     * @param ListMapper $list
161
     */
162
    protected function configureListFields(ListMapper $list)
163
    {
164
        $list->add('id', Type\IntegerType::class)
165
            ->add('key', Type\TextType::class)
166
            ->add('domain', Type\TextType::class)
167
        ;
168
169
        $localesToShow = count($this->filterLocales) > 0 ? $this->filterLocales : $this->managedLocales;
170
171
        foreach ($localesToShow as $locale) {
172
            $fieldDescription = $this->modelManager->getNewFieldDescriptionInstance($this->getClass(), $locale);
173
            $fieldDescription->setTemplate('SludioHelperBundle:Lexik:CRUD/base_inline_translation_field.html.twig');
174
            $fieldDescription->setOption('locale', $locale);
175
            $fieldDescription->setOption('editable', $this->editableOptions);
176
            $list->add($fieldDescription);
177
        }
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function buildDatagrid()
184
    {
185
        if ($this->datagrid) {
186
            return;
187
        }
188
189
        $filterParameters = $this->getFilterParameters();
190
191
        // transform _sort_by from a string to a FieldDescriptionInterface for the datagrid.
192
        if (isset($filterParameters['locale']) && is_array($filterParameters['locale'])) {
193
            $this->filterLocales = array_key_exists('value', $filterParameters['locale']) ? $filterParameters['locale']['value'] : $this->managedLocales;
194
        }
195
196
        parent::buildDatagrid();
197
    }
198
199
    /**
200
     * @param FormMapper $form
201
     */
202
    protected function configureFormFields(FormMapper $form)
203
    {
204
        $subject = $this->getSubject();
205
206
        if (null === $subject->getId()) {
207
            $subject->setDomain($this->getDefaultDomain());
208
        }
209
210
        $form->add('key', Type\TextareaType::class)->add('domain', Type\TextareaType::class);
211
    }
212
213
    /**
214
     * @return ContainerInterface
215
     */
216
    protected function getContainer()
217
    {
218
        return $this->getConfigurationPool()->getContainer();
219
    }
220
221
    /**
222
     * @return string
223
     */
224
    protected function getDefaultDomain()
225
    {
226
        return $this->getContainer()->getParameter('sludio_helper.lexik.default_domain');
227
    }
228
229
    /**
230
     * {@inheritdoc}
231
     */
232
    public function getBatchActions()
233
    {
234
        $actions = parent::getBatchActions();
235
        $actions['download'] = [
236
            'label' => $this->trans($this->getLabelTranslatorStrategy()
237
                ->getLabel('download', 'batch', 'SludioHelperBundle')),
238
            'ask_confirmation' => false,
239
        ];
240
241
        return $actions;
242
    }
243
244
    public function initialize()
245
    {
246
        parent::initialize();
247
        $this->managedLocales = $this->getContainer()->getParameter('lexik_translation.managed_locales');
248
    }
249
}