Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
b081365f CW |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
a30c801b | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
b081365f | 5 | | | |
a30c801b TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
b081365f CW |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * @package CiviCRM_APIv3 | |
14 | */ | |
6a488035 TO |
15 | |
16 | /** | |
61fe4988 EM |
17 | * Get information about fields for a given api request. |
18 | * | |
19 | * Getfields information is used for documentation, validation, default setting | |
6a488035 TO |
20 | * We first query the scheme using the $dao->fields function & then augment |
21 | * that information by calling the _spec functions that apply to the relevant function | |
22 | * Note that we use 'unique' field names as described in the xml/schema files | |
23 | * for get requests & just field name for create. This is because some get functions | |
24 | * access multiple objects e.g. contact api accesses is_deleted from the activity | |
25 | * table & from the contact table | |
26 | * | |
cf470720 TO |
27 | * @param array $apiRequest |
28 | * Api request as an array. Keys are. | |
16b10e64 CW |
29 | * - entity: string |
30 | * - action: string | |
31 | * - version: string | |
32 | * - function: callback (mixed) | |
33 | * - params: array, varies | |
9d32e6f7 | 34 | * |
26a700db CW |
35 | * @param bool $unique |
36 | * Determines whether to key by unique field names (only affects get-type) actions | |
37 | * | |
a6c01b45 | 38 | * @return array |
72b3a70c | 39 | * API success object |
6a488035 | 40 | */ |
26a700db | 41 | function civicrm_api3_generic_getfields($apiRequest, $unique = TRUE) { |
cf8f0fff | 42 | static $results = []; |
6a488035 | 43 | if ((CRM_Utils_Array::value('cache_clear', $apiRequest['params']))) { |
cf8f0fff | 44 | $results = []; |
6a488035 | 45 | // we will also clear pseudoconstants here - should potentially be moved to relevant BAO classes |
37547b77 | 46 | CRM_Core_PseudoConstant::flush(); |
9b873358 | 47 | if (!empty($apiRequest['params']['fieldname'])) { |
6a488035 TO |
48 | CRM_Utils_PseudoConstant::flushConstant($apiRequest['params']['fieldname']); |
49 | } | |
9b873358 | 50 | if (!empty($apiRequest['params']['option_group_id'])) { |
cf8f0fff | 51 | $optionGroupName = civicrm_api('option_group', 'getvalue', [ |
9d32e6f7 EM |
52 | 'version' => 3, |
53 | 'id' => $apiRequest['params']['option_group_id'], | |
54 | 'return' => 'name', | |
cf8f0fff | 55 | ]); |
9b873358 | 56 | if (is_string($optionGroupName)) { |
6a488035 TO |
57 | CRM_Utils_PseudoConstant::flushConstant(_civicrm_api_get_camel_name($optionGroupName)); |
58 | } | |
59 | } | |
60 | } | |
4846df91 CW |
61 | $entity = $apiRequest['entity']; |
62 | $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity); | |
6a488035 | 63 | $subentity = CRM_Utils_Array::value('contact_type', $apiRequest['params']); |
4846df91 | 64 | $action = CRM_Utils_Array::value('action', $apiRequest['params']); |
fce45a2f | 65 | $sequential = empty($apiRequest['params']['sequential']) ? 0 : 1; |
cf8f0fff CW |
66 | $apiRequest['params']['options'] = CRM_Utils_Array::value('options', $apiRequest['params'], []); |
67 | $optionsToResolve = (array) CRM_Utils_Array::value('get_options', $apiRequest['params']['options'], []); | |
acadf548 | 68 | |
54df0f0c | 69 | if (!$action || $action == 'getvalue' || $action == 'getcount') { |
6a488035 TO |
70 | $action = 'get'; |
71 | } | |
2ce25e93 CW |
72 | // If no options, return results from cache |
73 | if (!$apiRequest['params']['options'] && isset($results[$entity . $subentity]) && isset($action, $results[$entity . $subentity]) | |
ceccbc35 | 74 | && isset($action, $results[$entity . $subentity][$sequential])) { |
75 | return $results[$entity . $subentity][$action][$sequential]; | |
6a488035 TO |
76 | } |
77 | // defaults based on data model and API policy | |
78 | switch ($action) { | |
79 | case 'getfields': | |
9c8096cb | 80 | $values = _civicrm_api_get_fields($entity, FALSE, $apiRequest['params']); |
f2b53f26 | 81 | return civicrm_api3_create_success($values, $apiRequest['params'], $entity, 'getfields'); |
ea100cb5 | 82 | |
6a488035 TO |
83 | case 'create': |
84 | case 'update': | |
85 | case 'replace': | |
86 | $unique = FALSE; | |
87 | case 'get': | |
2b6e1174 CW |
88 | case 'getsingle': |
89 | case 'getcount': | |
9c32b8f6 | 90 | case 'getstat': |
6a488035 | 91 | $metadata = _civicrm_api_get_fields($apiRequest['entity'], $unique, $apiRequest['params']); |
9b873358 | 92 | if (empty($metadata['id'])) { |
9ec90e57 | 93 | // if id is not set we will set it eg. 'id' from 'case_id', case_id will be an alias |
22e263ad | 94 | if (!empty($metadata[strtolower($apiRequest['entity']) . '_id'])) { |
4846df91 CW |
95 | $metadata['id'] = $metadata[$lowercase_entity . '_id']; |
96 | unset($metadata[$lowercase_entity . '_id']); | |
cf8f0fff | 97 | $metadata['id']['api.aliases'] = [$lowercase_entity . '_id']; |
9ec90e57 | 98 | } |
99 | } | |
92e4c2a5 | 100 | else { |
9ec90e57 | 101 | // really the preference would be to set the unique name in the xml |
102 | // question is which is a less risky fix this close to a release - setting in xml for the known failure | |
103 | // (note) or setting for all api where fields is returning 'id' & we want to accept 'note_id' @ the api layer | |
104 | // nb we don't officially accept note_id anyway - rationale here is more about centralising a now-tested | |
105 | // inconsistency | |
cf8f0fff | 106 | $metadata['id']['api.aliases'] = [$lowercase_entity . '_id']; |
6a488035 TO |
107 | } |
108 | break; | |
109 | ||
110 | case 'delete': | |
cf8f0fff CW |
111 | $metadata = [ |
112 | 'id' => [ | |
3a8e9315 | 113 | 'title' => $entity . ' ID', |
6a488035 | 114 | 'api.required' => 1, |
cf8f0fff | 115 | 'api.aliases' => [$lowercase_entity . '_id'], |
b2402735 | 116 | 'type' => CRM_Utils_Type::T_INT, |
7c31ae57 SL |
117 | ], |
118 | ]; | |
6a488035 TO |
119 | break; |
120 | ||
f7b2fef0 CW |
121 | // Note: adding setvalue case here instead of in a generic spec function because |
122 | // some APIs override the generic setvalue fn which causes the generic spec to be overlooked. | |
123 | case 'setvalue': | |
cf8f0fff CW |
124 | $metadata = [ |
125 | 'field' => [ | |
3a8e9315 | 126 | 'title' => 'Field name', |
786ad6e1 | 127 | 'api.required' => 1, |
f7b2fef0 | 128 | 'type' => CRM_Utils_Type::T_STRING, |
cf8f0fff CW |
129 | ], |
130 | 'id' => [ | |
f7b2fef0 CW |
131 | 'title' => $entity . ' ID', |
132 | 'api.required' => 1, | |
133 | 'type' => CRM_Utils_Type::T_INT, | |
cf8f0fff CW |
134 | ], |
135 | 'value' => [ | |
f7b2fef0 CW |
136 | 'title' => 'Value', |
137 | 'description' => "Field value to set", | |
138 | 'api.required' => 1, | |
cf8f0fff CW |
139 | ], |
140 | ]; | |
141 | if (array_intersect(['all', 'field'], $optionsToResolve)) { | |
142 | $options = civicrm_api3_generic_getfields(['entity' => $entity, ['params' => ['action' => 'create']]]); | |
2ce25e93 | 143 | $metadata['field']['options'] = CRM_Utils_Array::collect('title', $options['values']); |
acadf548 | 144 | } |
c866eb5f TO |
145 | break; |
146 | ||
6a488035 TO |
147 | default: |
148 | // oddballs are on their own | |
cf8f0fff | 149 | $metadata = []; |
6a488035 TO |
150 | } |
151 | ||
16f3bd02 | 152 | // Hack for product api to pass tests. |
153 | if (!is_string($apiRequest['params']['options'])) { | |
154 | // Normalize this for the sake of spec funcions | |
155 | $apiRequest['params']['options']['get_options'] = $optionsToResolve; | |
156 | } | |
2ce25e93 | 157 | |
6a488035 | 158 | // find any supplemental information |
cf8f0fff | 159 | $hypApiRequest = ['entity' => $apiRequest['entity'], 'action' => $action, 'version' => $apiRequest['version']]; |
021cfd95 CW |
160 | if ($action == 'getsingle') { |
161 | $hypApiRequest['action'] = 'get'; | |
162 | } | |
c65db512 | 163 | try { |
048222df | 164 | list ($apiProvider, $hypApiRequest) = \Civi::service('civi_api_kernel')->resolve($hypApiRequest); |
378e2654 TO |
165 | if (isset($hypApiRequest['function'])) { |
166 | $helper = '_' . $hypApiRequest['function'] . '_spec'; | |
0db6c3e1 TO |
167 | } |
168 | else { | |
378e2654 TO |
169 | // not implemented MagicFunctionProvider |
170 | $helper = NULL; | |
171 | } | |
0db6c3e1 TO |
172 | } |
173 | catch (\Civi\API\Exception\NotImplementedException $e) { | |
c65db512 TO |
174 | $helper = NULL; |
175 | } | |
6a488035 TO |
176 | if (function_exists($helper)) { |
177 | // alter | |
6a386447 | 178 | $helper($metadata, $apiRequest); |
6a488035 TO |
179 | } |
180 | ||
6a488035 | 181 | foreach ($metadata as $fieldname => $fieldSpec) { |
a419926d CW |
182 | // Ensure 'name' is set |
183 | if (!isset($fieldSpec['name'])) { | |
184 | $metadata[$fieldname]['name'] = $fieldname; | |
185 | } | |
b432ddaa | 186 | _civicrm_api3_generic_get_metadata_options($metadata, $apiRequest, $fieldname, $fieldSpec); |
fce45a2f | 187 | |
2ce25e93 | 188 | // Convert options to "sequential" format |
fce45a2f | 189 | if ($sequential && !empty($metadata[$fieldname]['options'])) { |
2ce25e93 CW |
190 | $metadata[$fieldname]['options'] = CRM_Utils_Array::makeNonAssociative($metadata[$fieldname]['options']); |
191 | } | |
6a488035 TO |
192 | } |
193 | ||
a14e9d08 | 194 | $results[$entity][$action][$sequential] = civicrm_api3_create_success($metadata, $apiRequest['params'], $entity, 'getfields'); |
ceccbc35 | 195 | return $results[$entity][$action][$sequential]; |
6a488035 TO |
196 | } |
197 | ||
26a700db CW |
198 | /** |
199 | * Get metadata for a field | |
200 | * | |
201 | * @param array $apiRequest | |
202 | * | |
203 | * @return array | |
204 | * API success object | |
205 | */ | |
206 | function civicrm_api3_generic_getfield($apiRequest) { | |
207 | $params = $apiRequest['params']; | |
208 | $sequential = !empty($params['sequential']); | |
209 | $fieldName = _civicrm_api3_api_resolve_alias($apiRequest['entity'], $params['name'], $params['action']); | |
210 | if (!$fieldName) { | |
211 | return civicrm_api3_create_error("The field '{$params['name']}' doesn't exist."); | |
212 | } | |
213 | // Turn off sequential to make the field easier to find | |
214 | $apiRequest['params']['sequential'] = 0; | |
d60a6fba CW |
215 | if (isset($params['get_options'])) { |
216 | $apiRequest['params']['options']['get_options_context'] = $params['get_options']; | |
26a700db CW |
217 | $apiRequest['params']['options']['get_options'] = $fieldName; |
218 | } | |
26a700db CW |
219 | $result = civicrm_api3_generic_getfields($apiRequest, FALSE); |
220 | $result = $result['values'][$fieldName]; | |
221 | // Fix sequential options since we forced it off | |
222 | if ($sequential && !empty($result['options'])) { | |
223 | $result['options'] = CRM_Utils_Array::makeNonAssociative($result['options']); | |
224 | } | |
225 | return civicrm_api3_create_success($result, $apiRequest['params'], $apiRequest['entity'], 'getfield'); | |
226 | } | |
227 | ||
e8e8f3ad | 228 | /** |
229 | * Get metadata for getfield action. | |
230 | * | |
231 | * @param array $params | |
232 | * @param array $apiRequest | |
233 | * | |
234 | * @throws \CiviCRM_API3_Exception | |
235 | * @throws \Exception | |
236 | */ | |
26a700db | 237 | function _civicrm_api3_generic_getfield_spec(&$params, $apiRequest) { |
cf8f0fff CW |
238 | $params = [ |
239 | 'name' => [ | |
26a700db CW |
240 | 'title' => 'Field name', |
241 | 'description' => 'Name or alias of field to lookup', | |
242 | 'api.required' => 1, | |
243 | 'type' => CRM_Utils_Type::T_STRING, | |
cf8f0fff CW |
244 | ], |
245 | 'action' => [ | |
26a700db CW |
246 | 'title' => 'API Action', |
247 | 'api.required' => 1, | |
248 | 'type' => CRM_Utils_Type::T_STRING, | |
cf8f0fff CW |
249 | 'api.aliases' => ['api_action'], |
250 | ], | |
251 | 'get_options' => [ | |
d60a6fba CW |
252 | 'title' => 'Get Options', |
253 | 'description' => 'Context for which to get field options, or null to skip fetching options.', | |
26a700db CW |
254 | 'type' => CRM_Utils_Type::T_STRING, |
255 | 'options' => CRM_Core_DAO::buildOptionsContext(), | |
cf8f0fff CW |
256 | 'api.aliases' => ['context'], |
257 | ], | |
258 | ]; | |
26a700db | 259 | // Add available options to these params if requested |
cf8f0fff | 260 | if (array_intersect(['all', 'action'], $apiRequest['params']['options']['get_options'])) { |
26a700db CW |
261 | $actions = civicrm_api3($apiRequest['entity'], 'getactions'); |
262 | $actions = array_combine($actions['values'], $actions['values']); | |
263 | // Let's not go meta-crazy | |
264 | CRM_Utils_Array::remove($actions, 'getactions', 'getoptions', 'getfields', 'getfield', 'getcount', 'getrefcount', 'getsingle', 'getlist', 'getvalue', 'setvalue', 'update'); | |
265 | $params['action']['options'] = $actions; | |
266 | } | |
267 | } | |
268 | ||
6a488035 | 269 | /** |
9d32e6f7 | 270 | * API return function to reformat results as count. |
6a488035 | 271 | * |
cf470720 TO |
272 | * @param array $apiRequest |
273 | * Api request as an array. Keys are. | |
6a488035 | 274 | * |
77b97be7 | 275 | * @throws API_Exception |
df8d3074 | 276 | * @return int |
72b3a70c | 277 | * count of results |
6a488035 TO |
278 | */ |
279 | function civicrm_api3_generic_getcount($apiRequest) { | |
972322c5 | 280 | $apiRequest['params']['options']['is_count'] = TRUE; |
6a488035 | 281 | $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']); |
37fa58b0 | 282 | if (is_numeric(CRM_Utils_Array::value('values', $result))) { |
972322c5 | 283 | return (int) $result['values']; |
284 | } | |
22e263ad | 285 | if (!isset($result['count'])) { |
8335b10a | 286 | throw new API_Exception(ts('Unexpected result from getcount') . print_r($result, TRUE)); |
287 | } | |
6a488035 TO |
288 | return $result['count']; |
289 | } | |
290 | ||
291 | /** | |
9d32e6f7 | 292 | * API return function to reformat results as single result. |
6a488035 | 293 | * |
cf470720 TO |
294 | * @param array $apiRequest |
295 | * Api request as an array. Keys are. | |
6a488035 | 296 | * |
df8d3074 | 297 | * @return int |
72b3a70c | 298 | * count of results |
6a488035 TO |
299 | */ |
300 | function civicrm_api3_generic_getsingle($apiRequest) { | |
9d32e6f7 | 301 | // So the first entity is always result['values'][0]. |
6a488035 TO |
302 | $apiRequest['params']['sequential'] = 1; |
303 | $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']); | |
304 | if ($result['is_error'] !== 0) { | |
305 | return $result; | |
306 | } | |
307 | if ($result['count'] === 1) { | |
308 | return $result['values'][0]; | |
309 | } | |
310 | if ($result['count'] !== 1) { | |
cf8f0fff | 311 | return civicrm_api3_create_error("Expected one " . $apiRequest['entity'] . " but found " . $result['count'], ['count' => $result['count']]); |
6a488035 TO |
312 | } |
313 | return civicrm_api3_create_error("Undefined behavior"); | |
314 | } | |
315 | ||
316 | /** | |
9d32e6f7 | 317 | * API return function to reformat results as single value. |
6a488035 | 318 | * |
cf470720 TO |
319 | * @param array $apiRequest |
320 | * Api request as an array. Keys are. | |
6a488035 | 321 | * |
df8d3074 | 322 | * @return int |
72b3a70c | 323 | * count of results |
6a488035 TO |
324 | */ |
325 | function civicrm_api3_generic_getvalue($apiRequest) { | |
326 | $apiRequest['params']['sequential'] = 1; | |
327 | $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']); | |
328 | if ($result['is_error'] !== 0) { | |
329 | return $result; | |
330 | } | |
331 | if ($result['count'] !== 1) { | |
cf8f0fff | 332 | $result = civicrm_api3_create_error("Expected one " . $apiRequest['entity'] . " but found " . $result['count'], ['count' => $result['count']]); |
6a488035 TO |
333 | return $result; |
334 | } | |
335 | ||
336 | // we only take "return=" as valid options | |
a7488080 | 337 | if (!empty($apiRequest['params']['return'])) { |
6a488035 | 338 | if (!isset($result['values'][0][$apiRequest['params']['return']])) { |
cf8f0fff | 339 | return civicrm_api3_create_error("field " . $apiRequest['params']['return'] . " unset or not existing", ['invalid_field' => $apiRequest['params']['return']]); |
6a488035 TO |
340 | } |
341 | ||
342 | return $result['values'][0][$apiRequest['params']['return']]; | |
343 | } | |
344 | ||
cf8f0fff | 345 | return civicrm_api3_create_error("missing param return=field you want to read the value of", ['error_type' => 'mandatory_missing', 'missing_param' => 'return']); |
6a488035 TO |
346 | } |
347 | ||
4e87860d | 348 | /** |
9d32e6f7 EM |
349 | * Get count of contact references. |
350 | * | |
c490a46a | 351 | * @param array $params |
3bdf1f3a | 352 | * @param array $apiRequest |
4e87860d | 353 | */ |
5050d5d4 | 354 | function _civicrm_api3_generic_getrefcount_spec(&$params, $apiRequest) { |
9c8096cb | 355 | $params['id']['api.required'] = 1; |
5050d5d4 CW |
356 | $params['id']['title'] = $apiRequest['entity'] . ' ID'; |
357 | $params['id']['type'] = CRM_Utils_Type::T_INT; | |
9c8096cb TO |
358 | } |
359 | ||
360 | /** | |
9d32e6f7 | 361 | * API to determine if a record is in-use. |
9c8096cb | 362 | * |
cf470720 TO |
363 | * @param array $apiRequest |
364 | * Api request as an array. | |
9c8096cb TO |
365 | * |
366 | * @throws API_Exception | |
a6c01b45 | 367 | * @return array |
72b3a70c | 368 | * API result (int 0 or 1) |
9c8096cb TO |
369 | */ |
370 | function civicrm_api3_generic_getrefcount($apiRequest) { | |
371 | $entityToClassMap = CRM_Core_DAO_AllCoreTables::daoToClass(); | |
372 | if (!isset($entityToClassMap[$apiRequest['entity']])) { | |
373 | throw new API_Exception("The entity '{$apiRequest['entity']}' is unknown or unsupported by 'getrefcount'. Consider implementing this API.", 'getrefcount_unsupported'); | |
374 | } | |
375 | $daoClass = $entityToClassMap[$apiRequest['entity']]; | |
376 | ||
377 | /* @var $dao CRM_Core_DAO */ | |
378 | $dao = new $daoClass(); | |
379 | $dao->id = $apiRequest['params']['id']; | |
380 | if ($dao->find(TRUE)) { | |
381 | return civicrm_api3_create_success($dao->getReferenceCounts()); | |
382 | } | |
383 | else { | |
cf8f0fff | 384 | return civicrm_api3_create_success([]); |
9c8096cb TO |
385 | } |
386 | } | |
387 | ||
6a488035 | 388 | /** |
9d32e6f7 | 389 | * API wrapper for replace function. |
6a488035 | 390 | * |
cf470720 TO |
391 | * @param array $apiRequest |
392 | * Api request as an array. Keys are. | |
6a488035 | 393 | * |
df8d3074 | 394 | * @return int |
72b3a70c | 395 | * count of results |
6a488035 TO |
396 | */ |
397 | function civicrm_api3_generic_replace($apiRequest) { | |
398 | return _civicrm_api3_generic_replace($apiRequest['entity'], $apiRequest['params']); | |
399 | } | |
400 | ||
401 | /** | |
9d32e6f7 | 402 | * API wrapper for getoptions function. |
6a488035 | 403 | * |
cf470720 TO |
404 | * @param array $apiRequest |
405 | * Api request as an array. | |
6a488035 | 406 | * |
a6c01b45 | 407 | * @return array |
16b10e64 | 408 | * Array of results |
6a488035 TO |
409 | */ |
410 | function civicrm_api3_generic_getoptions($apiRequest) { | |
9d32e6f7 | 411 | // Resolve aliases. |
70f7ba9e CW |
412 | $fieldName = _civicrm_api3_api_resolve_alias($apiRequest['entity'], $apiRequest['params']['field']); |
413 | if (!$fieldName) { | |
414 | return civicrm_api3_create_error("The field '{$apiRequest['params']['field']}' doesn't exist."); | |
415 | } | |
a4a33486 | 416 | // Validate 'context' from params |
786ad6e1 CW |
417 | $context = CRM_Utils_Array::value('context', $apiRequest['params']); |
418 | CRM_Core_DAO::buildOptionsContext($context); | |
ac50ae37 | 419 | unset($apiRequest['params']['context'], $apiRequest['params']['field'], $apiRequest['params']['condition']); |
70f7ba9e | 420 | |
786ad6e1 | 421 | $baoName = _civicrm_api3_get_BAO($apiRequest['entity']); |
b7ceb253 | 422 | $options = $baoName::buildOptions($fieldName, $context, $apiRequest['params']); |
ee2b1c1c | 423 | if ($options === FALSE) { |
70f7ba9e | 424 | return civicrm_api3_create_error("The field '{$fieldName}' has no associated option list."); |
6a488035 | 425 | } |
15a1171a CW |
426 | // Support 'sequential' output as a non-associative array |
427 | if (!empty($apiRequest['params']['sequential'])) { | |
b7ceb253 | 428 | $options = CRM_Utils_Array::makeNonAssociative($options); |
15a1171a | 429 | } |
b7ceb253 | 430 | return civicrm_api3_create_success($options, $apiRequest['params'], $apiRequest['entity'], 'getoptions'); |
6a488035 TO |
431 | } |
432 | ||
f7b2fef0 CW |
433 | /** |
434 | * Provide metadata for this generic action | |
435 | * | |
436 | * @param $params | |
437 | * @param $apiRequest | |
438 | */ | |
439 | function _civicrm_api3_generic_getoptions_spec(&$params, $apiRequest) { | |
cf8f0fff CW |
440 | $params += [ |
441 | 'field' => [ | |
f7b2fef0 CW |
442 | 'title' => 'Field name', |
443 | 'api.required' => 1, | |
444 | 'type' => CRM_Utils_Type::T_STRING, | |
cf8f0fff CW |
445 | ], |
446 | 'context' => [ | |
f7b2fef0 | 447 | 'title' => 'Context', |
f7b2fef0 | 448 | 'type' => CRM_Utils_Type::T_STRING, |
a2407bc0 | 449 | 'options' => CRM_Core_DAO::buildOptionsContext(), |
cf8f0fff CW |
450 | ], |
451 | ]; | |
acadf548 | 452 | |
a2407bc0 | 453 | // Add available fields if requested |
cf8f0fff CW |
454 | if (array_intersect(['all', 'field'], $apiRequest['params']['options']['get_options'])) { |
455 | $fields = civicrm_api3_generic_getfields(['entity' => $apiRequest['entity'], ['params' => ['action' => 'create']]]); | |
456 | $params['field']['options'] = []; | |
acadf548 CW |
457 | foreach ($fields['values'] as $name => $field) { |
458 | if (isset($field['pseudoconstant']) || CRM_Utils_Array::value('type', $field) == CRM_Utils_Type::T_BOOLEAN) { | |
2ce25e93 | 459 | $params['field']['options'][$name] = CRM_Utils_Array::value('title', $field, $name); |
acadf548 CW |
460 | } |
461 | } | |
acadf548 | 462 | } |
3370a7b1 MD |
463 | |
464 | $entityName = _civicrm_api_get_entity_name_from_camel($apiRequest['entity']); | |
465 | $getOptionsSpecFunction = '_civicrm_api3_' . $entityName . '_getoptions_spec'; | |
466 | ||
467 | if (function_exists($getOptionsSpecFunction)) { | |
468 | $getOptionsSpecFunction($params); | |
469 | } | |
f7b2fef0 CW |
470 | } |
471 | ||
11e09c59 | 472 | /** |
dc64d047 EM |
473 | * Get metadata. |
474 | * | |
6a488035 TO |
475 | * Function fills the 'options' array on the metadata returned by getfields if |
476 | * 1) the param option 'get_options' is defined - e.g. $params['options']['get_options'] => array('custom_1) | |
477 | * (this is passed in as the $fieldsToResolve array) | |
478 | * 2) the field is a pseudoconstant and is NOT an FK | |
479 | * - the reason for this is that checking / transformation is done on pseudoconstants but | |
480 | * - if the field is an FK then mysql will enforce the data quality (& we have handling on failure) | |
7c285037 | 481 | * @todo - if may be we should define a 'resolve' key on the pseudoconstant for when these rules are not fine enough |
6a488035 TO |
482 | * |
483 | * This function is only split out for the purpose of code clarity / comment block documentation | |
77b97be7 | 484 | * |
cf470720 TO |
485 | * @param array $metadata |
486 | * The array of metadata that will form the result of the getfields function. | |
72b3a70c | 487 | * @param array $apiRequest |
cf470720 TO |
488 | * @param string $fieldname |
489 | * Field currently being processed. | |
490 | * @param array $fieldSpec | |
491 | * Metadata for that field. | |
6a488035 | 492 | */ |
b432ddaa | 493 | function _civicrm_api3_generic_get_metadata_options(&$metadata, $apiRequest, $fieldname, $fieldSpec) { |
3a8e9315 | 494 | if (empty($fieldSpec['pseudoconstant']) && empty($fieldSpec['option_group_id'])) { |
6a488035 TO |
495 | return; |
496 | } | |
497 | ||
b432ddaa CW |
498 | $fieldsToResolve = $apiRequest['params']['options']['get_options']; |
499 | ||
ed8abbbb | 500 | if (!empty($metadata[$fieldname]['options']) || (!in_array($fieldname, $fieldsToResolve) && !in_array('all', $fieldsToResolve))) { |
70f7ba9e CW |
501 | return; |
502 | } | |
503 | ||
b432ddaa CW |
504 | // Allow caller to specify context |
505 | $context = CRM_Utils_Array::value('get_options_context', $apiRequest['params']['options']); | |
506 | // Default to api action if it is a supported context. | |
507 | if (!$context) { | |
508 | $action = CRM_Utils_Array::value('action', $apiRequest['params']); | |
509 | $contexts = CRM_Core_DAO::buildOptionsContext(); | |
510 | if (isset($contexts[$action])) { | |
511 | $context = $action; | |
512 | } | |
513 | } | |
514 | ||
cf8f0fff | 515 | $options = civicrm_api($apiRequest['entity'], 'getoptions', ['version' => 3, 'field' => $fieldname, 'context' => $context]); |
6a488035 TO |
516 | if (is_array(CRM_Utils_Array::value('values', $options))) { |
517 | $metadata[$fieldname]['options'] = $options['values']; | |
518 | } | |
519 | } |