|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ClientBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use ClientBundle\Entity\Category; |
|
6
|
|
|
use ClientBundle\Form\Type\CategoryForm; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
9
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class CategoryController |
|
14
|
|
|
*/ |
|
15
|
|
|
class CategoryController extends Controller |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @param int $id |
|
19
|
|
|
* @param Request $request |
|
20
|
|
|
* @Method({"GET", "POST"}) |
|
21
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
|
22
|
|
|
*/ |
|
23
|
|
View Code Duplication |
public function showAction($id, Request $request) |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
26
|
|
|
$category = $em->getRepository('ClientBundle:Category')->find($id); |
|
27
|
|
|
|
|
28
|
|
|
if (!$category) { |
|
29
|
|
|
throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $id)); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$form = $this->createForm(CategoryForm::class, $category); |
|
33
|
|
|
if ($this->isGranted('ROLE_ADMIN')) { |
|
34
|
|
|
$form->add('delete', SubmitType::class); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$form->handleRequest($request); |
|
38
|
|
|
|
|
39
|
|
|
if ($request->isMethod('POST') && $form->isSubmitted() && $form->isValid()) { |
|
40
|
|
|
$category = $form->getData(); |
|
41
|
|
|
if ($this->isGranted('ROLE_ADMIN') && $form->get('delete')->isClicked()) { |
|
|
|
|
|
|
42
|
|
|
return $this->redirectToRoute('category_del', array('id' => $category->getId())); |
|
43
|
|
|
} |
|
44
|
|
|
$em->flush(); |
|
45
|
|
|
|
|
46
|
|
|
return $this->redirectToRoute('category_home'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $this->render('ClientBundle:Category:edit.html.twig', ['form' => $form->createView(), 'category' => $category]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
54
|
|
|
* @Method("GET") |
|
55
|
|
|
*/ |
|
56
|
|
|
public function homeAction() |
|
57
|
|
|
{ |
|
58
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
59
|
|
|
$categories = $em->getRepository('ClientBundle:Category')->findAll(); |
|
60
|
|
|
|
|
61
|
|
|
return $this->render('ClientBundle:Category:home.html.twig', ['categories' => $categories]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param Request $request |
|
66
|
|
|
* @Method({"GET", "POST"}) |
|
67
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
|
68
|
|
|
*/ |
|
69
|
|
View Code Duplication |
public function addAction(Request $request) |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
72
|
|
|
$category = new Category(); |
|
73
|
|
|
$form = $this->createForm(CategoryForm::class, $category); |
|
74
|
|
|
|
|
75
|
|
|
$form->handleRequest($request); |
|
76
|
|
|
|
|
77
|
|
|
if ($request->isMethod('POST') && $form->isSubmitted() && $form->isValid()) { |
|
78
|
|
|
$category = $form->getData(); |
|
79
|
|
|
$em->persist($category); |
|
80
|
|
|
$em->flush(); |
|
81
|
|
|
|
|
82
|
|
|
return $this->redirectToRoute('category_home'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $this->render('ClientBundle:Category:edit.html.twig', ['form' => $form->createView(), 'category' => $category]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param int $id |
|
90
|
|
|
* @Method("GET") |
|
91
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
|
92
|
|
|
*/ |
|
93
|
|
|
public function delAction($id) |
|
94
|
|
|
{ |
|
95
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
96
|
|
|
$category = $em->getRepository('ClientBundle:Category')->find($id); |
|
97
|
|
|
if (!$category) { |
|
98
|
|
|
throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $id)); |
|
99
|
|
|
} else { |
|
100
|
|
|
$em->remove($category); |
|
101
|
|
|
$em->flush(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->redirectToRoute('category_home'); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
//php bin/console doctrine:database:create |
|
108
|
|
|
//php bin/console doctrine:schema:update --force |
|
109
|
|
|
|
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.