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 | * | |
13 | * @param array $apiRequest api request as an array. Keys are | |
14 | * - entity: string | |
15 | * - action: string | |
16 | * - version: string | |
17 | * - function: callback (mixed) | |
18 | * - params: array, varies | |
19 | * @return array API success object | |
20 | */ | |
21 | function civicrm_api3_generic_getfields($apiRequest) { | |
22 | static $results = array(); | |
23 | if ((CRM_Utils_Array::value('cache_clear', $apiRequest['params']))) { | |
24 | $results = array(); | |
25 | // we will also clear pseudoconstants here - should potentially be moved to relevant BAO classes | |
37547b77 | 26 | CRM_Core_PseudoConstant::flush(); |
6a488035 TO |
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']); | |
f2b53f26 | 59 | return civicrm_api3_create_success($values, $apiRequest['params'], $entity, 'getfields'); |
6a488035 TO |
60 | case 'create': |
61 | case 'update': | |
62 | case 'replace': | |
63 | $unique = FALSE; | |
64 | case 'get': | |
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 | |
68 | if(!empty($metadata[strtolower($apiRequest['entity']) . '_id'])) { | |
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( | |
86 | 'id' => array('title' => 'Unique Identifier', | |
87 | 'api.required' => 1, | |
88 | 'api.aliases' => array($lcase_entity . '_id'), | |
89 | )); | |
90 | break; | |
91 | ||
92 | case 'getoptions': | |
93 | $metadata = array( | |
786ad6e1 CW |
94 | 'field' => array( |
95 | 'title' => 'Field to retrieve options for', | |
96 | 'api.required' => 1, | |
97 | ), | |
98 | 'context' => array( | |
99 | 'title' => 'Context string', | |
100 | ), | |
101 | ); | |
6a488035 TO |
102 | break; |
103 | default: | |
104 | // oddballs are on their own | |
105 | $metadata = array(); | |
106 | } | |
107 | ||
108 | // find any supplemental information | |
109 | $hypApiRequest = array('entity' => $apiRequest['entity'], 'action' => $action, 'version' => $apiRequest['version']); | |
110 | $hypApiRequest += _civicrm_api_resolve($hypApiRequest); | |
111 | $helper = '_' . $hypApiRequest['function'] . '_spec'; | |
112 | if (function_exists($helper)) { | |
113 | // alter | |
114 | $helper($metadata); | |
115 | } | |
116 | ||
a4c5e9a3 | 117 | $fieldsToResolve = (array) CRM_Utils_Array::value('get_options', $apiOptions, array()); |
6a488035 TO |
118 | |
119 | foreach ($metadata as $fieldname => $fieldSpec) { | |
70f7ba9e | 120 | _civicrm_api3_generic_get_metadata_options($metadata, $apiRequest['entity'], $fieldname, $fieldSpec, $fieldsToResolve); |
6a488035 TO |
121 | } |
122 | ||
123 | $results[$entity][$action] = civicrm_api3_create_success($metadata, $apiRequest['params'], NULL, 'getfields'); | |
124 | return $results[$entity][$action]; | |
125 | } | |
126 | ||
127 | /** | |
128 | * API return function to reformat results as count | |
129 | * | |
130 | * @param array $apiRequest api request as an array. Keys are | |
131 | * | |
132 | * @return integer count of results | |
133 | */ | |
134 | function civicrm_api3_generic_getcount($apiRequest) { | |
135 | $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']); | |
136 | return $result['count']; | |
137 | } | |
138 | ||
139 | /** | |
140 | * API return function to reformat results as single result | |
141 | * | |
142 | * @param array $apiRequest api request as an array. Keys are | |
143 | * | |
144 | * @return integer count of results | |
145 | */ | |
146 | function civicrm_api3_generic_getsingle($apiRequest) { | |
147 | // so the first entity is always result['values'][0] | |
148 | $apiRequest['params']['sequential'] = 1; | |
149 | $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']); | |
150 | if ($result['is_error'] !== 0) { | |
151 | return $result; | |
152 | } | |
153 | if ($result['count'] === 1) { | |
154 | return $result['values'][0]; | |
155 | } | |
156 | if ($result['count'] !== 1) { | |
157 | return civicrm_api3_create_error("Expected one " . $apiRequest['entity'] . " but found " . $result['count'], array('count' => $result['count'])); | |
158 | } | |
159 | return civicrm_api3_create_error("Undefined behavior"); | |
160 | } | |
161 | ||
162 | /** | |
163 | * API return function to reformat results as single value | |
164 | * | |
165 | * @param array $apiRequest api request as an array. Keys are | |
166 | * | |
167 | * @return integer count of results | |
168 | */ | |
169 | function civicrm_api3_generic_getvalue($apiRequest) { | |
170 | $apiRequest['params']['sequential'] = 1; | |
171 | $result = civicrm_api($apiRequest['entity'], 'get', $apiRequest['params']); | |
172 | if ($result['is_error'] !== 0) { | |
173 | return $result; | |
174 | } | |
175 | if ($result['count'] !== 1) { | |
176 | $result = civicrm_api3_create_error("Expected one " . $apiRequest['entity'] . " but found " . $result['count'], array('count' => $result['count'])); | |
177 | return $result; | |
178 | } | |
179 | ||
180 | // we only take "return=" as valid options | |
181 | if (CRM_Utils_Array::value('return', $apiRequest['params'])) { | |
182 | if (!isset($result['values'][0][$apiRequest['params']['return']])) { | |
183 | return civicrm_api3_create_error("field " . $apiRequest['params']['return'] . " unset or not existing", array('invalid_field' => $apiRequest['params']['return'])); | |
184 | } | |
185 | ||
186 | return $result['values'][0][$apiRequest['params']['return']]; | |
187 | } | |
188 | ||
189 | return civicrm_api3_create_error("missing param return=field you want to read the value of", array('error_type' => 'mandatory_missing', 'missing_param' => 'return')); | |
190 | } | |
191 | ||
192 | /** | |
193 | * API wrapper for replace function | |
194 | * | |
195 | * @param array $apiRequest api request as an array. Keys are | |
196 | * | |
197 | * @return integer count of results | |
198 | */ | |
199 | function civicrm_api3_generic_replace($apiRequest) { | |
200 | return _civicrm_api3_generic_replace($apiRequest['entity'], $apiRequest['params']); | |
201 | } | |
202 | ||
203 | /** | |
204 | * API wrapper for getoptions function | |
205 | * | |
ee2b1c1c | 206 | * @param array $apiRequest api request as an array. |
6a488035 TO |
207 | * |
208 | * @return array of results | |
209 | */ | |
210 | function civicrm_api3_generic_getoptions($apiRequest) { | |
70f7ba9e CW |
211 | // Resolve aliases |
212 | $fieldName = _civicrm_api3_api_resolve_alias($apiRequest['entity'], $apiRequest['params']['field']); | |
213 | if (!$fieldName) { | |
214 | return civicrm_api3_create_error("The field '{$apiRequest['params']['field']}' doesn't exist."); | |
215 | } | |
a4a33486 | 216 | // Validate 'context' from params |
786ad6e1 CW |
217 | $context = CRM_Utils_Array::value('context', $apiRequest['params']); |
218 | CRM_Core_DAO::buildOptionsContext($context); | |
70f7ba9e | 219 | |
786ad6e1 CW |
220 | $baoName = _civicrm_api3_get_BAO($apiRequest['entity']); |
221 | $options = $baoName::buildOptions($fieldName, $context); | |
ee2b1c1c | 222 | if ($options === FALSE) { |
70f7ba9e | 223 | return civicrm_api3_create_error("The field '{$fieldName}' has no associated option list."); |
6a488035 | 224 | } |
ee2b1c1c | 225 | return civicrm_api3_create_success($options); |
6a488035 TO |
226 | } |
227 | ||
11e09c59 | 228 | /** |
6a488035 TO |
229 | * Function fills the 'options' array on the metadata returned by getfields if |
230 | * 1) the param option 'get_options' is defined - e.g. $params['options']['get_options'] => array('custom_1) | |
231 | * (this is passed in as the $fieldsToResolve array) | |
232 | * 2) the field is a pseudoconstant and is NOT an FK | |
233 | * - the reason for this is that checking / transformation is done on pseudoconstants but | |
234 | * - if the field is an FK then mysql will enforce the data quality (& we have handling on failure) | |
235 | * @todo - if may be we should define a 'resolve' key on the psuedoconstant for when these rules are not fine enough | |
236 | * 3) if there is an 'enum' then it is split up into the relevant options | |
237 | * | |
238 | * This function is only split out for the purpose of code clarity / comment block documentation | |
239 | * @param array $metadata the array of metadata that will form the result of the getfields function | |
240 | * @param string $fieldname field currently being processed | |
241 | * @param array $fieldSpec metadata for that field | |
242 | * @param array $fieldsToResolve anny field resolutions specifically requested | |
243 | */ | |
70f7ba9e | 244 | function _civicrm_api3_generic_get_metadata_options(&$metadata, $entity, $fieldname, $fieldSpec, $fieldsToResolve){ |
aa7e7ff0 | 245 | if(empty($fieldSpec['pseudoconstant']) && empty($fieldSpec['enumValues'])) { |
6a488035 TO |
246 | return; |
247 | } | |
248 | ||
ed8abbbb | 249 | if (!empty($metadata[$fieldname]['options']) || (!in_array($fieldname, $fieldsToResolve) && !in_array('all', $fieldsToResolve))) { |
70f7ba9e CW |
250 | return; |
251 | } | |
252 | ||
253 | $options = civicrm_api($entity, 'getoptions', array('version' => 3, 'field' => $fieldname)); | |
6a488035 TO |
254 | if (is_array(CRM_Utils_Array::value('values', $options))) { |
255 | $metadata[$fieldname]['options'] = $options['values']; | |
256 | } | |
257 | } |