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

GroupAdmin::configureActionButtons()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 11
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\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('copy-with-lib-chart', $this->getRouterIdParameter() . '/copy-with-lib-chart')
44
            ->add('add-lib-chart', $this->getRouterIdParameter() . '/add-lib-chart')
45
            ->add('set-video-proof-only', $this->getRouterIdParameter() . '/set-video-proof-only');
46
    }
47
48
    public function configureActionButtons(array $buttonList, string $action, ?object $object = null): array
49
    {
50
        if (in_array($action, ['show', 'edit', 'acl']) && $object) {
51
            $buttonList['add-lib-chart'] = [
52
                'template' => '@VideoGamesRecordsCore/Admin/ActionButton/btn.add_lib_chart.html.twig',
53
            ];
54
            $buttonList['set-video-proof-only'] = [
55
                'template' => '@VideoGamesRecordsCore/Admin/ActionButton/btn.set_video_proof_only.html.twig',
56
            ];
57
        }
58
        return $buttonList;
59
    }
60
61
    /**
62
    /**
63
     * @param FormMapper $form
64
     */
65
    protected function configureFormFields(FormMapper $form): void
66
    {
67
        $gameOptions = [];
68
        if (($this->hasRequest()) && ($this->isCurrentRoute('create'))) {
69
            $idGame = $this->getRequest()->get('idGame', null);
70
            if ($idGame !== null) {
71
                $this->getRequest()->getSession()->set('vgrcorebundle_admin_group.idGame', $idGame);
72
            }
73
74
            if ($this->getRequest()->getSession()->has('vgrcorebundle_admin_group.idGame')) {
75
                $idGame = $this->getRequest()->getSession()->get('vgrcorebundle_admin_group.idGame');
76
                $entityManager = $this->getModelManager()
77
                    ->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

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