From 299e39467029457c6a42a205b54f1bf571dff235 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 6 Apr 2020 11:36:37 -0400 Subject: [PATCH] Cleanup customField prepareCreate function --- CRM/Core/BAO/CustomField.php | 69 ++++++++++++++---------------------- 1 file changed, 26 insertions(+), 43 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index d7d1fc3cd332..499fa0ab46b9 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -1876,52 +1876,40 @@ protected static function prepareCreate($params) { } } else { - $params['column_name'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', - $params['id'], - 'column_name' - ); + $params['column_name'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'column_name'); } - switch (CRM_Utils_Array::value('html_type', $params)) { - case 'Select Date': - if (empty($params['date_format'])) { - $config = CRM_Core_Config::singleton(); - $params['date_format'] = $config->dateInputFormat; - } - break; + $htmlType = $params['html_type'] ?? NULL; + $dataType = $params['data_type'] ?? NULL; - case 'CheckBox': - case 'Multi-Select': - if (isset($params['default_checkbox_option'])) { - $tempArray = array_keys($params['default_checkbox_option']); - $defaultArray = []; - foreach ($tempArray as $k => $v) { - if ($params['option_value'][$v]) { - $defaultArray[] = $params['option_value'][$v]; - } - } + if ($htmlType === 'Select Date' && empty($params['date_format'])) { + $params['date_format'] = Civi::settings()->get('dateInputFormat'); + } - if (!empty($defaultArray)) { - // also add the separator before and after the value per new convention (CRM-1604) - $params['default_value'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $defaultArray) . CRM_Core_DAO::VALUE_SEPARATOR; + if ($htmlType === 'CheckBox' || $htmlType === 'Multi-Select') { + if (isset($params['default_checkbox_option'])) { + $defaultArray = []; + foreach (array_keys($params['default_checkbox_option']) as $k => $v) { + if ($params['option_value'][$v]) { + $defaultArray[] = $params['option_value'][$v]; } } - else { - if (!empty($params['default_option']) && isset($params['option_value'][$params['default_option']]) - ) { - $params['default_value'] = $params['option_value'][$params['default_option']]; - } + + if (!empty($defaultArray)) { + // also add the separator before and after the value per new convention (CRM-1604) + $params['default_value'] = CRM_Utils_Array::implodePadded($defaultArray); } - break; + } + else { + if (!empty($params['default_option']) && isset($params['option_value'][$params['default_option']])) { + $params['default_value'] = $params['option_value'][$params['default_option']]; + } + } } - $htmlType = $params['html_type'] ?? NULL; - $dataType = $params['data_type'] ?? NULL; - $allowedOptionTypes = ['String', 'Int', 'Float', 'Money']; - // create any option group & values if required - if ($htmlType != 'Text' && in_array($dataType, $allowedOptionTypes) - ) { + $allowedOptionTypes = ['String', 'Int', 'Float', 'Money']; + if ($htmlType != 'Text' && in_array($dataType, $allowedOptionTypes)) { //CRM-16659: if option_value then create an option group for this custom field. if ($params['option_type'] == 1 && (empty($params['option_group_id']) || !empty($params['option_value']))) { // first create an option group for this custom group @@ -1952,14 +1940,9 @@ protected static function prepareCreate($params) { // retrieve it from one of the other custom fields which use this option group if (empty($params['default_value'])) { //don't insert only value separator as default value, CRM-4579 - $defaultValue = self::getOptionGroupDefault($params['option_group_id'], - $htmlType - ); + $defaultValue = self::getOptionGroupDefault($params['option_group_id'], $htmlType); - if (!CRM_Utils_System::isNull(explode(CRM_Core_DAO::VALUE_SEPARATOR, - $defaultValue - )) - ) { + if (!CRM_Utils_System::isNull(explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaultValue))) { $params['default_value'] = $defaultValue; } }