CRM-12768 fix & document api option group creation on custom_field.create'
authoreileen <eileen@fuzion.co.nz>
Thu, 6 Jun 2013 03:34:00 +0000 (15:34 +1200)
committereileen <eileen@fuzion.co.nz>
Thu, 6 Jun 2013 03:34:00 +0000 (15:34 +1200)
CRM/Core/BAO/CustomField.php
api/v3/CustomField.php
tests/phpunit/api/v3/CustomFieldTest.php

index f6687f5c6587fc9345e46a7d17563c7dcdccbcda..892b98a01b782c602c10ef66323fa2bf27a020ac 100644 (file)
@@ -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();
           }
         }
       }
index 149075df6de50b9bdc24e3c22e88e0006e01b6ca..7eeda3daf2b330199c748b81ca23efc96e3ccb74 100644 (file)
  */
 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,
+  );
 }
 
 /**
index aa842a64ef27a86a12c171bf14b2bbd9c2431405..9deb78b5b2ceff04b6e22434af6c160f0ddcf08c 100644 (file)
@@ -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);
   }
 }