|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vivait\AuthBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
7
|
|
|
use Vivait\AuthBundle\Entity\Tenant; |
|
8
|
|
|
|
|
9
|
|
|
class TenantController extends Controller { |
|
10
|
|
View Code Duplication |
public function indexAction() { |
|
|
|
|
|
|
11
|
|
|
################################################ SETTINGS ################################################ |
|
12
|
|
|
$repo = 'VivaitAuthBundle:Tenant'; |
|
13
|
|
|
$twig = 'VivaitAuthBundle:Default:tenants.html.twig'; |
|
14
|
|
|
############################################################################################################ |
|
15
|
|
|
$db = $this->getDoctrine() |
|
16
|
|
|
->getRepository($repo) |
|
17
|
|
|
->findAll(); |
|
18
|
|
|
|
|
19
|
|
|
$params[''] = ''; |
|
|
|
|
|
|
20
|
|
|
return $this->render($twig, array('db' => $db, 'params' => $params)); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function editAction(Request $request) { |
|
24
|
|
|
################################################ SETTINGS ################################################ |
|
25
|
|
|
$name = 'tenant'; |
|
26
|
|
|
$repo = 'VivaitAuthBundle:Tenant'; |
|
27
|
|
|
$formtpl['title'] = 'Add/Edit ' . ucfirst($name); |
|
|
|
|
|
|
28
|
|
|
$obj = new Tenant(); |
|
29
|
|
|
$key = $request->query->get('id', 0); |
|
30
|
|
|
$foreign_objs = array( # array( |
|
31
|
|
|
# 'repo' => 'VivaBravoBundle:Product', |
|
|
|
|
|
|
32
|
|
|
# 'key' => $request->query->get('pid', 0), |
|
|
|
|
|
|
33
|
|
|
# 'method' => 'setProduct', |
|
|
|
|
|
|
34
|
|
|
# 'name' => 'product'), |
|
|
|
|
|
|
35
|
|
|
); |
|
36
|
|
|
############################################################################################################ |
|
37
|
|
|
|
|
38
|
|
View Code Duplication |
if(!$key) { |
|
|
|
|
|
|
39
|
|
|
### CREATING A NEW OBJECT ### |
|
40
|
|
|
|
|
41
|
|
|
#if there are foreign objects that should be bound to this object, bind them all here |
|
42
|
|
|
foreach($foreign_objs as $fo) { |
|
43
|
|
|
$foreign_obj = $this->getDoctrine() |
|
44
|
|
|
->getRepository($fo['repo']) |
|
45
|
|
|
->find($fo['key']); |
|
46
|
|
|
if(!$foreign_obj) { |
|
47
|
|
|
$this->get('session')->getFlashBag()->add('error', sprintf("Could not find the %s", $fo['name'])); |
|
48
|
|
|
return $this->redirect($request->query->get('parent', $request->request->get('parent', $request->headers->get('referer')))); |
|
49
|
|
|
} |
|
50
|
|
|
call_user_func(array($obj, $fo['method'], $foreign_obj)); |
|
51
|
|
|
} |
|
52
|
|
|
} else { |
|
53
|
|
|
### EDITING AN EXISTING OBJECT ### |
|
54
|
|
|
$obj = $this->getDoctrine() |
|
55
|
|
|
->getRepository($repo) |
|
56
|
|
|
->find($key); |
|
57
|
|
|
|
|
58
|
|
|
if(!$obj) { |
|
59
|
|
|
$this->get('session')->getFlashBag()->add('error', sprintf("Could not find the %s", $name)); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
############################################## CREATE FORM ############################################### |
|
64
|
|
|
$form = $this->createFormBuilder($obj) |
|
65
|
|
|
->add('code', 'text', array('label' => 'Account')) |
|
66
|
|
|
->add('tenant', 'text', array('label' => 'Tenant')) |
|
67
|
|
|
->add('active', 'checkbox') |
|
68
|
|
|
->add('licenseduntil', 'datetime', array( |
|
69
|
|
|
'label' => 'Licensed Until', |
|
70
|
|
|
'format' => 'yyyy-MM-dd HH:mm', |
|
71
|
|
|
'widget' => 'single_text', |
|
72
|
|
|
'datetimepicker' => true, |
|
73
|
|
|
)) |
|
74
|
|
|
->add('users', 'entity', array( |
|
75
|
|
|
'class' => 'VivaitAuthBundle:User', |
|
76
|
|
|
'property' => 'fullname', |
|
77
|
|
|
'multiple' => true, |
|
78
|
|
|
'attr' => array('size' => 15), |
|
79
|
|
|
'required' => true, |
|
80
|
|
|
'label' => 'Users', |
|
81
|
|
|
'by_reference'=>false |
|
82
|
|
|
)) |
|
83
|
|
|
->getForm(); |
|
84
|
|
|
############################################################################################################ |
|
85
|
|
|
|
|
86
|
|
View Code Duplication |
if($request->isMethod('POST')) { |
|
|
|
|
|
|
87
|
|
|
$form->bind($request); |
|
88
|
|
|
if($form->isValid()) { |
|
89
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
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 = 'tenant'; |
|
108
|
|
|
$repo = 'VivaitAuthBundle:Tenant'; |
|
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
|
|
|
|
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.