diff --git a/src/app/indexingModel/controllers/IndexingModelController.php b/src/app/indexingModel/controllers/IndexingModelController.php index 5231fdd94c0735f991fccf8d4bd157d30fe27589..82acbc76a3f8a54b26ff61a9772c5528e49488bf 100644 --- a/src/app/indexingModel/controllers/IndexingModelController.php +++ b/src/app/indexingModel/controllers/IndexingModelController.php @@ -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) { diff --git a/src/app/indexingModel/models/IndexingModelModel.php b/src/app/indexingModel/models/IndexingModelModel.php index 13ef9c3619dbd93f2e43a636e2a86cf756b6f748..a829531372dcfbf4925be0e7dbb9b48aa22b44f1 100644 --- a/src/app/indexingModel/models/IndexingModelModel.php +++ b/src/app/indexingModel/models/IndexingModelModel.php @@ -72,7 +72,8 @@ class IndexingModelModel 'category' => $args['category'], '"default"' => $args['default'], 'owner' => $args['owner'], - 'private' => $args['private'] + 'private' => $args['private'], + 'master' => $args['master'] ] ]);