1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Controller\Admin; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\Role; |
6
|
|
|
use AppBundle\Form\Type\RoleType; |
7
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
11
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
12
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class AdminController |
17
|
|
|
* @package AppBundle\Controller\Admin |
18
|
|
|
* @Route("/admin") |
19
|
|
|
*/ |
20
|
|
|
class RoleController extends Controller |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param Request $request |
24
|
|
|
* @param $page |
25
|
|
|
* @Method({"GET", "POST"}) |
26
|
|
|
* @Route("/roles/{pager}/{page}", name="rolesAdmin", |
27
|
|
|
* defaults={"pager": "page", "page": 1}, |
28
|
|
|
* requirements={ |
29
|
|
|
* "pager": "page", |
30
|
|
|
* "page": "[1-9]\d*", |
31
|
|
|
* }) |
32
|
|
|
* @Template("AppBundle:admin:roles.html.twig") |
33
|
|
|
* |
34
|
|
|
* @return Response |
35
|
|
|
*/ |
36
|
|
|
public function roleAction(Request $request, $page = 1) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$em = $this->getDoctrine()->getManager(); |
39
|
|
|
$roles = $em->getRepository('AppBundle:Role') |
40
|
|
|
->findAll(); |
41
|
|
|
|
42
|
|
|
return [ |
43
|
|
|
'roles' => $roles, |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $id |
49
|
|
|
* @param $action |
50
|
|
|
* @param Request $request |
51
|
|
|
* @Route("/role/{action}/{id}", name="roleEdit", |
52
|
|
|
* defaults={"id": 0}, |
53
|
|
|
* requirements={ |
54
|
|
|
* "action": "new|edit", |
55
|
|
|
* "id": "\d+" |
56
|
|
|
* }) |
57
|
|
|
* @Method({"GET", "POST"}) |
58
|
|
|
* @Template("AppBundle:admin/form:role.html.twig") |
59
|
|
|
* |
60
|
|
|
* @return Response |
61
|
|
|
*/ |
62
|
|
|
public function editRoleAction($id, $action, Request $request) |
63
|
|
|
{ |
64
|
|
|
$em = $this->getDoctrine()->getManager(); |
65
|
|
|
if ($action == "edit") { |
66
|
|
|
$role = $em->getRepository('AppBundle:Role') |
67
|
|
|
->find($id); |
68
|
|
|
$title = 'Edit role id: '.$id; |
69
|
|
|
} |
70
|
|
|
else { |
71
|
|
|
$role = new Role(); |
72
|
|
|
$title = 'Create new role'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
$form = $this->createForm(RoleType::class, $role, [ |
77
|
|
|
'em' => $em, |
78
|
|
|
'action' => $this->generateUrl('roleEdit', ['action' => $action, 'id' => $id]), |
79
|
|
|
'method' => Request::METHOD_POST, |
80
|
|
|
]) |
81
|
|
|
->add('save', SubmitType::class, array('label' => 'Save')); |
82
|
|
|
|
83
|
|
|
if ($request->getMethod() == 'POST') { |
84
|
|
|
$form->handleRequest($request); |
85
|
|
|
if ($form->isValid()) { |
86
|
|
|
$em->persist($role); |
87
|
|
|
$em->flush(); |
88
|
|
|
|
89
|
|
|
return $this->redirectToRoute('rolesAdmin'); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return [ |
94
|
|
|
'title' => $title, |
95
|
|
|
'form' => $form->createView(), |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param $id |
101
|
|
|
* @param Request $request |
102
|
|
|
* @Route("/role/delete/{id}", name="roleDelete", |
103
|
|
|
* requirements={ |
104
|
|
|
* "id": "\d+" |
105
|
|
|
* }) |
106
|
|
|
* @Method({"GET", "POST"}) |
107
|
|
|
* @Template("AppBundle:admin/form:delete.html.twig") |
108
|
|
|
* |
109
|
|
|
* @return Response |
110
|
|
|
*/ |
111
|
|
|
public function deleteRoleAction($id, Request $request) |
112
|
|
|
{ |
113
|
|
|
$em = $this->getDoctrine()->getManager(); |
114
|
|
|
$role = $em->getRepository('AppBundle:Role') |
115
|
|
|
->find($id); |
116
|
|
|
|
117
|
|
|
$countUsers = count($role->getUsers()); |
118
|
|
|
|
119
|
|
|
$message = 'You want to delete role "' . $role->getName() . '" (id: ' . $id . '). '; |
120
|
|
|
$message .= 'Related records: users (count: ' . $countUsers . '). '; |
121
|
|
|
|
122
|
|
View Code Duplication |
if ($countUsers == 0) { |
|
|
|
|
123
|
|
|
$message .= 'Are you sure, you want to continue?'; |
124
|
|
|
|
125
|
|
|
$form = $this->createFormBuilder($role) |
|
|
|
|
126
|
|
|
->setAction($this->generateUrl('roleDelete', ['id' => $id])) |
127
|
|
|
->setMethod('POST') |
128
|
|
|
->add('delete', SubmitType::class, array( |
129
|
|
|
'label' => 'Continue', |
130
|
|
|
'attr' => [ |
131
|
|
|
'class' => 'btn btn-default' |
132
|
|
|
], |
133
|
|
|
) |
134
|
|
|
) |
135
|
|
|
->getForm(); |
136
|
|
|
|
137
|
|
|
if ($request->getMethod() == 'POST') { |
138
|
|
|
$form->handleRequest($request); |
139
|
|
|
if ($form->isValid()) { |
140
|
|
|
$em->remove($role); |
141
|
|
|
$em->flush(); |
142
|
|
|
|
143
|
|
|
return $this->redirectToRoute('rolesAdmin'); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$renderedForm = $form->createView(); |
148
|
|
|
} |
149
|
|
|
else { |
150
|
|
|
$message .= 'You must to delete related records before.'; |
151
|
|
|
$renderedForm = ''; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return [ |
155
|
|
|
'message' => $message, |
156
|
|
|
'form' => $renderedForm, |
157
|
|
|
]; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.