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

TranslationAdmin::setDefaultSelections()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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 unknown $name
0 ignored issues
show
Bug introduced by
The type Sludio\HelperBundle\Lexik\Admin\unknown 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...
129
     *
130
     * @return multitype:|NULL
131
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment multitype:|NULL at position 0 could not be parsed: Unknown type name 'multitype:' at position 0 in multitype:|NULL.
Loading history...
132
    public function getTemplate($name)
133
    {
134
        if ($name === 'layout') {
135
            return 'SludioHelperBundle:Lexik:translation_layout.html.twig';
136
        }
137
138
        if ($name === 'list') {
139
            return 'SludioHelperBundle:Lexik:CRUD\list.html.twig';
140
        }
141
142
        return parent::getTemplate($name);
143
    }
144
145
    /**
146
     * @param string $name
147
     *
148
     * @return string
149
     */
150
    public function getOriginalTemplate($name)
151
    {
152
        return parent::getTemplate($name);
153
    }
154
155
    /**
156
     * @param RouteCollection $collection
157
     */
158
    protected function configureRoutes(RouteCollection $collection)
159
    {
160
        $collection->add('clear_cache')->add('create_trans_unit');
161
    }
162
163
    /**
164
     * @param ListMapper $list
165
     */
166
    protected function configureListFields(ListMapper $list)
167
    {
168
        $list->add('id', Type\IntegerType::class)
169
            ->add('key', Type\TextType::class)
170
            ->add('domain', Type\TextType::class)
171
        ;
172
173
        $localesToShow = count($this->filterLocales) > 0 ? $this->filterLocales : $this->managedLocales;
174
175
        foreach ($localesToShow as $locale) {
176
            $fieldDescription = $this->modelManager->getNewFieldDescriptionInstance($this->getClass(), $locale);
177
            $fieldDescription->setTemplate('SludioHelperBundle:Lexik:CRUD/base_inline_translation_field.html.twig');
178
            $fieldDescription->setOption('locale', $locale);
179
            $fieldDescription->setOption('editable', $this->editableOptions);
180
            $list->add($fieldDescription);
181
        }
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187
    public function buildDatagrid()
188
    {
189
        if ($this->datagrid) {
190
            return;
191
        }
192
193
        $filterParameters = $this->getFilterParameters();
194
195
        // transform _sort_by from a string to a FieldDescriptionInterface for the datagrid.
196
        if (isset($filterParameters['locale']) && is_array($filterParameters['locale'])) {
197
            $this->filterLocales = array_key_exists('value', $filterParameters['locale']) ? $filterParameters['locale']['value'] : $this->managedLocales;
198
        }
199
200
        parent::buildDatagrid();
201
    }
202
203
    /**
204
     * @param FormMapper $form
205
     */
206
    protected function configureFormFields(FormMapper $form)
207
    {
208
        $subject = $this->getSubject();
209
210
        if (null === $subject->getId()) {
211
            $subject->setDomain($this->getDefaultDomain());
212
        }
213
214
        $form->add('key', Type\TextareaType::class)->add('domain', Type\TextareaType::class);
215
    }
216
217
    /**
218
     * @return ContainerInterface
219
     */
220
    protected function getContainer()
221
    {
222
        return $this->getConfigurationPool()->getContainer();
223
    }
224
225
    /**
226
     * @return string
227
     */
228
    protected function getDefaultDomain()
229
    {
230
        return $this->getContainer()->getParameter('sludio_helper.lexik.default_domain');
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236
    public function getBatchActions()
237
    {
238
        $actions = parent::getBatchActions();
239
        $actions['download'] = [
240
            'label' => $this->trans($this->getLabelTranslatorStrategy()
241
                ->getLabel('download', 'batch', 'SludioHelperBundle')),
242
            'ask_confirmation' => false,
243
        ];
244
245
        return $actions;
246
    }
247
248
    public function initialize()
249
    {
250
        parent::initialize();
251
        $this->managedLocales = $this->getContainer()->getParameter('lexik_translation.managed_locales');
252
    }
253
}