1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Controller; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\User; |
6
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
7
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
10
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
13
|
|
|
|
14
|
|
|
class UserController extends Controller |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @Route("/users", name="users_list") |
18
|
|
|
* @Template("@App/users.html.twig") |
19
|
|
|
* |
20
|
|
|
* @return array |
21
|
|
|
*/ |
22
|
|
|
public function usersListAction(Request $request) |
23
|
|
|
{ |
24
|
|
|
$em = $this->getDoctrine()->getManager(); |
25
|
|
|
$users = $em->getRepository(User::class)->getUsersByParams($request->query); |
26
|
|
|
if(!$users) { |
27
|
|
|
return [ |
28
|
|
|
'error' => 'Nothing found!' |
29
|
|
|
]; |
30
|
|
|
} |
31
|
|
|
$paginator = $this->get('knp_paginator'); |
32
|
|
|
$pagination = $paginator->paginate($users, $request->query->getInt('page', 1), 10); |
33
|
|
|
return [ |
34
|
|
|
"users" => $pagination |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @Route("/user/activate/{id}", name="activate_user") |
40
|
|
|
* @Template("@App/add.html.twig") |
41
|
|
|
* |
42
|
|
|
* @param User $user |
43
|
|
|
* @return RedirectResponse |
44
|
|
|
*/ |
45
|
|
|
public function userActivateAction(User $user) |
46
|
|
|
{ |
47
|
|
|
$em = $this->getDoctrine()->getManager(); |
48
|
|
|
$user->setStatus($user->isEnabled() ? false : true); |
49
|
|
|
$em->persist($user); |
50
|
|
|
$em->flush(); |
51
|
|
|
|
52
|
|
|
return new RedirectResponse($this->generateUrl("users_list")); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @Route("/user/add", name="add_user") |
57
|
|
|
* @Template("@App/add.html.twig") |
58
|
|
|
* |
59
|
|
|
* @return array|RedirectResponse |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function userAddAction(Request $request) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$em = $this->getDoctrine()->getManager(); |
64
|
|
|
$user = new User(); |
65
|
|
|
$form = $this->createForm('AppBundle\Form\UserType', $user, [ |
66
|
|
|
'action' => $this->generateUrl('add_user'), |
67
|
|
|
'method' => 'POST', |
68
|
|
|
'validation_groups' => array('registration'), |
69
|
|
|
]) |
70
|
|
|
->add('Save', SubmitType::class, array( |
71
|
|
|
'attr' => ['class' => 'btn pull-right btn-warning'] |
72
|
|
|
)); |
73
|
|
|
$form->handleRequest($request); |
74
|
|
|
if ($form->isValid()) { |
75
|
|
|
$em->persist($user); |
76
|
|
|
$em->flush(); |
77
|
|
|
|
78
|
|
|
return new RedirectResponse($this->generateUrl("users_list")); |
79
|
|
|
} |
80
|
|
|
return ['form' => $form->createView()]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @Route("/user/edit/{id}", name="edit_user") |
85
|
|
|
* @Template("@App/add.html.twig") |
86
|
|
|
* |
87
|
|
|
* @return array|RedirectResponse |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
public function userEditAction(Request $request, User $user) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$em = $this->getDoctrine()->getManager(); |
92
|
|
|
$form = $this->createForm('AppBundle\Form\UserType', $user, [ |
93
|
|
|
'action' => $this->generateUrl('edit_user', array('id' => $user->getId())), |
94
|
|
|
'method' => 'POST', |
95
|
|
|
'validation_groups' => array('edit'), |
96
|
|
|
]) |
97
|
|
|
->add('Save', SubmitType::class, array( |
98
|
|
|
'attr' => ['class' => 'btn pull-right btn-warning'] |
99
|
|
|
)); |
100
|
|
|
$form->handleRequest($request); |
101
|
|
|
if ($form->isValid()) { |
102
|
|
|
$em->persist($user); |
103
|
|
|
$em->flush(); |
104
|
|
|
|
105
|
|
|
return new RedirectResponse($this->generateUrl("users_list")); |
106
|
|
|
} |
107
|
|
|
return ['form' => $form->createView()]; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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.