From ecd8552d66518836fdca0de37965648bccac2260 Mon Sep 17 00:00:00 2001 From: Damien <damien.burel@maarch.org> Date: Thu, 26 Nov 2020 09:43:35 +0100 Subject: [PATCH] FEAT #10118 TIME 0:15 Add constraint + refactoring --- migration/21.04/2104.sql | 3 ++- sql/structure.sql | 3 ++- .../attachment/models/AttachmentTypeModel.php | 20 +++++++++++++++++++ .../controllers/AlfrescoController.php | 5 ++--- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/migration/21.04/2104.sql b/migration/21.04/2104.sql index e23016251dc..c59f7502adf 100644 --- a/migration/21.04/2104.sql +++ b/migration/21.04/2104.sql @@ -20,6 +20,7 @@ CREATE TABLE attachment_types chrono BOOLEAN NOT NULL, version_enabled BOOLEAN NOT NULL, new_version_default BOOLEAN NOT NULL, - CONSTRAINT attachment_types_pkey PRIMARY KEY (id) + CONSTRAINT attachment_types_pkey PRIMARY KEY (id), + CONSTRAINT attachment_types_unique_key UNIQUE (type_id) ) WITH (OIDS=FALSE); diff --git a/sql/structure.sql b/sql/structure.sql index ad8039d6a94..74ccdb47e9d 100755 --- a/sql/structure.sql +++ b/sql/structure.sql @@ -1505,6 +1505,7 @@ CREATE TABLE attachment_types chrono BOOLEAN NOT NULL, version_enabled BOOLEAN NOT NULL, new_version_default BOOLEAN NOT NULL, - CONSTRAINT attachment_types_pkey PRIMARY KEY (id) + CONSTRAINT attachment_types_pkey PRIMARY KEY (id), + CONSTRAINT attachment_types_unique_key UNIQUE (type_id) ) WITH (OIDS=FALSE); diff --git a/src/app/attachment/models/AttachmentTypeModel.php b/src/app/attachment/models/AttachmentTypeModel.php index 34bb8587dda..9706086e0e6 100644 --- a/src/app/attachment/models/AttachmentTypeModel.php +++ b/src/app/attachment/models/AttachmentTypeModel.php @@ -58,6 +58,26 @@ class AttachmentTypeModel return $type[0]; } + public static function getByTypeId(array $args) + { + ValidatorModel::notEmpty($args, ['typeId']); + ValidatorModel::stringType($args, ['typeId']); + ValidatorModel::arrayType($args, ['select']); + + $type = DatabaseModel::select([ + 'select' => empty($args['select']) ? ['*'] : $args['select'], + 'table' => ['attachment_types'], + 'where' => ['type_id = ?'], + 'data' => [$args['typeId']], + ]); + + if (empty($type[0])) { + return []; + } + + return $type[0]; + } + public static function create(array $args) { ValidatorModel::notEmpty($args, ['type_id', 'label', 'visible', 'email_link', 'signable', 'version_enabled', 'new_version_default', 'chrono']); diff --git a/src/app/external/alfresco/controllers/AlfrescoController.php b/src/app/external/alfresco/controllers/AlfrescoController.php index e79ee9bd89e..ff833e09cc7 100644 --- a/src/app/external/alfresco/controllers/AlfrescoController.php +++ b/src/app/external/alfresco/controllers/AlfrescoController.php @@ -795,9 +795,8 @@ class AlfrescoController if (!empty($alfrescoParameters['mapping']['attachment'])) { foreach ($alfrescoParameters['mapping']['attachment'] as $key => $alfrescoParameter) { if ($alfrescoParameter == 'typeLabel') { - $attachmentsTypes = AttachmentTypeModel::get(['select' => ['type_id', 'label']]); - $attachmentsTypes = array_column($attachmentsTypes, 'label', 'type_id'); - $properties[$key] = $attachmentsTypes[$attachment['attachment_type']]; + $attachmentType = AttachmentTypeModel::getByTypeId(['select' => ['label'], 'typeId' => $attachment['attachment_type']]); + $properties[$key] = $attachmentType['label'] ?? ''; } else { $properties[$key] = $attachment[$alfrescoParameter]; } -- GitLab