if ((CRM_Utils_Array::value('cache_clear', $apiRequest['params']))) {
$results = array();
// we will also clear pseudoconstants here - should potentially be moved to relevant BAO classes
+ CRM_Core_PseudoConstant::flush();
if(!empty($apiRequest['params']['fieldname'])){
CRM_Utils_PseudoConstant::flushConstant($apiRequest['params']['fieldname']);
}
$lcase_entity = _civicrm_api_get_entity_name_from_camel($entity);
$subentity = CRM_Utils_Array::value('contact_type', $apiRequest['params']);
$action = strtolower(CRM_Utils_Array::value('action', $apiRequest['params']));
+ $sequential = empty($apiRequest['params']) ? 0 : 1;
$apiOptions = CRM_Utils_Array::value('options', $apiRequest['params'], array());
if ($action == 'getvalue' || $action == 'getvalue' || $action == 'getcount') {
$action = 'get';
}
// determines whether to use unique field names - seem comment block above
$unique = TRUE;
- if (isset($results[$entity . $subentity]) && CRM_Utils_Array::value($action, $results[$entity])
- && empty($apiOptions)) {
- return $results[$entity . $subentity][$action];
+ if (empty($apiOptions) && isset($results[$entity . $subentity]) && isset($action, $results[$entity . $subentity])
+ && isset($action, $results[$entity . $subentity][$sequential])) {
+ return $results[$entity . $subentity][$action][$sequential];
}
// defaults based on data model and API policy
switch ($action) {
'id' => array('title' => 'Unique Identifier',
'api.required' => 1,
'api.aliases' => array($lcase_entity . '_id'),
+ 'type' => CRM_Utils_Type::T_INT,
));
break;
case 'getoptions':
$metadata = array(
- 'field' => array('title' => 'Field to retrieve options for',
- 'api.required' => 1,
- ));
+ 'field' => array(
+ 'title' => 'Field to retrieve options for',
+ 'api.required' => 1,
+ ),
+ 'context' => array(
+ 'title' => 'Context string',
+ ),
+ );
break;
default:
// oddballs are on their own
$helper($metadata);
}
- $fieldsToResolve = CRM_Utils_Array::value('get_options', $apiOptions, array());
+ $fieldsToResolve = (array) CRM_Utils_Array::value('get_options', $apiOptions, array());
foreach ($metadata as $fieldname => $fieldSpec) {
- _civicrm_api3_generic_get_metadata_options($metadata, $fieldname, $fieldSpec, $fieldsToResolve);
+ _civicrm_api3_generic_get_metadata_options($metadata, $apiRequest['entity'], $fieldname, $fieldSpec, $fieldsToResolve);
}
- $results[$entity][$action] = civicrm_api3_create_success($metadata, $apiRequest['params'], NULL, 'getfields');
- return $results[$entity][$action];
+ $results[$entity][$action][$sequential] = civicrm_api3_create_success($metadata, $apiRequest['params'], NULL, 'getfields');
+ return $results[$entity][$action][$sequential];
}
/**
* @return integer count of results
*/
function civicrm_api3_generic_getcount($apiRequest) {
+ $apiRequest['params']['options']['is_count'] = TRUE;
$result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']);
+ if(is_numeric (CRM_Utils_Array::value('values', $result))) {
+ return (int) $result['values'];
+ }
return $result['count'];
}
* @return array of results
*/
function civicrm_api3_generic_getoptions($apiRequest) {
- $daoName = _civicrm_api3_get_DAO($apiRequest['entity']);
- $options = $daoName::buildOptions($apiRequest['params']['field']);
+ // 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);
+
+ $baoName = _civicrm_api3_get_BAO($apiRequest['entity']);
+ $options = $baoName::buildOptions($fieldName, $context);
if ($options === FALSE) {
- return civicrm_api3_create_error("The field '{$apiRequest['params']['field']}' either doesn't exist or has no associated option list.");
+ return civicrm_api3_create_error("The field '{$fieldName}' has no associated option list.");
}
return civicrm_api3_create_success($options);
}
* @param array $fieldSpec metadata for that field
* @param array $fieldsToResolve anny field resolutions specifically requested
*/
-function _civicrm_api3_generic_get_metadata_options(&$metadata, $fieldname, $fieldSpec, $fieldsToResolve){
- if (array_key_exists('enumValues', $fieldSpec)) {
- // use of a space after the comma is inconsistent in xml
- $enumStr = str_replace(', ', ',', $fieldSpec['enumValues']);
- $metadata[$fieldname]['options'] = explode(',', $enumStr);
+function _civicrm_api3_generic_get_metadata_options(&$metadata, $entity, $fieldname, $fieldSpec, $fieldsToResolve){
+ if(empty($fieldSpec['pseudoconstant']) && empty($fieldSpec['enumValues'])) {
return;
}
- if(empty($fieldSpec['pseudoconstant'])){
- return ;
- }
- elseif(!empty($fieldSpec['FKClassName']) && !in_array($fieldname, $fieldsToResolve)){
+ if (!empty($metadata[$fieldname]['options']) || (!in_array($fieldname, $fieldsToResolve) && !in_array('all', $fieldsToResolve))) {
return;
}
- if(substr($fieldname, -3) == '_id'){
- $metadata[$fieldname]['api.aliases'][] = substr($fieldname, 0, -3);
- }
- $pseudoParams = $fieldSpec['pseudoconstant'];
- $pseudoParams['version'] = 3;
- $options = civicrm_api('constant', 'get', $pseudoParams);
+ $options = civicrm_api($entity, 'getoptions', array('version' => 3, 'field' => $fieldname));
if (is_array(CRM_Utils_Array::value('values', $options))) {
$metadata[$fieldname]['options'] = $options['values'];
}