From: Eileen McNaughton Date: Thu, 1 May 2014 01:28:41 +0000 (-0700) Subject: CRM-14449 api move custom formatting call to utils X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=8295042e603d60d94f32fcd47b602f712aa261cb;p=civicrm-core.git CRM-14449 api move custom formatting call to utils --- diff --git a/api/v3/utils.php b/api/v3/utils.php index fdebc49a95..097c6c28c8 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -831,15 +831,6 @@ function _civicrm_api3_dao_to_array($dao, $params = NULL, $uniqueFields = TRUE, if(isset($dao->count)) { return $dao->count; } - //if custom fields are required we will endeavour to set them . NB passing $entity in might be a bit clunky / unrequired - if (!empty($entity) && !empty($params['return']) && is_array($params['return'])) { - foreach ($params['return'] as $return) { - if (substr($return, 0, 6) == 'custom') { - $custom = TRUE; - } - } - } - $fields = array_keys(_civicrm_api3_build_fields_array($dao, $uniqueFields)); @@ -854,7 +845,8 @@ function _civicrm_api3_dao_to_array($dao, $params = NULL, $uniqueFields = TRUE, } } $result[$dao->id] = $tmp; - if (!empty($custom)) { + + if(_civicrm_api3_custom_fields_are_required($entity, $params)) { _civicrm_api3_custom_data_get($result[$dao->id], $entity, $dao->id); } } @@ -863,6 +855,27 @@ function _civicrm_api3_dao_to_array($dao, $params = NULL, $uniqueFields = TRUE, return $result; } +/** + * We currently retrieve all custom fields or none at this level so if we know the entity + * && it can take custom fields & there is the string 'custom' in their return request we get them all, they are filtered on the way out + * @todo filter so only required fields are queried + * + * @param $params + * @param string $entity - entity name in CamelCase + * + * @return bool + */ +function _civicrm_api3_custom_fields_are_required($entity, $params) { + if (!array_key_exists($entity, CRM_Core_BAO_CustomQuery::$extendsMap)) { + return FALSE; + } + $options = _civicrm_api3_get_options_from_params($params); + //we check for possibility of 'custom' => 1 as well as specific custom fields + $returnString = implode('', $options['return']) . implode('', array_keys($options['return'])); + if(stristr($returnString, 'custom')) { + return TRUE; + } +} /** * Converts an object to an array * @@ -910,6 +923,22 @@ function _civicrm_api3_custom_format_params($params, &$values, $extends, $entity } } +/** + * @param $params + * @param $entity + */ +function _civicrm_api3_format_params_for_create(&$params, $entity) { + $nonGenericEntities = array('Contact', 'Individual', 'Household', 'Organization'); + + $customFieldEntities = array_diff_key(CRM_Core_BAO_CustomQuery::$extendsMap, array_fill_keys($nonGenericEntities, 1)); + if(!array_key_exists($entity, $customFieldEntities)) { + return; + } + $values = array(); + _civicrm_api3_custom_format_params($params, $values, $entity); + $params = array_merge($params, $values); +} + /** * @deprecated * This function ensures that we have the right input parameters @@ -1005,7 +1034,7 @@ function _civicrm_api3_basic_get($bao_name, &$params, $returnAsSuccess = TRUE, $ * @return array */ function _civicrm_api3_basic_create($bao_name, &$params, $entity = NULL) { - + _civicrm_api3_format_params_for_create($params, $entity); $args = array(&$params); if (!empty($entity)) { $ids = array($entity => CRM_Utils_Array::value('id', $params));