Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
* @brief ParametersController
* @author dev <dev@maarch.org>
* @ingroup core
*/
/**
* @brief Indexing Model Controller
* @author dev@maarch.org
*/
namespace IndexingModel\controllers;
use Group\models\ServiceModel;
use IndexingModel\models\IndexingModelFieldModel;
use IndexingModel\models\IndexingModelModel;
use Respect\Validation\Validator;
use Slim\Http\Request;
use Slim\Http\Response;
class IndexingModelController
{
public function create(Request $request, Response $response)
{
$body = $request->getParsedBody();
if (!Validator::stringType()->notEmpty()->validate($body['label'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body label is empty or not a string']);
}
if (ServiceModel::hasService(['id' => 'admin_indexing_models', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) {
$body['private'] = empty($body['private']) ? 'false' : 'true';
} else {
$body['private'] = true;
}
$modelId = IndexingModelModel::create([
'label' => $body['label'],
'default' => 'false',
'owner' => $GLOBALS['id'],
'private' => $body['private']
]);
foreach ($body['fields'] as $field) {
IndexingModelFieldModel::create([
'model_id' => $modelId,
'label' => $field['label'],
'mandatory' => empty($field['mandatory']) ? 'false' : 'true',
'value' => empty($field['value']) ? null : $field['value'],
'unit' => empty($field['unit']) ? null : $field['unit']
]);
}
return $response->withStatus(204);
}
public function update(Request $request, Response $response, array $args)
{
$body = $request->getParsedBody();
if (!Validator::stringType()->notEmpty()->validate($body['label'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body label is empty or not a string']);
}
$model = IndexingModelModel::getById(['select' => ['owner', 'private'], 'id' => $args['id']]);
if (empty($model)) {
return $response->withStatus(400)->withJson(['errors' => 'Model not found']);
} elseif ($model['private'] && $model['owner'] != $GLOBALS['id']) {
return $response->withStatus(400)->withJson(['errors' => 'Model out of perimeter']);
} elseif (!$model['private'] && !ServiceModel::hasService(['id' => 'admin_indexing_models', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) {
return $response->withStatus(400)->withJson(['errors' => 'Model out of perimeter']);
}
IndexingModelModel::update([
'set' => [
'label' => $body['label']
],
'where' => ['id = ?'],
'data' => [$args['id']]
]);
IndexingModelFieldModel::delete(['where' => ['model_id = ?'], 'data' => [$args['id']]]);
foreach ($body['fields'] as $field) {
IndexingModelFieldModel::create([
'model_id' => $args['id'],
'label' => $field['label'],
'mandatory' => empty($field['mandatory']) ? 'false' : 'true',
'value' => empty($field['value']) ? null : $field['value'],
'unit' => empty($field['unit']) ? null : $field['unit']
]);
}
return $response->withStatus(204);
}
public function delete(Request $request, Response $response, array $args)
{
$model = IndexingModelModel::getById(['select' => ['owner', 'private'], 'id' => $args['id']]);
if (empty($model)) {
return $response->withStatus(400)->withJson(['errors' => 'Model not found']);
} elseif ($model['private'] && $model['owner'] != $GLOBALS['id']) {
return $response->withStatus(400)->withJson(['errors' => 'Model out of perimeter']);
} elseif (!$model['private'] && !ServiceModel::hasService(['id' => 'admin_indexing_models', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) {
return $response->withStatus(400)->withJson(['errors' => 'Model out of perimeter']);
}
IndexingModelModel::delete([
'where' => ['id = ?'],
'data' => [$args['id']]
]);
IndexingModelFieldModel::delete(['where' => ['model_id = ?'], 'data' => [$args['id']]]);
return $response->withStatus(204);
}
}