Passed
Push — develop ( 346e97...18f462 )
by BENARD
05:02
created

GroupAdmin::getLibGroup()   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\ModelFilter;
14
use Sonata\Form\Type\CollectionType;
15
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
16
use Symfony\Component\Form\Extension\Core\Type\TextType;
17
use Symfony\Component\Intl\Locale;
18
19
class GroupAdmin extends AbstractAdmin
20
{
21
    protected $baseRouteName = 'vgrcorebundle_admin_group';
22
23
    /**
24
     * @return string
25
     */
26
    private function getLibGame(): string
27
    {
28
        $locale = Locale::getDefault();
29
        return ($locale == 'fr') ? 'libGameFr' : 'libGameEn';
30
    }
31
32
    /**
33
     * @param RouteCollectionInterface $collection
34
     */
35
    protected function configureRoutes(RouteCollectionInterface $collection): void
36
    {
37
        $collection->remove('export')
38
            ->add('copy', $this->getRouterIdParameter() . '/copy')
39
            ->add('copy-with-lib-chart', $this->getRouterIdParameter() . '/copy-with-lib-chart')
40
            ->add('add-lib-chart', $this->getRouterIdParameter() . '/add-lib-chart');
41
    }
42
43
    /**
44
     * @param array $actions
45
     * @return array|\mixed[][]
46
     */
47
    /*protected function configureDashboardActions(array $actions): array
48
    {
49
        $subject = $this->getSubject();
50
        $actions['addlibchart'] = [
51
            'label' => 'add_lib_chart',
52
            'url' => $this->generateUrl('add-lib-chart', ['id' => $subject->getId()]),
53
            'icon' => 'import',
54
            'translation_domain' => 'SonataAdminBundle',
55
        ];
56
        return $actions;
57
    }*/
58
59
    /**
60
     * @param FormMapper $form
61
     */
62
    protected function configureFormFields(FormMapper $form): void
63
    {
64
        $gameOptions = [];
65
        if (($this->hasRequest()) && ($this->isCurrentRoute('create'))) {
66
            $idGame = $this->getRequest()->get('idGame', null);
67
            if ($idGame !== null) {
68
                $this->getRequest()->getSession()->set('vgrcorebundle_admin_group.idGame', $idGame);
69
            }
70
71
            if ($this->getRequest()->getSession()->has('vgrcorebundle_admin_group.idGame')) {
72
                $idGame = $this->getRequest()->getSession()->get('vgrcorebundle_admin_group.idGame');
73
                $entityManager = $this->getModelManager()
74
                    ->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

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