X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=api%2Fv3%2FGeneric.php;h=7344f1b2d1c9430130bfa6873c087598d17da042;hb=4c12c9b771bde931077f56685b632bd34504b238;hp=22936c54e4913dd2e95d6c6292dff4644eb655bb;hpb=37fa58b050ec0124dac4b7a295408951f6036524;p=civicrm-core.git diff --git a/api/v3/Generic.php b/api/v3/Generic.php index 22936c54e4..7344f1b2d1 100644 --- a/api/v3/Generic.php +++ b/api/v3/Generic.php @@ -1,8 +1,38 @@ fields function & then augment * that information by calling the _spec functions that apply to the relevant function * Note that we use 'unique' field names as described in the xml/schema files @@ -12,12 +42,14 @@ * * @param array $apiRequest * Api request as an array. Keys are. - * - entity: string - * - action: string - * - version: string - * - function: callback (mixed) - * - params: array, varies - * @return array API success object + * - entity: string + * - action: string + * - version: string + * - function: callback (mixed) + * - params: array, varies + * + * @return array + * API success object */ function civicrm_api3_generic_getfields($apiRequest) { static $results = array(); @@ -29,16 +61,20 @@ function civicrm_api3_generic_getfields($apiRequest) { CRM_Utils_PseudoConstant::flushConstant($apiRequest['params']['fieldname']); } if (!empty($apiRequest['params']['option_group_id'])) { - $optionGroupName = civicrm_api('option_group', 'getvalue', array('version' => 3, 'id' => $apiRequest['params']['option_group_id'], 'return' => 'name')); + $optionGroupName = civicrm_api('option_group', 'getvalue', array( + 'version' => 3, + 'id' => $apiRequest['params']['option_group_id'], + 'return' => 'name', + )); if (is_string($optionGroupName)) { CRM_Utils_PseudoConstant::flushConstant(_civicrm_api_get_camel_name($optionGroupName)); } } } - $entity = _civicrm_api_get_camel_name($apiRequest['entity']); - $lcase_entity = _civicrm_api_get_entity_name_from_camel($entity); + $entity = $apiRequest['entity']; + $lowercase_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'])); + $action = CRM_Utils_Array::value('action', $apiRequest['params']); $sequential = empty($apiRequest['params']) ? 0 : 1; $apiOptions = CRM_Utils_Array::value('options', $apiRequest['params'], array()); if (!$action || $action == 'getvalue' || $action == 'getcount') { @@ -55,6 +91,7 @@ function civicrm_api3_generic_getfields($apiRequest) { case 'getfields': $values = _civicrm_api_get_fields($entity, FALSE, $apiRequest['params']); return civicrm_api3_create_success($values, $apiRequest['params'], $entity, 'getfields'); + case 'create': case 'update': case 'replace': @@ -66,9 +103,9 @@ function civicrm_api3_generic_getfields($apiRequest) { if (empty($metadata['id'])) { // if id is not set we will set it eg. 'id' from 'case_id', case_id will be an alias if (!empty($metadata[strtolower($apiRequest['entity']) . '_id'])) { - $metadata['id'] = $metadata[$lcase_entity . '_id']; - unset($metadata[$lcase_entity . '_id']); - $metadata['id']['api.aliases'] = array($lcase_entity . '_id'); + $metadata['id'] = $metadata[$lowercase_entity . '_id']; + unset($metadata[$lowercase_entity . '_id']); + $metadata['id']['api.aliases'] = array($lowercase_entity . '_id'); } } else { @@ -77,7 +114,7 @@ function civicrm_api3_generic_getfields($apiRequest) { // (note) or setting for all api where fields is returning 'id' & we want to accept 'note_id' @ the api layer // nb we don't officially accept note_id anyway - rationale here is more about centralising a now-tested // inconsistency - $metadata['id']['api.aliases'] = array($lcase_entity . '_id'); + $metadata['id']['api.aliases'] = array($lowercase_entity . '_id'); } break; @@ -87,7 +124,7 @@ function civicrm_api3_generic_getfields($apiRequest) { 'title' => $entity . ' ID', 'name' => 'id', 'api.required' => 1, - 'api.aliases' => array($lcase_entity . '_id'), + 'api.aliases' => array($lowercase_entity . '_id'), 'type' => CRM_Utils_Type::T_INT, )); break; @@ -104,7 +141,8 @@ function civicrm_api3_generic_getfields($apiRequest) { 'title' => 'Context', ), ); - break; + break; + default: // oddballs are on their own $metadata = array(); @@ -133,6 +171,10 @@ function civicrm_api3_generic_getfields($apiRequest) { $fieldsToResolve = (array) CRM_Utils_Array::value('get_options', $apiOptions, array()); foreach ($metadata as $fieldname => $fieldSpec) { + // Ensure 'name' is set + if (!isset($fieldSpec['name'])) { + $metadata[$fieldname]['name'] = $fieldname; + } _civicrm_api3_generic_get_metadata_options($metadata, $apiRequest, $fieldname, $fieldSpec, $fieldsToResolve); } @@ -141,13 +183,14 @@ function civicrm_api3_generic_getfields($apiRequest) { } /** - * API return function to reformat results as count + * API return function to reformat results as count. * * @param array $apiRequest * Api request as an array. Keys are. * * @throws API_Exception - * @return integer count of results + * @return int + * count of results */ function civicrm_api3_generic_getcount($apiRequest) { $apiRequest['params']['options']['is_count'] = TRUE; @@ -162,15 +205,16 @@ function civicrm_api3_generic_getcount($apiRequest) { } /** - * API return function to reformat results as single result + * API return function to reformat results as single result. * * @param array $apiRequest * Api request as an array. Keys are. * - * @return integer count of results + * @return int + * count of results */ function civicrm_api3_generic_getsingle($apiRequest) { - // so the first entity is always result['values'][0] + // So the first entity is always result['values'][0]. $apiRequest['params']['sequential'] = 1; $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']); if ($result['is_error'] !== 0) { @@ -186,12 +230,13 @@ function civicrm_api3_generic_getsingle($apiRequest) { } /** - * API return function to reformat results as single value + * API return function to reformat results as single value. * * @param array $apiRequest * Api request as an array. Keys are. * - * @return integer count of results + * @return int + * count of results */ function civicrm_api3_generic_getvalue($apiRequest) { $apiRequest['params']['sequential'] = 1; @@ -217,6 +262,8 @@ function civicrm_api3_generic_getvalue($apiRequest) { } /** + * Get count of contact references. + * * @param array $params */ function _civicrm_api3_generic_getrefcount_spec(&$params) { @@ -225,13 +272,14 @@ function _civicrm_api3_generic_getrefcount_spec(&$params) { } /** - * API to determine if a record is in-use + * API to determine if a record is in-use. * * @param array $apiRequest * Api request as an array. * * @throws API_Exception - * @return array API result (int 0 or 1) + * @return array + * API result (int 0 or 1) */ function civicrm_api3_generic_getrefcount($apiRequest) { $entityToClassMap = CRM_Core_DAO_AllCoreTables::daoToClass(); @@ -252,27 +300,29 @@ function civicrm_api3_generic_getrefcount($apiRequest) { } /** - * API wrapper for replace function + * API wrapper for replace function. * * @param array $apiRequest * Api request as an array. Keys are. * - * @return integer count of results + * @return int + * count of results */ function civicrm_api3_generic_replace($apiRequest) { return _civicrm_api3_generic_replace($apiRequest['entity'], $apiRequest['params']); } /** - * API wrapper for getoptions function + * API wrapper for getoptions function. * * @param array $apiRequest * Api request as an array. * - * @return array of results + * @return array + * Array of results */ function civicrm_api3_generic_getoptions($apiRequest) { - // Resolve aliases + // 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."); @@ -295,6 +345,8 @@ function civicrm_api3_generic_getoptions($apiRequest) { } /** + * Get metadata. + * * Function fills the 'options' array on the metadata returned by getfields if * 1) the param option 'get_options' is defined - e.g. $params['options']['get_options'] => array('custom_1) * (this is passed in as the $fieldsToResolve array) @@ -307,7 +359,7 @@ function civicrm_api3_generic_getoptions($apiRequest) { * * @param array $metadata * The array of metadata that will form the result of the getfields function. - * @param $apiRequest + * @param array $apiRequest * @param string $fieldname * Field currently being processed. * @param array $fieldSpec