From b958933f72f7a6b2d7c0be78e739e81576815119 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 6 Jun 2013 15:34:00 +1200 Subject: [PATCH] CRM-12768 fix & document api option group creation on custom_field.create' --- CRM/Core/BAO/CustomField.php | 56 ++++++++++++------------ api/v3/CustomField.php | 15 +++---- tests/phpunit/api/v3/CustomFieldTest.php | 37 +++++++++++++++- 3 files changed, 67 insertions(+), 41 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index f6687f5c65..892b98a01b 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -179,8 +179,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { // create any option group & values if required if ($params['html_type'] != 'Text' && in_array($params['data_type'], array( - 'String', 'Int', 'Float', 'Money')) && - !empty($params['option_value']) && is_array($params['option_value']) + 'String', 'Int', 'Float', 'Money')) ) { $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', @@ -197,35 +196,34 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { $optionGroup->is_active = 1; $optionGroup->save(); $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 ($params['data_type']) { + 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); + } - - - 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 ($params['data_type']) { - 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(); } - - $optionValue->weight = $params['option_weight'][$k]; - $optionValue->is_active = CRM_Utils_Array::value($k, $params['option_status'], FALSE); - $optionValue->save(); } } } diff --git a/api/v3/CustomField.php b/api/v3/CustomField.php index 149075df6d..7eeda3daf2 100644 --- a/api/v3/CustomField.php +++ b/api/v3/CustomField.php @@ -65,16 +65,6 @@ */ function civicrm_api3_custom_field_create($params) { - if (!(CRM_Utils_Array::value('option_type', $params))) { - if (CRM_Utils_Array::value('id', $params)) { - $params['option_type'] = 2; - } - else { - $params['option_type'] = 1; - } - } - - // Array created for passing options in params if (isset($params['option_values']) && is_array($params['option_values'])) { foreach ($params['option_values'] as $key => $value) { @@ -98,6 +88,11 @@ function civicrm_api3_custom_field_create($params) { function _civicrm_api3_custom_field_create_spec(&$params) { $params['label']['api.required'] = 1; $params['custom_group_id']['api.required'] = 1; + $params['option_type'] = array( + 'title' => 'This (boolean) field tells the BAO to create an option group for the field if the field type is appropriate', + 'api.default' => 1, + 'type' => CRM_Utils_Type::T_BOOL, + ); } /** diff --git a/tests/phpunit/api/v3/CustomFieldTest.php b/tests/phpunit/api/v3/CustomFieldTest.php index aa842a64ef..9deb78b5b2 100644 --- a/tests/phpunit/api/v3/CustomFieldTest.php +++ b/tests/phpunit/api/v3/CustomFieldTest.php @@ -220,6 +220,39 @@ class api_v3_CustomFieldTest extends CiviUnitTestCase { /** * check with data type - Options with option_values */ + function testCustomFieldCreateWithEmptyOptionGroup() { + $customGroup = $this->customGroupCreate('Contact', 'select_test_group', 3); + $params = array( + 'custom_group_id' => $customGroup['id'], + 'label' => 'Country', + 'html_type' => 'Select', + 'data_type' => 'String', + 'weight' => 4, + 'is_required' => 1, + 'is_searchable' => 0, + 'is_active' => 1, + 'version' => $this->_apiversion, + ); + + $customField = civicrm_api('custom_field', 'create', $params); + $this->assertAPISuccess($customField); + $this->assertNotNull($customField['id']); + $optionGroupID = civicrm_api('custom_field', 'getvalue', array( + 'version' => 3, + 'id' => $customField['id'], + 'return' => 'option_group_id', + )); + + $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0)); + $optionGroup = civicrm_api('option_group', 'getsingle', array( + 'version' => 3, 'id' => $optionGroupID)); + $this->assertEquals($optionGroup['title'],'Country'); + $optionValueCount = civicrm_api('option_value', 'getcount', array( + 'version' => 3, 'option_group_id' => $optionGroupID)); + $this->assertEquals(0, $optionValueCount); + } + + function testCustomFieldCreateWithOptionValues() { $customGroup = $this->customGroupCreate('Contact', 'select_test_group', 3); @@ -252,7 +285,7 @@ class api_v3_CustomFieldTest extends CiviUnitTestCase { $customField = civicrm_api('custom_field', 'create', $params); - $this->assertEquals($customField['is_error'], 0); + $this->assertAPISuccess($customField); $this->assertNotNull($customField['id']); $getFieldsParams = array( 'options' => array('get_options' => 'custom_' . $customField['id']), @@ -329,7 +362,7 @@ class api_v3_CustomFieldTest extends CiviUnitTestCase { ); $customField = civicrm_api('custom_field', 'delete', $customOptionValueFields); - $this->assertEquals($customField['is_error'], 0); + $this->assertAPISuccess($customField); } } -- 2.25.1