1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Controller; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\User; |
6
|
|
|
use AppBundle\Entity\DTO\Filter; |
7
|
|
|
use AppBundle\Form\FilterType; |
8
|
|
|
use AppBundle\Form\User\EditType; |
9
|
|
|
use AppBundle\Form\User\RegistrationType; |
10
|
|
|
use AppBundle\Form\User\ActivationType; |
11
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
12
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
13
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
15
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
16
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
|
19
|
|
|
class UserController extends Controller |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @Route("/users", name="users_list") |
23
|
|
|
* @Template("@App/users.html.twig") |
24
|
|
|
* |
25
|
|
|
* @param Request $request |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
|
|
public function listAction(Request $request) |
29
|
|
|
{ |
30
|
|
|
$em = $this->getDoctrine()->getManager(); |
31
|
|
|
$filter = new Filter; |
32
|
|
|
$filterForm = $this->createForm(FilterType::class, $filter) |
33
|
|
|
->add("Search", SubmitType::class, array( |
34
|
|
|
"attr" => array("class" => "fa fa-search") |
35
|
|
|
)); |
36
|
|
|
$filterForm->handleRequest($request); |
37
|
|
|
$users = $this->get('knp_paginator')->paginate( |
38
|
|
|
$em->getRepository(User::class)->selectUsersByParams($filter), |
39
|
|
|
$request->query->getInt('page', 1), |
40
|
|
|
10 |
41
|
|
|
); |
42
|
|
|
$activationForm = []; |
43
|
|
|
foreach ($users as $user) { |
44
|
|
|
$activationForm[$user->getId()] = $this->createForm(ActivationType::class, $user, [ |
45
|
|
|
'method' => "PUT", |
46
|
|
|
'action' => $this->generateUrl('activate_user', array('id' => $user->getId())), |
47
|
|
|
'validation_groups' => array('edit'), |
48
|
|
|
]) |
49
|
|
|
->createView(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return [ |
53
|
|
|
"users" => $users, |
54
|
|
|
"filterForm" => $filterForm->createView(), |
55
|
|
|
"activationForm" => $activationForm |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @Route("/user/activate/{id}", name="activate_user") |
61
|
|
|
* @Template("@App/add.html.twig") |
62
|
|
|
* |
63
|
|
|
* @Method("PUT") |
64
|
|
|
* @param Request $request |
65
|
|
|
* @param User $user |
66
|
|
|
* @return RedirectResponse |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function activationAction(Request $request, User $user) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$em = $this->getDoctrine()->getManager(); |
71
|
|
|
$form = $this->createForm(ActivationType::class, $user, [ |
72
|
|
|
'method' => "PUT", |
73
|
|
|
'action' => $this->generateUrl('activate_user', array('id' => $user->getId())), |
74
|
|
|
'validation_groups' => array('edit'), |
75
|
|
|
]); |
76
|
|
|
$form->handleRequest($request); |
77
|
|
|
if ($form->isValid()) { |
78
|
|
|
$em->persist($user); |
79
|
|
|
$em->flush(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->redirect($this->generateUrl("users_list")); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @Route("/user/add", name="add_user") |
87
|
|
|
* @Template("@App/add.html.twig") |
88
|
|
|
* |
89
|
|
|
* @param Request $request |
90
|
|
|
* @return array|RedirectResponse |
91
|
|
|
*/ |
92
|
|
|
public function addAction(Request $request) |
93
|
|
|
{ |
94
|
|
|
$em = $this->getDoctrine()->getManager(); |
95
|
|
|
$user = new User(); |
96
|
|
|
$form = $this->createForm(RegistrationType::class, $user, [ |
97
|
|
|
'action' => $this->generateUrl('add_user'), |
98
|
|
|
'validation_groups' => array('registration'), |
99
|
|
|
]); |
100
|
|
|
$form->handleRequest($request); |
101
|
|
|
if ($form->isSubmitted()) |
102
|
|
|
if ($form->isValid()) { |
103
|
|
|
$em->persist($user); |
104
|
|
|
$em->flush(); |
105
|
|
|
|
106
|
|
|
return $this->redirect($this->generateUrl("users_list")); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return ['form' => $form->createView()]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @Route("/user/edit/{id}", name="edit_user") |
114
|
|
|
* @Template("@App/add.html.twig") |
115
|
|
|
* |
116
|
|
|
* @param Request $request |
117
|
|
|
* @param User $user |
118
|
|
|
* @return array|RedirectResponse |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
public function editAction(Request $request, User $user) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$em = $this->getDoctrine()->getManager(); |
123
|
|
|
$form = $this->createForm(EditType::class, $user, [ |
124
|
|
|
'action' => $this->generateUrl('edit_user', array('id' => $user->getId())), |
125
|
|
|
'method' => 'POST', |
126
|
|
|
'validation_groups' => array('edit'), |
127
|
|
|
]); |
128
|
|
|
$form->handleRequest($request); |
129
|
|
|
if ($form->isValid()) { |
130
|
|
|
$em->persist($user); |
131
|
|
|
$em->flush(); |
132
|
|
|
|
133
|
|
|
return $this->redirect($this->generateUrl("users_list")); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return ['form' => $form->createView()]; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
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.