1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
7
|
|
|
use Cdf\BiCoreBundle\Utils\Entity\EntityUtils; |
8
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
9
|
|
|
use Cdf\BiCoreBundle\Utils\Entity\Finder; |
10
|
|
|
|
11
|
|
|
trait FiCoreCrudInlineControllerTrait |
12
|
|
|
{ |
13
|
|
|
|
14
|
2 |
|
private function checkAggiornaRight($id) |
15
|
|
|
{ |
16
|
2 |
|
if ($id === 0) { |
17
|
|
|
if (!$this->getPermessi()->canCreate()) { |
|
|
|
|
18
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per creare questo contenuto"); |
|
|
|
|
19
|
|
|
} |
20
|
|
|
} else { |
21
|
2 |
|
if (!$this->getPermessi()->canUpdate()) { |
22
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per modificare questo contenuto"); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
} |
25
|
1 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Inline existing table entity. |
29
|
|
|
*/ |
30
|
2 |
|
public function aggiorna(Request $request, $id, $token) |
31
|
|
|
{ |
32
|
2 |
|
$this->checkAggiornaRight($id); |
33
|
1 |
|
$values = $request->get("values"); |
|
|
|
|
34
|
|
|
|
35
|
1 |
|
if ($id == 0) { |
36
|
|
|
$risultato = $this->insertinline($values, $token); |
37
|
|
|
} else { |
38
|
1 |
|
$risultato = $this->updateinline($id, $values, $token); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
return $risultato; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function insertinline($values, $token) |
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
$this->checkAggiornaRight(0); |
48
|
|
|
|
49
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
50
|
|
|
$controller = $this->getController(); |
|
|
|
|
51
|
|
|
$entityclass = $this->getEntityClassName(); |
|
|
|
|
52
|
|
|
|
53
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
54
|
|
|
|
55
|
|
|
//Insert |
56
|
|
|
$entity = new $entityclass(); |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
$isValidToken = $this->isCsrfTokenValid(0, $token); |
|
|
|
|
60
|
|
|
|
61
|
|
|
if (!$isValidToken) { |
62
|
|
|
throw $this->createNotFoundException('Token non valido'); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$querydaeseguire = false; |
66
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
|
|
|
|
67
|
|
|
foreach ($values as $value) { |
68
|
|
|
$fieldpieces = explode(".", $value["fieldname"]); |
|
|
|
|
69
|
|
|
$table = $fieldpieces[0]; |
|
|
|
|
70
|
|
|
//Si prende in considerazione solo i campi strettamente legati a questa entity |
71
|
|
|
if ($table == $controller && count($fieldpieces) == 2) { |
72
|
|
|
$field = ucfirst($fieldpieces[1]); |
|
|
|
|
73
|
|
|
$fieldvalue = $this->getValueAggiorna($value); |
74
|
|
|
if ($value["fieldtype"] == 'join') { |
|
|
|
|
75
|
|
|
$entityfinder = new Finder($em, $field); |
76
|
|
|
$joinclass = $entityfinder->getClassNameFromEntityName(); |
|
|
|
|
77
|
|
|
$fieldvalue = $em->getRepository($joinclass)->find($fieldvalue); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
if ($accessor->isWritable($entity, $field)) { |
82
|
|
|
$accessor->setValue($entity, $field, $fieldvalue); |
83
|
|
|
} else { |
84
|
|
|
throw new \Exception($field . ' non modificabile'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$querydaeseguire = true; |
88
|
|
|
} else { |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
if ($querydaeseguire) { |
93
|
|
|
$em->persist($entity); |
94
|
|
|
$em->flush(); |
95
|
|
|
$em->clear(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return new \Symfony\Component\HttpFoundation\JsonResponse(array("errcode" => 0, "message" => "Registrazione eseguita")); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
protected function updateinline($id, $values, $token) |
102
|
|
|
{ |
103
|
|
|
|
104
|
1 |
|
$this->checkAggiornaRight($id); |
105
|
|
|
|
106
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
107
|
1 |
|
$controller = $this->getController(); |
|
|
|
|
108
|
1 |
|
$entityclass = $this->getEntityClassName(); |
109
|
|
|
|
110
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
111
|
1 |
|
$queryBuilder = $em->createQueryBuilder(); |
112
|
|
|
|
113
|
|
|
//Update |
114
|
1 |
|
$entity = $em->getRepository($entityclass)->find($id); |
115
|
1 |
|
if (!$entity) { |
116
|
1 |
|
throw $this->createNotFoundException('Impossibile trovare l\'entità ' . $controller . ' per il record con id ' . $id); |
117
|
|
|
} |
118
|
|
|
$queryBuilder |
119
|
1 |
|
->update($entityclass, "u") |
|
|
|
|
120
|
1 |
|
->where("u.id = :id") |
|
|
|
|
121
|
1 |
|
->setParameter("id", $id); |
|
|
|
|
122
|
|
|
|
123
|
1 |
|
$isValidToken = $this->isCsrfTokenValid($id, $token); |
124
|
|
|
|
125
|
1 |
|
if (!$isValidToken) { |
126
|
1 |
|
throw $this->createNotFoundException('Token non valido'); |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
$querydaeseguire = false; |
130
|
|
|
|
131
|
1 |
|
foreach ($values as $value) { |
132
|
1 |
|
$fieldpieces = explode(".", $value["fieldname"]); |
|
|
|
|
133
|
1 |
|
$table = $fieldpieces[0]; |
|
|
|
|
134
|
|
|
//Si prende in considerazione solo i campi strettamente legati a questa entity |
135
|
1 |
|
if ($table == $controller && count($fieldpieces) == 2) { |
136
|
1 |
|
$field = $fieldpieces[1]; |
137
|
1 |
|
if ($value["fieldtype"] == 'join') { |
|
|
|
|
138
|
|
|
$field = lcfirst($field . "_id"); |
|
|
|
|
139
|
|
|
} |
140
|
1 |
|
$entityutils = new EntityUtils($em); |
|
|
|
|
141
|
1 |
|
$property = $entityutils->getEntityProperties($field, $entity); |
|
|
|
|
142
|
1 |
|
$nomefunzioneget = $property["get"]; |
|
|
|
|
143
|
1 |
|
if ($nomefunzioneget != $value["fieldvalue"]) { |
|
|
|
|
144
|
1 |
|
$querydaeseguire = true; |
145
|
1 |
|
$fieldvalue = $this->getValueAggiorna($value); |
|
|
|
|
146
|
1 |
|
$queryBuilder->set("u." . $field, ':' . $field); |
|
|
|
|
147
|
1 |
|
$queryBuilder->setParameter($field, $fieldvalue); |
148
|
|
|
} |
149
|
|
|
} else { |
150
|
1 |
|
continue; |
151
|
|
|
} |
152
|
|
|
} |
153
|
1 |
|
if ($querydaeseguire) { |
154
|
1 |
|
$queryBuilder->getQuery()->execute(); |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
return new \Symfony\Component\HttpFoundation\JsonResponse(array("errcode" => 0, "message" => "Registrazione eseguita")); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
private function getValueAggiorna($field) |
161
|
|
|
{ |
162
|
1 |
|
$fieldvalue = $field["fieldvalue"]; |
|
|
|
|
163
|
1 |
|
if ($fieldvalue == '') { |
164
|
|
|
$fieldvalue = null; |
165
|
|
|
} else { |
166
|
1 |
|
$fieldtype = $field["fieldtype"]; |
|
|
|
|
167
|
1 |
|
if ($fieldtype == "date") { |
|
|
|
|
168
|
|
|
} |
169
|
1 |
|
if ($fieldtype == "date") { |
|
|
|
|
170
|
|
|
$fieldvalue = \DateTime::createFromFormat("d/m/Y", $field["fieldvalue"]); |
|
|
|
|
171
|
|
|
if ($fieldvalue === false) { |
172
|
|
|
throw new \Exception("Formato data non valido"); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
} |
175
|
1 |
|
if ($fieldtype == "datetime") { |
|
|
|
|
176
|
|
|
$fieldvalue = \DateTime::createFromFormat("d/m/Y H:i", $field["fieldvalue"]); |
|
|
|
|
177
|
|
|
if ($fieldvalue === false) { |
178
|
|
|
throw new \Exception("Formato data ora non valido"); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
1 |
|
return $fieldvalue; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|