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