Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | // $Id$ | |
3 | ||
4 | /** | |
5 | * Get information about fields for a given api request. Getfields information | |
6 | * is used for documentation, validation, default setting | |
7 | * We first query the scheme using the $dao->fields function & then augment | |
8 | * that information by calling the _spec functions that apply to the relevant function | |
9 | * Note that we use 'unique' field names as described in the xml/schema files | |
10 | * for get requests & just field name for create. This is because some get functions | |
11 | * access multiple objects e.g. contact api accesses is_deleted from the activity | |
12 | * table & from the contact table | |
13 | * | |
14 | * @param array $apiRequest api request as an array. Keys are | |
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 | |
27 | if(!empty($apiRequest['params']['fieldname'])){ | |
28 | CRM_Utils_PseudoConstant::flushConstant($apiRequest['params']['fieldname']); | |
29 | } | |
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)); | |
34 | } | |
35 | } | |
36 | } | |
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 | $apiOptions = CRM_Utils_Array::value('options', $apiRequest['params'], array()); | |
42 | if ($action == 'getvalue' || $action == 'getvalue' || $action == 'getcount') { | |
43 | $action = 'get'; | |
44 | } | |
45 | ||
46 | if (empty($action)) { | |
47 | $action='get'; | |
48 | } | |
49 | // determines whether to use unique field names - seem comment block above | |
50 | $unique = TRUE; | |
51 | if (isset($results[$entity . $subentity]) && CRM_Utils_Array::value($action, $results[$entity]) | |
52 | && empty($apiOptions)) { | |
53 | return $results[$entity . $subentity][$action]; | |
54 | } | |
55 | // defaults based on data model and API policy | |
56 | switch ($action) { | |
57 | case 'getfields': | |
58 | $values = _civicrm_api_get_fields($entity, false, $apiRequest['params']); | |
59 | $results[$entity][$action] = civicrm_api3_create_success($values, | |
60 | $apiRequest['params'], $entity, 'getfields' | |
61 | ); | |
62 | return $results[$entity][$action]; | |
63 | ||
64 | case 'getfields': | |
65 | return civicrm_api3_create_success(_civicrm_api_get_fields($apiRequest['entity'])); | |
66 | case 'create': | |
67 | case 'update': | |
68 | case 'replace': | |
69 | $unique = FALSE; | |
70 | case 'get': | |
71 | $metadata = _civicrm_api_get_fields($apiRequest['entity'], $unique, $apiRequest['params']); | |
72 | if (empty($metadata['id']) && !empty($metadata[$apiRequest['entity'] . '_id'])) { | |
73 | $metadata['id'] = $metadata[$lcase_entity . '_id']; | |
74 | $metadata['id']['api.aliases'] = array($lcase_entity . '_id'); | |
75 | unset($metadata[$lcase_entity . '_id']); | |
76 | } | |
77 | break; | |
78 | ||
79 | case 'delete': | |
80 | $metadata = array( | |
81 | 'id' => array('title' => 'Unique Identifier', | |
82 | 'api.required' => 1, | |
83 | 'api.aliases' => array($lcase_entity . '_id'), | |
84 | )); | |
85 | break; | |
86 | ||
87 | case 'getoptions': | |
88 | $metadata = array( | |
89 | 'field' => array('title' => 'Field to retrieve options for', | |
90 | 'api.required' => 1, | |
91 | )); | |
92 | break; | |
93 | default: | |
94 | // oddballs are on their own | |
95 | $metadata = array(); | |
96 | } | |
97 | ||
98 | // find any supplemental information | |
99 | $hypApiRequest = array('entity' => $apiRequest['entity'], 'action' => $action, 'version' => $apiRequest['version']); | |
100 | $hypApiRequest += _civicrm_api_resolve($hypApiRequest); | |
101 | $helper = '_' . $hypApiRequest['function'] . '_spec'; | |
102 | if (function_exists($helper)) { | |
103 | // alter | |
104 | $helper($metadata); | |
105 | } | |
106 | ||
107 | $fieldsToResolve = CRM_Utils_Array::value('get_options', $apiOptions, array()); | |
108 | ||
109 | foreach ($metadata as $fieldname => $fieldSpec) { | |
110 | _civicrm_api3_generic_get_metadata_options($metadata, $fieldname, $fieldSpec, $fieldsToResolve); | |
111 | } | |
112 | ||
113 | $results[$entity][$action] = civicrm_api3_create_success($metadata, $apiRequest['params'], NULL, 'getfields'); | |
114 | return $results[$entity][$action]; | |
115 | } | |
116 | ||
117 | /** | |
118 | * API return function to reformat results as count | |
119 | * | |
120 | * @param array $apiRequest api request as an array. Keys are | |
121 | * | |
122 | * @return integer count of results | |
123 | */ | |
124 | function civicrm_api3_generic_getcount($apiRequest) { | |
125 | $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']); | |
126 | return $result['count']; | |
127 | } | |
128 | ||
129 | /** | |
130 | * API return function to reformat results as single result | |
131 | * | |
132 | * @param array $apiRequest api request as an array. Keys are | |
133 | * | |
134 | * @return integer count of results | |
135 | */ | |
136 | function civicrm_api3_generic_getsingle($apiRequest) { | |
137 | // so the first entity is always result['values'][0] | |
138 | $apiRequest['params']['sequential'] = 1; | |
139 | $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']); | |
140 | if ($result['is_error'] !== 0) { | |
141 | return $result; | |
142 | } | |
143 | if ($result['count'] === 1) { | |
144 | return $result['values'][0]; | |
145 | } | |
146 | if ($result['count'] !== 1) { | |
147 | return civicrm_api3_create_error("Expected one " . $apiRequest['entity'] . " but found " . $result['count'], array('count' => $result['count'])); | |
148 | } | |
149 | return civicrm_api3_create_error("Undefined behavior"); | |
150 | } | |
151 | ||
152 | /** | |
153 | * API return function to reformat results as single value | |
154 | * | |
155 | * @param array $apiRequest api request as an array. Keys are | |
156 | * | |
157 | * @return integer count of results | |
158 | */ | |
159 | function civicrm_api3_generic_getvalue($apiRequest) { | |
160 | $apiRequest['params']['sequential'] = 1; | |
161 | $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']); | |
162 | if ($result['is_error'] !== 0) { | |
163 | return $result; | |
164 | } | |
165 | if ($result['count'] !== 1) { | |
166 | $result = civicrm_api3_create_error("Expected one " . $apiRequest['entity'] . " but found " . $result['count'], array('count' => $result['count'])); | |
167 | return $result; | |
168 | } | |
169 | ||
170 | // we only take "return=" as valid options | |
171 | if (CRM_Utils_Array::value('return', $apiRequest['params'])) { | |
172 | if (!isset($result['values'][0][$apiRequest['params']['return']])) { | |
173 | return civicrm_api3_create_error("field " . $apiRequest['params']['return'] . " unset or not existing", array('invalid_field' => $apiRequest['params']['return'])); | |
174 | } | |
175 | ||
176 | return $result['values'][0][$apiRequest['params']['return']]; | |
177 | } | |
178 | ||
179 | return civicrm_api3_create_error("missing param return=field you want to read the value of", array('error_type' => 'mandatory_missing', 'missing_param' => 'return')); | |
180 | } | |
181 | ||
182 | /** | |
183 | * API wrapper for replace function | |
184 | * | |
185 | * @param array $apiRequest api request as an array. Keys are | |
186 | * | |
187 | * @return integer count of results | |
188 | */ | |
189 | function civicrm_api3_generic_replace($apiRequest) { | |
190 | return _civicrm_api3_generic_replace($apiRequest['entity'], $apiRequest['params']); | |
191 | } | |
192 | ||
193 | /** | |
194 | * API wrapper for getoptions function | |
195 | * | |
196 | * @param array $apiRequest api request as an array. Keys are | |
197 | * | |
198 | * @return array of results | |
199 | */ | |
200 | function civicrm_api3_generic_getoptions($apiRequest) { | |
201 | $field = $apiRequest['params']['field']; | |
202 | $getFieldsArray = array( | |
203 | 'version' => 3, | |
204 | 'action' => 'create', | |
205 | 'options' => array('get_options' => $field), | |
206 | ); | |
207 | // First try to retrieve the options from getfields | |
208 | $result = civicrm_api($apiRequest['entity'], 'getfields', $getFieldsArray); | |
209 | if (!isset($result['values'][$field]) && isset($result['values'][$field . '_id'])) { | |
210 | $field = $field . '_id'; | |
211 | } | |
212 | if (!empty($result['values'][$field]['options'])) { | |
213 | return civicrm_api3_create_success($result['values'][$field]['options']); | |
214 | } | |
215 | // If that didn't work, try the constant api | |
216 | if (substr($field, -3) == '_id') { | |
217 | // Convert foo_id to just plain foo | |
218 | $field = substr($field, 0, -3); | |
219 | } | |
220 | $params = array('name' => _civicrm_api_get_camel_name($field)); | |
221 | $entity = strtolower($apiRequest['entity']); | |
222 | if ($entity == 'contribution') { | |
223 | $params['class'] = 'CRM_Contribute_PseudoConstant'; | |
224 | } | |
225 | elseif ($entity == 'event' || $entity == 'participant') { | |
226 | $params['class'] = 'CRM_Event_PseudoConstant'; | |
227 | } | |
228 | elseif (strpos($entity, 'membership') === 0) { | |
229 | $params['class'] = 'CRM_Member_PseudoConstant'; | |
230 | } | |
231 | require_once 'api/v3/Constant.php'; | |
232 | return civicrm_api3_constant_get($params); | |
233 | } | |
234 | ||
11e09c59 | 235 | /** |
6a488035 TO |
236 | * Function fills the 'options' array on the metadata returned by getfields if |
237 | * 1) the param option 'get_options' is defined - e.g. $params['options']['get_options'] => array('custom_1) | |
238 | * (this is passed in as the $fieldsToResolve array) | |
239 | * 2) the field is a pseudoconstant and is NOT an FK | |
240 | * - the reason for this is that checking / transformation is done on pseudoconstants but | |
241 | * - if the field is an FK then mysql will enforce the data quality (& we have handling on failure) | |
242 | * @todo - if may be we should define a 'resolve' key on the psuedoconstant for when these rules are not fine enough | |
243 | * 3) if there is an 'enum' then it is split up into the relevant options | |
244 | * | |
245 | * This function is only split out for the purpose of code clarity / comment block documentation | |
246 | * @param array $metadata the array of metadata that will form the result of the getfields function | |
247 | * @param string $fieldname field currently being processed | |
248 | * @param array $fieldSpec metadata for that field | |
249 | * @param array $fieldsToResolve anny field resolutions specifically requested | |
250 | */ | |
251 | function _civicrm_api3_generic_get_metadata_options(&$metadata, $fieldname, $fieldSpec, $fieldsToResolve){ | |
252 | if (array_key_exists('enumValues', $fieldSpec)) { | |
253 | // use of a space after the comma is inconsistent in xml | |
254 | $enumStr = str_replace(', ', ',', $fieldSpec['enumValues']); | |
255 | $metadata[$fieldname]['options'] = explode(',', $enumStr); | |
256 | return; | |
257 | } | |
258 | ||
259 | if(empty($fieldSpec['pseudoconstant'])){ | |
260 | return ; | |
261 | } | |
262 | elseif(!empty($fieldSpec['FKClassName']) && !in_array($fieldname, $fieldsToResolve)){ | |
263 | return; | |
264 | } | |
265 | if(substr($fieldname, -3) == '_id'){ | |
266 | $metadata[$fieldname]['api.aliases'][] = substr($fieldname, 0, -3); | |
267 | } | |
268 | ||
269 | $pseudoParams = $fieldSpec['pseudoconstant']; | |
270 | $pseudoParams['version'] = 3; | |
271 | $options = civicrm_api('constant', 'get', $pseudoParams); | |
272 | if (is_array(CRM_Utils_Array::value('values', $options))) { | |
273 | $metadata[$fieldname]['options'] = $options['values']; | |
274 | } | |
275 | } |