From 1e93ec375837ee0bd08f734923e82d47e31c8cb1 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Mon, 22 Jun 2020 15:15:52 +0200
Subject: [PATCH] FEAT #13195 TIME 3:20 Custom fields fixes

---
 .../controllers/MergeController.php           |  5 +++--
 .../controllers/CustomFieldController.php     | 19 ++++++++++++++++++-
 .../resource/controllers/ExportController.php |  2 +-
 .../controllers/SummarySheetController.php    |  5 +++--
 src/frontend/lang/lang-en.ts                  |  2 ++
 src/frontend/lang/lang-fr.ts                  |  2 ++
 src/frontend/lang/lang-nl.ts                  |  3 +++
 7 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/src/app/contentManagement/controllers/MergeController.php b/src/app/contentManagement/controllers/MergeController.php
index 3fb25ac34ed..735427a32ad 100644
--- a/src/app/contentManagement/controllers/MergeController.php
+++ b/src/app/contentManagement/controllers/MergeController.php
@@ -283,16 +283,17 @@ class MergeController
         $customFieldsIds = array_keys($customs);
         if (!empty($customFieldsIds)) {
             $customFields = CustomFieldModel::get([
-                'select' => ['id', 'values'],
+                'select' => ['id', 'values', 'type'],
                 'where'  => ['id in (?)'],
                 'data'   => [$customFieldsIds]
             ]);
             $customFieldsValues = array_column($customFields, 'values', 'id');
+            $customFieldsTypes = array_column($customFields, 'type', 'id');
 
             foreach ($customs as $customId => $custom) {
                 $rawValues = json_decode($customFieldsValues[$customId], true);
 
-                if (!empty($rawValues['table'])) {
+                if (!empty($rawValues['table']) && in_array($customFieldsTypes[$customId], ['radio', 'select', 'checkbox'])) {
                     $rawValues = CustomFieldModel::getValuesSQL($rawValues);
                     $rawValues = array_column($rawValues, 'label', 'key');
                     if (is_array($custom)) {
diff --git a/src/app/customField/controllers/CustomFieldController.php b/src/app/customField/controllers/CustomFieldController.php
index cec30b6c346..d514e70d3cf 100644
--- a/src/app/customField/controllers/CustomFieldController.php
+++ b/src/app/customField/controllers/CustomFieldController.php
@@ -30,6 +30,8 @@ use SrcCore\models\CoreConfigModel;
 
 class CustomFieldController
 {
+    const NUMERIC_TYPES = ['smallint', 'integer', 'bigint', 'decimal', 'numeric', 'real', 'double precision', 'serial', 'bigserial'];
+
     public function get(Request $request, Response $response)
     {
         $queryParams = $request->getQueryParams();
@@ -42,6 +44,11 @@ class CustomFieldController
             if (empty($queryParams['admin']) || !PrivilegeController::hasPrivilege(['privilegeId' => 'admin_custom_fields', 'userId' => $GLOBALS['id']])) {
                 if (!empty($customFields[$key]['values']['table'])) {
                     $customFields[$key]['values'] = CustomFieldModel::getValuesSQL($customFields[$key]['values']);
+                    if ($customField['type'] == 'string') {
+                        $customFields[$key]['values'][0]['key'] = (string)$customFields[$key]['values'][0]['key'];
+                    } elseif ($customField['type'] == 'integer') {
+                        $customFields[$key]['values'][0]['key'] = (int)$customFields[$key]['values'][0]['key'];
+                    }
                 } elseif (!empty($customFields[$key]['values'])) {
                     $values = $customFields[$key]['values'];
                     $customFields[$key]['values'] = [];
@@ -308,9 +315,19 @@ class CustomFieldController
                 return ['errors' => 'Body values[label] column is not allowed'];
             }
             if ($body['type'] == 'date' && stripos($columns[$value['column']], 'timestamp') === false) {
-                return ['errors' => 'Body values[label] column is not a date'];
+                return ['errors' => 'Body values[label] column is not a date', 'lang' => 'invalidColumnType'];
+            } elseif ($body['type'] == 'integer' && !in_array($columns[$value['column']], self::NUMERIC_TYPES)) {
+                return ['errors' => 'Body values[label] column is not an integer', 'lang' => 'invalidColumnType'];
+            } elseif (in_array($body['type'], ['date', 'integer']) && (!empty($value['delimiterStart']) || !empty($value['delimiterEnd']))) {
+                return ['errors' => 'Delimiters are forbidden for this type', 'lang' => 'forbiddenDelimiterType'];
             }
         }
+        if ($body['type'] == 'date' && stripos($columns[$body['values']['key']], 'timestamp') === false) {
+            return ['errors' => 'Body values[label] column is not a date', 'lang' => 'invalidColumnType'];
+        }
+        if ($body['type'] == 'integer' && !in_array($columns[$body['values']['key']], self::NUMERIC_TYPES)) {
+            return ['errors' => 'Body values[label] column is not an integer', 'lang' => 'invalidColumnType'];
+        }
         if (stripos($body['values']['clause'], 'select') !== false) {
             return ['errors' => 'Clause is not valid', 'lang' => 'invalidClause'];
         }
diff --git a/src/app/resource/controllers/ExportController.php b/src/app/resource/controllers/ExportController.php
index 9f0b5029a2e..a93ab9b18eb 100755
--- a/src/app/resource/controllers/ExportController.php
+++ b/src/app/resource/controllers/ExportController.php
@@ -746,7 +746,7 @@ class ExportController
             $line .= "\n";
             $line .= "{$customValues[0]['latitude']},{$customValues[0]['longitude']}";
             $customValues = $line;
-        } elseif (!empty($values['table'])) {
+        } elseif (!empty($values['table']) && in_array($field['type'], ['radio', 'select', 'checkbox'])) {
             $values = CustomFieldModel::getValuesSQL($values);
 
             $values = array_column($values, 'label', 'key');
diff --git a/src/app/resource/controllers/SummarySheetController.php b/src/app/resource/controllers/SummarySheetController.php
index a6df23063a3..aa0695b597e 100755
--- a/src/app/resource/controllers/SummarySheetController.php
+++ b/src/app/resource/controllers/SummarySheetController.php
@@ -318,12 +318,13 @@ class SummarySheetController
                 if (!empty($customFieldsIds)) {
                     // get the label of the custom fields
                     $customFields = CustomFieldModel::get([
-                        'select' => ['id', 'label', 'values'],
+                        'select' => ['id', 'label', 'values', 'type'],
                         'where'  => ['id in (?)'],
                         'data'   => [$customFieldsIds]
                     ]);
 
                     $customFieldsRawValues = array_column($customFields, 'values', 'id');
+                    $customFieldsRawTypes = array_column($customFields, 'type', 'id');
                     $customFields = array_column($customFields, 'label', 'id');
 
                     $customFieldsValues = $customFieldsValues[0]['custom_fields'] ?? null;
@@ -357,7 +358,7 @@ class SummarySheetController
                     foreach ($customFieldsIds as $customFieldsId) {
                         $label = $customFields[$customFieldsId];
                         $rawValues = json_decode($customFieldsRawValues[$customFieldsId], true);
-                        if (!empty($rawValues['table'])) {
+                        if (!empty($rawValues['table']) && in_array($customFieldsRawTypes[$customFieldsId], ['radio', 'select', 'checkbox'])) {
                             $rawValues = CustomFieldModel::getValuesSQL($rawValues);
 
                             $rawValues = array_column($rawValues, 'label', 'key');
diff --git a/src/frontend/lang/lang-en.ts b/src/frontend/lang/lang-en.ts
index ba2ba9c8e46..40c7e996e3c 100755
--- a/src/frontend/lang/lang-en.ts
+++ b/src/frontend/lang/lang-en.ts
@@ -1692,6 +1692,8 @@ export const LANG_EN = {
     "testSucceeded": "Test succeeded",
     "test": "Test",
     "invalidClause" : "Invalid clause",
+    "invalidColumnType" : "One of selected columns does not match type",
+    "forbiddenDelimiterType" : "Delimiters are forbidden for this type",
     "canUpdateIndexingModel": "Can change indexing model",
     "options": "Options",
     "contactsDuplicates": "Contacts duplicates",
diff --git a/src/frontend/lang/lang-fr.ts b/src/frontend/lang/lang-fr.ts
index a1ddc38cf6d..15dcaf9a49c 100755
--- a/src/frontend/lang/lang-fr.ts
+++ b/src/frontend/lang/lang-fr.ts
@@ -1694,6 +1694,8 @@ export const LANG_FR = {
     "testSucceeded": "Le test a réussi",
     "test": "Tester",
     "invalidClause" : "Clause non valide",
+    "invalidColumnType" : "Une des colonnes selectionnées ne correspond pas au type choisi",
+    "forbiddenDelimiterType" : "Les délimiteurs sont interdits pour ce type",
     "canUpdateIndexingModel": "Pouvoir changer le modèle d'indexation",
     "options": "Options",
     "contactsDuplicates": "Doublons de contacts",
diff --git a/src/frontend/lang/lang-nl.ts b/src/frontend/lang/lang-nl.ts
index 92df8625ccd..3f56710b505 100755
--- a/src/frontend/lang/lang-nl.ts
+++ b/src/frontend/lang/lang-nl.ts
@@ -1658,6 +1658,9 @@ export const LANG_NL = {
     "accountLocked": "Too many connections attemps. Retry in", //_TO_TRANSLATE
     "modelUsedByResources": "This model is used by resources, you can't delete it.", //_TO_TRANSLATE
     "mustChangePassword": "Please, you must change your password.", //_TO_TRANSLATE
+    "invalidClause" : "Invalid clause",//_TO_TRANSLATE
+    "invalidColumnType" : "One of selected columns does not match type",//_TO_TRANSLATE
+    "forbiddenDelimiterType" : "Delimiters are forbidden for this type", //_TO_TRANSLATE
     "linkedResources": "Main document (Linked mails)", //_TO_TRANSLATE
     "linkedResourcesAttachments": "Attachments (linked mails)", //_TO_TRANSLATE
     "accessNotFound": "Access not found", //_TO_TRANSLATE
-- 
GitLab