From 24871985168cd37bd5d75b23d746ba627132552b Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 2 Jun 2017 13:43:54 -0400 Subject: [PATCH] CRM-20091 - Add customValue.gettree api --- CRM/Core/BAO/CustomGroup.php | 1 + api/v3/CustomValue.php | 121 +++++++++++++++++++++++ tests/phpunit/api/v3/CustomValueTest.php | 20 ++++ 3 files changed, 142 insertions(+) diff --git a/CRM/Core/BAO/CustomGroup.php b/CRM/Core/BAO/CustomGroup.php index 69a1bd6611..74eac1fc6d 100644 --- a/CRM/Core/BAO/CustomGroup.php +++ b/CRM/Core/BAO/CustomGroup.php @@ -385,6 +385,7 @@ class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup { $tableData = array( 'civicrm_custom_field' => array( 'id', + 'name', 'label', 'column_name', 'data_type', diff --git a/api/v3/CustomValue.php b/api/v3/CustomValue.php index 27cf82c4c2..d926068c03 100644 --- a/api/v3/CustomValue.php +++ b/api/v3/CustomValue.php @@ -233,3 +233,124 @@ function _civicrm_api3_custom_value_get_spec(&$params) { $params['entity_id']['api.required'] = 1; $params['entity_id']['title'] = 'Entity ID'; } + +/** + * CustomValue.gettree API specification + * + * @param array $spec description of fields supported by this API call + * @return void + */ +function _civicrm_api3_custom_value_gettree_spec(&$spec) { + $spec['entity_id'] = array( + 'title' => 'Entity Id', + 'description' => 'Id of entity', + 'type' => CRM_Utils_Type::T_INT, + 'api.required' => 1, + ); + $entities = civicrm_api3('Entity', 'get'); + $entities = array_diff($entities['values'], $entities['deprecated']); + $spec['entity_type'] = array( + 'title' => 'Entity Type', + 'description' => 'API name of entity type, e.g. "Contact"', + 'type' => CRM_Utils_Type::T_STRING, + 'api.required' => 1, + 'options' => array_combine($entities, $entities), + ); +} + +/** + * CustomValue.gettree API + * + * @param array $params + * @return array API result + * @throws API_Exception + */ +function civicrm_api3_custom_value_gettree($params) { + $ret = array(); + $options = _civicrm_api3_get_options_from_params($params); + $toReturn = array( + 'custom_group' => array(), + 'custom_field' => array(), + 'custom_value' => array(), + ); + foreach (array_keys($options['return']) as $r) { + list($type, $field) = explode('.', $r); + if (isset($toReturn[$type])) { + $toReturn[$type][] = $field; + } + } + switch ($params['entity_type']) { + case 'Contact': + $ret = array('entityType' => 'contact_type', 'subTypes' => 'contact_sub_type'); + break; + + case 'Activity': + case 'Campaign': + case 'Case': + case 'Contribution': + case 'Event': + case 'Grant': + case 'Membership': + case 'Relationship': + $ret = array('subTypes' => strtolower($params['entity_type']) . '_type_id'); + break; + + case 'Participant': + // todo + } + $treeParams = array( + 'entityType' => $params['entity_type'], + 'subTypes' => array(), + 'subName' => NULL, + ); + // Fetch entity data for custom group type/sub-type + // Also verify access permissions (api3 will throw an exception if permission denied) + if ($ret || !empty($params['check_permissions'])) { + $entityData = civicrm_api3($params['entity_type'], 'getsingle', array( + 'id' => $params['entity_id'], + 'return' => array_merge(array('id'), array_values($ret)), + )); + foreach ($ret as $param => $key) { + if (isset($entityData[$key])) { + $treeParams[$param] = $entityData[$key]; + } + } + } + $tree = CRM_Core_BAO_CustomGroup::getTree($treeParams['entityType'], NULL, $params['entity_id'], NULL, $treeParams['subTypes'], $treeParams['subName'], TRUE, NULL, FALSE, CRM_Utils_Array::value('check_permissions', $params, TRUE)); + unset($tree['info']); + $result = array(); + foreach ($tree as $group) { + $result[$group['name']] = array(); + $groupToReturn = $toReturn['custom_group'] ? $toReturn['custom_group'] : array_keys($group); + foreach ($groupToReturn as $item) { + $result[$group['name']][$item] = CRM_Utils_Array::value($item, $group); + } + $result[$group['name']]['fields'] = array(); + foreach ($group['fields'] as $fieldInfo) { + $field = array('value' => NULL); + $fieldToReturn = $toReturn['custom_field'] ? $toReturn['custom_field'] : array_keys($fieldInfo); + foreach ($fieldToReturn as $item) { + $field[$item] = CRM_Utils_Array::value($item, $fieldInfo); + } + unset($field['customValue']); + if (!empty($fieldInfo['customValue'])) { + $field['value'] = CRM_Utils_Array::first($fieldInfo['customValue']); + foreach (array_keys($field['value']) as $key) { + if ($toReturn['custom_value'] && !in_array($key, $toReturn['custom_value'])) { + unset($field['value'][$key]); + } + } + if (!$toReturn['custom_value'] || in_array('display', $toReturn['custom_value'])) { + $field['value']['display'] = CRM_Core_BAO_CustomField::displayValue($field['value']['data'], $fieldInfo); + } + } + if (empty($params['sequential'])) { + $result[$group['name']]['fields'][$fieldInfo['name']] = $field; + } + else { + $result[$group['name']]['fields'][] = $field; + } + } + } + return civicrm_api3_create_success($result, $params, 'CustomValue', 'gettree'); +} diff --git a/tests/phpunit/api/v3/CustomValueTest.php b/tests/phpunit/api/v3/CustomValueTest.php index dcdf42338b..970b77c376 100644 --- a/tests/phpunit/api/v3/CustomValueTest.php +++ b/tests/phpunit/api/v3/CustomValueTest.php @@ -397,4 +397,24 @@ class api_v3_CustomValueTest extends CiviUnitTestCase { $this->assertEquals($params[$controlFieldName], $result[$controlFieldName]); } + public function testGettree() { + $cg = $this->callAPISuccess('CustomGroup', 'create', array( + 'title' => 'TestGettree', + 'extends' => 'Individual', + )); + $cf = $this->callAPISuccess('CustomField', 'create', array( + 'custom_group_id' => $cg['id'], + 'label' => 'Got Options', + 'name' => 'got_options', + "data_type" => "String", + "html_type" => "Multi-Select", + 'option_values' => array('1' => 'One', '2' => 'Two', '3' => 'Three'), + )); + $fieldName = 'custom_' . $cf['id']; + $contact = $this->individualCreate(array($fieldName => array('2', '3'))); + $tree = $this->callAPISuccess('CustomValue', 'gettree', array('entity_type' => 'Contact', 'entity_id' => $contact)); + $this->assertEquals(array('2', '3'), $tree['values']['TestGettree']['fields']['got_options']['value']['data']); + $this->assertEquals('Two, Three', $tree['values']['TestGettree']['fields']['got_options']['value']['display']); + } + } -- 2.25.1