4 * Get information about fields for a given api request. Getfields information
5 * is used for documentation, validation, default setting
6 * We first query the scheme using the $dao->fields function & then augment
7 * that information by calling the _spec functions that apply to the relevant function
8 * Note that we use 'unique' field names as described in the xml/schema files
9 * for get requests & just field name for create. This is because some get functions
10 * access multiple objects e.g. contact api accesses is_deleted from the activity
11 * table & from the contact table
13 * @param array $apiRequest api request as an array. Keys are
17 * - function: callback (mixed)
18 * - params: array, varies
19 * @return array API success object
21 function civicrm_api3_generic_getfields($apiRequest) {
22 static $results = array();
23 if ((CRM_Utils_Array
::value('cache_clear', $apiRequest['params']))) {
25 // we will also clear pseudoconstants here - should potentially be moved to relevant BAO classes
26 CRM_Core_PseudoConstant
::flush();
27 if(!empty($apiRequest['params']['fieldname'])){
28 CRM_Utils_PseudoConstant
::flushConstant($apiRequest['params']['fieldname']);
30 if(!empty($apiRequest['params']['option_group_id'])){
31 $optionGroupName = civicrm_api('option_group', 'getvalue', array('version' => 3, 'id' => $apiRequest['params']['option_group_id'], 'return' => 'name') );
32 if(is_string($optionGroupName)){
33 CRM_Utils_PseudoConstant
::flushConstant(_civicrm_api_get_camel_name($optionGroupName));
37 $entity = _civicrm_api_get_camel_name($apiRequest['entity']);
38 $lcase_entity = _civicrm_api_get_entity_name_from_camel($entity);
39 $subentity = CRM_Utils_Array
::value('contact_type', $apiRequest['params']);
40 $action = strtolower(CRM_Utils_Array
::value('action', $apiRequest['params']));
41 $sequential = empty($apiRequest['params']) ?
0 : 1;
42 $apiOptions = CRM_Utils_Array
::value('options', $apiRequest['params'], array());
43 if ($action == 'getvalue' ||
$action == 'getvalue' ||
$action == 'getcount') {
50 // determines whether to use unique field names - seem comment block above
52 if (empty($apiOptions) && isset($results[$entity . $subentity]) && isset($action, $results[$entity . $subentity])
53 && isset($action, $results[$entity . $subentity][$sequential])) {
54 return $results[$entity . $subentity][$action][$sequential];
56 // defaults based on data model and API policy
59 $values = _civicrm_api_get_fields($entity, false, $apiRequest['params']);
60 return civicrm_api3_create_success($values, $apiRequest['params'], $entity, 'getfields');
66 $metadata = _civicrm_api_get_fields($apiRequest['entity'], $unique, $apiRequest['params']);
67 if (empty($metadata['id'])){
68 // if id is not set we will set it eg. 'id' from 'case_id', case_id will be an alias
69 if(!empty($metadata[strtolower($apiRequest['entity']) . '_id'])) {
70 $metadata['id'] = $metadata[$lcase_entity . '_id'];
71 unset($metadata[$lcase_entity . '_id']);
72 $metadata['id']['api.aliases'] = array($lcase_entity . '_id');
76 // really the preference would be to set the unique name in the xml
77 // question is which is a less risky fix this close to a release - setting in xml for the known failure
78 // (note) or setting for all api where fields is returning 'id' & we want to accept 'note_id' @ the api layer
79 // nb we don't officially accept note_id anyway - rationale here is more about centralising a now-tested
81 $metadata['id']['api.aliases'] = array($lcase_entity . '_id');
87 'id' => array('title' => 'Unique Identifier',
89 'api.aliases' => array($lcase_entity . '_id'),
90 'type' => CRM_Utils_Type
::T_INT
,
97 'title' => 'Field to retrieve options for',
101 'title' => 'Context string',
106 // oddballs are on their own
110 // find any supplemental information
111 $hypApiRequest = array('entity' => $apiRequest['entity'], 'action' => $action, 'version' => $apiRequest['version']);
112 $hypApiRequest +
= _civicrm_api_resolve($hypApiRequest);
113 $helper = '_' . $hypApiRequest['function'] . '_spec';
114 if (function_exists($helper)) {
119 $fieldsToResolve = (array) CRM_Utils_Array
::value('get_options', $apiOptions, array());
121 foreach ($metadata as $fieldname => $fieldSpec) {
122 _civicrm_api3_generic_get_metadata_options($metadata, $apiRequest['entity'], $fieldname, $fieldSpec, $fieldsToResolve);
125 $results[$entity][$action][$sequential] = civicrm_api3_create_success($metadata, $apiRequest['params'], NULL, 'getfields');
126 return $results[$entity][$action][$sequential];
130 * API return function to reformat results as count
132 * @param array $apiRequest api request as an array. Keys are
134 * @return integer count of results
136 function civicrm_api3_generic_getcount($apiRequest) {
137 $apiRequest['params']['options']['is_count'] = TRUE;
138 $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']);
139 if(is_numeric (CRM_Utils_Array
::value('values', $result))) {
140 return (int) $result['values'];
142 if(!isset($result['count'])) {
143 throw new API_Exception(ts('Unexpected result from getcount') . print_r($result, TRUE));
145 return $result['count'];
149 * API return function to reformat results as single result
151 * @param array $apiRequest api request as an array. Keys are
153 * @return integer count of results
155 function civicrm_api3_generic_getsingle($apiRequest) {
156 // so the first entity is always result['values'][0]
157 $apiRequest['params']['sequential'] = 1;
158 $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']);
159 if ($result['is_error'] !== 0) {
162 if ($result['count'] === 1) {
163 return $result['values'][0];
165 if ($result['count'] !== 1) {
166 return civicrm_api3_create_error("Expected one " . $apiRequest['entity'] . " but found " . $result['count'], array('count' => $result['count']));
168 return civicrm_api3_create_error("Undefined behavior");
172 * API return function to reformat results as single value
174 * @param array $apiRequest api request as an array. Keys are
176 * @return integer count of results
178 function civicrm_api3_generic_getvalue($apiRequest) {
179 $apiRequest['params']['sequential'] = 1;
180 $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']);
181 if ($result['is_error'] !== 0) {
184 if ($result['count'] !== 1) {
185 $result = civicrm_api3_create_error("Expected one " . $apiRequest['entity'] . " but found " . $result['count'], array('count' => $result['count']));
189 // we only take "return=" as valid options
190 if (CRM_Utils_Array
::value('return', $apiRequest['params'])) {
191 if (!isset($result['values'][0][$apiRequest['params']['return']])) {
192 return civicrm_api3_create_error("field " . $apiRequest['params']['return'] . " unset or not existing", array('invalid_field' => $apiRequest['params']['return']));
195 return $result['values'][0][$apiRequest['params']['return']];
198 return civicrm_api3_create_error("missing param return=field you want to read the value of", array('error_type' => 'mandatory_missing', 'missing_param' => 'return'));
202 * API wrapper for replace function
204 * @param array $apiRequest api request as an array. Keys are
206 * @return integer count of results
208 function civicrm_api3_generic_replace($apiRequest) {
209 return _civicrm_api3_generic_replace($apiRequest['entity'], $apiRequest['params']);
213 * API wrapper for getoptions function
215 * @param array $apiRequest api request as an array.
217 * @return array of results
219 function civicrm_api3_generic_getoptions($apiRequest) {
221 $fieldName = _civicrm_api3_api_resolve_alias($apiRequest['entity'], $apiRequest['params']['field']);
223 return civicrm_api3_create_error("The field '{$apiRequest['params']['field']}' doesn't exist.");
225 // Validate 'context' from params
226 $context = CRM_Utils_Array
::value('context', $apiRequest['params']);
227 CRM_Core_DAO
::buildOptionsContext($context);
228 unset($apiRequest['params']['context'], $apiRequest['params']['field']);
230 $baoName = _civicrm_api3_get_BAO($apiRequest['entity']);
231 $options = $baoName::buildOptions($fieldName, $context, $apiRequest['params']);
232 if ($options === FALSE) {
233 return civicrm_api3_create_error("The field '{$fieldName}' has no associated option list.");
235 return civicrm_api3_create_success($options);
239 * Function fills the 'options' array on the metadata returned by getfields if
240 * 1) the param option 'get_options' is defined - e.g. $params['options']['get_options'] => array('custom_1)
241 * (this is passed in as the $fieldsToResolve array)
242 * 2) the field is a pseudoconstant and is NOT an FK
243 * - the reason for this is that checking / transformation is done on pseudoconstants but
244 * - if the field is an FK then mysql will enforce the data quality (& we have handling on failure)
245 * @todo - if may be we should define a 'resolve' key on the psuedoconstant for when these rules are not fine enough
246 * 3) if there is an 'enum' then it is split up into the relevant options
248 * This function is only split out for the purpose of code clarity / comment block documentation
249 * @param array $metadata the array of metadata that will form the result of the getfields function
250 * @param string $fieldname field currently being processed
251 * @param array $fieldSpec metadata for that field
252 * @param array $fieldsToResolve anny field resolutions specifically requested
254 function _civicrm_api3_generic_get_metadata_options(&$metadata, $entity, $fieldname, $fieldSpec, $fieldsToResolve){
255 if(empty($fieldSpec['pseudoconstant']) && empty($fieldSpec['enumValues'])) {
259 if (!empty($metadata[$fieldname]['options']) ||
(!in_array($fieldname, $fieldsToResolve) && !in_array('all', $fieldsToResolve))) {
263 $options = civicrm_api($entity, 'getoptions', array('version' => 3, 'field' => $fieldname));
264 if (is_array(CRM_Utils_Array
::value('values', $options))) {
265 $metadata[$fieldname]['options'] = $options['values'];