diff --git a/src/app/contact/controllers/ContactCustomFieldController.php b/src/app/contact/controllers/ContactCustomFieldController.php index 88acf07bfd3c922c9c775eef926cc8522b2f0f7f..4988691b7e47fcbafde442c2c3b76af52da35e22 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 fe5a1c6b1b3622bbbf51d3d7a4ddc847b53fbf75..5620fef61a8bd9f98a9dba5089231c9e934afafc 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'],