1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
7
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
10
|
|
|
use AppBundle\Entity\Group; |
11
|
|
|
use AppBundle\Form\Type\GroupType; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Group controller. |
15
|
|
|
* |
16
|
|
|
* @Route("/admin/groups") |
17
|
|
|
*/ |
18
|
|
|
class GroupController extends Controller |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Lists all Group entities. |
22
|
|
|
* |
23
|
|
|
* @Route("/", name="admin_groups") |
24
|
|
|
* @Method("GET") |
25
|
|
|
* @Template() |
26
|
|
|
*/ |
27
|
|
|
public function indexAction() |
28
|
|
|
{ |
29
|
|
|
$em = $this->getDoctrine()->getManager(); |
30
|
|
|
$entities = $em->getRepository('AppBundle:Group')->findAll(); |
31
|
|
|
|
32
|
|
|
return array( |
33
|
|
|
'entities' => $entities, |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Finds and displays a Group entity. |
39
|
|
|
* |
40
|
|
|
* @Route("/{id}/show", name="admin_groups_show", requirements={"id"="\d+"}) |
41
|
|
|
* @Method("GET") |
42
|
|
|
* @Template() |
43
|
|
|
*/ |
44
|
|
|
public function showAction(Group $group) |
45
|
|
|
{ |
46
|
|
|
$deleteForm = $this->createDeleteForm($group->getId(), 'admin_groups_delete'); |
47
|
|
|
|
48
|
|
|
return array( |
49
|
|
|
'group' => $group, |
50
|
|
|
'delete_form' => $deleteForm->createView(), |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Displays a form to create a new Group entity. |
56
|
|
|
* |
57
|
|
|
* @Route("/new", name="admin_groups_new") |
58
|
|
|
* @Method("GET") |
59
|
|
|
* @Template() |
60
|
|
|
*/ |
61
|
|
|
public function newAction() |
62
|
|
|
{ |
63
|
|
|
$group = new Group(); |
64
|
|
|
$form = $this->createForm(new GroupType(), $group); |
65
|
|
|
$form->add('roles', 'choice', array( |
66
|
|
|
'choices' => $this->getExistingRoles(), |
67
|
|
|
'data' => $group->getRoles(), |
68
|
|
|
'label' => 'Roles', |
69
|
|
|
'translation_domain' => 'admin', |
70
|
|
|
'expanded' => true, |
71
|
|
|
'multiple' => true, |
72
|
|
|
'mapped' => true, |
73
|
|
|
)); |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
return array( |
77
|
|
|
'group' => $group, |
78
|
|
|
'form' => $form->createView(), |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Creates a new Group entity. |
84
|
|
|
* |
85
|
|
|
* @Route("/create", name="admin_groups_create") |
86
|
|
|
* @Method("POST") |
87
|
|
|
* @Template("AppBundle:Group:new.html.twig") |
88
|
|
|
*/ |
89
|
|
|
public function createAction(Request $request) |
90
|
|
|
{ |
91
|
|
|
$group = new Group(); |
92
|
|
|
$form = $this->createForm(new GroupType(), $group); |
93
|
|
|
$form->add('roles', 'choice', array( |
94
|
|
|
'choices' => $this->getExistingRoles(), |
95
|
|
|
'data' => $group->getRoles(), |
96
|
|
|
'label' => 'Roles', |
97
|
|
|
'expanded' => true, |
98
|
|
|
'multiple' => true, |
99
|
|
|
'mapped' => true, |
100
|
|
|
)); |
101
|
|
|
if ($form->handleRequest($request)->isValid()) { |
102
|
|
|
$em = $this->getDoctrine()->getManager(); |
103
|
|
|
$em->persist($group); |
104
|
|
|
$em->flush(); |
105
|
|
|
|
106
|
|
|
return $this->redirect( |
107
|
|
|
$this->generateUrl( |
108
|
|
|
'admin_groups_show', |
109
|
|
|
array('id' => $group->getId()) |
110
|
|
|
) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return array( |
115
|
|
|
'group' => $group, |
116
|
|
|
'form' => $form->createView(), |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Displays a form to edit an existing Group entity. |
122
|
|
|
* |
123
|
|
|
* @Route("/{id}/edit", name="admin_groups_edit", requirements={"id"="\d+"}) |
124
|
|
|
* @Method("GET") |
125
|
|
|
* @Template() |
126
|
|
|
*/ |
127
|
|
|
public function editAction(Group $group) |
128
|
|
|
{ |
129
|
|
|
$editForm = $this->createForm( |
130
|
|
|
new GroupType(), |
131
|
|
|
$group, |
132
|
|
|
array( |
133
|
|
|
'action' => $this->generateUrl( |
134
|
|
|
'admin_groups_update', |
135
|
|
|
array('id' => $group->getId()) |
136
|
|
|
), |
137
|
|
|
'method' => 'PUT', |
138
|
|
|
) |
139
|
|
|
); |
140
|
|
|
$editForm->add('roles', 'choice', array( |
141
|
|
|
'choices' => $this->getExistingRoles(), |
142
|
|
|
'data' => $group->getRoles(), |
143
|
|
|
'label' => 'Roles', |
144
|
|
|
'expanded' => true, |
145
|
|
|
'multiple' => true, |
146
|
|
|
'mapped' => true, |
147
|
|
|
)); |
148
|
|
|
$deleteForm = $this->createDeleteForm( |
149
|
|
|
$group->getId(), |
150
|
|
|
'admin_groups_delete' |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
return array( |
154
|
|
|
'group' => $group, |
155
|
|
|
'edit_form' => $editForm->createView(), |
156
|
|
|
'delete_form' => $deleteForm->createView(), |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Edits an existing Group entity. |
162
|
|
|
* |
163
|
|
|
* @Route("/{id}/update", name="admin_groups_update", requirements={"id"="\d+"}) |
164
|
|
|
* @Method("PUT") |
165
|
|
|
* @Template("AppBundle:Group:edit.html.twig") |
166
|
|
|
*/ |
167
|
|
|
public function updateAction(Group $group, Request $request) |
168
|
|
|
{ |
169
|
|
|
$editForm = $this->createForm(new GroupType(), $group, array( |
170
|
|
|
'action' => $this->generateUrl('admin_groups_update', array('id' => $group->getId())), |
171
|
|
|
'method' => 'PUT', |
172
|
|
|
)); |
173
|
|
|
$editForm->add('roles', 'choice', array( |
174
|
|
|
'choices' => $this->getExistingRoles(), |
175
|
|
|
'data' => $group->getRoles(), |
176
|
|
|
'label' => 'Roles', |
177
|
|
|
'expanded' => true, |
178
|
|
|
'multiple' => true, |
179
|
|
|
'mapped' => true, |
180
|
|
|
)); |
181
|
|
|
if ($editForm->handleRequest($request)->isValid()) { |
182
|
|
|
$this->getDoctrine()->getManager()->flush(); |
183
|
|
|
|
184
|
|
|
return $this->redirect($this->generateUrl('admin_groups_edit', array('id' => $group->getId()))); |
185
|
|
|
} |
186
|
|
|
$deleteForm = $this->createDeleteForm($group->getId(), 'admin_groups_delete'); |
187
|
|
|
|
188
|
|
|
return array( |
189
|
|
|
'group' => $group, |
190
|
|
|
'edit_form' => $editForm->createView(), |
191
|
|
|
'delete_form' => $deleteForm->createView(), |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Deletes a Group entity. |
197
|
|
|
* |
198
|
|
|
* @Route("/{id}/delete", name="admin_groups_delete", requirements={"id"="\d+"}) |
199
|
|
|
* @Method("DELETE") |
200
|
|
|
*/ |
201
|
|
|
public function deleteAction(Group $group, Request $request) |
202
|
|
|
{ |
203
|
|
|
$form = $this->createDeleteForm($group->getId(), 'admin_groups_delete'); |
204
|
|
|
|
205
|
|
|
// @TODO refactoriser dans le modèle |
206
|
|
|
$em = $this->getDoctrine()->getManager(); |
207
|
|
|
$users = $group->getUsers(); |
208
|
|
|
foreach ($users as $user) { |
209
|
|
|
$user->getGroups()->removeElement($group); |
210
|
|
|
} |
211
|
|
|
$em->flush(); |
212
|
|
|
|
213
|
|
|
$this->get('fos_user.group_manager')->deleteGroup($group); |
214
|
|
|
|
215
|
|
|
if ($form->handleRequest($request)->isValid()) { |
216
|
|
|
$em = $this->getDoctrine()->getManager(); |
217
|
|
|
$em->remove($group); |
218
|
|
|
$em->flush(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $this->redirect($this->generateUrl('admin_groups')); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Create Delete form |
226
|
|
|
* |
227
|
|
|
* @param integer $id |
228
|
|
|
* @param string $route |
229
|
|
|
* @return \Symfony\Component\Form\Form |
230
|
|
|
*/ |
231
|
|
|
protected function createDeleteForm($id, $route) |
232
|
|
|
{ |
233
|
|
|
return $this->createFormBuilder(null, array('attr' => array('id' => 'delete'))) |
234
|
|
|
->setAction($this->generateUrl($route, array('id' => $id))) |
235
|
|
|
->setMethod('DELETE') |
236
|
|
|
->getForm() |
237
|
|
|
; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Get the existing roles |
242
|
|
|
* |
243
|
|
|
* @return array Array of roles |
244
|
|
|
*/ |
245
|
|
|
private function getExistingRoles() |
246
|
|
|
{ |
247
|
|
|
$roleHierarchy = $this->container->getParameter('security.role_hierarchy.roles'); |
248
|
|
|
$roles = array_keys($roleHierarchy); |
249
|
|
|
|
250
|
|
|
foreach ($roles as $role) { |
251
|
|
|
$theRoles[$role] = $role; |
|
|
|
|
252
|
|
|
} |
253
|
|
|
return $theRoles; |
|
|
|
|
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.