Passed
Push — develop ( c0c420...1d51ce )
by BENARD
06:14
created

GameAdmin::configureActionButtons()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 3
nc 2
nop 3
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\ChoiceFilter;
16
use Sonata\DoctrineORMAdminBundle\Filter\ModelFilter;
17
use Sonata\Form\Type\CollectionType;
18
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
19
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
20
use Symfony\Component\Form\Extension\Core\Type\DateType;
21
use Symfony\Component\Form\Extension\Core\Type\TextType;
22
use VideoGamesRecords\CoreBundle\ValueObject\GameStatus;
23
24
class GameAdmin extends AbstractAdmin
25
{
26
    protected $baseRouteName = 'vgrcorebundle_admin_game';
27
28
    /**
29
     * @param RouteCollectionInterface $collection
30
     */
31
    protected function configureRoutes(RouteCollectionInterface $collection): void
32
    {
33
        parent::configureRoutes($collection);
34
        $collection
35
            ->add('copy', $this->getRouterIdParameter() . '/copy')
36
            ->add('maj', $this->getRouterIdParameter() . '/maj')
37
            ->add('set-video-proof-only', $this->getRouterIdParameter() . '/set-video-proof-only');
38
    }
39
40
    public function configureActionButtons(array $buttonList, string $action, ?object $object = null): array
41
    {
42
        if (in_array($action, ['show', 'edit', 'acl']) && $object) {
43
            $buttonList['set-video-proof-only'] = [
44
                'template' => '@VideoGamesRecordsCore/Admin/ActionButton/btn.set_video_proof_only.html.twig',
45
            ];
46
        }
47
        return $buttonList;
48
    }
49
50
    /**
51
     * @param array<mixed> $sortValues
52
     * @return void
53
     */
54
    protected function configureDefaultSortValues(array &$sortValues): void
55
    {
56
        $sortValues['_page'] = 1;
57
        $sortValues['_sort_order'] = 'DESC';
58
        $sortValues['_sort_by'] = 'id';
59
    }
60
61
    /**
62
     * @return string[]
63
     */
64
    protected function configureExportFields(): array
65
    {
66
        return ['id', 'libGameEn', 'libGameFr', 'serie', 'status', 'picture', 'platforms'];
67
    }
68
69
70
    /**
71
     * @param FormMapper $form
72
     */
73
    protected function configureFormFields(FormMapper $form): void
74
    {
75
        $form
76
            ->add('serie', ModelAutocompleteType::class, [
77
                'property' => 'libSerie',
78
                'label' => 'label.serie',
79
                'required' => false,
80
            ])
81
            ->add('libGameEn', TextType::class, [
82
                'label' => 'label.name.en',
83
                'required' => true,
84
            ])
85
            ->add('libGameFr', TextType::class, [
86
                'label' => 'label.name.fr',
87
                'required' => false,
88
            ])
89
            ->add('rules', null, ['required' => false, 'expanded' => false, 'label' => 'label.rules'])
90
            ->add('badge', ModelListType::class, [
91
                'btn_add' => true,
92
                'btn_list' => true,
93
                'btn_edit' => false,
94
                'btn_delete' => false,
95
                'btn_catalogue' => true,
96
                'label' => 'label.badge',
97
            ])
98
            ->add('forum', ModelListType::class, [
99
                'btn_add' => true,
100
                'btn_list' => true,
101
                'btn_edit' => false,
102
                'btn_delete' => false,
103
                'btn_catalogue' => true,
104
                'label' => 'label.forum',
105
            ])
106
            ->add('picture', TextType::class, [
107
                'label' => 'label.picture',
108
                'required' => false,
109
            ])
110
            ->add('downloadUrl', TextType::class, [
111
                'label' => 'label.downloadUrl',
112
                'required' => false,
113
            ])
114
            ->add(
115
                'status',
116
                ChoiceType::class,
117
                [
118
                    'label' => 'label.status',
119
                    'choices' => GameStatus::getStatusChoices(),
120
                ]
121
            )
122
            ->add('publishedAt', DateType::class, [
123
                'label' => 'label.publishedAt',
124
                'required' => false,
125
                'years' => range(2004, date('Y'))
126
            ])
127
            ->add('isRank', CheckboxType::class, [
128
                'label' => 'label.boolRanking',
129
                'required' => false,
130
            ])
131
            ->add(
132
                'platforms',
133
                null,
134
                [
135
                    'label' => 'label.platforms',
136
                    'required' => false,
137
                    'expanded' => false,
138
                    'query_builder' =>
139
                        function ($er) {
140
                            $qb = $er->createQueryBuilder('p');
141
                            $qb->orderBy('p.libPlatform', 'ASC');
142
                            return $qb;
143
                        }
144
                ]
145
            )
146
            ->end()
147
            ->with('label.groups')
148
            ->add(
149
                'groups',
150
                CollectionType::class,
151
                array(
152
                    'label' => 'label.groups',
153
                    'by_reference' => false,
154
                    'type_options' => array(
155
                        // Prevents the "Delete" option from being displayed
156
                        'delete' => true,
157
                        'delete_options' => array(
158
                            // You may otherwise choose to put the field but hide it
159
                            'type' => CheckboxType::class,
160
                            // In that case, you need to fill in the options as well
161
                            'type_options' => array(
162
                                'mapped' => false,
163
                                'required' => false,
164
                            )
165
                        )
166
                    ),
167
                ),
168
                array(
169
                    'edit' => 'inline',
170
                    'inline' => 'table',
171
                )
172
            )
173
            ->end();
174
    }
175
176
    /**
177
     * @param DatagridMapper $filter
178
     */
179
    protected function configureDatagridFilters(DatagridMapper $filter): void
180
    {
181
        $filter
182
            ->add('id', null, ['label' => 'label.id'])
183
            ->add('serie', ModelFilter::class, [
184
                'field_type' => ModelAutocompleteType::class,
185
                'field_options' => ['property' => 'libSerie'],
186
                'label' => 'label.serie'
187
            ])
188
            ->add('libGameEn', null, ['label' => 'label.name.en'])
189
            ->add('libGameFr', null, ['label' => 'label.name.fr'])
190
            ->add('nbChart', null, ['label' => 'label.nbChart'])
191
            ->add(
192
                'platforms',
193
                null,
194
                [
195
                    'label' => 'label.platforms',
196
                    'field_options' => [
197
                        'multiple' => true,
198
                    ]
199
                ]
200
            )
201
            ->add('status', ChoiceFilter::class, [
202
                'label' => 'label.status',
203
                'field_type' => ChoiceType::class,
204
                'field_options' => [
205
                    'choices' => GameStatus::getStatusChoices(),
206
                    'multiple' => false,
207
                ]
208
            ])
209
            ->add('isRank', null, ['label' => 'label.boolRanking']);
210
    }
211
212
    /**
213
     * @param ListMapper $list
214
     */
215
    protected function configureListFields(ListMapper $list): void
216
    {
217
        $btns = [
218
            'maj' => [
219
                'template' => '@VideoGamesRecordsCore/Admin/object_maj_link.html.twig'
220
            ],
221
            'history' => [
222
                'template' => '@VideoGamesRecordsCore/Admin/game_history_link.html.twig'
223
            ],
224
        ];
225
        if ($this->hasAccess('create')) {
226
            $btns = array_merge($btns, [
227
                'copy' => [
228
                    'template' => '@VideoGamesRecordsCore/Admin/game_copy_link.html.twig'
229
                ],
230
                'add_group' => [
231
                    'template' => '@VideoGamesRecordsCore/Admin/game_add_group_link.html.twig'
232
                ]
233
            ]);
234
        }
235
236
        $list
237
            ->addIdentifier('id', null, ['label' => 'label.id'])
238
            ->add('libGameEn', null, ['label' => 'label.game.en', 'editable' => true])
239
            ->add('libGameFr', null, ['label' => 'label.game.fr', 'editable' => true])
240
            ->add('platforms', null, ['label' => 'label.platforms'])
241
            ->add(
242
                'picture',
243
                null,
244
                [
245
                    'label' => 'label.picture',
246
                    'editable' => true
247
                ]
248
            )
249
            ->add(
250
                'badge.picture',
251
                null,
252
                [
253
                    'label' => 'label.badge',
254
                    'editable' => true
255
                ]
256
            )
257
            ->add(
258
                'status',
259
                'choice',
260
                [
261
                    'label' => 'label.status',
262
                    'editable' => true,
263
                    'choices' => GameStatus::getReverseStatusChoices(),
264
                ]
265
            )
266
            ->add('_action', 'actions', [
267
                'actions' => array_merge(
268
                    [
269
                        'show' => [],
270
                        'edit' => [],
271
                        'groups' => [
272
                            'template' => '@VideoGamesRecordsCore/Admin/game_groups_link.html.twig'
273
                        ]
274
                    ],
275
                    $btns
276
                )
277
            ]);
278
    }
279
280
    /**
281
     * @param ShowMapper $show
282
     */
283
    protected function configureShowFields(ShowMapper $show): void
284
    {
285
        $show
286
            ->add('id', null, ['label' => 'label.id'])
287
            ->add('libGameEn', null, ['label' => 'label.name.en'])
288
            ->add('libGameFr', null, ['label' => 'label.name.fr'])
289
            ->add('platforms', null, ['label' => 'label.platforms'])
290
            ->add('nbChart', null, ['label' => 'label.nbChart'])
291
            ->add('nbVideo', null, ['label' => 'label.nbVideo'])
292
            ->add('downloadUrl', null, ['label' => 'label.downloadUrl'])
293
            ->add('picture', null, ['label' => 'label.picture'])
294
            ->add('badge', null, ['label' => 'label.badge'])
295
            ->add('status', null, ['label' => 'label.status'])
296
            ->add('forum', null, ['label' => 'label.forum'])
297
            ->add('groups', null, ['label' => 'label.groups']);
298
    }
299
}
300