|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Controller\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\District; |
|
6
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
10
|
|
|
use AppBundle\Form\DistrictType; |
|
11
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
|
12
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
|
13
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @Security("has_role('ROLE_MANAGER')") |
|
18
|
|
|
* @Route("/admin") |
|
19
|
|
|
*/ |
|
20
|
|
|
class AdminDistrictController extends Controller |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @Route("/districts", name="admin_districts") |
|
24
|
|
|
* @Method("GET") |
|
25
|
|
|
*/ |
|
26
|
1 |
|
public function districtsAction(Request $request) |
|
27
|
|
|
{ |
|
28
|
1 |
|
$districts = $this->getDoctrine()->getRepository('AppBundle:District')->findAll(); |
|
29
|
1 |
|
return $this->render('@App/admin/district/districts.html.twig', array('districts' => $districts)); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @Route("/district/show/{slug}", name="admin_district_show") |
|
34
|
|
|
* @Method("GET") |
|
35
|
|
|
* @ParamConverter("district", options={"mapping": {"slug": "slug"}}) |
|
36
|
|
|
*/ |
|
37
|
1 |
|
public function districtShowAction(District $district, Request $request) |
|
38
|
|
|
{ |
|
39
|
1 |
|
$deleteForm = $this->createDeleteForm($district); |
|
40
|
1 |
|
return $this->render('@App/admin/district/show_district.html.twig', array( |
|
41
|
1 |
|
'district' => $district, |
|
42
|
1 |
|
'delete_form' => $deleteForm->createView(), |
|
43
|
1 |
|
)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @Route("/district/new", name="admin_district_new") |
|
48
|
|
|
* @Method({"GET", "POST"}) |
|
49
|
|
|
* @Security("has_role('ROLE_ADMIN')") |
|
50
|
|
|
*/ |
|
51
|
1 |
View Code Duplication |
public function newDistrictAction(Request $request) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
1 |
|
$entityManager = $this->getDoctrine()->getManager(); |
|
54
|
1 |
|
$district = new District(); |
|
55
|
|
|
//$this->denyAccessUnlessGranted('create', $estate); |
|
|
|
|
|
|
56
|
1 |
|
$form = $this->createForm(DistrictType::class, $district)->add('saveAndCreateNew', SubmitType::class); |
|
57
|
1 |
|
$form->handleRequest($request); |
|
58
|
1 |
|
if ($form->isSubmitted() && $form->isValid()) { |
|
59
|
|
|
$entityManager->persist($district); |
|
60
|
|
|
$entityManager->flush(); |
|
61
|
|
|
$nextAction = $form->get('saveAndCreateNew')->isClicked() |
|
|
|
|
|
|
62
|
|
|
? 'admin_district_new' |
|
63
|
|
|
: 'admin_districts'; |
|
64
|
|
|
return $this->redirectToRoute($nextAction); |
|
65
|
|
|
} |
|
66
|
1 |
|
return $this->render('@App/admin/district/new_district.html.twig', array( |
|
67
|
1 |
|
'district' => $district, |
|
68
|
1 |
|
'form' => $form->createView(), |
|
69
|
1 |
|
)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @Route("/district/edit/{slug}", name="admin_district_edit") |
|
74
|
|
|
* @Method({"GET", "POST"}) |
|
75
|
|
|
* @Security("has_role('ROLE_ADMIN')") |
|
76
|
|
|
* @ParamConverter("district", options={"mapping": {"slug": "slug"}}) |
|
77
|
|
|
*/ |
|
78
|
1 |
View Code Duplication |
public function districtEditAction(District $district, Request $request) |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
1 |
|
$entityManager = $this->getDoctrine()->getManager(); |
|
81
|
1 |
|
$editForm = $this->createForm(DistrictType::class, $district); |
|
82
|
1 |
|
$deleteForm = $this->createDeleteForm($district); |
|
83
|
1 |
|
$editForm->handleRequest($request); |
|
84
|
1 |
|
if ($editForm->isSubmitted() && $editForm->isValid()) { |
|
85
|
|
|
$entityManager->persist($district); |
|
86
|
|
|
$entityManager->flush(); |
|
87
|
|
|
return $this->redirectToRoute('admin_districts'); |
|
88
|
|
|
} |
|
89
|
1 |
|
return $this->render('@App/admin/district/edit_district.html.twig', array( |
|
90
|
1 |
|
'district' => $district, |
|
91
|
1 |
|
'edit_form' => $editForm->createView(), |
|
92
|
1 |
|
'delete_form' => $deleteForm->createView(), |
|
93
|
1 |
|
)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @Route("/district/delete/{slug}", name="admin_district_delete") |
|
98
|
|
|
* @Method("DELETE") |
|
99
|
|
|
* @Security("has_role('ROLE_ADMIN')") |
|
100
|
|
|
* @ParamConverter("district", options={"mapping": {"slug": "slug"}}) |
|
101
|
|
|
*/ |
|
102
|
|
View Code Duplication |
public function DistrictDeleteAction(Request $request, District $district) |
|
|
|
|
|
|
103
|
|
|
{ |
|
104
|
|
|
$form = $this->createDeleteForm($district); |
|
105
|
|
|
$form->handleRequest($request); |
|
106
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
107
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
|
108
|
|
|
|
|
109
|
|
|
$entityManager->remove($district); |
|
110
|
|
|
$entityManager->flush(); |
|
111
|
|
|
} |
|
112
|
|
|
return $this->redirectToRoute('admin_districts'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
2 |
View Code Duplication |
private function createDeleteForm(District $district) |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
2 |
|
return $this->createFormBuilder() |
|
|
|
|
|
|
118
|
2 |
|
->setAction($this->generateUrl('admin_district_delete', array('slug' => $district->getSlug()))) |
|
119
|
2 |
|
->setMethod('DELETE') |
|
120
|
2 |
|
->getForm() |
|
121
|
2 |
|
; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
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.