|
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\ContactType; |
|
10
|
|
|
use ClientBundle\Form\Type\ContactTypeForm; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class ContactTypeController |
|
14
|
|
|
*/ |
|
15
|
|
|
class ContactTypeController 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
|
|
|
$contactType = $em->getRepository('ClientBundle:ContactType')->find($id); |
|
27
|
|
|
if (!$contactType) { |
|
28
|
|
|
throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $id)); |
|
29
|
|
|
} else { |
|
30
|
|
|
$form = $this->createForm(ContactTypeForm::class, $contactType); |
|
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
|
|
|
$contactType = $form->getData(); |
|
39
|
|
|
if ($this->isGranted('ROLE_ADMIN') && $form->get('delete')->isClicked()) { |
|
|
|
|
|
|
40
|
|
|
return $this->redirectToRoute('contacttype_del', array('id' => $contactType->getId())); |
|
41
|
|
|
} |
|
42
|
|
|
$em->flush(); |
|
43
|
|
|
|
|
44
|
|
|
return $this->redirectToRoute('contacttype_home'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $this->render('ClientBundle:ContactType:edit.html.twig', ['form' => $form->createView(), 'contactType' => $contactType]); |
|
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
|
|
|
$contactType = $em->getRepository('ClientBundle:ContactType')->findAll(); |
|
59
|
|
|
|
|
60
|
|
|
return $this->render('ClientBundle:ContactType:home.html.twig', ['contactTypes' => $contactType]); |
|
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
|
|
|
$contactType = new ContactType(); |
|
72
|
|
|
$form = $this->createForm(ContactTypeForm::class, $contactType); |
|
73
|
|
|
|
|
74
|
|
|
$form->handleRequest($request); |
|
75
|
|
|
|
|
76
|
|
|
if ($request->isMethod('POST') && $form->isSubmitted() && $form->isValid()) { |
|
77
|
|
|
$contactType = $form->getData(); |
|
78
|
|
|
$em->persist($contactType); |
|
79
|
|
|
$em->flush(); |
|
80
|
|
|
|
|
81
|
|
|
return $this->redirectToRoute('contacttype_home'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->render('ClientBundle:ContactType:edit.html.twig', ['form' => $form->createView(), 'contactType' => $contactType]); |
|
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
|
|
|
$contactType = $em->getRepository('ClientBundle:ContactType')->find($id); |
|
96
|
|
|
if (!$contactType) { |
|
97
|
|
|
throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $id)); |
|
98
|
|
|
} else { |
|
99
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
100
|
|
|
$contacts = $em->getRepository('ClientBundle:Contact')->findBy(['type' => $id]); |
|
101
|
|
|
foreach ($contacts as $contact) { |
|
102
|
|
|
$client = $contact->getClient(); |
|
103
|
|
|
$client->removeContact($contact); |
|
104
|
|
|
$em->remove($contact); |
|
105
|
|
|
} |
|
106
|
|
|
$em->remove($contactType); |
|
107
|
|
|
$em->flush(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $this->redirectToRoute('contacttype_home'); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
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.