From 554a91f60164873c0cac331ede2fa2b2fc7bf8eb Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 27 Jun 2019 15:23:55 +1200 Subject: [PATCH] [REF] extract createOptionValue function in CustomField::create --- CRM/Core/BAO/CustomField.php | 65 +++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 36391dba09..e6ab4b38d0 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -237,32 +237,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { $params['option_group_id'] = $optionGroup->id; if (!empty($params['option_value']) && is_array($params['option_value'])) { foreach ($params['option_value'] as $k => $v) { - if (strlen(trim($v))) { - $optionValue = new CRM_Core_DAO_OptionValue(); - $optionValue->option_group_id = $optionGroup->id; - $optionValue->label = $params['option_label'][$k]; - $optionValue->name = CRM_Utils_String::titleToVar($params['option_label'][$k]); - switch ($dataType) { - case 'Money': - $optionValue->value = CRM_Utils_Rule::cleanMoney($v); - break; - - case 'Int': - $optionValue->value = intval($v); - break; - - case 'Float': - $optionValue->value = floatval($v); - break; - - default: - $optionValue->value = trim($v); - } - - $optionValue->weight = $params['option_weight'][$k]; - $optionValue->is_active = CRM_Utils_Array::value($k, $params['option_status'], FALSE); - $optionValue->save(); - } + self::createOptionValue($params, $v, $optionGroup, $k, $dataType); } } } @@ -2014,6 +1989,44 @@ WHERE id IN ( %1, %2 ) CRM_Utils_System::flushCache(); } + /** + * Create an option value for a custom field option group ID. + * + * @param array $params + * @param string $value + * @param \CRM_Core_DAO_OptionGroup $optionGroup + * @param string $optionName + * @param string $dataType + */ + protected static function createOptionValue(&$params, $value, CRM_Core_DAO_OptionGroup $optionGroup, $optionName, $dataType) { + if (strlen(trim($value))) { + $optionValue = new CRM_Core_DAO_OptionValue(); + $optionValue->option_group_id = $optionGroup->id; + $optionValue->label = $params['option_label'][$optionName]; + $optionValue->name = CRM_Utils_String::titleToVar($params['option_label'][$optionName]); + switch ($dataType) { + case 'Money': + $optionValue->value = CRM_Utils_Rule::cleanMoney($value); + break; + + case 'Int': + $optionValue->value = intval($value); + break; + + case 'Float': + $optionValue->value = floatval($value); + break; + + default: + $optionValue->value = trim($value); + } + + $optionValue->weight = $params['option_weight'][$optionName]; + $optionValue->is_active = CRM_Utils_Array::value($optionName, $params['option_status'], FALSE); + $optionValue->save(); + } + } + /** * Move custom data from one contact to another. * -- 2.25.1