1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Workarea\Controller; |
4
|
|
|
|
5
|
|
|
use Drone\Mvc\AbstractionController; |
6
|
|
|
use Drone\Dom\Element\Form; |
7
|
|
|
use Drone\Validator\FormValidator; |
8
|
|
|
use Drone\Db\TableGateway\EntityAdapter; |
9
|
|
|
use Drone\Db\TableGateway\TableGateway; |
10
|
|
|
use Drone\Network\Http; |
11
|
|
|
use Auth\Model\User as UserModel; |
12
|
|
|
use Workarea\Model\ConnectionType; |
13
|
|
|
use Workarea\Model\ConnectionTypeField; |
14
|
|
|
use Workarea\Model\UserConnection; |
15
|
|
|
use Workarea\Model\UserConnectionsTable; |
16
|
|
|
use Workarea\Model\UserConnectionDetails; |
17
|
|
|
|
18
|
|
|
class Connections extends AbstractionController |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var integer |
22
|
|
|
*/ |
23
|
|
|
private $identity; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var EntityAdapter |
27
|
|
|
*/ |
28
|
|
|
private $usersEntity; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var EntityAdapter |
32
|
|
|
*/ |
33
|
|
|
private $connectionTypesEntity; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var EntityAdapter |
37
|
|
|
*/ |
38
|
|
|
private $connectionFieldsEntity; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var EntityAdapter |
42
|
|
|
*/ |
43
|
|
|
private $userConnectionEntity; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var EntityAdapter |
47
|
|
|
*/ |
48
|
|
|
private $userConnectionDetailsEntity; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return integer |
52
|
|
|
*/ |
53
|
|
|
private function getIdentity() |
54
|
|
|
{ |
55
|
|
|
$config = include 'module/Auth/config/user.config.php'; |
56
|
|
|
$method = $config["authentication"]["method"]; |
57
|
|
|
$key = $config["authentication"]["key"]; |
58
|
|
|
|
59
|
|
|
switch ($method) |
60
|
|
|
{ |
61
|
|
|
case '_COOKIE': |
62
|
|
|
|
63
|
|
|
$user = $this->getUsersEntity()->select([ |
64
|
|
|
"USERNAME" => $_COOKIE[$key] |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
break; |
68
|
|
|
|
69
|
|
|
case '_SESSION': |
70
|
|
|
|
71
|
|
|
$user = $this->getUsersEntity()->select([ |
72
|
|
|
"USERNAME" => $_SESSION[$key] |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
break; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$user = array_shift($user); |
79
|
|
|
|
80
|
|
|
return $user->USER_ID; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return UsersEntity |
|
|
|
|
85
|
|
|
*/ |
86
|
|
|
private function getUsersEntity() |
87
|
|
|
{ |
88
|
|
|
if (!is_null($this->usersEntity)) |
89
|
|
|
return $this->usersEntity; |
|
|
|
|
90
|
|
|
|
91
|
|
|
$this->usersEntity = new EntityAdapter(new TableGateway(new UserModel())); |
92
|
|
|
|
93
|
|
|
return $this->usersEntity; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return EntityAdapter |
98
|
|
|
*/ |
99
|
|
|
private function getConnectionTypesEntity() |
100
|
|
|
{ |
101
|
|
|
if (!is_null($this->connectionTypesEntity)) |
102
|
|
|
return $this->connectionTypesEntity; |
103
|
|
|
|
104
|
|
|
$this->connectionTypesEntity = new EntityAdapter(new TableGateway(new ConnectionType())); |
105
|
|
|
|
106
|
|
|
return $this->connectionTypesEntity; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return EntityAdapter |
111
|
|
|
*/ |
112
|
|
|
private function getConnectionFieldsEntity() |
113
|
|
|
{ |
114
|
|
|
if (!is_null($this->connectionFieldsEntity)) |
115
|
|
|
return $this->connectionFieldsEntity; |
116
|
|
|
|
117
|
|
|
$this->connectionFieldsEntity = new EntityAdapter(new TableGateway(new ConnectionTypeField())); |
118
|
|
|
|
119
|
|
|
return $this->connectionFieldsEntity; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return EntityAdapter |
124
|
|
|
*/ |
125
|
|
|
private function getUserConnectionEntity() |
126
|
|
|
{ |
127
|
|
|
if (!is_null($this->userConnectionEntity)) |
128
|
|
|
return $this->userConnectionEntity; |
129
|
|
|
|
130
|
|
|
$this->userConnectionEntity = new EntityAdapter(new UserConnectionsTable(new UserConnection())); |
131
|
|
|
|
132
|
|
|
return $this->userConnectionEntity; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return EntityAdapter |
137
|
|
|
*/ |
138
|
|
|
private function getUserConnectionDetailsEntity() |
139
|
|
|
{ |
140
|
|
|
if (!is_null($this->userConnectionDetailsEntity)) |
141
|
|
|
return $this->userConnectionDetailsEntity; |
142
|
|
|
|
143
|
|
|
$this->userConnectionDetailsEntity = new EntityAdapter(new TableGateway(new UserConnectionDetails())); |
144
|
|
|
|
145
|
|
|
return $this->userConnectionDetailsEntity; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Lists all user connections |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
public function list() |
154
|
|
|
{ |
155
|
|
|
# data to send |
156
|
|
|
$data = array(); |
157
|
|
|
|
158
|
|
|
# environment settings |
159
|
|
|
$this->setTerminal(true); # set terminal |
160
|
|
|
|
161
|
|
|
# TRY-CATCH-BLOCK |
162
|
|
|
try { |
163
|
|
|
|
164
|
|
|
# STANDARD VALIDATIONS [check method] |
165
|
|
|
if (!$this->isGet()) |
166
|
|
|
{ |
167
|
|
|
$http = new Http(); |
168
|
|
|
$http->writeStatus($http::HTTP_METHOD_NOT_ALLOWED); |
169
|
|
|
|
170
|
|
|
die('Error ' . $http::HTTP_METHOD_NOT_ALLOWED .' (' . $http->getStatusText($http::HTTP_METHOD_NOT_ALLOWED) . ')!!'); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$data["connections"] = $this->getUserConnectionEntity()->select([ |
174
|
|
|
"USER_ID" => $this->getIdentity(), |
175
|
|
|
"STATE" => "A" |
176
|
|
|
]); |
177
|
|
|
|
178
|
|
|
# SUCCESS-MESSAGE |
179
|
|
|
$data["process"] = "success"; |
180
|
|
|
} |
181
|
|
|
catch (\Drone\Exception\Exception $e) |
182
|
|
|
{ |
183
|
|
|
# ERROR-MESSAGE |
184
|
|
|
$data["process"] = "warning"; |
185
|
|
|
$data["message"] = $e->getMessage(); |
186
|
|
|
} |
187
|
|
|
catch (\Exception $e) |
188
|
|
|
{ |
189
|
|
|
$file = str_replace('\\', '', __CLASS__); |
190
|
|
|
$storage = new \Drone\Exception\Storage("cache/$file.json"); |
191
|
|
|
|
192
|
|
|
# stores the error code |
193
|
|
|
if (($errorCode = $storage->store($e)) === false) |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
$errors = $storage->getErrors(); |
196
|
|
|
|
197
|
|
|
# if error storing is not possible, handle it (internal app error) |
198
|
|
|
$this->handleErrors($errors, __METHOD__); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$data["code"] = $errorCode; |
202
|
|
|
$data["message"] = $e->getMessage(); |
203
|
|
|
|
204
|
|
|
$config = include 'config/application.config.php'; |
205
|
|
|
$data["dev_mode"] = $config["environment"]["dev_mode"]; |
206
|
|
|
|
207
|
|
|
# redirect view |
208
|
|
|
$this->setMethod('error'); |
209
|
|
|
|
210
|
|
|
return $data; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $data; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Deletes a connection |
218
|
|
|
* |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
|
public function delete() |
222
|
|
|
{ |
223
|
|
|
clearstatcache(); |
224
|
|
|
session_write_close(); |
225
|
|
|
|
226
|
|
|
# data to send |
227
|
|
|
$data = array(); |
228
|
|
|
|
229
|
|
|
# environment settings |
230
|
|
|
$post = $this->getPost(); # catch $_POST |
231
|
|
|
$this->setTerminal(true); # set terminal |
232
|
|
|
|
233
|
|
|
# TRY-CATCH-BLOCK |
234
|
|
|
try { |
235
|
|
|
|
236
|
|
|
# STANDARD VALIDATIONS [check method] |
237
|
|
|
if (!$this->isPost()) |
238
|
|
|
{ |
239
|
|
|
$http = new Http(); |
240
|
|
|
$http->writeStatus($http::HTTP_METHOD_NOT_ALLOWED); |
241
|
|
|
|
242
|
|
|
die('Error ' . $http::HTTP_METHOD_NOT_ALLOWED .' (' . $http->getStatusText($http::HTTP_METHOD_NOT_ALLOWED) . ')!!'); |
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
# STANDARD VALIDATIONS [check needed arguments] |
246
|
|
|
$needles = ['id']; |
247
|
|
|
|
248
|
|
|
array_walk($needles, function(&$item) use ($post) { |
249
|
|
|
if (!array_key_exists($item, $post)) |
250
|
|
|
{ |
251
|
|
|
$http = new Http(); |
252
|
|
|
$http->writeStatus($http::HTTP_BAD_REQUEST); |
253
|
|
|
|
254
|
|
|
die('Error ' . $http::HTTP_BAD_REQUEST .' (' . $http->getStatusText($http::HTTP_BAD_REQUEST) . ')!!'); |
|
|
|
|
255
|
|
|
} |
256
|
|
|
}); |
257
|
|
|
|
258
|
|
|
$components = [ |
259
|
|
|
"attributes" => [ |
260
|
|
|
"id" => [ |
261
|
|
|
"required" => true, |
262
|
|
|
"type" => "number" |
263
|
|
|
] |
264
|
|
|
], |
265
|
|
|
]; |
266
|
|
|
|
267
|
|
|
$options = [ |
268
|
|
|
"id" => [ |
269
|
|
|
"label" => "Id" |
270
|
|
|
] |
271
|
|
|
]; |
272
|
|
|
|
273
|
|
|
$form = new Form($components); |
274
|
|
|
$form->fill($post); |
275
|
|
|
|
276
|
|
|
$validator = new FormValidator($form, $options); |
277
|
|
|
$validator->validate(); |
278
|
|
|
|
279
|
|
|
$data["validator"] = $validator; |
280
|
|
|
|
281
|
|
|
# form validation |
282
|
|
|
if (!$validator->isValid()) |
283
|
|
|
{ |
284
|
|
|
$data["messages"] = $validator->getMessages(); |
285
|
|
|
throw new \Drone\Exception\Exception("Form validation errors", 300); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
$connection = $this->getUserConnectionEntity()->select([ |
289
|
|
|
"USER_CONN_ID" => $post["id"] |
290
|
|
|
]); |
291
|
|
|
|
292
|
|
|
if (!count($connection)) |
293
|
|
|
throw new \Exception("The Connection does not exists"); |
294
|
|
|
|
295
|
|
|
$connection = array_shift($connection); |
296
|
|
|
|
297
|
|
|
if ($connection->STATE == 'I') |
298
|
|
|
throw new \Drone\Exception\Exception("This connection was deleted", 300); |
299
|
|
|
|
300
|
|
|
$connection->exchangeArray([ |
301
|
|
|
"STATE" => 'I' |
302
|
|
|
]); |
303
|
|
|
|
304
|
|
|
$this->getUserConnectionEntity()->update($connection, [ |
305
|
|
|
"USER_CONN_ID" => $post["id"] |
306
|
|
|
]); |
307
|
|
|
|
308
|
|
|
$data["id"] = $post["id"]; |
309
|
|
|
|
310
|
|
|
# SUCCESS-MESSAGE |
311
|
|
|
$data["process"] = "success"; |
312
|
|
|
} |
313
|
|
|
catch (\Drone\Exception\Exception $e) |
314
|
|
|
{ |
315
|
|
|
# ERROR-MESSAGE |
316
|
|
|
$data["process"] = "warning"; |
317
|
|
|
$data["message"] = $e->getMessage(); |
318
|
|
|
} |
319
|
|
|
catch (\Exception $e) |
320
|
|
|
{ |
321
|
|
|
$file = str_replace('\\', '', __CLASS__); |
322
|
|
|
$storage = new \Drone\Exception\Storage("cache/$file.json"); |
323
|
|
|
|
324
|
|
|
# stores the error code |
325
|
|
|
if (($errorCode = $storage->store($e)) === false) |
|
|
|
|
326
|
|
|
{ |
327
|
|
|
$errors = $storage->getErrors(); |
328
|
|
|
|
329
|
|
|
# if error storing is not possible, handle it (internal app error) |
330
|
|
|
$this->handleErrors($errors, __METHOD__); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
$data["code"] = $errorCode; |
334
|
|
|
$data["message"] = $e->getMessage(); |
335
|
|
|
|
336
|
|
|
$config = include 'config/application.config.php'; |
337
|
|
|
$data["dev_mode"] = $config["environment"]["dev_mode"]; |
338
|
|
|
|
339
|
|
|
# redirect view |
340
|
|
|
$this->setMethod('error'); |
341
|
|
|
|
342
|
|
|
return $data; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
return $data; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Adds a connection |
350
|
|
|
* |
351
|
|
|
* @return array |
352
|
|
|
*/ |
353
|
|
|
public function add() |
354
|
|
|
{ |
355
|
|
|
clearstatcache(); |
356
|
|
|
session_write_close(); |
357
|
|
|
|
358
|
|
|
# data to send |
359
|
|
|
$data = array(); |
360
|
|
|
|
361
|
|
|
# environment settings |
362
|
|
|
$post = $this->getPost(); # catch $_POST |
363
|
|
|
$this->setTerminal(true); # set terminal |
364
|
|
|
|
365
|
|
|
# TRY-CATCH-BLOCK |
366
|
|
|
try { |
367
|
|
|
|
368
|
|
|
if ($this->isGet()) |
369
|
|
|
{ |
370
|
|
|
$types = $data["types"] = $this->getConnectionTypesEntity()->select([]); |
371
|
|
|
|
372
|
|
|
$fields = $this->getConnectionFieldsEntity()->select([]); |
373
|
|
|
|
374
|
|
|
$fieldTypes = []; |
375
|
|
|
|
376
|
|
|
foreach ($types as $type) |
377
|
|
|
{ |
378
|
|
|
$fieldTypes[$type->CONN_TYPE_ID] = []; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
foreach ($fields as $field) |
382
|
|
|
{ |
383
|
|
|
$fieldTypes[$field->CONN_TYPE_ID][$field->CONN_IDENTI_ID] = $field; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
$data["fieldTypes"] = $fieldTypes; |
387
|
|
|
|
388
|
|
|
# SUCCESS-MESSAGE |
389
|
|
|
$data["process"] = "register-form"; |
390
|
|
|
} |
391
|
|
|
else if ($this->isPost()) |
392
|
|
|
{ |
393
|
|
|
# STANDARD VALIDATIONS [check needed arguments] |
394
|
|
|
$needles = ['field', 'type', 'aliasname']; |
395
|
|
|
|
396
|
|
|
array_walk($needles, function(&$item) use ($post) { |
397
|
|
|
if (!array_key_exists($item, $post)) |
398
|
|
|
{ |
399
|
|
|
$http = new Http(); |
400
|
|
|
$http->writeStatus($http::HTTP_BAD_REQUEST); |
401
|
|
|
|
402
|
|
|
die('Error ' . $http::HTTP_BAD_REQUEST .' (' . $http->getStatusText($http::HTTP_BAD_REQUEST) . ')!!'); |
|
|
|
|
403
|
|
|
} |
404
|
|
|
}); |
405
|
|
|
|
406
|
|
|
$components = [ |
407
|
|
|
"attributes" => [ |
408
|
|
|
"type" => [ |
409
|
|
|
"required" => true, |
410
|
|
|
"type" => "number" |
411
|
|
|
], |
412
|
|
|
"aliasname" => [ |
413
|
|
|
"required" => true, |
414
|
|
|
"type" => "text", |
415
|
|
|
"minlength" => 2, |
416
|
|
|
"maxlength" => 100 |
417
|
|
|
], |
418
|
|
|
"field" => [ |
419
|
|
|
"required" => false, |
420
|
|
|
"type" => "text", |
421
|
|
|
"minlength" => 2, |
422
|
|
|
"maxlength" => 50 |
423
|
|
|
], |
424
|
|
|
], |
425
|
|
|
]; |
426
|
|
|
|
427
|
|
|
$options = [ |
428
|
|
|
"type" => [ |
429
|
|
|
"label" => "Type" |
430
|
|
|
], |
431
|
|
|
"aliasname" => [ |
432
|
|
|
"label" => "Connection name", |
433
|
|
|
"validators" => [ |
434
|
|
|
"Alnum" => ["allowWhiteSpace" => true] |
435
|
|
|
], |
436
|
|
|
], |
437
|
|
|
"field" => [ |
438
|
|
|
"label" => "Connection Parameter" |
439
|
|
|
], |
440
|
|
|
]; |
441
|
|
|
|
442
|
|
|
$form = new Form($components); |
443
|
|
|
$form->fill($post); |
444
|
|
|
|
445
|
|
|
$validator = new FormValidator($form, $options); |
446
|
|
|
$validator->validate(); |
447
|
|
|
|
448
|
|
|
$data["validator"] = $validator; |
449
|
|
|
|
450
|
|
|
# form validation |
451
|
|
|
if (!$validator->isValid()) |
452
|
|
|
{ |
453
|
|
|
$data["messages"] = $validator->getMessages(); |
454
|
|
|
throw new \Drone\Exception\Exception("Form validation errors", 300); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
$this->getUserConnectionEntity()->getTableGateway()->getDriver()->getDb()->beginTransaction(); |
458
|
|
|
|
459
|
|
|
$userConnection = new UserConnection(); |
460
|
|
|
$user_conn_id = $this->getUserConnectionEntity()->getTableGateway()->getNextId(); |
|
|
|
|
461
|
|
|
|
462
|
|
|
$userConnection->exchangeArray([ |
463
|
|
|
"USER_CONN_ID" => $user_conn_id, |
464
|
|
|
"USER_ID" => $this->getIdentity(), |
465
|
|
|
"CONN_TYPE_ID" => $post["type"], |
466
|
|
|
"CONNECTION_NAME" => $post["aliasname"], |
467
|
|
|
"STATE" => 'A' |
468
|
|
|
]); |
469
|
|
|
|
470
|
|
|
$this->getUserConnectionEntity()->insert($userConnection); |
471
|
|
|
|
472
|
|
|
foreach ($post['field'][$post["type"]] as $field_number => $field_value) |
473
|
|
|
{ |
474
|
|
|
$userconnectionDetails = new UserConnectionDetails(); |
475
|
|
|
|
476
|
|
|
$userconnectionDetails->exchangeArray([ |
477
|
|
|
"USER_CONN_ID" => $user_conn_id, |
478
|
|
|
"CONN_IDENTI_ID" => $field_number, |
479
|
|
|
"FIELD_VALUE" => $field_value |
480
|
|
|
]); |
481
|
|
|
|
482
|
|
|
$this->getUserConnectionDetailsEntity()->insert($userconnectionDetails); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
$this->getUserConnectionEntity()->getTableGateway()->getDriver()->getDb()->endTransaction(); |
486
|
|
|
|
487
|
|
|
# SUCCESS-MESSAGE |
488
|
|
|
$data["process"] = "process-response"; |
489
|
|
|
} |
490
|
|
|
else |
491
|
|
|
{ |
492
|
|
|
$http = new Http(); |
493
|
|
|
$http->writeStatus($http::HTTP_METHOD_NOT_ALLOWED); |
494
|
|
|
|
495
|
|
|
die('Error ' . $http::HTTP_METHOD_NOT_ALLOWED .' (' . $http->getStatusText($http::HTTP_METHOD_NOT_ALLOWED) . ')!!'); |
|
|
|
|
496
|
|
|
} |
497
|
|
|
} |
498
|
|
|
catch (\Drone\Exception\Exception $e) |
499
|
|
|
{ |
500
|
|
|
# ERROR-MESSAGE |
501
|
|
|
$data["process"] = "warning"; |
502
|
|
|
$data["message"] = $e->getMessage(); |
503
|
|
|
} |
504
|
|
|
catch (\Exception $e) |
505
|
|
|
{ |
506
|
|
|
$file = str_replace('\\', '', __CLASS__); |
507
|
|
|
$storage = new \Drone\Exception\Storage("cache/$file.json"); |
508
|
|
|
|
509
|
|
|
# stores the error code |
510
|
|
|
if (($errorCode = $storage->store($e)) === false) |
|
|
|
|
511
|
|
|
{ |
512
|
|
|
$errors = $storage->getErrors(); |
513
|
|
|
|
514
|
|
|
# if error storing is not possible, handle it (internal app error) |
515
|
|
|
$this->handleErrors($errors, __METHOD__); |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
$data["code"] = $errorCode; |
519
|
|
|
$data["message"] = $e->getMessage(); |
520
|
|
|
|
521
|
|
|
$config = include 'config/application.config.php'; |
522
|
|
|
$data["dev_mode"] = $config["environment"]["dev_mode"]; |
523
|
|
|
|
524
|
|
|
# redirect view |
525
|
|
|
$this->setMethod('error'); |
526
|
|
|
|
527
|
|
|
return $data; |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
return $data; |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* Updates a connection |
535
|
|
|
* |
536
|
|
|
* @return array |
537
|
|
|
*/ |
538
|
|
|
public function edit() |
539
|
|
|
{ |
540
|
|
|
clearstatcache(); |
541
|
|
|
session_write_close(); |
542
|
|
|
|
543
|
|
|
# data to send |
544
|
|
|
$data = array(); |
545
|
|
|
|
546
|
|
|
# environment settings |
547
|
|
|
$post = $this->getPost(); # catch $_POST |
548
|
|
|
$get = $_GET; # catch $_GET |
549
|
|
|
$this->setTerminal(true); # set terminal |
550
|
|
|
|
551
|
|
|
# TRY-CATCH-BLOCK |
552
|
|
|
try { |
553
|
|
|
|
554
|
|
|
if ($this->isGet()) |
555
|
|
|
{ |
556
|
|
|
# STANDARD VALIDATIONS [check needed arguments] |
557
|
|
|
$needles = ['id']; |
558
|
|
|
|
559
|
|
|
array_walk($needles, function(&$item) use ($get) { |
560
|
|
|
if (!array_key_exists($item, $get)) |
561
|
|
|
{ |
562
|
|
|
$http = new Http(); |
563
|
|
|
$http->writeStatus($http::HTTP_BAD_REQUEST); |
564
|
|
|
|
565
|
|
|
die('Error ' . $http::HTTP_BAD_REQUEST .' (' . $http->getStatusText($http::HTTP_BAD_REQUEST) . ')!!'); |
|
|
|
|
566
|
|
|
} |
567
|
|
|
}); |
568
|
|
|
|
569
|
|
|
$types = $data["types"] = $this->getConnectionTypesEntity()->select([]); |
570
|
|
|
|
571
|
|
|
$fields = $this->getConnectionFieldsEntity()->select([]); |
572
|
|
|
|
573
|
|
|
$fieldTypes = []; |
574
|
|
|
|
575
|
|
|
foreach ($types as $type) |
576
|
|
|
{ |
577
|
|
|
$fieldTypes[$type->CONN_TYPE_ID] = []; |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
foreach ($fields as $field) |
581
|
|
|
{ |
582
|
|
|
$fieldTypes[$field->CONN_TYPE_ID][$field->CONN_IDENTI_ID] = $field; |
583
|
|
|
} |
584
|
|
|
|
585
|
|
|
$data["fieldTypes"] = $fieldTypes; |
586
|
|
|
|
587
|
|
|
$connection = $this->getUserConnectionEntity()->select([ |
588
|
|
|
"USER_CONN_ID" => $get["id"] |
589
|
|
|
]); |
590
|
|
|
|
591
|
|
|
if (!count($connection)) |
592
|
|
|
throw new \Exception("The Connection does not exists"); |
593
|
|
|
|
594
|
|
|
$connection = array_shift($connection); |
595
|
|
|
|
596
|
|
|
if ($connection->STATE == 'I') |
597
|
|
|
throw new \Drone\Exception\Exception("This connection was deleted", 300); |
598
|
|
|
|
599
|
|
|
$connection_details = $this->getUserConnectionDetailsEntity()->select([ |
600
|
|
|
"USER_CONN_ID" => $get["id"] |
601
|
|
|
]); |
602
|
|
|
|
603
|
|
|
$_connection_details = []; |
604
|
|
|
|
605
|
|
|
foreach ($connection_details as $details) |
606
|
|
|
{ |
607
|
|
|
$_connection_details[$details->CONN_IDENTI_ID] = $details; |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
$data["connection"] = $connection; |
611
|
|
|
$data["connection_details"] = $_connection_details; |
612
|
|
|
|
613
|
|
|
# SUCCESS-MESSAGE |
614
|
|
|
$data["process"] = "update-form"; |
615
|
|
|
} |
616
|
|
|
else if ($this->isPost()) |
617
|
|
|
{ |
618
|
|
|
# STANDARD VALIDATIONS [check needed arguments] |
619
|
|
|
$needles = ['_conn_id', 'type', 'aliasname']; |
620
|
|
|
|
621
|
|
|
array_walk($needles, function(&$item) use ($post) { |
622
|
|
|
if (!array_key_exists($item, $post)) |
623
|
|
|
{ |
624
|
|
|
$http = new Http(); |
625
|
|
|
$http->writeStatus($http::HTTP_BAD_REQUEST); |
626
|
|
|
|
627
|
|
|
die('Error ' . $http::HTTP_BAD_REQUEST .' (' . $http->getStatusText($http::HTTP_BAD_REQUEST) . ')!!'); |
|
|
|
|
628
|
|
|
} |
629
|
|
|
}); |
630
|
|
|
|
631
|
|
|
$components = [ |
632
|
|
|
"attributes" => [ |
633
|
|
|
"type" => [ |
634
|
|
|
"required" => true, |
635
|
|
|
"type" => "number" |
636
|
|
|
], |
637
|
|
|
"aliasname" => [ |
638
|
|
|
"required" => true, |
639
|
|
|
"type" => "text", |
640
|
|
|
"minlength" => 2, |
641
|
|
|
"maxlength" => 100 |
642
|
|
|
], |
643
|
|
|
"field" => [ |
644
|
|
|
"required" => false, |
645
|
|
|
"type" => "text", |
646
|
|
|
"minlength" => 2, |
647
|
|
|
"maxlength" => 50 |
648
|
|
|
], |
649
|
|
|
], |
650
|
|
|
]; |
651
|
|
|
|
652
|
|
|
$options = [ |
653
|
|
|
"type" => [ |
654
|
|
|
"label" => "Type" |
655
|
|
|
], |
656
|
|
|
"aliasname" => [ |
657
|
|
|
"label" => "Connection name", |
658
|
|
|
"validators" => [ |
659
|
|
|
"Alnum" => ["allowWhiteSpace" => true] |
660
|
|
|
], |
661
|
|
|
], |
662
|
|
|
"field" => [ |
663
|
|
|
"label" => "Connection Param", |
664
|
|
|
], |
665
|
|
|
]; |
666
|
|
|
|
667
|
|
|
$form = new Form($components); |
668
|
|
|
$form->fill($post); |
669
|
|
|
|
670
|
|
|
$validator = new FormValidator($form, $options); |
671
|
|
|
$validator->validate(); |
672
|
|
|
|
673
|
|
|
$data["validator"] = $validator; |
674
|
|
|
|
675
|
|
|
# form validation |
676
|
|
|
if (!$validator->isValid()) |
677
|
|
|
{ |
678
|
|
|
$data["messages"] = $validator->getMessages(); |
679
|
|
|
throw new \Drone\Exception\Exception("Form validation errors"); |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
$this->getUserConnectionEntity()->getTableGateway()->getDriver()->getDb()->beginTransaction(); |
683
|
|
|
|
684
|
|
|
$userConnection = $this->getUserConnectionEntity()->select([ |
685
|
|
|
"USER_CONN_ID" => $post["_conn_id"], |
686
|
|
|
]); |
687
|
|
|
|
688
|
|
|
if (!count($userConnection)) |
689
|
|
|
throw new \Exception("The Connection does not exists"); |
690
|
|
|
|
691
|
|
|
$userConnection = array_shift($userConnection); |
692
|
|
|
|
693
|
|
|
if ($userConnection->STATE == 'I') |
694
|
|
|
throw new \Drone\Exception\Exception("This connection was deleted", 300); |
695
|
|
|
|
696
|
|
|
$userConnection->exchangeArray([ |
697
|
|
|
"CONN_TYPE_ID" => $post["type"], |
698
|
|
|
"CONNECTION_NAME" => $post["aliasname"] |
699
|
|
|
]); |
700
|
|
|
|
701
|
|
|
$this->getUserConnectionEntity()->update($userConnection, [ |
702
|
|
|
"USER_CONN_ID" => $post["_conn_id"], |
703
|
|
|
]); |
704
|
|
|
|
705
|
|
|
$this->getUserConnectionDetailsEntity()->delete([ |
706
|
|
|
"USER_CONN_ID" => $post["_conn_id"] |
707
|
|
|
]); |
708
|
|
|
|
709
|
|
|
foreach ($post['field'][$post["type"]] as $field_number => $field_value) |
710
|
|
|
{ |
711
|
|
|
$userconnectionDetails = new UserConnectionDetails(); |
712
|
|
|
|
713
|
|
|
$userconnectionDetails->exchangeArray([ |
714
|
|
|
"USER_CONN_ID" => $post["_conn_id"], |
715
|
|
|
"CONN_IDENTI_ID" => $field_number, |
716
|
|
|
"FIELD_VALUE" => $field_value |
717
|
|
|
]); |
718
|
|
|
|
719
|
|
|
$this->getUserConnectionDetailsEntity()->insert($userconnectionDetails); |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
$this->getUserConnectionEntity()->getTableGateway()->getDriver()->getDb()->endTransaction(); |
723
|
|
|
|
724
|
|
|
# SUCCESS-MESSAGE |
725
|
|
|
$data["process"] = "process-response"; |
726
|
|
|
} |
727
|
|
|
else |
728
|
|
|
{ |
729
|
|
|
$http = new Http(); |
730
|
|
|
$http->writeStatus($http::HTTP_METHOD_NOT_ALLOWED); |
731
|
|
|
|
732
|
|
|
die('Error ' . $http::HTTP_METHOD_NOT_ALLOWED .' (' . $http->getStatusText($http::HTTP_METHOD_NOT_ALLOWED) . ')!!'); |
|
|
|
|
733
|
|
|
} |
734
|
|
|
} |
735
|
|
|
catch (\Drone\Exception\Exception $e) |
736
|
|
|
{ |
737
|
|
|
# ERROR-MESSAGE |
738
|
|
|
$data["process"] = "warning"; |
739
|
|
|
$data["message"] = $e->getMessage(); |
740
|
|
|
} |
741
|
|
|
catch (\Exception $e) |
742
|
|
|
{ |
743
|
|
|
$file = str_replace('\\', '', __CLASS__); |
744
|
|
|
$storage = new \Drone\Exception\Storage("cache/$file.json"); |
745
|
|
|
|
746
|
|
|
# stores the error code |
747
|
|
|
if (($errorCode = $storage->store($e)) === false) |
|
|
|
|
748
|
|
|
{ |
749
|
|
|
$errors = $storage->getErrors(); |
750
|
|
|
|
751
|
|
|
# if error storing is not possible, handle it (internal app error) |
752
|
|
|
$this->handleErrors($errors, __METHOD__); |
753
|
|
|
} |
754
|
|
|
|
755
|
|
|
$data["code"] = $errorCode; |
756
|
|
|
$data["message"] = $e->getMessage(); |
757
|
|
|
|
758
|
|
|
$config = include 'config/application.config.php'; |
759
|
|
|
$data["dev_mode"] = $config["environment"]["dev_mode"]; |
760
|
|
|
|
761
|
|
|
# redirect view |
762
|
|
|
$this->setMethod('error'); |
763
|
|
|
|
764
|
|
|
return $data; |
765
|
|
|
} |
766
|
|
|
|
767
|
|
|
return $data; |
768
|
|
|
} |
769
|
|
|
|
770
|
|
|
private function handleErrors(Array $errors, $method) |
771
|
|
|
{ |
772
|
|
|
if (count($errors)) |
773
|
|
|
{ |
774
|
|
|
$errorInformation = ""; |
775
|
|
|
|
776
|
|
|
foreach ($errors as $errno => $error) |
777
|
|
|
{ |
778
|
|
|
$errorInformation .= |
779
|
|
|
"<strong style='color: #a94442'>". |
780
|
|
|
$method |
781
|
|
|
. "</strong>: <span style='color: #e24f4c'>{$error}</span> \n<br />"; |
782
|
|
|
} |
783
|
|
|
|
784
|
|
|
$hd = @fopen('cache/errors.txt', "a"); |
785
|
|
|
|
786
|
|
|
if (!$hd || !@fwrite($hd, $errorInformation)) |
|
|
|
|
787
|
|
|
{ |
788
|
|
|
# error storing are not mandatory! |
789
|
|
|
} |
790
|
|
|
else |
791
|
|
|
@fclose($hd); |
|
|
|
|
792
|
|
|
|
793
|
|
|
$config = include 'config/application.config.php'; |
794
|
|
|
$dev = $config["environment"]["dev_mode"]; |
795
|
|
|
|
796
|
|
|
if ($dev) |
797
|
|
|
echo $errorInformation; |
798
|
|
|
} |
799
|
|
|
} |
800
|
|
|
} |