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