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
|
|
View Code Duplication |
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
|
|
|
$form = $this->createFormBuilder($roles) |
|
|
|
|
43
|
|
|
->setAction($this->generateUrl('rolesAdmin')) |
44
|
|
|
->setMethod('POST') |
45
|
|
|
->add('roles', ChoiceType::class, array( |
46
|
|
|
'choices' => $roles, |
47
|
|
|
'choices_as_values' => true, |
48
|
|
|
'expanded' => true, |
49
|
|
|
'multiple' => true, |
50
|
|
|
'choice_value' => 'id', |
51
|
|
|
'label' => false, |
52
|
|
|
'choice_label' => 'id', |
53
|
|
|
) |
54
|
|
|
) |
55
|
|
|
->add('delete', SubmitType::class, array( |
56
|
|
|
'label' => 'Remove', |
57
|
|
|
'attr' => [ |
58
|
|
|
'class' => 'btn btn-xs btn-danger' |
59
|
|
|
], |
60
|
|
|
) |
61
|
|
|
) |
62
|
|
|
->getForm(); |
63
|
|
|
|
64
|
|
|
if ($request->getMethod() == 'POST') { |
65
|
|
|
$form->handleRequest($request); |
66
|
|
|
if ($form->isValid()) { |
67
|
|
|
$data = $form->getData(); |
68
|
|
|
foreach ($data['roles'] as $role) { |
69
|
|
|
$em->remove($role); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
try { |
73
|
|
|
$em->flush(); |
74
|
|
|
} catch (\Exception $e) { |
75
|
|
|
return $this->render( |
76
|
|
|
'AppBundle:admin:failure.html.twig', |
77
|
|
|
array( |
78
|
|
|
'message' => 'Deleting record is failed. Because record has relation to other records or another reasons.', |
79
|
|
|
) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->redirectToRoute('rolesAdmin'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return [ |
88
|
|
|
'roles' => $roles, |
89
|
|
|
'delete' => $form->createView(), |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $id |
95
|
|
|
* @param $action |
96
|
|
|
* @param Request $request |
97
|
|
|
* @Route("/role/{action}/{id}", name="roleEdit", |
98
|
|
|
* defaults={"id": 0}, |
99
|
|
|
* requirements={ |
100
|
|
|
* "action": "new|edit", |
101
|
|
|
* "id": "\d+" |
102
|
|
|
* }) |
103
|
|
|
* @Method({"GET", "POST"}) |
104
|
|
|
* @Template("AppBundle:admin/form:role.html.twig") |
105
|
|
|
* |
106
|
|
|
* @return Response |
107
|
|
|
*/ |
108
|
|
|
public function editRoleAction($id, $action, Request $request) |
109
|
|
|
{ |
110
|
|
|
$em = $this->getDoctrine()->getManager(); |
111
|
|
|
if ($action == "edit") { |
112
|
|
|
$role = $em->getRepository('AppBundle:Role') |
113
|
|
|
->find($id); |
114
|
|
|
$title = 'Edit role id: '.$id; |
115
|
|
|
} |
116
|
|
|
else { |
117
|
|
|
$role = new Role(); |
118
|
|
|
$title = 'Create new role'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
$form = $this->createForm(RoleType::class, $role, [ |
123
|
|
|
'em' => $em, |
124
|
|
|
'action' => $this->generateUrl('roleEdit', ['action' => $action, 'id' => $id]), |
125
|
|
|
'method' => Request::METHOD_POST, |
126
|
|
|
]) |
127
|
|
|
->add('save', SubmitType::class, array('label' => 'Save')); |
128
|
|
|
|
129
|
|
|
if ($request->getMethod() == 'POST') { |
130
|
|
|
$form->handleRequest($request); |
131
|
|
|
if ($form->isValid()) { |
132
|
|
|
$em->persist($role); |
133
|
|
|
$em->flush(); |
134
|
|
|
|
135
|
|
|
return $this->redirectToRoute('rolesAdmin'); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return [ |
140
|
|
|
'title' => $title, |
141
|
|
|
'form' => $form->createView(), |
142
|
|
|
]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
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.