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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; |
11
|
|
|
use AppBundle\Entity\User; |
12
|
|
|
use AppBundle\Form\Type\UserType; |
13
|
|
|
use AppBundle\Form\Type\UserFilterType; |
14
|
|
|
use Symfony\Component\Form\FormInterface; |
15
|
|
|
use Doctrine\ORM\QueryBuilder; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* User controller. |
19
|
|
|
* |
20
|
|
|
* @Route("/admin/users") |
21
|
|
|
*/ |
22
|
|
|
class UserController extends Controller |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Lists all User entities. |
26
|
|
|
* |
27
|
|
|
* @Route("/", name="admin_users") |
28
|
|
|
* @Method("GET") |
29
|
|
|
* @Template() |
30
|
|
|
*/ |
31
|
|
|
public function indexAction() |
32
|
|
|
{ |
33
|
|
|
$em = $this->getDoctrine()->getManager(); |
34
|
|
|
$form = $this->createForm(new UserFilterType()); |
35
|
|
|
if (!is_null($response = $this->saveFilter($form, 'user', 'admin_users'))) { |
36
|
|
|
return $response; |
37
|
|
|
} |
38
|
|
|
$qb = $em->getRepository('AppBundle:User')->createQueryBuilder('u'); |
39
|
|
|
$paginator = $this->filter($form, $qb, 'user'); |
40
|
|
|
|
41
|
|
|
return array( |
42
|
|
|
'form' => $form->createView(), |
43
|
|
|
'paginator' => $paginator, |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Finds and displays a User entity. |
49
|
|
|
* |
50
|
|
|
* @Route("/{id}/show", name="admin_users_show", requirements={"id"="\d+"}) |
51
|
|
|
* @Method("GET") |
52
|
|
|
* @Template() |
53
|
|
|
*/ |
54
|
|
|
public function showAction(User $user) |
55
|
|
|
{ |
56
|
|
|
$deleteForm = $this->createDeleteForm($user->getId(), 'admin_users_delete'); |
57
|
|
|
|
58
|
|
|
return array( |
59
|
|
|
'user' => $user, |
60
|
|
|
'delete_form' => $deleteForm->createView(), |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Displays a form to create a new User entity. |
66
|
|
|
* |
67
|
|
|
* @Route("/new", name="admin_users_new") |
68
|
|
|
* @Method("GET") |
69
|
|
|
* @Template() |
70
|
|
|
*/ |
71
|
|
|
public function newAction() |
72
|
|
|
{ |
73
|
|
|
$user = new User(); |
74
|
|
|
$form = $this->createForm(new UserType(), $user); |
75
|
|
|
|
76
|
|
|
return array( |
77
|
|
|
'user' => $user, |
78
|
|
|
'form' => $form->createView(), |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Creates a new User entity. |
84
|
|
|
* |
85
|
|
|
* @Route("/create", name="admin_users_create") |
86
|
|
|
* @Method("POST") |
87
|
|
|
* @Template("AppBundle:User:new.html.twig") |
88
|
|
|
*/ |
89
|
|
|
public function createAction(Request $request) |
90
|
|
|
{ |
91
|
|
|
$user = new User(); |
92
|
|
|
$form = $this->createForm(new UserType(), $user); |
93
|
|
View Code Duplication |
if ($form->handleRequest($request)->isValid()) { |
|
|
|
|
94
|
|
|
$user->setEnabled(true); |
95
|
|
|
$userManager = $this->get('fos_user.user_manager'); |
96
|
|
|
$userManager->updateUser($user); |
97
|
|
|
|
98
|
|
|
return $this->redirect( |
99
|
|
|
$this->generateUrl( |
100
|
|
|
'admin_users_show', |
101
|
|
|
array('id' => $user->getId()) |
102
|
|
|
) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return array( |
107
|
|
|
'user' => $user, |
108
|
|
|
'form' => $form->createView(), |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Displays a form to edit an existing User entity. |
114
|
|
|
* |
115
|
|
|
* @Route("/{id}/edit", name="admin_users_edit", requirements={"id"="\d+"}) |
116
|
|
|
* @Method("GET") |
117
|
|
|
* @Template() |
118
|
|
|
*/ |
119
|
|
|
public function editAction(User $user) |
120
|
|
|
{ |
121
|
|
|
$editForm = $this->createForm(new UserType(), $user, array( |
122
|
|
|
'action' => $this->generateUrl('admin_users_update', array('id' => $user->getId())), |
123
|
|
|
'method' => 'PUT', |
124
|
|
|
'passwordRequired' => false, |
125
|
|
|
'lockedRequired' => true |
126
|
|
|
)); |
127
|
|
|
$deleteForm = $this->createDeleteForm($user->getId(), 'admin_users_delete'); |
128
|
|
|
|
129
|
|
|
return array( |
130
|
|
|
'user' => $user, |
131
|
|
|
'edit_form' => $editForm->createView(), |
132
|
|
|
'delete_form' => $deleteForm->createView(), |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Edits an existing User entity. |
138
|
|
|
* |
139
|
|
|
* @Route("/{id}/update", name="admin_users_update", requirements={"id"="\d+"}) |
140
|
|
|
* @Method("PUT") |
141
|
|
|
* @Template("AppBundle:User:edit.html.twig") |
142
|
|
|
*/ |
143
|
|
|
public function updateAction(User $user, Request $request) |
144
|
|
|
{ |
145
|
|
|
$editForm = $this->createForm(new UserType(), $user, array( |
146
|
|
|
'action' => $this->generateUrl('admin_users_update', array('id' => $user->getId())), |
147
|
|
|
'method' => 'PUT', |
148
|
|
|
'passwordRequired' => false, |
149
|
|
|
'lockedRequired' => true |
150
|
|
|
)); |
151
|
|
View Code Duplication |
if ($editForm->handleRequest($request)->isValid()) { |
|
|
|
|
152
|
|
|
$userManager = $this->get('fos_user.user_manager'); |
153
|
|
|
$userManager->updateUser($user); |
154
|
|
|
|
155
|
|
|
return $this->redirect($this->generateUrl('admin_users_edit', array('id' => $user->getId()))); |
156
|
|
|
} |
157
|
|
|
$deleteForm = $this->createDeleteForm($user->getId(), 'admin_users_delete'); |
158
|
|
|
|
159
|
|
|
return array( |
160
|
|
|
'user' => $user, |
161
|
|
|
'edit_form' => $editForm->createView(), |
162
|
|
|
'delete_form' => $deleteForm->createView(), |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Save order. |
169
|
|
|
* |
170
|
|
|
* @Route("/order/{field}/{type}", name="admin_users_sort") |
171
|
|
|
*/ |
172
|
|
|
public function sortAction($field, $type) |
173
|
|
|
{ |
174
|
|
|
$this->setOrder('user', $field, $type); |
175
|
|
|
|
176
|
|
|
return $this->redirect($this->generateUrl('admin_users')); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $name session name |
181
|
|
|
* @param string $field field name |
182
|
|
|
* @param string $type sort type ("ASC"/"DESC") |
183
|
|
|
*/ |
184
|
|
|
protected function setOrder($name, $field, $type = 'ASC') |
185
|
|
|
{ |
186
|
|
|
$this->getRequest()->getSession()->set('sort.' . $name, array('field' => $field, 'type' => $type)); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string $name |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
|
|
protected function getOrder($name) |
194
|
|
|
{ |
195
|
|
|
$session = $this->getRequest()->getSession(); |
|
|
|
|
196
|
|
|
|
197
|
|
|
return $session->has('sort.' . $name) ? $session->get('sort.' . $name) : null; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param QueryBuilder $qb |
202
|
|
|
* @param string $name |
203
|
|
|
*/ |
204
|
|
View Code Duplication |
protected function addQueryBuilderSort(QueryBuilder $qb, $name) |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
$alias = current($qb->getDQLPart('from'))->getAlias(); |
207
|
|
|
if (is_array($order = $this->getOrder($name))) { |
208
|
|
|
$qb->orderBy($alias . '.' . $order['field'], $order['type']); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Save filters |
214
|
|
|
* |
215
|
|
|
* @param FormInterface $form |
216
|
|
|
* @param string $name route/entity name |
217
|
|
|
* @param string $route route name, if different from entity name |
218
|
|
|
* @param array $params possible route parameters |
219
|
|
|
* @return Response |
220
|
|
|
*/ |
221
|
|
|
protected function saveFilter(FormInterface $form, $name, $route = null, array $params = null) |
222
|
|
|
{ |
223
|
|
|
$request = $this->getRequest(); |
|
|
|
|
224
|
|
|
$url = $this->generateUrl($route ?: $name, is_null($params) ? array() : $params); |
225
|
|
|
if ($request->query->has('submit-filter') && $form->handleRequest($request)->isValid()) { |
226
|
|
|
$request->getSession()->set('filter.' . $name, $request->query->get($form->getName())); |
227
|
|
|
|
228
|
|
|
return $this->redirect($url); |
229
|
|
|
} elseif ($request->query->has('reset-filter')) { |
230
|
|
|
$request->getSession()->set('filter.' . $name, null); |
231
|
|
|
|
232
|
|
|
return $this->redirect($url); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Filter form |
238
|
|
|
* |
239
|
|
|
* @param FormInterface $form |
240
|
|
|
* @param QueryBuilder $qb |
241
|
|
|
* @param string $name |
242
|
|
|
* @return \Knp\Component\Pager\Pagination\PaginationInterface |
243
|
|
|
*/ |
244
|
|
|
protected function filter(FormInterface $form, QueryBuilder $qb, $name) |
245
|
|
|
{ |
246
|
|
|
if (!is_null($values = $this->getFilter($name))) { |
247
|
|
|
if ($form->submit($values)->isValid()) { |
248
|
|
|
$this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
// possible sorting |
253
|
|
|
$this->addQueryBuilderSort($qb, $name); |
254
|
|
|
return $this->get('knp_paginator')->paginate($qb, $this->getRequest()->query->get('page', 1), 20); |
|
|
|
|
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Get filters from session |
259
|
|
|
* |
260
|
|
|
* @param string $name |
261
|
|
|
* @return array |
262
|
|
|
*/ |
263
|
|
|
protected function getFilter($name) |
264
|
|
|
{ |
265
|
|
|
return $this->getRequest()->getSession()->get('filter.' . $name); |
|
|
|
|
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Deletes a User entity. |
270
|
|
|
* |
271
|
|
|
* @Security("has_role('ROLE_SUPER_ADMIN')") |
272
|
|
|
* @Route("/{id}/delete", name="admin_users_delete", requirements={"id"="\d+"}) |
273
|
|
|
* @Method("DELETE") |
274
|
|
|
*/ |
275
|
|
|
public function deleteAction(User $user, Request $request) |
276
|
|
|
{ |
277
|
|
|
$form = $this->createDeleteForm($user->getId(), 'admin_users_delete'); |
278
|
|
|
if ($form->handleRequest($request)->isValid()) { |
279
|
|
|
$em = $this->getDoctrine()->getManager(); |
280
|
|
|
$em->remove($user); |
281
|
|
|
$em->flush(); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
return $this->redirect($this->generateUrl('admin_users')); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Create Delete form |
289
|
|
|
* |
290
|
|
|
* @param integer $id |
291
|
|
|
* @param string $route |
292
|
|
|
* @return \Symfony\Component\Form\Form |
293
|
|
|
*/ |
294
|
|
|
protected function createDeleteForm($id, $route) |
295
|
|
|
{ |
296
|
|
|
return $this->createFormBuilder(null, array('attr' => array('id' => 'delete'))) |
|
|
|
|
297
|
|
|
->setAction($this->generateUrl($route, array('id' => $id))) |
298
|
|
|
->setMethod('DELETE') |
299
|
|
|
->getForm() |
300
|
|
|
; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
} |
304
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.