Skip to content

Commit

Permalink
Merge pull request #16996 from colemanw/customCleanup
Browse files Browse the repository at this point in the history
[REF] Cleanup customField prepareCreate function
  • Loading branch information
eileenmcnaughton authored Apr 7, 2020
2 parents 0f8ba10 + 299e394 commit 6e2d65e
Showing 1 changed file with 26 additions and 43 deletions.
69 changes: 26 additions & 43 deletions CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 6e2d65e

Please sign in to comment.