diff --git a/src/app/indexingModel/controllers/IndexingModelController.php b/src/app/indexingModel/controllers/IndexingModelController.php index 3f493ef22d4dd878edce61047e9ed53443fb83eb..7ea73aedbc6169cd9f52a4364af263b0f6a1f10a 100755 --- a/src/app/indexingModel/controllers/IndexingModelController.php +++ b/src/app/indexingModel/controllers/IndexingModelController.php @@ -24,10 +24,13 @@ use History\controllers\HistoryController; use IndexingModel\models\IndexingModelFieldModel; use IndexingModel\models\IndexingModelModel; use Resource\controllers\IndexingController; +use Resource\controllers\ResController; use Resource\models\ResModel; +use Resource\models\ResourceContactModel; use Respect\Validation\Validator; use Slim\Http\Request; use Slim\Http\Response; +use Tag\models\ResourceTagModel; class IndexingModelController { @@ -337,6 +340,30 @@ class IndexingModelController } } + $allResourcesUsingModel = ResModel::get(['select' => ['res_id'], 'where' => ['model_id = ?'], 'data' => [$args['id']]]); + $allResourcesUsingModel = array_column($allResourcesUsingModel, 'res_id'); + + if (!empty($allResourcesUsingModel)) { + $oldFieldList = IndexingModelFieldModel::get(['select' => ['identifier'], 'where' => ['model_id = ?'], 'data' => [$args['id']]]); + $oldFieldList = array_column($oldFieldList, 'identifier'); + + $newFieldList = array_column($body['fields'], 'identifier'); + + ResController::resetResourceFields(['oldFieldList' => $oldFieldList, 'newFieldList' => $newFieldList, 'modelId' => $args['id']]); + + $fieldsToDelete = array_diff($oldFieldList, $newFieldList); + + if (in_array('senders', $fieldsToDelete)) { + ResourceContactModel::delete(['where' => ['res_id in (?)', 'mode = ?'], 'data' => [$allResourcesUsingModel, 'sender']]); + } + if (in_array('recipients', $fieldsToDelete)) { + ResourceContactModel::delete(['where' => ['res_id in (?)', 'mode = ?'], 'data' => [$allResourcesUsingModel, 'recipient']]); + } + if (in_array('tags', $fieldsToDelete)) { + ResourceTagModel::delete(['where' => ['res_id in (?)'], 'data' => [$allResourcesUsingModel]]); + } + } + IndexingModelFieldModel::delete(['where' => ['model_id = ?'], 'data' => [$args['id']]]); foreach ($body['fields'] as $field) { diff --git a/src/app/resource/controllers/ResController.php b/src/app/resource/controllers/ResController.php index 13eb1d84fdfd4ed89451f343ef7b92382f00a60d..1947ac8b60f1f42e52659f5dc2096c3571cde03c 100755 --- a/src/app/resource/controllers/ResController.php +++ b/src/app/resource/controllers/ResController.php @@ -277,38 +277,7 @@ class ResController extends ResourceControlController ]); $newModelFields = array_column($newModelFields, 'identifier'); - $set = []; - $setToNull = [ - 'confidentiality' => 'confidentiality', - 'admission_date' => 'arrivalDate', - 'departure_date' => 'departureDate', - 'doc_date' => 'documentDate', - 'process_limit_date' => 'processLimitDate', - 'initiator' => 'initiator', - 'destination' => 'destination', - 'priority' => 'priority' - ]; - foreach ($setToNull as $key => $field) { - if (in_array($field, $resourceModelFields) && !in_array($field, $newModelFields)) { - $set[$key] = null; - } - } - - $newModelHasCustomFields = false; - foreach ($newModelFields as $newModelField) { - if (strpos($newModelField, 'indexingCustomField_') !== false) { - $newModelHasCustomFields = true; - break; - } - } - - if (!empty($resource['custom_fields']) && !$newModelHasCustomFields) { - $set['custom_fields'] = '{}'; - } - - if (!empty($set)) { - ResModel::update(['set' => $set, 'where' => ['res_id = ?'], 'data' => [$args['resId']]]); - } + ResController::resetResourceFields(['oldFieldList' => $resourceModelFields, 'newFieldList' => $newModelFields, 'resId' => $args['resId']]); } if (!empty($resource['filename']) && !empty($body['encodedFile'])) { @@ -1353,4 +1322,63 @@ class ResController extends ResourceControlController return $response->withJson(['information' => $resource]); } + + public static function resetResourceFields(array $args) + { + ValidatorModel::notEmpty($args, ['oldFieldList', 'newFieldList']); + ValidatorModel::arrayType($args, ['oldFieldList', 'newFieldList']); + ValidatorModel::intVal($args, ['resId', 'modelId']); + + if (empty($args['resId']) && empty($args['modelId'])) { + return false; + } + + $resourceModelFields = $args['oldFieldList']; + $newModelFields = $args['newFieldList']; + + // Set res_letterbox fields to null + $set = []; + $setToNull = [ + 'confidentiality' => 'confidentiality', + 'admission_date' => 'arrivalDate', + 'departure_date' => 'departureDate', + 'doc_date' => 'documentDate', + 'process_limit_date' => 'processLimitDate', + 'initiator' => 'initiator', + 'destination' => 'destination', + 'priority' => 'priority' + ]; + foreach ($setToNull as $key => $field) { + if (in_array($field, $resourceModelFields) && !in_array($field, $newModelFields)) { + $set[$key] = null; + } + } + + // Checking if model has a custom field. If yes, the customs are reset in the update, if not, we set it to an empty JSON object + $newModelHasCustomFields = false; + foreach ($newModelFields as $newModelField) { + if (strpos($newModelField, 'indexingCustomField_') !== false) { + $newModelHasCustomFields = true; + break; + } + } + if (!empty($resource['custom_fields']) && !$newModelHasCustomFields) { + $set['custom_fields'] = '{}'; + } + + if (!empty($set)) { + $where = []; + $data = []; + if (!empty($args['resId'])) { + $where = ['res_id = ?']; + $data = [$args['resId']]; + } elseif (!empty($args['modelId'])) { + $where = ['model_id = ?']; + $data = [$args['modelId']]; + } + ResModel::update(['set' => $set, 'where' => $where, 'data' => $data]); + } + + return true; + } }