|
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
|
|
|
public function categoriesAction(Request $request) |
|
28
|
|
|
{ |
|
29
|
|
|
$em = $this->getDoctrine()->getManager()->getRepository('AppBundle\Entity\Category'); |
|
30
|
|
|
$categories = $em->childrenHierarchy(); |
|
31
|
|
|
return $this->render("@App/admin/category/categories.html.twig", ['categories' => $categories]); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @Route("/category_root/new", name="admin_category_root_new") |
|
36
|
|
|
* @Method({"GET", "POST"}) |
|
37
|
|
|
* @Security("has_role('ROLE_ADMIN')") |
|
38
|
|
|
*/ |
|
39
|
|
View Code Duplication |
public function newCategoryRootAction(Request $request) |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
|
42
|
|
|
$category = new Category(); |
|
43
|
|
|
$form = $this->createForm(CategoryType::class, $category); |
|
44
|
|
|
$form->handleRequest($request); |
|
45
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
46
|
|
|
$category->setParent(null); |
|
47
|
|
|
$entityManager->persist($category); |
|
48
|
|
|
$entityManager->flush(); |
|
49
|
|
|
return $this->redirectToRoute('admin_categories'); |
|
50
|
|
|
} |
|
51
|
|
|
return $this->render('@App/admin/category/new_category_root.html.twig', array( |
|
52
|
|
|
'category' => $category, |
|
53
|
|
|
'form' => $form->createView(), |
|
54
|
|
|
)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
View Code Duplication |
private function createDeleteForm(Category $category) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
return $this->createFormBuilder() |
|
63
|
|
|
->setAction($this->generateUrl('admin_district_delete', array('slug' => $category->getSlug()))) |
|
64
|
|
|
->setMethod('DELETE') |
|
65
|
|
|
->getForm() |
|
66
|
|
|
; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
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.