|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vivait\AuthBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
8
|
|
|
use Vivait\AuthBundle\Entity\Group; |
|
9
|
|
|
use Doctrine\ORM\Query; |
|
10
|
|
|
|
|
11
|
|
|
class GroupController extends Controller |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
View Code Duplication |
public function indexAction() { |
|
|
|
|
|
|
15
|
|
|
################################################ SETTINGS ################################################ |
|
16
|
|
|
$repo = 'VivaitAuthBundle:Group'; |
|
17
|
|
|
$twig = 'VivaitAuthBundle:Default:groups.html.twig'; |
|
18
|
|
|
############################################################################################################ |
|
19
|
|
|
$db = $this->getDoctrine() |
|
20
|
|
|
->getRepository($repo) |
|
21
|
|
|
->findAll(); |
|
22
|
|
|
|
|
23
|
|
|
$params = array(); |
|
24
|
|
|
return $this->render($twig, array('db' => $db, 'params' => $params)); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function editAction(Request $request) { |
|
28
|
|
|
################################################ SETTINGS ################################################ |
|
29
|
|
|
$name = 'group'; |
|
30
|
|
|
$repo = 'VivaitAuthBundle:Group'; |
|
31
|
|
|
$formtpl['title'] = 'Add/Edit ' . ucfirst($name); |
|
|
|
|
|
|
32
|
|
|
$obj = new Group(); |
|
33
|
|
|
$key = $request->query->get('id', 0); |
|
34
|
|
|
$foreign_objs = array( |
|
35
|
|
|
# array( |
|
36
|
|
|
# 'repo' => 'VivaBravoBundle:Product', |
|
|
|
|
|
|
37
|
|
|
# 'key' => $request->query->get('pid', 0), |
|
|
|
|
|
|
38
|
|
|
# 'method' => 'setProduct', |
|
|
|
|
|
|
39
|
|
|
# 'name' => 'product'), |
|
|
|
|
|
|
40
|
|
|
); |
|
41
|
|
|
############################################################################################################ |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
View Code Duplication |
if(!$key) { |
|
|
|
|
|
|
45
|
|
|
### CREATING A NEW OBJECT ### |
|
46
|
|
|
|
|
47
|
|
|
#if there are foreign objects that should be bound to this object, bind them all here |
|
48
|
|
|
foreach($foreign_objs as $fo) { |
|
49
|
|
|
$foreign_obj = $this->getDoctrine() |
|
50
|
|
|
->getRepository($fo['repo']) |
|
51
|
|
|
->find($fo['key']); |
|
52
|
|
|
if(!$foreign_obj) { |
|
53
|
|
|
$this->get('session')->getFlashBag()->add('error', sprintf("Could not find the %s", $fo['name'])); |
|
54
|
|
|
return $this->redirect($request->query->get('parent', $request->request->get('parent', $request->headers->get('referer')))); |
|
55
|
|
|
} |
|
56
|
|
|
call_user_func(array($obj, $fo['method'], $foreign_obj)); |
|
57
|
|
|
} |
|
58
|
|
|
} else { |
|
59
|
|
|
### EDITING AN EXISTING OBJECT ### |
|
60
|
|
|
$obj = $this->getDoctrine() |
|
61
|
|
|
->getRepository($repo) |
|
62
|
|
|
->find($key); |
|
63
|
|
|
|
|
64
|
|
|
if(!$obj) { |
|
65
|
|
|
$this->get('session')->getFlashBag()->add('error', sprintf("Could not find the %s", $name)); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
############################################## CREATE FORM ############################################### |
|
71
|
|
|
$form = $this->createFormBuilder($obj) |
|
72
|
|
|
->add('name', 'text',array('label' => 'Name')) |
|
73
|
|
|
->add('role', 'text',array('label' => 'Role')) |
|
74
|
|
|
->add('users', 'entity', array( |
|
75
|
|
|
'class' => 'VivaitAuthBundle:User', |
|
76
|
|
|
'property' => 'fullname', |
|
77
|
|
|
'multiple' => true, |
|
78
|
|
|
'required' => true, |
|
79
|
|
|
'label' => 'Users', |
|
80
|
|
|
'by_reference' => false #USE THIS ALONG WITH A $owning->addInverse($this); IN THE addOwning() FUNCTION TO TRIGGER AN UPDATE WHEN ON INVERSE SIDE |
|
81
|
|
|
)) |
|
82
|
|
|
->getForm(); |
|
83
|
|
|
############################################################################################################ |
|
84
|
|
|
|
|
85
|
|
View Code Duplication |
if($request->isMethod('POST')) { |
|
|
|
|
|
|
86
|
|
|
$form->bind($request); |
|
87
|
|
|
if($form->isValid()) { |
|
88
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
89
|
|
|
|
|
90
|
|
|
$em->persist($obj); |
|
91
|
|
|
$em->flush(); |
|
92
|
|
|
$this->get('session')->getFlashBag()->add('success', sprintf('The %s has been %s successfully', $name, $key ? 'modified' : 'created')); |
|
93
|
|
|
return $this->render('VivaitBootstrapBundle:Default:redirect.html.twig', array('redirect' => $request->query->get('parent', $request->request->get('parent', $request->headers->get('referer'))))); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
if(isset($form)) { |
|
97
|
|
|
$formtpl['form'] = $form->createView(); |
|
98
|
|
|
} |
|
99
|
|
|
$formtpl['action'] = $this->generateUrl($this->container->get('request')->get('_route'),$request->query->all()); |
|
100
|
|
|
|
|
101
|
|
|
return $this->render('VivaitBootstrapBundle:Default:form.html.twig', array( |
|
102
|
|
|
'form' => array_merge($formtpl, array('parent' => $request->query->get('parent', $request->request->get('parent', $request->headers->get('referer'))))))); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
View Code Duplication |
public function deleteAction(Request $request) { |
|
|
|
|
|
|
106
|
|
|
################################################ SETTINGS ################################################ |
|
107
|
|
|
$name = 'group'; |
|
108
|
|
|
$repo = 'VivaitAuthBundle:Group'; |
|
109
|
|
|
$id = $request->query->get('id', 0); |
|
110
|
|
|
$msg_notfound = "The $name could not be found"; |
|
111
|
|
|
$msg_success = "The $name has been removed"; |
|
112
|
|
|
############################################################################################################ |
|
113
|
|
|
|
|
114
|
|
|
$obj = $this->getDoctrine() |
|
115
|
|
|
->getRepository($repo) |
|
116
|
|
|
->find($id); |
|
117
|
|
|
|
|
118
|
|
|
if(!$obj) { |
|
119
|
|
|
$this->get('session')->getFlashBag()->add('error', $msg_notfound); |
|
120
|
|
|
} else { |
|
121
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
122
|
|
|
$em->remove($obj); |
|
123
|
|
|
$em->flush(); |
|
124
|
|
|
$this->get('session')->getFlashBag()->add('success', $msg_success); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $this->redirect($request->headers->get('referer')); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
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.