Skip to content
Snippets Groups Projects
Commit b5806c5a authored by Guillaume Heurtier's avatar Guillaume Heurtier
Browse files

FEAT #11272 TIME 5:00 model creation now support child model

parent 642ef817
No related branches found
No related tags found
No related merge requests found
......@@ -60,11 +60,47 @@ class IndexingModelController
$categories = ResModel::getCategories();
$categories = array_column($categories, 'id');
if (!Validator::stringType()->notEmpty()->validate($body['label'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body label is empty or not a string']);
} elseif (!Validator::stringType()->notEmpty()->validate($body['category']) || !in_array($body['category'], $categories)) {
return $response->withStatus(400)->withJson(['errors' => "Body category is empty, not a string or not a valid category"]);
}
if (Validator::intVal()->notEmpty()->validate($body['master'])) {
$masterModel = IndexingModelModel::getById(['id' => $body['master']]);
if (empty($masterModel)) {
return $response->withStatus(400)->withJson(['errors' => 'Master model not found']);
}
$fieldsMaster = IndexingModelFieldModel::get(['select' => ['identifier', 'mandatory', 'default_value', 'unit'], 'where' => ['model_id = ?'], 'data' => [$body['master']]]);
foreach ($fieldsMaster as $key => $value) {
$fieldsMaster[$key]['default_value'] = json_decode($value['default_value'], true);
}
// Look for fields in master model
// if field in master is not in child, return an error
// if field is not in master but in child, is ignored
$arrayTmp = [];
foreach ($fieldsMaster as $field) {
$found = false;
foreach ($body['fields'] as $value) {
if ($value['identifier'] == $field['identifier'] && $value['mandatory'] == $field['mandatory'] && $value['unit'] == $field['unit']) {
array_push($arrayTmp, $value);
$found = true;
break;
}
}
if (!$found) {
return $response->withStatus(400)->withJson(['errors' => "Field '" . $field['identifier'] . "' from master model is missing"]);
}
}
$body['fields'] = $arrayTmp;
}
foreach ($body['fields'] as $key => $field) {
if (!Validator::stringType()->notEmpty()->validate($field['identifier'])) {
return $response->withStatus(400)->withJson(['errors' => "Body fields[{$key}] identifier is empty or not a string"]);
......@@ -85,7 +121,8 @@ class IndexingModelController
'category' => $body['category'],
'default' => $body['default'],
'owner' => $GLOBALS['id'],
'private' => $body['private']
'private' => $body['private'],
'master' => $body['master']
]);
foreach ($body['fields'] as $field) {
......
......@@ -72,7 +72,8 @@ class IndexingModelModel
'category' => $args['category'],
'"default"' => $args['default'],
'owner' => $args['owner'],
'private' => $args['private']
'private' => $args['private'],
'master' => $args['master']
]
]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment