Passed
Push — develop ( a969ed...346e97 )
by BENARD
05:38
created

GameAdmin::getLibGame()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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