Exemple #1
0
/**
 * API wrapper for getoptions function.
 *
 * @param array $apiRequest
 *   Api request as an array.
 *
 * @return array
 *   Array of results
 */
function civicrm_api3_generic_getoptions($apiRequest)
{
    // Resolve aliases.
    $fieldName = _civicrm_api3_api_resolve_alias($apiRequest['entity'], $apiRequest['params']['field']);
    if (!$fieldName) {
        return civicrm_api3_create_error("The field '{$apiRequest['params']['field']}' doesn't exist.");
    }
    // Validate 'context' from params
    $context = CRM_Utils_Array::value('context', $apiRequest['params']);
    CRM_Core_DAO::buildOptionsContext($context);
    unset($apiRequest['params']['context'], $apiRequest['params']['field']);
    $baoName = _civicrm_api3_get_BAO($apiRequest['entity']);
    $options = $baoName::buildOptions($fieldName, $context, $apiRequest['params']);
    if ($options === FALSE) {
        return civicrm_api3_create_error("The field '{$fieldName}' has no associated option list.");
    }
    // Support 'sequential' output as a non-associative array
    if (!empty($apiRequest['params']['sequential'])) {
        $options = CRM_Utils_Array::makeNonAssociative($options);
    }
    return civicrm_api3_create_success($options, $apiRequest['params'], $apiRequest['entity'], 'getoptions');
}
Exemple #2
0
/**
 * Returns the canonical name of a field.
 *
 * @param $entity
 *   api entity name (string should already be standardized - no camelCase).
 * @param $fieldName
 *   any variation of a field's name (name, unique_name, api.alias).
 *
 * @return bool|string
 *   fieldName or FALSE if the field does not exist
 */
function _civicrm_api3_api_resolve_alias($entity, $fieldName, $action = 'create')
{
    if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) {
        return $fieldName;
    }
    if ($fieldName == _civicrm_api_get_entity_name_from_camel($entity) . '_id') {
        return 'id';
    }
    $result = civicrm_api($entity, 'getfields', array('version' => 3, 'action' => $action));
    $meta = $result['values'];
    if (!isset($meta[$fieldName]['name']) && isset($meta[$fieldName . '_id'])) {
        $fieldName = $fieldName . '_id';
    }
    if (isset($meta[$fieldName])) {
        return $meta[$fieldName]['name'];
    }
    foreach ($meta as $info) {
        if ($fieldName == CRM_Utils_Array::value('uniqueName', $info)) {
            return $info['name'];
        }
        if (array_search($fieldName, CRM_Utils_Array::value('api.aliases', $info, array())) !== FALSE) {
            return $info['name'];
        }
    }
    // Create didn't work, try with get
    if ($action == 'create') {
        return _civicrm_api3_api_resolve_alias($entity, $fieldName, 'get');
    }
    return FALSE;
}