1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Controller\Admin; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\Module; |
6
|
|
|
use AppBundle\Form\ModuleType; |
7
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
11
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
|
14
|
|
|
class ModuleController extends Controller |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @Route("/admin/module/new", name="create_module") |
18
|
|
|
* @Template("@App/admin/module/createModule.html.twig") |
19
|
|
|
*/ |
20
|
1 |
View Code Duplication |
public function createModuleAction(Request $request) |
|
|
|
|
21
|
|
|
{ |
22
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
23
|
|
|
|
24
|
1 |
|
$module = new Module(); |
25
|
1 |
|
$form = $this->createForm(ModuleType::class, $module); |
26
|
|
|
|
27
|
1 |
|
$form->handleRequest($request); |
28
|
|
|
|
29
|
1 |
|
if ($form->isValid()) { |
30
|
|
|
$em->persist($module); |
31
|
|
|
$em->flush(); |
32
|
|
|
|
33
|
|
|
return $this->redirectToRoute('create_question', array('idModule' => $module->getId())); |
34
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
return ['form' => $form->createView()]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @Route("/admin/module/edit/{id}", name="edit_module") |
42
|
|
|
* @Template("@App/admin/module/editModule.html.twig") |
43
|
|
|
*/ |
44
|
1 |
View Code Duplication |
public function editModuleAction(Request $request, $id) |
|
|
|
|
45
|
|
|
{ |
46
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
47
|
|
|
|
48
|
1 |
|
$module = $em->getRepository('AppBundle:Module') |
49
|
1 |
|
->find($id); |
50
|
1 |
|
$form = $this->createForm(ModuleType::class, $module); |
51
|
|
|
|
52
|
1 |
|
$form->handleRequest($request); |
53
|
|
|
|
54
|
1 |
|
if ($form->isValid()) { |
55
|
|
|
$em->flush(); |
56
|
|
|
|
57
|
|
|
return $this->redirectToRoute('show_module'); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
return ['module' => $module, |
61
|
1 |
|
'form' => $form->createView()]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @Route("/admin/module/remove/{id}", name="remove_module") |
66
|
|
|
* @Method("DELETE") |
67
|
|
|
*/ |
68
|
1 |
|
public function removeModuleAction($id) |
69
|
|
|
{ |
70
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
71
|
|
|
|
72
|
1 |
|
$module = $em->getRepository('AppBundle:Module') |
73
|
1 |
|
->find($id); |
74
|
1 |
|
$users = $em->getRepository('AppBundle:User') |
75
|
1 |
|
->findChoiceModules($module->getId()); |
76
|
1 |
|
foreach ($users as $item) { |
77
|
|
|
$item->removeChosenModule($module->getId()); |
78
|
1 |
|
} |
79
|
|
|
|
80
|
1 |
|
$em->remove($module); |
81
|
1 |
|
$em->flush(); |
82
|
|
|
|
83
|
1 |
|
return $this->redirectToRoute('show_module'); |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @Route("/admin/module", name="show_module") |
89
|
|
|
* @Template("@App/admin/module/showmodule.html.twig") |
90
|
|
|
*/ |
91
|
1 |
|
public function showModuleAction() |
92
|
|
|
{ |
93
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
94
|
|
|
|
95
|
1 |
|
$module = $em->getRepository('AppBundle:Module') |
96
|
1 |
|
->findBy([],['createdAt' => 'DESC']); |
97
|
|
|
|
98
|
1 |
|
$form_delete = []; |
99
|
|
|
|
100
|
1 |
|
foreach ($module as $item) { |
101
|
1 |
|
$form_delete[$item->getId()] = $this->createFormDelete($item->getId())->createView(); |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
return [ |
105
|
1 |
|
'modules' => $module, |
106
|
|
|
'form_remove' => $form_delete |
107
|
1 |
|
]; |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return \Symfony\Component\Form\Form |
113
|
|
|
*/ |
114
|
1 |
View Code Duplication |
private function createFormDelete($id) |
|
|
|
|
115
|
|
|
{ |
116
|
1 |
|
return $this->createFormBuilder() |
|
|
|
|
117
|
1 |
|
->setAction($this->generateUrl('remove_module', ['id' => $id])) |
118
|
1 |
|
->setMethod('DELETE') |
119
|
1 |
|
->add('submit', SubmitType::class, [ |
120
|
1 |
|
'label' => ' ', |
121
|
|
|
'attr' => [ |
122
|
1 |
|
'class' => 'glyphicon glyphicon-remove btn-link', |
123
|
|
|
'onclick' => 'return confirm("Are you sure?")' |
124
|
1 |
|
] |
125
|
1 |
|
]) |
126
|
1 |
|
->getForm(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
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.