|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Controller\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\Category; |
|
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 Symfony\Component\Form\Extension\Core\Type\SubmitType; |
|
11
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
|
12
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; |
|
13
|
|
|
use AppBundle\Form\CategoryType; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @Security("has_role('ROLE_MANAGER')") |
|
19
|
|
|
* @Route("/admin") |
|
20
|
|
|
*/ |
|
21
|
|
|
class AdminCategoryController extends Controller |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @Route("/categories", name="admin_categories") |
|
25
|
|
|
* @Method("GET") |
|
26
|
|
|
*/ |
|
27
|
|
View Code Duplication |
public function categoriesAction(Request $request) |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
|
30
|
|
|
$repo = $entityManager->getRepository('AppBundle\Entity\Category'); |
|
31
|
|
|
$categories = $repo->childrenHierarchy(); |
|
32
|
|
|
return $this->render("@App/admin/category/categories.html.twig", ['categories' => $categories]); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @Route("/category_root/new", name="admin_category_root_new") |
|
37
|
|
|
* @Method({"GET", "POST"}) |
|
38
|
|
|
* @Security("has_role('ROLE_ADMIN')") |
|
39
|
|
|
*/ |
|
40
|
|
View Code Duplication |
public function newCategoryRootAction(Request $request) |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
|
43
|
|
|
$repo = $entityManager->getRepository('AppBundle\Entity\Category'); |
|
44
|
|
|
$category = new Category(); |
|
45
|
|
|
$form = $this->createForm(CategoryType::class, $category); |
|
46
|
|
|
$form->handleRequest($request); |
|
47
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
48
|
|
|
$category->setParent(null); |
|
49
|
|
|
$entityManager->persist($category); |
|
50
|
|
|
$repo->verify(); |
|
51
|
|
|
$repo->recover(); |
|
52
|
|
|
$entityManager->flush(); |
|
53
|
|
|
return $this->redirectToRoute('admin_categories'); |
|
54
|
|
|
} |
|
55
|
|
|
return $this->render('@App/admin/category/new_category_root.html.twig', array( |
|
56
|
|
|
'category' => $category, |
|
57
|
|
|
'form' => $form->createView(), |
|
58
|
|
|
)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @Route("/category/new", name="admin_category_new") |
|
63
|
|
|
* @Method({"GET", "POST"}) |
|
64
|
|
|
* @Security("has_role('ROLE_ADMIN')") |
|
65
|
|
|
*/ |
|
66
|
|
|
public function newCategoryAction(Request $request) |
|
67
|
|
|
{ |
|
68
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
|
69
|
|
|
$repo = $entityManager->getRepository('AppBundle\Entity\Category'); |
|
70
|
|
|
$category = new Category(); |
|
71
|
|
|
$form = $this->createForm(CategoryType::class, $category, array('isForm_cat' => true)); |
|
72
|
|
|
$form->handleRequest($request); |
|
73
|
|
View Code Duplication |
if ($form->isSubmitted() && $form->isValid()) { |
|
|
|
|
|
|
74
|
|
|
$parent = $form['parent']->getData(); |
|
75
|
|
|
$category->setParent($parent); |
|
76
|
|
|
$parent->addChild($category); |
|
77
|
|
|
$entityManager->persist($category); |
|
78
|
|
|
$repo->verify(); |
|
79
|
|
|
$repo->recover(); |
|
80
|
|
|
$entityManager->flush(); |
|
81
|
|
|
return $this->redirectToRoute('admin_categories'); |
|
82
|
|
|
} |
|
83
|
|
|
return $this->render('@App/admin/category/new_category.html.twig', array( |
|
84
|
|
|
'category' => $category, |
|
85
|
|
|
'form' => $form->createView(), |
|
86
|
|
|
)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @Route("/category/edit/{slug}", name="admin_category_edit") |
|
91
|
|
|
* @Method({"GET", "POST"}) |
|
92
|
|
|
* @Security("has_role('ROLE_ADMIN')") |
|
93
|
|
|
* @ParamConverter("category", options={"mapping": {"slug": "slug"}}) |
|
94
|
|
|
*/ |
|
95
|
|
|
public function categoryEditAction(Category $category, Request $request) |
|
96
|
|
|
{ |
|
97
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
|
98
|
|
|
$repo = $entityManager->getRepository('AppBundle\Entity\Category'); |
|
99
|
|
|
$editForm = $this->createForm(CategoryType::class, $category, array('isForm_cat' => true)); |
|
100
|
|
|
$editForm->handleRequest($request); |
|
101
|
|
View Code Duplication |
if ($editForm->isSubmitted() && $editForm->isValid()) { |
|
|
|
|
|
|
102
|
|
|
$parent = $editForm['parent']->getData(); |
|
103
|
|
|
$category->setParent($parent); |
|
104
|
|
|
$entityManager->persist($category); |
|
105
|
|
|
$repo->verify(); |
|
106
|
|
|
$repo->recover(); |
|
107
|
|
|
$entityManager->flush(); |
|
108
|
|
|
return $this->redirectToRoute('admin_categories'); |
|
109
|
|
|
} |
|
110
|
|
|
return $this->render('@App/admin/category/edit_category.html.twig', array( |
|
111
|
|
|
'category' => $category, |
|
112
|
|
|
'edit_form' => $editForm->createView(), |
|
113
|
|
|
)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @Route("/category/delete/{slug}", name="admin_category_delete") |
|
118
|
|
|
* @Method({"GET", "DELETE"}) |
|
119
|
|
|
* @Security("has_role('ROLE_ADMIN')") |
|
120
|
|
|
* @ParamConverter("estate", options={"mapping": {"slug": "slug"}}) |
|
121
|
|
|
*/ |
|
122
|
|
View Code Duplication |
public function categoryDeleteAction(Request $request, Category $category) |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
|
125
|
|
|
$repo = $entityManager->getRepository('AppBundle\Entity\Category'); |
|
126
|
|
|
$deleteForm = $this->createForm(CategoryType::class, $category,['method' => 'DELETE']); |
|
127
|
|
|
$deleteForm->handleRequest($request); |
|
128
|
|
|
if ($deleteForm->isSubmitted() && $deleteForm->isValid()) { |
|
129
|
|
|
$entityManager->remove($category); |
|
130
|
|
|
$repo->verify(); |
|
131
|
|
|
$repo->recover(); |
|
132
|
|
|
$entityManager->flush(); |
|
133
|
|
|
return $this->redirectToRoute('admin_categories'); |
|
134
|
|
|
} |
|
135
|
|
|
return $this->render('@App/admin/category/delete_category.html.twig', array( |
|
136
|
|
|
'category' => $category, |
|
137
|
|
|
'delete_form' => $deleteForm->createView(), |
|
138
|
|
|
)); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @Route("/category/up/{slug}", name="admin_category_up") |
|
143
|
|
|
* @Method({"GET", "POST"}) |
|
144
|
|
|
* @Security("has_role('ROLE_ADMIN')") |
|
145
|
|
|
* @ParamConverter("category", options={"mapping": {"slug": "slug"}}) |
|
146
|
|
|
*/ |
|
147
|
|
View Code Duplication |
public function categoryUpAction(Request $request, Category $category) |
|
|
|
|
|
|
148
|
|
|
{ |
|
149
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
|
150
|
|
|
$repo = $entityManager->getRepository('AppBundle\Entity\Category'); |
|
151
|
|
|
if ($category->getParent()){ |
|
152
|
|
|
$repo->moveUp($category); |
|
153
|
|
|
$repo->verify(); |
|
154
|
|
|
$repo->recover(); |
|
155
|
|
|
$entityManager->flush(); |
|
156
|
|
|
} |
|
157
|
|
|
return $this->redirectToRoute('admin_categories'); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @Route("/category/down/{slug}", name="admin_category_down") |
|
162
|
|
|
* @Method("GET") |
|
163
|
|
|
* @Security("has_role('ROLE_ADMIN')") |
|
164
|
|
|
* @ParamConverter("category", options={"mapping": {"slug": "slug"}}) |
|
165
|
|
|
*/ |
|
166
|
|
View Code Duplication |
public function categoryDownAction(Request $request, Category $category) |
|
|
|
|
|
|
167
|
|
|
{ |
|
168
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
|
169
|
|
|
$repo = $entityManager->getRepository('AppBundle\Entity\Category'); |
|
170
|
|
|
if ($category->getParent()){ |
|
171
|
|
|
$repo->moveDown($category); |
|
172
|
|
|
$repo->verify(); |
|
173
|
|
|
$repo->recover(); |
|
174
|
|
|
$entityManager->flush(); |
|
175
|
|
|
} |
|
176
|
|
|
return $this->redirectToRoute('admin_categories'); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
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.