diff --git a/src/app/resource/controllers/IndexingController.php b/src/app/resource/controllers/IndexingController.php index 5a39996a909f6cb094b293470ebb5a885b09aba0..4d07dd32ccbb7463b6d072fcae9d340cf0cb177c 100755 --- a/src/app/resource/controllers/IndexingController.php +++ b/src/app/resource/controllers/IndexingController.php @@ -19,6 +19,7 @@ use Action\models\ActionModel; use Doctype\models\DoctypeModel; use Entity\models\EntityModel; use Group\models\GroupModel; +use IndexingModel\models\IndexingModelFieldModel; use Parameter\models\ParameterModel; use Priority\models\PriorityModel; use Resource\models\ResModel; @@ -77,13 +78,14 @@ class IndexingController return $response->withStatus(400)->withJson(['errors' => 'Action is not linked to this group']); } - $action = ActionModel::getById(['id' => $args['actionId'], 'select' => ['component']]); + $action = ActionModel::getById(['id' => $args['actionId'], 'select' => ['component', 'required_fields']]); if (empty($action['component'])) { return $response->withStatus(400)->withJson(['errors' => 'Action component does not exist']); } if (!array_key_exists($action['component'], ActionMethodController::COMPONENTS_ACTIONS)) { return $response->withStatus(400)->withJson(['errors' => 'Action method does not exist']); } + $actionRequiredFields = json_decode($action['required_fields']); $resource = ResModel::getById(['resId' => $body['resource'], 'select' => ['status']]); if (empty($resource)) { @@ -95,6 +97,25 @@ class IndexingController $body['data'] = empty($body['data']) ? [] : $body['data']; $body['note'] = empty($body['note']) ? null : $body['note']; + if (!empty($actionRequiredFields)) { + $resource = ResModel::getById(['resId' => $body['resource'], 'select' => ['model_id', 'custom_fields']]); + $model = $resource['model_id']; + $resourceCustomFields = json_decode($resource['custom_fields'], true); + $modelFields = IndexingModelFieldModel::get([ + 'select' => ['identifier'], + 'where' => ['model_id = ?', "identifier LIKE 'indexingCustomField_%'"], + 'data' => [$model] + ]); + $modelFields = array_column($modelFields, 'identifier'); + + foreach ($actionRequiredFields as $actionRequiredField) { + $idCustom = explode("_", $actionRequiredField)[1]; + if (in_array($actionRequiredField, $modelFields) && empty($resourceCustomFields[$idCustom])) { + return $response->withStatus(400)->withJson(['errors' => 'Missing required custom field to do action']); + } + } + } + $method = ActionMethodController::COMPONENTS_ACTIONS[$action['component']]; if (!empty($method)) { $methodResponse = ActionMethodController::$method(['resId' => $body['resource'], 'data' => $body['data'], 'note' => $body['note']]); diff --git a/src/app/resource/controllers/ResourceListController.php b/src/app/resource/controllers/ResourceListController.php index 445214718ef641259966a284e38512e638161da2..a80acb38550cb2a743d177ef04e653c7cede3b52 100644 --- a/src/app/resource/controllers/ResourceListController.php +++ b/src/app/resource/controllers/ResourceListController.php @@ -26,6 +26,7 @@ use Entity\models\EntityModel; use Entity\models\ListInstanceModel; use Folder\models\FolderModel; use Group\models\GroupModel; +use IndexingModel\models\IndexingModelFieldModel; use Note\models\NoteModel; use Priority\models\PriorityModel; use Resource\models\ResModel; @@ -388,13 +389,14 @@ class ResourceListController return $response->withStatus(400)->withJson(['errors' => 'Action is not linked to this group basket']); } - $action = ActionModel::getById(['id' => $aArgs['actionId'], 'select' => ['component']]); + $action = ActionModel::getById(['id' => $aArgs['actionId'], 'select' => ['component', 'required_fields']]); if (empty($action['component'])) { return $response->withStatus(400)->withJson(['errors' => 'Action component does not exist']); } if (!array_key_exists($action['component'], ActionMethodController::COMPONENTS_ACTIONS)) { return $response->withStatus(400)->withJson(['errors' => 'Action method does not exist']); } + $actionRequiredFields = json_decode($action['required_fields']); $user = UserModel::getById(['id' => $aArgs['userId'], 'select' => ['user_id']]); $whereClause = PreparedClauseController::getPreparedClause(['clause' => $basket['basket_clause'], 'login' => $user['user_id']]); @@ -437,6 +439,25 @@ class ResourceListController $method = ActionMethodController::COMPONENTS_ACTIONS[$action['component']]; $methodResponses = []; foreach ($resourcesForAction as $key => $resId) { + if (!empty($actionRequiredFields)) { + $resource = ResModel::getById(['resId' => $resId, 'select' => ['model_id', 'custom_fields']]); + $model = $resource['model_id']; + $resourceCustomFields = json_decode($resource['custom_fields'], true); + $modelFields = IndexingModelFieldModel::get([ + 'select' => ['identifier'], + 'where' => ['model_id = ?', "identifier LIKE 'indexingCustomField_%'"], + 'data' => [$model] + ]); + $modelFields = array_column($modelFields, 'identifier'); + + foreach ($actionRequiredFields as $actionRequiredField) { + $idCustom = explode("_", $actionRequiredField)[1]; + if (in_array($actionRequiredField, $modelFields) && empty($resourceCustomFields[$idCustom])) { + return $response->withStatus(400)->withJson(['errors' => 'Missing required custom field to do action']); + } + } + } + if (!empty($method)) { $methodResponse = ActionMethodController::$method(['resId' => $resId, 'data' => $body['data'], 'note' => $body['note']]);