GroupAdmin::configureRoutes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VideoGamesRecords\CoreBundle\Admin;
6
7
use Sonata\AdminBundle\Admin\AbstractAdmin;
8
use Sonata\AdminBundle\Datagrid\DatagridMapper;
9
use Sonata\AdminBundle\Datagrid\ListMapper;
10
use Sonata\AdminBundle\Form\FormMapper;
11
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
12
use Sonata\AdminBundle\Form\Type\ModelListType;
13
use Sonata\AdminBundle\Route\RouteCollectionInterface;
14
use Sonata\AdminBundle\Show\ShowMapper;
15
use Sonata\DoctrineORMAdminBundle\Filter\ModelFilter;
16
use Sonata\Form\Type\CollectionType;
17
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
18
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
19
use Symfony\Component\Form\Extension\Core\Type\TextType;
20
use Symfony\Component\Intl\Locale;
21
use VideoGamesRecords\CoreBundle\ValueObject\GroupOrderBy;
22
23
final class GroupAdmin extends AbstractAdmin
24
{
25
    protected $baseRouteName = 'vgrcorebundle_admin_group';
26
27
    /**
28
     * @return string
29
     */
30
    private function getLibGame(): string
31
    {
32
        $locale = Locale::getDefault();
33
        return ($locale == 'fr') ? 'libGameFr' : 'libGameEn';
34
    }
35
36
    /**
37
     * @param RouteCollectionInterface $collection
38
     */
39
    protected function configureRoutes(RouteCollectionInterface $collection): void
40
    {
41
        $collection->remove('export')
42
            ->add('copy', $this->getRouterIdParameter() . '/copy')
43
            ->add('add-lib-chart', $this->getRouterIdParameter() . '/add-lib-chart')
44
            ->add('set-video-proof-only', $this->getRouterIdParameter() . '/set-video-proof-only');
45
    }
46
47
    public function configureActionButtons(array $buttonList, string $action, ?object $object = null): array
48
    {
49
        if (in_array($action, ['show', 'edit', 'acl']) && $object) {
50
            $buttonList['copy'] = [
51
                'template' => '@VideoGamesRecordsCore/Admin/ActionButton/btn.copy.html.twig',
52
            ];
53
            $buttonList['add-lib-chart'] = [
54
                'template' => '@VideoGamesRecordsCore/Admin/ActionButton/btn.add_lib_chart.html.twig',
55
            ];
56
            $buttonList['set-video-proof-only'] = [
57
                'template' => '@VideoGamesRecordsCore/Admin/ActionButton/btn.set_video_proof_only.html.twig',
58
            ];
59
            $buttonList['add-chart'] = [
60
                'template' => '@VideoGamesRecordsCore/Admin/Object/Group/btn.add_chart.html.twig',
61
            ];
62
        }
63
        return $buttonList;
64
    }
65
66
    /**
67
    /**
68
     * @param FormMapper $form
69
     */
70
    protected function configureFormFields(FormMapper $form): void
71
    {
72
        $gameOptions = [];
73
        if (($this->hasRequest()) && ($this->isCurrentRoute('create'))) {
74
            $idGame = $this->getRequest()->get('idGame', null);
75
            if ($idGame !== null) {
76
                $this->getRequest()->getSession()->set('vgrcorebundle_admin_group.idGame', $idGame);
77
            }
78
79
            if ($this->getRequest()->getSession()->has('vgrcorebundle_admin_group.idGame')) {
80
                $idGame = $this->getRequest()->getSession()->get('vgrcorebundle_admin_group.idGame');
81
                $entityManager = $this->getModelManager()
82
                    ->getEntityManager('VideoGamesRecords\CoreBundle\Entity\Game');
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not exist on Sonata\AdminBundle\Model\ModelManagerInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Sonata\AdminBundle\Model\LockInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

82
                    ->/** @scrutinizer ignore-call */ getEntityManager('VideoGamesRecords\CoreBundle\Entity\Game');
Loading history...
83
                $game = $entityManager->getReference('VideoGamesRecords\CoreBundle\Entity\Game', $idGame);
84
                $gameOptions = ['data' => $game];
85
            }
86
        }
87
88
        $form
89
            ->add('id', TextType::class, [
90
                'label' => 'label.id',
91
                'attr' => [
92
                    'readonly' => true,
93
                ]
94
            ])
95
            ->add('libGroupEn', TextType::class, [
96
                'label' => 'label.name.en',
97
                'required' => true,
98
            ])
99
            ->add('libGroupFr', TextType::class, [
100
                'label' => 'label.name.fr',
101
                'required' => false,
102
            ])
103
            ->add('isRank', CheckboxType::class, [
104
                'label' => 'label.boolRanking',
105
                'required' => false,
106
            ])
107
            ->add(
108
                'orderBy',
109
                ChoiceType::class,
110
                [
111
                    'label' => 'label.orderBy',
112
                    'choices' => GroupOrderBy::getStatusChoices(),
113
                ]
114
            )
115
        ;
116
117
        if ($this->isCurrentRoute('create') || $this->isCurrentRoute('edit')) {
118
            $btnCalalogue = $this->isCurrentRoute('create');
119
            $form->
120
                add(
121
                    'game',
122
                    ModelListType::class,
123
                    array_merge(
124
                        $gameOptions,
125
                        [
126
                        'data_class' => null,
127
                        'btn_add' => false,
128
                        'btn_list' => $btnCalalogue,
129
                        'btn_edit' => false,
130
                        'btn_delete' => false,
131
                        'btn_catalogue' => $btnCalalogue,
132
                        'label' => 'label.game',
133
                        ]
134
                    )
135
                );
136
        }
137
138
        $form->add('isDlc', CheckboxType::class, [
139
            'label' => 'label.isDlc',
140
            'required' => false,
141
        ]);
142
143
        $subject = $this->getSubject();
144
145
        if (
146
            (strpos(
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (strpos($this->getReques...ject->getCharts()) < 50, Probably Intended Meaning: strpos($this->getRequest...ect->getCharts()) < 50)
Loading history...
147
                $this->getRequest()
148
                        ->getPathInfo(),
149
                'videogamesrecords/core/group'
150
            ) || (($this->getRequest()
151
                            ->getPathInfo() == '/admin/core/append-form-field-element') && ($this->getRequest(
152
                            )->query->get('_sonata_admin') == 'sonata.admin.vgr.group'))) && (count(
153
                                $subject->getCharts()
154
                            ) < 50)
155
        ) {
156
            $form->end()
157
                ->with('label.charts')
158
                ->add(
159
                    'charts',
160
                    CollectionType::class,
161
                    array(
162
                        'label' => 'label.charts',
163
                        'by_reference' => false,
164
                        'help' => 'label.libs.help',
165
                        'type_options' => array(
166
                            // Prevents the "Delete" option from being displayed
167
                            'delete' => true,
168
                            'delete_options' => array(
169
                                // You may otherwise choose to put the field but hide it
170
                                'type' => CheckboxType::class,
171
                                // In that case, you need to fill in the options as well
172
                                'type_options' => array(
173
                                    'mapped' => false,
174
                                    'required' => false,
175
                                )
176
                            )
177
                        ),
178
                    ),
179
                    array(
180
                        'edit' => 'inline',
181
                        'inline' => 'table',
182
                    )
183
                )
184
                ->end();
185
        }
186
    }
187
188
    /**
189
     * @param DatagridMapper $filter
190
     */
191
    protected function configureDatagridFilters(DatagridMapper $filter): void
192
    {
193
        $filter
194
            ->add('id', null, ['label' => 'label.id'])
195
            ->add('libGroupEn', null, ['label' => 'label.name.en'])
196
            ->add('libGroupFr', null, ['label' => 'label.name.fr'])
197
            ->add('isDlc', null, ['label' => 'label.isDlc'])
198
            ->add(
199
                'game',
200
                ModelFilter::class,
201
                [
202
                    'field_type' => ModelAutocompleteType::class,
203
                    'field_options' => ['property' => $this->getLibGame()],
204
                    'label' => 'label.game'
205
                ]
206
            )
207
        ;
208
    }
209
210
    /**
211
     * @param ListMapper $list
212
     */
213
    protected function configureListFields(ListMapper $list): void
214
    {
215
        $btns = [];
216
        if ($this->hasAccess('create')) {
217
            $btns = [
218
                'copy' => [
219
                    'template' => '@VideoGamesRecordsCore/Admin/Object/Group/link.copy.html.twig'
220
                ],
221
                'add_chart' => [
222
                    'template' => '@VideoGamesRecordsCore/Admin/Object/Group/link.add_chart.html.twig'
223
                ],
224
            ];
225
        }
226
227
        $list
228
            ->addIdentifier('id', null, ['label' => 'label.id'])
229
            ->add('libGroupEn', null, ['label' => 'label.group.en', 'editable' => true])
230
            ->add('libGroupFr', null, ['label' => 'label.group.fr', 'editable' => true])
231
            ->add('nbChart', null, ['label' => 'label.nbChart'])
232
            ->add('game', null, [
233
                'associated_property' => $this->getLibGame(),
234
                'label' => 'label.game',
235
            ])
236
            ->add(
237
                'orderBy',
238
                'choice',
239
                [
240
                    'label' => 'label.orderBy',
241
                    'editable' => true,
242
                    'choices' => GroupOrderBy::getStatusChoices(),
243
                ]
244
            )
245
            ->add('isDlc', 'boolean', ['label' => 'label.isDlc'])
246
            ->add('_action', 'actions', [
247
                'actions' =>
248
                    array_merge(
249
                        [
250
                            'show' => [],
251
                            'edit' => [],
252
                            'groups' => [
253
                                'template' => '@VideoGamesRecordsCore/Admin/Object/Group/link.charts.html.twig'
254
                            ]
255
                        ],
256
                        $btns
257
                    )
258
            ]);
259
    }
260
261
    /**
262
     * @param ShowMapper $show
263
     */
264
    protected function configureShowFields(ShowMapper $show): void
265
    {
266
        $show
267
            ->add('id', null, ['label' => 'label.id'])
268
            ->add('libGroupEn', null, ['label' => 'label.name.en'])
269
            ->add('libGroupFr', null, ['label' => 'label.name.fr'])
270
            ->add('nbChart', null, ['label' => 'label.nbChart'])
271
            ->add('isDlc', null, ['label' => 'label.isDlc'])
272
            ->add('game', null, [
273
                'associated_property' => $this->getLibGame(),
274
                'label' => 'label.game',
275
            ])
276
            ->add('charts', null, ['label' => 'label.charts']);
277
    }
278
}
279