|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* UserController controller des utilisateurs. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP Version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @author Quétier Laurent <[email protected]> |
|
8
|
|
|
* @copyright 2014 Dev-Int GLSR |
|
9
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
10
|
|
|
* |
|
11
|
|
|
* @version since 1.0.0 |
|
12
|
|
|
* |
|
13
|
|
|
* @link https://github.com/Dev-Int/glsr |
|
14
|
|
|
*/ |
|
15
|
|
|
namespace AppBundle\Controller; |
|
16
|
|
|
|
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use AppBundle\Controller\AbstractController; |
|
19
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
20
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
21
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
|
22
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; |
|
23
|
|
|
use AppBundle\Entity\User; |
|
24
|
|
|
use AppBundle\Form\Type\UserType; |
|
25
|
|
|
use AppBundle\Form\Type\UserFilterType; |
|
26
|
|
|
use Symfony\Component\Form\FormInterface; |
|
27
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* User controller. |
|
31
|
|
|
* |
|
32
|
|
|
* @category Controller |
|
33
|
|
|
* |
|
34
|
|
|
* @Route("/admin/users") |
|
35
|
|
|
*/ |
|
36
|
|
|
class UserController extends AbstractController |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* Lists all User entities. |
|
40
|
|
|
* |
|
41
|
|
|
* @Route("/", name="admin_users") |
|
42
|
|
|
* @Method("GET") |
|
43
|
|
|
* @Template() |
|
44
|
|
|
*/ |
|
45
|
|
|
public function indexAction() |
|
46
|
|
|
{ |
|
47
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
48
|
|
|
$form = $this->createForm(new UserFilterType()); |
|
49
|
|
|
if (!is_null($response = $this->saveFilter($form, 'user', 'admin_users'))) { |
|
50
|
|
|
return $response; |
|
51
|
|
|
} |
|
52
|
|
|
$qb = $em->getRepository('AppBundle:User')->createQueryBuilder('u'); |
|
53
|
|
|
$paginator = $this->filter($form, $qb, 'user'); |
|
54
|
|
|
|
|
55
|
|
|
return array( |
|
56
|
|
|
'form' => $form->createView(), |
|
57
|
|
|
'paginator' => $paginator, |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Finds and displays a User entity. |
|
63
|
|
|
* |
|
64
|
|
|
* @Route("/{id}/show", name="admin_users_show", requirements={"id"="\d+"}) |
|
65
|
|
|
* @Method("GET") |
|
66
|
|
|
* @Template() |
|
67
|
|
|
*/ |
|
68
|
|
|
public function showAction(User $user) |
|
69
|
|
|
{ |
|
70
|
|
|
$deleteForm = $this->createDeleteForm($user->getId(), 'admin_users_delete'); |
|
71
|
|
|
|
|
72
|
|
|
return array( |
|
73
|
|
|
'user' => $user, |
|
74
|
|
|
'delete_form' => $deleteForm->createView(), |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Displays a form to create a new User entity. |
|
80
|
|
|
* |
|
81
|
|
|
* @Route("/new", name="admin_users_new") |
|
82
|
|
|
* @Method("GET") |
|
83
|
|
|
* @Template() |
|
84
|
|
|
*/ |
|
85
|
|
|
public function newAction() |
|
86
|
|
|
{ |
|
87
|
|
|
$user = new User(); |
|
88
|
|
|
$form = $this->createForm(new UserType(), $user); |
|
89
|
|
|
|
|
90
|
|
|
return array( |
|
91
|
|
|
'user' => $user, |
|
92
|
|
|
'form' => $form->createView(), |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Creates a new User entity. |
|
98
|
|
|
* |
|
99
|
|
|
* @Route("/create", name="admin_users_create") |
|
100
|
|
|
* @Method("POST") |
|
101
|
|
|
* @Template("AppBundle:User:new.html.twig") |
|
102
|
|
|
*/ |
|
103
|
|
|
public function createAction(Request $request) |
|
104
|
|
|
{ |
|
105
|
|
|
$user = new User(); |
|
106
|
|
|
$form = $this->createForm(new UserType(), $user); |
|
107
|
|
|
if ($form->handleRequest($request)->isValid()) { |
|
108
|
|
|
$user->setEnabled(true); |
|
109
|
|
|
$userManager = $this->get('fos_user.user_manager'); |
|
110
|
|
|
$userManager->updateUser($user); |
|
111
|
|
|
|
|
112
|
|
|
return $this->redirectToRoute('admin_users_show', array('id', $user->getId())); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return array( |
|
116
|
|
|
'user' => $user, |
|
117
|
|
|
'form' => $form->createView(), |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Displays a form to edit an existing User entity. |
|
123
|
|
|
* |
|
124
|
|
|
* @Route("/{id}/edit", name="admin_users_edit", requirements={"id"="\d+"}) |
|
125
|
|
|
* @Method("GET") |
|
126
|
|
|
* @Template() |
|
127
|
|
|
*/ |
|
128
|
|
|
public function editAction(User $user) |
|
129
|
|
|
{ |
|
130
|
|
|
$editForm = $this->createForm(new UserType(), $user, array( |
|
131
|
|
|
'action' => $this->generateUrl('admin_users_update', array('id' => $user->getId())), |
|
132
|
|
|
'method' => 'PUT', |
|
133
|
|
|
'passwordRequired' => false, |
|
134
|
|
|
'lockedRequired' => true |
|
135
|
|
|
)); |
|
136
|
|
|
$deleteForm = $this->createDeleteForm($user->getId(), 'admin_users_delete'); |
|
137
|
|
|
|
|
138
|
|
|
return array( |
|
139
|
|
|
'user' => $user, |
|
140
|
|
|
'edit_form' => $editForm->createView(), |
|
141
|
|
|
'delete_form' => $deleteForm->createView(), |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Edits an existing User entity. |
|
147
|
|
|
* |
|
148
|
|
|
* @Route("/{id}/update", name="admin_users_update", requirements={"id"="\d+"}) |
|
149
|
|
|
* @Method("PUT") |
|
150
|
|
|
* @Template("AppBundle:User:edit.html.twig") |
|
151
|
|
|
*/ |
|
152
|
|
View Code Duplication |
public function updateAction(User $user, Request $request) |
|
|
|
|
|
|
153
|
|
|
{ |
|
154
|
|
|
$editForm = $this->createForm(new UserType(), $user, array( |
|
155
|
|
|
'action' => $this->generateUrl('admin_users_update', array('id' => $user->getId())), |
|
156
|
|
|
'method' => 'PUT', |
|
157
|
|
|
'passwordRequired' => false, |
|
158
|
|
|
'lockedRequired' => true |
|
159
|
|
|
)); |
|
160
|
|
|
if ($editForm->handleRequest($request)->isValid()) { |
|
161
|
|
|
$userManager = $this->get('fos_user.user_manager'); |
|
162
|
|
|
$userManager->updateUser($user); |
|
163
|
|
|
|
|
164
|
|
|
return $this->redirectToRoute('admin_users_edit', array('id' => $user->getId())); |
|
165
|
|
|
} |
|
166
|
|
|
$deleteForm = $this->createDeleteForm($user->getId(), 'admin_users_delete'); |
|
167
|
|
|
|
|
168
|
|
|
return array( |
|
169
|
|
|
'user' => $user, |
|
170
|
|
|
'edit_form' => $editForm->createView(), |
|
171
|
|
|
'delete_form' => $deleteForm->createView(), |
|
172
|
|
|
); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Save order. |
|
178
|
|
|
* |
|
179
|
|
|
* @Route("/order/{field}/{type}", name="admin_users_sort") |
|
180
|
|
|
*/ |
|
181
|
|
|
public function sortAction($field, $type) |
|
182
|
|
|
{ |
|
183
|
|
|
$this->setOrder('user', $field, $type); |
|
184
|
|
|
|
|
185
|
|
|
return $this->redirectToRoute('admin_users'); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Save filters |
|
190
|
|
|
* |
|
191
|
|
|
* @param FormInterface $form |
|
192
|
|
|
* @param string $name route/entity name |
|
193
|
|
|
* @param string $route route name, if different from entity name |
|
194
|
|
|
* @param Request $request Request |
|
195
|
|
|
* @param array $params possible route parameters |
|
196
|
|
|
* @return Response |
|
197
|
|
|
*/ |
|
198
|
|
|
protected function saveFilter( |
|
199
|
|
|
FormInterface $form, |
|
200
|
|
|
$name, |
|
201
|
|
|
$route = null, |
|
202
|
|
|
Request $request = null, |
|
203
|
|
|
array $params = null |
|
204
|
|
|
) { |
|
205
|
|
|
$url = $this->generateUrl($route ?: $name, is_null($params) ? array() : $params); |
|
206
|
|
|
if (isset($request)) { |
|
207
|
|
|
if ($request->query->has('submit-filter') && $form->handleRequest($request)->isValid()) { |
|
208
|
|
|
$request->getSession()->set('filter.' . $name, $request->query->get($form->getName())); |
|
209
|
|
|
|
|
210
|
|
|
return $this->redirect($url); |
|
211
|
|
|
} elseif ($request->query->has('reset-filter')) { |
|
212
|
|
|
$request->getSession()->set('filter.' . $name, null); |
|
213
|
|
|
|
|
214
|
|
|
return $this->redirect($url); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Filter form |
|
221
|
|
|
* |
|
222
|
|
|
* @param FormInterface $form |
|
223
|
|
|
* @param QueryBuilder $qb |
|
224
|
|
|
* @param string $name |
|
225
|
|
|
* @return \Knp\Component\Pager\Pagination\PaginationInterface |
|
226
|
|
|
*/ |
|
227
|
|
|
protected function filter(FormInterface $form, QueryBuilder $qb, $name) |
|
228
|
|
|
{ |
|
229
|
|
|
if (!is_null($values = $this->getFilter($name))) { |
|
230
|
|
|
if ($form->submit($values)->isValid()) { |
|
231
|
|
|
$this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
// possible sorting |
|
236
|
|
|
$this->addQueryBuilderSort($qb, $name); |
|
237
|
|
|
return $this->get('knp_paginator')->paginate($qb, $this->getRequest()->query->get('page', 1), 20); |
|
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Get filters from session |
|
242
|
|
|
* |
|
243
|
|
|
* @param string $name |
|
244
|
|
|
* @return array |
|
245
|
|
|
*/ |
|
246
|
|
|
protected function getFilter($name) |
|
247
|
|
|
{ |
|
248
|
|
|
return $this->getRequest()->getSession()->get('filter.' . $name); |
|
|
|
|
|
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Deletes a User entity. |
|
253
|
|
|
* |
|
254
|
|
|
* @Security("has_role('ROLE_SUPER_ADMIN')") |
|
255
|
|
|
* @Route("/{id}/delete", name="admin_users_delete", requirements={"id"="\d+"}) |
|
256
|
|
|
* @Method("DELETE") |
|
257
|
|
|
*/ |
|
258
|
|
|
public function deleteAction(User $user, Request $request) |
|
259
|
|
|
{ |
|
260
|
|
|
$form = $this->createDeleteForm($user->getId(), 'admin_users_delete'); |
|
261
|
|
|
if ($form->handleRequest($request)->isValid()) { |
|
262
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
263
|
|
|
$em->remove($user); |
|
264
|
|
|
$em->flush(); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
return $this->redirectToRoute('admin_users'); |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|
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.