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