UserAdmin::configureListFields()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 46
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 26
nc 4
nop 1
dl 0
loc 46
rs 9.504
c 0
b 0
f 0
1
<?php
2
3
namespace PiedWeb\CMSBundle\Extension\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\Form\Type\DatePickerType;
10
use Sonata\Form\Type\ImmutableArrayType;
11
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
12
use Symfony\Component\Form\Extension\Core\Type\TextType;
13
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
14
15
class UserAdmin extends AbstractAdmin
16
{
17
    use AdminTrait;
0 ignored issues
show
Bug introduced by
The trait PiedWeb\CMSBundle\Extension\Admin\AdminTrait requires the property $query which is not provided by PiedWeb\CMSBundle\Extension\Admin\UserAdmin.
Loading history...
18
19
    protected $datagridValues = [
20
        '_page' => 1,
21
        '_sort_order' => 'DESC',
22
        '_sort_by' => 'createdAt',
23
    ];
24
25
    protected function exists(string $name): bool
26
    {
27
        return method_exists($this->userClass, 'get'.$name);
28
    }
29
30
    protected function configureFormFields(FormMapper $formMapper): void
31
    {
32
        // Forbid edition of other admin account except for super admin
33
        if (($this->getSubject()->hasRole('ROLE_SUPER_ADMIN')
34
            && $this->getUser()->getId() !== $this->getSubject()->getId())) {
35
            throw new AccessDeniedException('u can\'t edit this user'); // TODO : do better
36
        }
37
38
        $now = new \DateTime();
39
40
        $formMapper
41
            ->with('admin.user.label.id', ['class' => 'col-md-4'])
42
            //->add('username')
43
            ->add('email', null, [
44
                'label' => 'admin.user.email.label',
45
            ])
46
            ->add('plainPassword', TextType::class, [
47
                'required' => (! $this->getSubject() || null === $this->getSubject()->getId()),
48
                'label' => 'admin.user.password.label',
49
            ])
50
            ->end();
51
52
        $formMapper
53
            ->with('admin.user.label.profile', ['class' => 'col-md-4']);
54
55
        if ($this->exists('DateOfBirth')) {
56
            $formMapper->add(
57
                'dateOfBirth',
58
                DatePickerType::class,
59
                [
60
                    'years' => range(1900, $now->format('Y')),
61
                    'dp_min_date' => '1-1-1900',
62
                    'dp_max_date' => $now->format('c'),
63
                    'required' => false,
64
                    'label' => 'admin.user.dateOfBirth.label',
65
                ]
66
            );
67
        }
68
        if ($this->exists('firstname')) {
69
            $formMapper->add(
70
                'firstname',
71
                TextType::class,
72
                [
73
                    'required' => false,
74
                    'label' => 'admin.user.firstname.label',
75
                ]
76
            );
77
        }
78
        if ($this->exists('lastname')) {
79
            $formMapper->add(
80
                'lastname',
81
                TextType::class,
82
                [
83
                    'required' => false,
84
                    'label' => 'admin.user.lastname.label',
85
                ]
86
            );
87
        }
88
        if ($this->exists('city')) {
89
            $formMapper->add(
90
                'city',
91
                TextType::class,
92
                [
93
                    'required' => false,
94
                    'label' => 'admin.user.city.label',
95
                ]
96
            );
97
        }
98
        if ($this->exists('phone')) {
99
            $formMapper->add(
100
                'phone',
101
                TextType::class,
102
                [
103
                    'required' => false,
104
                    'label' => 'admin.user.phone.label',
105
                ]
106
            );
107
        }
108
109
        $formMapper->end()
110
111
            ->with('admin.user.label.security', ['class' => 'col-md-4'])
112
            ->add('roles', ImmutableArrayType::class, [
113
                'label' => false,
114
                'keys' => [
115
                    ['0', ChoiceType::class, [
116
                        'required' => false,
117
                        'label' => 'admin.user.role.label',
118
                        'choices' => $this->getUser()->hasRole('ROLE_SUPER_ADMIN') ? [
119
                            'admin.user.role.super_admin' => 'ROLE_SUPER_ADMIN',
120
                            'admin.user.role.admin' => 'ROLE_ADMIN',
121
                            'admin.user.role.editor' => 'ROLE_EDITOR',
122
                            'admin.user.role.user' => 'ROLE_USER',
123
                        ] : [
124
                            'admin.user.role.admin' => 'ROLE_ADMIN',
125
                            'admin.user.role.editor' => 'ROLE_EDITOR',
126
                            'admin.user.role.user' => 'ROLE_USER',
127
                        ],
128
                    ]],
129
                ],
130
            ])
131
            ->end();
132
    }
133
134
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
135
    {
136
        $datagridMapper->add('id')
137
            ->add('email')
138
            //->add('groups')
139
        ;
140
    }
141
142
    protected function configureListFields(ListMapper $listMapper)
143
    {
144
        $listMapper
145
            ->add('email', null, [
146
                'label' => 'admin.user.email.label',
147
            ]);
148
        if ($this->exists('firstname')) {
149
            $listMapper->add(
150
                'firstname',
151
                TextType::class,
152
                [
153
                    'editable' => true,
154
                    'label' => 'admin.user.firstname.label',
155
                ]
156
            );
157
        }
158
        if ($this->exists('lastname')) {
159
            $listMapper->add(
160
                'lastname',
161
                TextType::class,
162
                [
163
                    'editable' => true,
164
                    'label' => 'admin.user.lastname.label',
165
                ]
166
            );
167
        }
168
169
        /*
170
* todo
171
        $listMapper->add('roles[0]', null, [
172
                'label' => 'admin.user.role.label',
173
            ]);
174
        /**/
175
        $listMapper
176
            ->add('createdAt', null, [
177
                'editable' => true,
178
                'label' => 'admin.user.createdAt.label',
179
            ])
180
            ->add('_action', null, [
181
                'actions' => [
182
                    'edit' => [],
183
                    'delete' => [],
184
                ],
185
                'row_align' => 'right',
186
                'header_class' => 'text-right',
187
                'label' => 'admin.action',
188
            ]);
189
    }
190
}
191