Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
b081365f CW |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.6 | | |
5 | +--------------------------------------------------------------------+ | |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
b081365f CW |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * @package CiviCRM_APIv3 | |
30 | */ | |
6a488035 TO |
31 | |
32 | /** | |
61fe4988 EM |
33 | * Get information about fields for a given api request. |
34 | * | |
35 | * Getfields information is used for documentation, validation, default setting | |
6a488035 TO |
36 | * We first query the scheme using the $dao->fields function & then augment |
37 | * that information by calling the _spec functions that apply to the relevant function | |
38 | * Note that we use 'unique' field names as described in the xml/schema files | |
39 | * for get requests & just field name for create. This is because some get functions | |
40 | * access multiple objects e.g. contact api accesses is_deleted from the activity | |
41 | * table & from the contact table | |
42 | * | |
cf470720 TO |
43 | * @param array $apiRequest |
44 | * Api request as an array. Keys are. | |
16b10e64 CW |
45 | * - entity: string |
46 | * - action: string | |
47 | * - version: string | |
48 | * - function: callback (mixed) | |
49 | * - params: array, varies | |
9d32e6f7 | 50 | * |
26a700db CW |
51 | * @param bool $unique |
52 | * Determines whether to key by unique field names (only affects get-type) actions | |
53 | * | |
a6c01b45 | 54 | * @return array |
72b3a70c | 55 | * API success object |
6a488035 | 56 | */ |
26a700db | 57 | function civicrm_api3_generic_getfields($apiRequest, $unique = TRUE) { |
6a488035 TO |
58 | static $results = array(); |
59 | if ((CRM_Utils_Array::value('cache_clear', $apiRequest['params']))) { | |
60 | $results = array(); | |
61 | // we will also clear pseudoconstants here - should potentially be moved to relevant BAO classes | |
37547b77 | 62 | CRM_Core_PseudoConstant::flush(); |
9b873358 | 63 | if (!empty($apiRequest['params']['fieldname'])) { |
6a488035 TO |
64 | CRM_Utils_PseudoConstant::flushConstant($apiRequest['params']['fieldname']); |
65 | } | |
9b873358 | 66 | if (!empty($apiRequest['params']['option_group_id'])) { |
9d32e6f7 EM |
67 | $optionGroupName = civicrm_api('option_group', 'getvalue', array( |
68 | 'version' => 3, | |
69 | 'id' => $apiRequest['params']['option_group_id'], | |
70 | 'return' => 'name', | |
71 | )); | |
9b873358 | 72 | if (is_string($optionGroupName)) { |
6a488035 TO |
73 | CRM_Utils_PseudoConstant::flushConstant(_civicrm_api_get_camel_name($optionGroupName)); |
74 | } | |
75 | } | |
76 | } | |
4846df91 CW |
77 | $entity = $apiRequest['entity']; |
78 | $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity); | |
6a488035 | 79 | $subentity = CRM_Utils_Array::value('contact_type', $apiRequest['params']); |
4846df91 | 80 | $action = CRM_Utils_Array::value('action', $apiRequest['params']); |
fce45a2f | 81 | $sequential = empty($apiRequest['params']['sequential']) ? 0 : 1; |
2ce25e93 CW |
82 | $apiRequest['params']['options'] = CRM_Utils_Array::value('options', $apiRequest['params'], array()); |
83 | $optionsToResolve = (array) CRM_Utils_Array::value('get_options', $apiRequest['params']['options'], array()); | |
acadf548 | 84 | |
54df0f0c | 85 | if (!$action || $action == 'getvalue' || $action == 'getcount') { |
6a488035 TO |
86 | $action = 'get'; |
87 | } | |
2ce25e93 CW |
88 | // If no options, return results from cache |
89 | if (!$apiRequest['params']['options'] && isset($results[$entity . $subentity]) && isset($action, $results[$entity . $subentity]) | |
ceccbc35 | 90 | && isset($action, $results[$entity . $subentity][$sequential])) { |
91 | return $results[$entity . $subentity][$action][$sequential]; | |
6a488035 TO |
92 | } |
93 | // defaults based on data model and API policy | |
94 | switch ($action) { | |
95 | case 'getfields': | |
9c8096cb | 96 | $values = _civicrm_api_get_fields($entity, FALSE, $apiRequest['params']); |
f2b53f26 | 97 | return civicrm_api3_create_success($values, $apiRequest['params'], $entity, 'getfields'); |
ea100cb5 | 98 | |
6a488035 TO |
99 | case 'create': |
100 | case 'update': | |
101 | case 'replace': | |
102 | $unique = FALSE; | |
103 | case 'get': | |
2b6e1174 CW |
104 | case 'getsingle': |
105 | case 'getcount': | |
9c32b8f6 | 106 | case 'getstat': |
6a488035 | 107 | $metadata = _civicrm_api_get_fields($apiRequest['entity'], $unique, $apiRequest['params']); |
9b873358 | 108 | if (empty($metadata['id'])) { |
9ec90e57 | 109 | // if id is not set we will set it eg. 'id' from 'case_id', case_id will be an alias |
22e263ad | 110 | if (!empty($metadata[strtolower($apiRequest['entity']) . '_id'])) { |
4846df91 CW |
111 | $metadata['id'] = $metadata[$lowercase_entity . '_id']; |
112 | unset($metadata[$lowercase_entity . '_id']); | |
113 | $metadata['id']['api.aliases'] = array($lowercase_entity . '_id'); | |
9ec90e57 | 114 | } |
115 | } | |
92e4c2a5 | 116 | else { |
9ec90e57 | 117 | // really the preference would be to set the unique name in the xml |
118 | // question is which is a less risky fix this close to a release - setting in xml for the known failure | |
119 | // (note) or setting for all api where fields is returning 'id' & we want to accept 'note_id' @ the api layer | |
120 | // nb we don't officially accept note_id anyway - rationale here is more about centralising a now-tested | |
121 | // inconsistency | |
4846df91 | 122 | $metadata['id']['api.aliases'] = array($lowercase_entity . '_id'); |
6a488035 TO |
123 | } |
124 | break; | |
125 | ||
126 | case 'delete': | |
127 | $metadata = array( | |
3a8e9315 CW |
128 | 'id' => array( |
129 | 'title' => $entity . ' ID', | |
6a488035 | 130 | 'api.required' => 1, |
4846df91 | 131 | 'api.aliases' => array($lowercase_entity . '_id'), |
b2402735 | 132 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 TO |
133 | )); |
134 | break; | |
135 | ||
f7b2fef0 CW |
136 | // Note: adding setvalue case here instead of in a generic spec function because |
137 | // some APIs override the generic setvalue fn which causes the generic spec to be overlooked. | |
138 | case 'setvalue': | |
6a488035 | 139 | $metadata = array( |
786ad6e1 | 140 | 'field' => array( |
3a8e9315 | 141 | 'title' => 'Field name', |
786ad6e1 | 142 | 'api.required' => 1, |
f7b2fef0 | 143 | 'type' => CRM_Utils_Type::T_STRING, |
786ad6e1 | 144 | ), |
f7b2fef0 CW |
145 | 'id' => array( |
146 | 'title' => $entity . ' ID', | |
147 | 'api.required' => 1, | |
148 | 'type' => CRM_Utils_Type::T_INT, | |
149 | ), | |
150 | 'value' => array( | |
151 | 'title' => 'Value', | |
152 | 'description' => "Field value to set", | |
153 | 'api.required' => 1, | |
786ad6e1 CW |
154 | ), |
155 | ); | |
acadf548 CW |
156 | if (array_intersect(array('all', 'field'), $optionsToResolve)) { |
157 | $options = civicrm_api3_generic_getfields(array('entity' => $entity, array('params' => array('action' => 'create')))); | |
2ce25e93 | 158 | $metadata['field']['options'] = CRM_Utils_Array::collect('title', $options['values']); |
acadf548 | 159 | } |
c866eb5f TO |
160 | break; |
161 | ||
6a488035 TO |
162 | default: |
163 | // oddballs are on their own | |
164 | $metadata = array(); | |
165 | } | |
166 | ||
2ce25e93 CW |
167 | // Normalize this for the sake of spec funcions |
168 | $apiRequest['params']['options']['get_options'] = $optionsToResolve; | |
169 | ||
6a488035 TO |
170 | // find any supplemental information |
171 | $hypApiRequest = array('entity' => $apiRequest['entity'], 'action' => $action, 'version' => $apiRequest['version']); | |
c65db512 TO |
172 | try { |
173 | list ($apiProvider, $hypApiRequest) = \Civi\Core\Container::singleton()->get('civi_api_kernel')->resolve($hypApiRequest); | |
378e2654 TO |
174 | if (isset($hypApiRequest['function'])) { |
175 | $helper = '_' . $hypApiRequest['function'] . '_spec'; | |
0db6c3e1 TO |
176 | } |
177 | else { | |
378e2654 TO |
178 | // not implemented MagicFunctionProvider |
179 | $helper = NULL; | |
180 | } | |
0db6c3e1 TO |
181 | } |
182 | catch (\Civi\API\Exception\NotImplementedException $e) { | |
c65db512 TO |
183 | $helper = NULL; |
184 | } | |
6a488035 TO |
185 | if (function_exists($helper)) { |
186 | // alter | |
6a386447 | 187 | $helper($metadata, $apiRequest); |
6a488035 TO |
188 | } |
189 | ||
6a488035 | 190 | foreach ($metadata as $fieldname => $fieldSpec) { |
a419926d CW |
191 | // Ensure 'name' is set |
192 | if (!isset($fieldSpec['name'])) { | |
193 | $metadata[$fieldname]['name'] = $fieldname; | |
194 | } | |
b432ddaa | 195 | _civicrm_api3_generic_get_metadata_options($metadata, $apiRequest, $fieldname, $fieldSpec); |
fce45a2f | 196 | |
2ce25e93 | 197 | // Convert options to "sequential" format |
fce45a2f | 198 | if ($sequential && !empty($metadata[$fieldname]['options'])) { |
2ce25e93 CW |
199 | $metadata[$fieldname]['options'] = CRM_Utils_Array::makeNonAssociative($metadata[$fieldname]['options']); |
200 | } | |
6a488035 TO |
201 | } |
202 | ||
a14e9d08 | 203 | $results[$entity][$action][$sequential] = civicrm_api3_create_success($metadata, $apiRequest['params'], $entity, 'getfields'); |
ceccbc35 | 204 | return $results[$entity][$action][$sequential]; |
6a488035 TO |
205 | } |
206 | ||
26a700db CW |
207 | /** |
208 | * Get metadata for a field | |
209 | * | |
210 | * @param array $apiRequest | |
211 | * | |
212 | * @return array | |
213 | * API success object | |
214 | */ | |
215 | function civicrm_api3_generic_getfield($apiRequest) { | |
216 | $params = $apiRequest['params']; | |
217 | $sequential = !empty($params['sequential']); | |
218 | $fieldName = _civicrm_api3_api_resolve_alias($apiRequest['entity'], $params['name'], $params['action']); | |
219 | if (!$fieldName) { | |
220 | return civicrm_api3_create_error("The field '{$params['name']}' doesn't exist."); | |
221 | } | |
222 | // Turn off sequential to make the field easier to find | |
223 | $apiRequest['params']['sequential'] = 0; | |
d60a6fba CW |
224 | if (isset($params['get_options'])) { |
225 | $apiRequest['params']['options']['get_options_context'] = $params['get_options']; | |
26a700db CW |
226 | $apiRequest['params']['options']['get_options'] = $fieldName; |
227 | } | |
26a700db CW |
228 | $result = civicrm_api3_generic_getfields($apiRequest, FALSE); |
229 | $result = $result['values'][$fieldName]; | |
230 | // Fix sequential options since we forced it off | |
231 | if ($sequential && !empty($result['options'])) { | |
232 | $result['options'] = CRM_Utils_Array::makeNonAssociative($result['options']); | |
233 | } | |
234 | return civicrm_api3_create_success($result, $apiRequest['params'], $apiRequest['entity'], 'getfield'); | |
235 | } | |
236 | ||
237 | ||
238 | function _civicrm_api3_generic_getfield_spec(&$params, $apiRequest) { | |
239 | $params = array( | |
240 | 'name' => array( | |
241 | 'title' => 'Field name', | |
242 | 'description' => 'Name or alias of field to lookup', | |
243 | 'api.required' => 1, | |
244 | 'type' => CRM_Utils_Type::T_STRING, | |
245 | ), | |
246 | 'action' => array( | |
247 | 'title' => 'API Action', | |
248 | 'api.required' => 1, | |
249 | 'type' => CRM_Utils_Type::T_STRING, | |
250 | 'api.aliases' => array('api_action'), | |
251 | ), | |
d60a6fba CW |
252 | 'get_options' => array( |
253 | 'title' => 'Get Options', | |
254 | 'description' => 'Context for which to get field options, or null to skip fetching options.', | |
26a700db CW |
255 | 'type' => CRM_Utils_Type::T_STRING, |
256 | 'options' => CRM_Core_DAO::buildOptionsContext(), | |
d60a6fba | 257 | 'api.aliases' => array('context'), |
26a700db CW |
258 | ), |
259 | ); | |
260 | // Add available options to these params if requested | |
261 | if (array_intersect(array('all', 'action'), $apiRequest['params']['options']['get_options'])) { | |
262 | $actions = civicrm_api3($apiRequest['entity'], 'getactions'); | |
263 | $actions = array_combine($actions['values'], $actions['values']); | |
264 | // Let's not go meta-crazy | |
265 | CRM_Utils_Array::remove($actions, 'getactions', 'getoptions', 'getfields', 'getfield', 'getcount', 'getrefcount', 'getsingle', 'getlist', 'getvalue', 'setvalue', 'update'); | |
266 | $params['action']['options'] = $actions; | |
267 | } | |
268 | } | |
269 | ||
6a488035 | 270 | /** |
9d32e6f7 | 271 | * API return function to reformat results as count. |
6a488035 | 272 | * |
cf470720 TO |
273 | * @param array $apiRequest |
274 | * Api request as an array. Keys are. | |
6a488035 | 275 | * |
77b97be7 | 276 | * @throws API_Exception |
df8d3074 | 277 | * @return int |
72b3a70c | 278 | * count of results |
6a488035 TO |
279 | */ |
280 | function civicrm_api3_generic_getcount($apiRequest) { | |
972322c5 | 281 | $apiRequest['params']['options']['is_count'] = TRUE; |
6a488035 | 282 | $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']); |
37fa58b0 | 283 | if (is_numeric(CRM_Utils_Array::value('values', $result))) { |
972322c5 | 284 | return (int) $result['values']; |
285 | } | |
22e263ad | 286 | if (!isset($result['count'])) { |
8335b10a | 287 | throw new API_Exception(ts('Unexpected result from getcount') . print_r($result, TRUE)); |
288 | } | |
6a488035 TO |
289 | return $result['count']; |
290 | } | |
291 | ||
292 | /** | |
9d32e6f7 | 293 | * API return function to reformat results as single result. |
6a488035 | 294 | * |
cf470720 TO |
295 | * @param array $apiRequest |
296 | * Api request as an array. Keys are. | |
6a488035 | 297 | * |
df8d3074 | 298 | * @return int |
72b3a70c | 299 | * count of results |
6a488035 TO |
300 | */ |
301 | function civicrm_api3_generic_getsingle($apiRequest) { | |
9d32e6f7 | 302 | // So the first entity is always result['values'][0]. |
6a488035 TO |
303 | $apiRequest['params']['sequential'] = 1; |
304 | $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']); | |
305 | if ($result['is_error'] !== 0) { | |
306 | return $result; | |
307 | } | |
308 | if ($result['count'] === 1) { | |
309 | return $result['values'][0]; | |
310 | } | |
311 | if ($result['count'] !== 1) { | |
312 | return civicrm_api3_create_error("Expected one " . $apiRequest['entity'] . " but found " . $result['count'], array('count' => $result['count'])); | |
313 | } | |
314 | return civicrm_api3_create_error("Undefined behavior"); | |
315 | } | |
316 | ||
317 | /** | |
9d32e6f7 | 318 | * API return function to reformat results as single value. |
6a488035 | 319 | * |
cf470720 TO |
320 | * @param array $apiRequest |
321 | * Api request as an array. Keys are. | |
6a488035 | 322 | * |
df8d3074 | 323 | * @return int |
72b3a70c | 324 | * count of results |
6a488035 TO |
325 | */ |
326 | function civicrm_api3_generic_getvalue($apiRequest) { | |
327 | $apiRequest['params']['sequential'] = 1; | |
328 | $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']); | |
329 | if ($result['is_error'] !== 0) { | |
330 | return $result; | |
331 | } | |
332 | if ($result['count'] !== 1) { | |
333 | $result = civicrm_api3_create_error("Expected one " . $apiRequest['entity'] . " but found " . $result['count'], array('count' => $result['count'])); | |
334 | return $result; | |
335 | } | |
336 | ||
337 | // we only take "return=" as valid options | |
a7488080 | 338 | if (!empty($apiRequest['params']['return'])) { |
6a488035 TO |
339 | if (!isset($result['values'][0][$apiRequest['params']['return']])) { |
340 | return civicrm_api3_create_error("field " . $apiRequest['params']['return'] . " unset or not existing", array('invalid_field' => $apiRequest['params']['return'])); | |
341 | } | |
342 | ||
343 | return $result['values'][0][$apiRequest['params']['return']]; | |
344 | } | |
345 | ||
346 | return civicrm_api3_create_error("missing param return=field you want to read the value of", array('error_type' => 'mandatory_missing', 'missing_param' => 'return')); | |
347 | } | |
348 | ||
4e87860d | 349 | /** |
9d32e6f7 EM |
350 | * Get count of contact references. |
351 | * | |
c490a46a | 352 | * @param array $params |
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 { | |
384 | return civicrm_api3_create_success(array()); | |
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); | |
a3d8b390 | 419 | unset($apiRequest['params']['context'], $apiRequest['params']['field']); |
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) { | |
f7b2fef0 CW |
440 | $params += array( |
441 | 'field' => array( | |
442 | 'title' => 'Field name', | |
443 | 'api.required' => 1, | |
444 | 'type' => CRM_Utils_Type::T_STRING, | |
445 | ), | |
446 | 'context' => array( | |
447 | 'title' => 'Context', | |
f7b2fef0 | 448 | 'type' => CRM_Utils_Type::T_STRING, |
a2407bc0 | 449 | 'options' => CRM_Core_DAO::buildOptionsContext(), |
f7b2fef0 CW |
450 | ), |
451 | ); | |
acadf548 | 452 | |
a2407bc0 | 453 | // Add available fields if requested |
2ce25e93 | 454 | if (array_intersect(array('all', 'field'), $apiRequest['params']['options']['get_options'])) { |
acadf548 | 455 | $fields = civicrm_api3_generic_getfields(array('entity' => $apiRequest['entity'], array('params' => array('action' => 'create')))); |
2ce25e93 | 456 | $params['field']['options'] = array(); |
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 | } |
f7b2fef0 CW |
463 | } |
464 | ||
11e09c59 | 465 | /** |
dc64d047 EM |
466 | * Get metadata. |
467 | * | |
6a488035 TO |
468 | * Function fills the 'options' array on the metadata returned by getfields if |
469 | * 1) the param option 'get_options' is defined - e.g. $params['options']['get_options'] => array('custom_1) | |
470 | * (this is passed in as the $fieldsToResolve array) | |
471 | * 2) the field is a pseudoconstant and is NOT an FK | |
472 | * - the reason for this is that checking / transformation is done on pseudoconstants but | |
473 | * - if the field is an FK then mysql will enforce the data quality (& we have handling on failure) | |
7c285037 | 474 | * @todo - if may be we should define a 'resolve' key on the pseudoconstant for when these rules are not fine enough |
6a488035 TO |
475 | * |
476 | * This function is only split out for the purpose of code clarity / comment block documentation | |
77b97be7 | 477 | * |
cf470720 TO |
478 | * @param array $metadata |
479 | * The array of metadata that will form the result of the getfields function. | |
72b3a70c | 480 | * @param array $apiRequest |
cf470720 TO |
481 | * @param string $fieldname |
482 | * Field currently being processed. | |
483 | * @param array $fieldSpec | |
484 | * Metadata for that field. | |
6a488035 | 485 | */ |
b432ddaa | 486 | function _civicrm_api3_generic_get_metadata_options(&$metadata, $apiRequest, $fieldname, $fieldSpec) { |
3a8e9315 | 487 | if (empty($fieldSpec['pseudoconstant']) && empty($fieldSpec['option_group_id'])) { |
6a488035 TO |
488 | return; |
489 | } | |
490 | ||
b432ddaa CW |
491 | $fieldsToResolve = $apiRequest['params']['options']['get_options']; |
492 | ||
ed8abbbb | 493 | if (!empty($metadata[$fieldname]['options']) || (!in_array($fieldname, $fieldsToResolve) && !in_array('all', $fieldsToResolve))) { |
70f7ba9e CW |
494 | return; |
495 | } | |
496 | ||
b432ddaa CW |
497 | // Allow caller to specify context |
498 | $context = CRM_Utils_Array::value('get_options_context', $apiRequest['params']['options']); | |
499 | // Default to api action if it is a supported context. | |
500 | if (!$context) { | |
501 | $action = CRM_Utils_Array::value('action', $apiRequest['params']); | |
502 | $contexts = CRM_Core_DAO::buildOptionsContext(); | |
503 | if (isset($contexts[$action])) { | |
504 | $context = $action; | |
505 | } | |
506 | } | |
507 | ||
508 | $options = civicrm_api($apiRequest['entity'], 'getoptions', array('version' => 3, 'field' => $fieldname, 'context' => $context)); | |
6a488035 TO |
509 | if (is_array(CRM_Utils_Array::value('values', $options))) { |
510 | $metadata[$fieldname]['options'] = $options['values']; | |
511 | } | |
512 | } |