From 80d57d8619b4f3d3b426ce677b7102a5cf3504a4 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Tue, 24 Dec 2019 15:50:27 +0100
Subject: [PATCH] FEAT #12636 TIME 2:00 Update custom values

---
 .../ContactCustomFieldController.php          | 20 ++++++++++++++++++-
 .../controllers/CustomFieldController.php     | 20 ++++++++++++++++++-
 2 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/src/app/contact/controllers/ContactCustomFieldController.php b/src/app/contact/controllers/ContactCustomFieldController.php
index 88acf07bfd3..4988691b7e4 100644
--- a/src/app/contact/controllers/ContactCustomFieldController.php
+++ b/src/app/contact/controllers/ContactCustomFieldController.php
@@ -98,7 +98,7 @@ class ContactCustomFieldController
             return $response->withStatus(400)->withJson(['errors' => 'Some values have the same name']);
         }
 
-        $field = ContactCustomFieldListModel::getById(['select' => [1], 'id' => $args['id']]);
+        $field = ContactCustomFieldListModel::getById(['select' => ['type', 'values'], 'id' => $args['id']]);
         if (empty($field)) {
             return $response->withStatus(400)->withJson(['errors' => 'Custom field not found']);
         }
@@ -108,6 +108,24 @@ class ContactCustomFieldController
             return $response->withStatus(400)->withJson(['errors' => 'Custom field with this label already exists']);
         }
 
+        if (in_array($field['type'], ['select', 'checkbox', 'radio'])) {
+            $values = json_decode($field['values'], true);
+            foreach ($values as $key => $value) {
+                if (!empty($body['values'][$key]) && $body['values'][$key] != $value) {
+                    ContactModel::update([
+                        'postSet'   => ['custom_fields' => "jsonb_insert(custom_fields, '{{$args['id']}, 0}', '\"{$body['values'][$key]}\"')"],
+                        'where'     => ["custom_fields->'{$args['id']}' @> ?"],
+                        'data'      => ["\"{$value}\""]
+                    ]);
+                    ContactModel::update([
+                        'postSet'   => ['custom_fields' => "jsonb_set(custom_fields, '{{$args['id']}}', (custom_fields->'{$args['id']}') - '{$value}')"],
+                        'where'     => ['1 = ?'],
+                        'data'      => [1]
+                    ]);
+                }
+            }
+        }
+
         ContactCustomFieldListModel::update([
             'set'   => [
                 'label'  => $body['label'],
diff --git a/src/app/customField/controllers/CustomFieldController.php b/src/app/customField/controllers/CustomFieldController.php
index fe5a1c6b1b3..5620fef61a8 100644
--- a/src/app/customField/controllers/CustomFieldController.php
+++ b/src/app/customField/controllers/CustomFieldController.php
@@ -100,7 +100,7 @@ class CustomFieldController
             return $response->withStatus(400)->withJson(['errors' => 'Some values have the same name']);
         }
 
-        $field = CustomFieldModel::getById(['select' => [1], 'id' => $args['id']]);
+        $field = CustomFieldModel::getById(['select' => ['type', 'values'], 'id' => $args['id']]);
         if (empty($field)) {
             return $response->withStatus(400)->withJson(['errors' => 'Custom field not found']);
         }
@@ -110,6 +110,24 @@ class CustomFieldController
             return $response->withStatus(400)->withJson(['errors' => 'Custom field with this label already exists']);
         }
 
+        if (in_array($field['type'], ['select', 'checkbox', 'radio'])) {
+            $values = json_decode($field['values'], true);
+            foreach ($values as $key => $value) {
+                if (!empty($body['values'][$key]) && $body['values'][$key] != $value) {
+                    ResModel::update([
+                        'postSet'   => ['custom_fields' => "jsonb_insert(custom_fields, '{{$args['id']}, 0}', '\"{$body['values'][$key]}\"')"],
+                        'where'     => ["custom_fields->'{$args['id']}' @> ?"],
+                        'data'      => ["\"{$value}\""]
+                    ]);
+                    ResModel::update([
+                        'postSet'   => ['custom_fields' => "jsonb_set(custom_fields, '{{$args['id']}}', (custom_fields->'{$args['id']}') - '{$value}')"],
+                        'where'     => ['1 = ?'],
+                        'data'      => [1]
+                    ]);
+                }
+            }
+        }
+
         CustomFieldModel::update([
             'set'   => [
                 'label'  => $body['label'],
-- 
GitLab