Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /** | |
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 | |
12 | * | |
cf470720 TO |
13 | * @param array $apiRequest |
14 | * Api request as an array. Keys are. | |
6a488035 TO |
15 | * - entity: string |
16 | * - action: string | |
17 | * - version: string | |
18 | * - function: callback (mixed) | |
19 | * - params: array, varies | |
a6c01b45 CW |
20 | * @return array |
21 | * API success object | |
6a488035 TO |
22 | */ |
23 | function civicrm_api3_generic_getfields($apiRequest) { | |
24 | static $results = array(); | |
25 | if ((CRM_Utils_Array::value('cache_clear', $apiRequest['params']))) { | |
26 | $results = array(); | |
27 | // we will also clear pseudoconstants here - should potentially be moved to relevant BAO classes | |
37547b77 | 28 | CRM_Core_PseudoConstant::flush(); |
9b873358 | 29 | if (!empty($apiRequest['params']['fieldname'])) { |
6a488035 TO |
30 | CRM_Utils_PseudoConstant::flushConstant($apiRequest['params']['fieldname']); |
31 | } | |
9b873358 | 32 | if (!empty($apiRequest['params']['option_group_id'])) { |
481a74f4 | 33 | $optionGroupName = civicrm_api('option_group', 'getvalue', array('version' => 3, 'id' => $apiRequest['params']['option_group_id'], 'return' => 'name')); |
9b873358 | 34 | if (is_string($optionGroupName)) { |
6a488035 TO |
35 | CRM_Utils_PseudoConstant::flushConstant(_civicrm_api_get_camel_name($optionGroupName)); |
36 | } | |
37 | } | |
38 | } | |
39 | $entity = _civicrm_api_get_camel_name($apiRequest['entity']); | |
40 | $lcase_entity = _civicrm_api_get_entity_name_from_camel($entity); | |
41 | $subentity = CRM_Utils_Array::value('contact_type', $apiRequest['params']); | |
42 | $action = strtolower(CRM_Utils_Array::value('action', $apiRequest['params'])); | |
ceccbc35 | 43 | $sequential = empty($apiRequest['params']) ? 0 : 1; |
6a488035 | 44 | $apiOptions = CRM_Utils_Array::value('options', $apiRequest['params'], array()); |
54df0f0c | 45 | if (!$action || $action == 'getvalue' || $action == 'getcount') { |
6a488035 TO |
46 | $action = 'get'; |
47 | } | |
6a488035 TO |
48 | // determines whether to use unique field names - seem comment block above |
49 | $unique = TRUE; | |
ceccbc35 | 50 | if (empty($apiOptions) && isset($results[$entity . $subentity]) && isset($action, $results[$entity . $subentity]) |
51 | && isset($action, $results[$entity . $subentity][$sequential])) { | |
52 | return $results[$entity . $subentity][$action][$sequential]; | |
6a488035 TO |
53 | } |
54 | // defaults based on data model and API policy | |
55 | switch ($action) { | |
56 | case 'getfields': | |
9c8096cb | 57 | $values = _civicrm_api_get_fields($entity, FALSE, $apiRequest['params']); |
f2b53f26 | 58 | return civicrm_api3_create_success($values, $apiRequest['params'], $entity, 'getfields'); |
6a488035 TO |
59 | case 'create': |
60 | case 'update': | |
61 | case 'replace': | |
62 | $unique = FALSE; | |
63 | case 'get': | |
2b6e1174 CW |
64 | case 'getsingle': |
65 | case 'getcount': | |
6a488035 | 66 | $metadata = _civicrm_api_get_fields($apiRequest['entity'], $unique, $apiRequest['params']); |
9b873358 | 67 | if (empty($metadata['id'])) { |
9ec90e57 | 68 | // if id is not set we will set it eg. 'id' from 'case_id', case_id will be an alias |
22e263ad | 69 | if (!empty($metadata[strtolower($apiRequest['entity']) . '_id'])) { |
9ec90e57 | 70 | $metadata['id'] = $metadata[$lcase_entity . '_id']; |
71 | unset($metadata[$lcase_entity . '_id']); | |
72 | $metadata['id']['api.aliases'] = array($lcase_entity . '_id'); | |
73 | } | |
74 | } | |
92e4c2a5 | 75 | else { |
9ec90e57 | 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 | |
80 | // inconsistency | |
6a488035 | 81 | $metadata['id']['api.aliases'] = array($lcase_entity . '_id'); |
6a488035 TO |
82 | } |
83 | break; | |
84 | ||
85 | case 'delete': | |
86 | $metadata = array( | |
3a8e9315 CW |
87 | 'id' => array( |
88 | 'title' => $entity . ' ID', | |
89 | 'name' => 'id', | |
6a488035 TO |
90 | 'api.required' => 1, |
91 | 'api.aliases' => array($lcase_entity . '_id'), | |
b2402735 | 92 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 TO |
93 | )); |
94 | break; | |
95 | ||
96 | case 'getoptions': | |
97 | $metadata = array( | |
786ad6e1 | 98 | 'field' => array( |
3a8e9315 CW |
99 | 'name' => 'field', |
100 | 'title' => 'Field name', | |
786ad6e1 CW |
101 | 'api.required' => 1, |
102 | ), | |
103 | 'context' => array( | |
3a8e9315 CW |
104 | 'name' => 'context', |
105 | 'title' => 'Context', | |
786ad6e1 CW |
106 | ), |
107 | ); | |
c866eb5f TO |
108 | break; |
109 | ||
6a488035 TO |
110 | default: |
111 | // oddballs are on their own | |
112 | $metadata = array(); | |
113 | } | |
114 | ||
115 | // find any supplemental information | |
116 | $hypApiRequest = array('entity' => $apiRequest['entity'], 'action' => $action, 'version' => $apiRequest['version']); | |
c65db512 TO |
117 | try { |
118 | list ($apiProvider, $hypApiRequest) = \Civi\Core\Container::singleton()->get('civi_api_kernel')->resolve($hypApiRequest); | |
378e2654 TO |
119 | if (isset($hypApiRequest['function'])) { |
120 | $helper = '_' . $hypApiRequest['function'] . '_spec'; | |
0db6c3e1 TO |
121 | } |
122 | else { | |
378e2654 TO |
123 | // not implemented MagicFunctionProvider |
124 | $helper = NULL; | |
125 | } | |
0db6c3e1 TO |
126 | } |
127 | catch (\Civi\API\Exception\NotImplementedException $e) { | |
c65db512 TO |
128 | $helper = NULL; |
129 | } | |
6a488035 TO |
130 | if (function_exists($helper)) { |
131 | // alter | |
6a386447 | 132 | $helper($metadata, $apiRequest); |
6a488035 TO |
133 | } |
134 | ||
a4c5e9a3 | 135 | $fieldsToResolve = (array) CRM_Utils_Array::value('get_options', $apiOptions, array()); |
6a488035 TO |
136 | |
137 | foreach ($metadata as $fieldname => $fieldSpec) { | |
ddaac11c | 138 | _civicrm_api3_generic_get_metadata_options($metadata, $apiRequest, $fieldname, $fieldSpec, $fieldsToResolve); |
6a488035 TO |
139 | } |
140 | ||
a14e9d08 | 141 | $results[$entity][$action][$sequential] = civicrm_api3_create_success($metadata, $apiRequest['params'], $entity, 'getfields'); |
ceccbc35 | 142 | return $results[$entity][$action][$sequential]; |
6a488035 TO |
143 | } |
144 | ||
145 | /** | |
146 | * API return function to reformat results as count | |
147 | * | |
cf470720 TO |
148 | * @param array $apiRequest |
149 | * Api request as an array. Keys are. | |
6a488035 | 150 | * |
77b97be7 | 151 | * @throws API_Exception |
a6c01b45 CW |
152 | * @return integer |
153 | * count of results | |
6a488035 TO |
154 | */ |
155 | function civicrm_api3_generic_getcount($apiRequest) { | |
972322c5 | 156 | $apiRequest['params']['options']['is_count'] = TRUE; |
6a488035 | 157 | $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']); |
37fa58b0 | 158 | if (is_numeric(CRM_Utils_Array::value('values', $result))) { |
972322c5 | 159 | return (int) $result['values']; |
160 | } | |
22e263ad | 161 | if (!isset($result['count'])) { |
8335b10a | 162 | throw new API_Exception(ts('Unexpected result from getcount') . print_r($result, TRUE)); |
163 | } | |
6a488035 TO |
164 | return $result['count']; |
165 | } | |
166 | ||
167 | /** | |
168 | * API return function to reformat results as single result | |
169 | * | |
cf470720 TO |
170 | * @param array $apiRequest |
171 | * Api request as an array. Keys are. | |
6a488035 | 172 | * |
a6c01b45 CW |
173 | * @return integer |
174 | * count of results | |
6a488035 TO |
175 | */ |
176 | function civicrm_api3_generic_getsingle($apiRequest) { | |
177 | // so the first entity is always result['values'][0] | |
178 | $apiRequest['params']['sequential'] = 1; | |
179 | $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']); | |
180 | if ($result['is_error'] !== 0) { | |
181 | return $result; | |
182 | } | |
183 | if ($result['count'] === 1) { | |
184 | return $result['values'][0]; | |
185 | } | |
186 | if ($result['count'] !== 1) { | |
187 | return civicrm_api3_create_error("Expected one " . $apiRequest['entity'] . " but found " . $result['count'], array('count' => $result['count'])); | |
188 | } | |
189 | return civicrm_api3_create_error("Undefined behavior"); | |
190 | } | |
191 | ||
192 | /** | |
193 | * API return function to reformat results as single value | |
194 | * | |
cf470720 TO |
195 | * @param array $apiRequest |
196 | * Api request as an array. Keys are. | |
6a488035 | 197 | * |
a6c01b45 CW |
198 | * @return integer |
199 | * count of results | |
6a488035 TO |
200 | */ |
201 | function civicrm_api3_generic_getvalue($apiRequest) { | |
202 | $apiRequest['params']['sequential'] = 1; | |
203 | $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']); | |
204 | if ($result['is_error'] !== 0) { | |
205 | return $result; | |
206 | } | |
207 | if ($result['count'] !== 1) { | |
208 | $result = civicrm_api3_create_error("Expected one " . $apiRequest['entity'] . " but found " . $result['count'], array('count' => $result['count'])); | |
209 | return $result; | |
210 | } | |
211 | ||
212 | // we only take "return=" as valid options | |
a7488080 | 213 | if (!empty($apiRequest['params']['return'])) { |
6a488035 TO |
214 | if (!isset($result['values'][0][$apiRequest['params']['return']])) { |
215 | return civicrm_api3_create_error("field " . $apiRequest['params']['return'] . " unset or not existing", array('invalid_field' => $apiRequest['params']['return'])); | |
216 | } | |
217 | ||
218 | return $result['values'][0][$apiRequest['params']['return']]; | |
219 | } | |
220 | ||
221 | return civicrm_api3_create_error("missing param return=field you want to read the value of", array('error_type' => 'mandatory_missing', 'missing_param' => 'return')); | |
222 | } | |
223 | ||
4e87860d | 224 | /** |
c490a46a | 225 | * @param array $params |
4e87860d | 226 | */ |
9c8096cb TO |
227 | function _civicrm_api3_generic_getrefcount_spec(&$params) { |
228 | $params['id']['api.required'] = 1; | |
4c41ecb2 | 229 | $params['id']['title'] = 'Entity ID'; |
9c8096cb TO |
230 | } |
231 | ||
232 | /** | |
233 | * API to determine if a record is in-use | |
234 | * | |
cf470720 TO |
235 | * @param array $apiRequest |
236 | * Api request as an array. | |
9c8096cb TO |
237 | * |
238 | * @throws API_Exception | |
a6c01b45 CW |
239 | * @return array |
240 | * API result (int 0 or 1) | |
9c8096cb TO |
241 | */ |
242 | function civicrm_api3_generic_getrefcount($apiRequest) { | |
243 | $entityToClassMap = CRM_Core_DAO_AllCoreTables::daoToClass(); | |
244 | if (!isset($entityToClassMap[$apiRequest['entity']])) { | |
245 | throw new API_Exception("The entity '{$apiRequest['entity']}' is unknown or unsupported by 'getrefcount'. Consider implementing this API.", 'getrefcount_unsupported'); | |
246 | } | |
247 | $daoClass = $entityToClassMap[$apiRequest['entity']]; | |
248 | ||
249 | /* @var $dao CRM_Core_DAO */ | |
250 | $dao = new $daoClass(); | |
251 | $dao->id = $apiRequest['params']['id']; | |
252 | if ($dao->find(TRUE)) { | |
253 | return civicrm_api3_create_success($dao->getReferenceCounts()); | |
254 | } | |
255 | else { | |
256 | return civicrm_api3_create_success(array()); | |
257 | } | |
258 | } | |
259 | ||
6a488035 TO |
260 | /** |
261 | * API wrapper for replace function | |
262 | * | |
cf470720 TO |
263 | * @param array $apiRequest |
264 | * Api request as an array. Keys are. | |
6a488035 | 265 | * |
a6c01b45 CW |
266 | * @return integer |
267 | * count of results | |
6a488035 TO |
268 | */ |
269 | function civicrm_api3_generic_replace($apiRequest) { | |
270 | return _civicrm_api3_generic_replace($apiRequest['entity'], $apiRequest['params']); | |
271 | } | |
272 | ||
273 | /** | |
274 | * API wrapper for getoptions function | |
275 | * | |
cf470720 TO |
276 | * @param array $apiRequest |
277 | * Api request as an array. | |
6a488035 | 278 | * |
a6c01b45 CW |
279 | * @return array |
280 | * of results | |
6a488035 TO |
281 | */ |
282 | function civicrm_api3_generic_getoptions($apiRequest) { | |
70f7ba9e CW |
283 | // Resolve aliases |
284 | $fieldName = _civicrm_api3_api_resolve_alias($apiRequest['entity'], $apiRequest['params']['field']); | |
285 | if (!$fieldName) { | |
286 | return civicrm_api3_create_error("The field '{$apiRequest['params']['field']}' doesn't exist."); | |
287 | } | |
a4a33486 | 288 | // Validate 'context' from params |
786ad6e1 CW |
289 | $context = CRM_Utils_Array::value('context', $apiRequest['params']); |
290 | CRM_Core_DAO::buildOptionsContext($context); | |
a3d8b390 | 291 | unset($apiRequest['params']['context'], $apiRequest['params']['field']); |
70f7ba9e | 292 | |
786ad6e1 | 293 | $baoName = _civicrm_api3_get_BAO($apiRequest['entity']); |
b7ceb253 | 294 | $options = $baoName::buildOptions($fieldName, $context, $apiRequest['params']); |
ee2b1c1c | 295 | if ($options === FALSE) { |
70f7ba9e | 296 | return civicrm_api3_create_error("The field '{$fieldName}' has no associated option list."); |
6a488035 | 297 | } |
15a1171a CW |
298 | // Support 'sequential' output as a non-associative array |
299 | if (!empty($apiRequest['params']['sequential'])) { | |
b7ceb253 | 300 | $options = CRM_Utils_Array::makeNonAssociative($options); |
15a1171a | 301 | } |
b7ceb253 | 302 | return civicrm_api3_create_success($options, $apiRequest['params'], $apiRequest['entity'], 'getoptions'); |
6a488035 TO |
303 | } |
304 | ||
11e09c59 | 305 | /** |
6a488035 TO |
306 | * Function fills the 'options' array on the metadata returned by getfields if |
307 | * 1) the param option 'get_options' is defined - e.g. $params['options']['get_options'] => array('custom_1) | |
308 | * (this is passed in as the $fieldsToResolve array) | |
309 | * 2) the field is a pseudoconstant and is NOT an FK | |
310 | * - the reason for this is that checking / transformation is done on pseudoconstants but | |
311 | * - if the field is an FK then mysql will enforce the data quality (& we have handling on failure) | |
7c285037 | 312 | * @todo - if may be we should define a 'resolve' key on the pseudoconstant for when these rules are not fine enough |
6a488035 TO |
313 | * |
314 | * This function is only split out for the purpose of code clarity / comment block documentation | |
77b97be7 | 315 | * |
cf470720 TO |
316 | * @param array $metadata |
317 | * The array of metadata that will form the result of the getfields function. | |
77b97be7 | 318 | * @param $apiRequest |
cf470720 TO |
319 | * @param string $fieldname |
320 | * Field currently being processed. | |
321 | * @param array $fieldSpec | |
322 | * Metadata for that field. | |
323 | * @param array $fieldsToResolve | |
324 | * Anny field resolutions specifically requested. | |
6a488035 | 325 | */ |
9b873358 | 326 | function _civicrm_api3_generic_get_metadata_options(&$metadata, $apiRequest, $fieldname, $fieldSpec, $fieldsToResolve) { |
3a8e9315 | 327 | if (empty($fieldSpec['pseudoconstant']) && empty($fieldSpec['option_group_id'])) { |
6a488035 TO |
328 | return; |
329 | } | |
330 | ||
ed8abbbb | 331 | if (!empty($metadata[$fieldname]['options']) || (!in_array($fieldname, $fieldsToResolve) && !in_array('all', $fieldsToResolve))) { |
70f7ba9e CW |
332 | return; |
333 | } | |
334 | ||
ddaac11c | 335 | $options = civicrm_api($apiRequest['entity'], 'getoptions', array('version' => 3, 'field' => $fieldname, 'sequential' => !empty($apiRequest['params']['sequential']))); |
6a488035 TO |
336 | if (is_array(CRM_Utils_Array::value('values', $options))) { |
337 | $metadata[$fieldname]['options'] = $options['values']; | |
338 | } | |
339 | } |