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
|
|
|
$paginator = $this->get('knp_paginator'); |
27
|
|
|
$pagination = $paginator->paginate($users, $request->query->getInt('page', 1), 10); |
28
|
|
|
return [ |
29
|
|
|
"users" => $pagination |
30
|
|
|
]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @Route("/user/activate/{id}", name="activate_user") |
35
|
|
|
* @Template("@App/add.html.twig") |
36
|
|
|
* |
37
|
|
|
* @return RedirectResponse |
38
|
|
|
*/ |
39
|
|
|
public function userActivateAction(Request $request, User $user) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$em = $this->getDoctrine()->getManager(); |
42
|
|
|
if($user->isEnabled()){ |
43
|
|
|
$user->setStatus(false); |
|
|
|
|
44
|
|
|
} else { |
45
|
|
|
$user->setStatus(true); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
$em->persist($user); |
48
|
|
|
$em->flush(); |
49
|
|
|
|
50
|
|
|
return new RedirectResponse($this->generateUrl("users_list")); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @Route("/user/add", name="add_user") |
55
|
|
|
* @Template("@App/add.html.twig") |
56
|
|
|
* |
57
|
|
|
* @return array|RedirectResponse |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function userAddAction(Request $request) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$em = $this->getDoctrine()->getManager(); |
62
|
|
|
$user = new User(); |
63
|
|
|
$form = $this->createForm('AppBundle\Form\UserType', $user, [ |
64
|
|
|
'action' => $this->generateUrl('add_user'), |
65
|
|
|
'method' => 'POST', |
66
|
|
|
'validation_groups' => array('registration'), |
67
|
|
|
]) |
68
|
|
|
->add('Save', SubmitType::class, array( |
69
|
|
|
'attr' => ['class' => 'btn pull-right btn-warning'] |
70
|
|
|
)); |
71
|
|
|
$form->handleRequest($request); |
72
|
|
|
if ($form->isValid()) { |
73
|
|
|
$em->persist($user); |
74
|
|
|
$em->flush(); |
75
|
|
|
|
76
|
|
|
return new RedirectResponse($this->generateUrl("users_list")); |
77
|
|
|
} |
78
|
|
|
return ['form' => $form->createView()]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @Route("/user/edit/{id}", name="edit_user") |
83
|
|
|
* @Template("@App/add.html.twig") |
84
|
|
|
* |
85
|
|
|
* @return array|RedirectResponse |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
public function userEditAction(Request $request, User $user) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$em = $this->getDoctrine()->getManager(); |
90
|
|
|
$form = $this->createForm('AppBundle\Form\UserType', $user, [ |
91
|
|
|
'action' => $this->generateUrl('edit_user', array('id' => $user->getId())), |
92
|
|
|
'method' => 'POST', |
93
|
|
|
'validation_groups' => array('edit'), |
94
|
|
|
]) |
95
|
|
|
->add('Save', SubmitType::class, array( |
96
|
|
|
'attr' => ['class' => 'btn pull-right btn-warning'] |
97
|
|
|
)); |
98
|
|
|
$form->handleRequest($request); |
99
|
|
|
if ($form->isValid()) { |
100
|
|
|
$em->persist($user); |
101
|
|
|
$em->flush(); |
102
|
|
|
|
103
|
|
|
return new RedirectResponse($this->generateUrl("users_list")); |
104
|
|
|
} |
105
|
|
|
return ['form' => $form->createView()]; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.