Commit | Line | Data |
---|---|---|
fe43b7c9 | 1 | <?php |
fe43b7c9 CW |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
a30c801b | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
b081365f | 5 | | | |
a30c801b TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
b081365f | 9 | +--------------------------------------------------------------------+ |
e70a7fc0 | 10 | */ |
b081365f CW |
11 | |
12 | /** | |
13 | * @package CiviCRM_APIv3 | |
14 | */ | |
15 | ||
fe43b7c9 | 16 | /** |
2fb1dd66 | 17 | * Generic api wrapper used for quicksearch and autocomplete. |
fe43b7c9 | 18 | * |
72b3a70c | 19 | * @param array $apiRequest |
2fb1dd66 | 20 | * |
fe43b7c9 | 21 | * @return mixed |
30420aa3 | 22 | * @throws \CiviCRM_API3_Exception |
fe43b7c9 CW |
23 | */ |
24 | function civicrm_api3_generic_getList($apiRequest) { | |
25 | $entity = _civicrm_api_get_entity_name_from_camel($apiRequest['entity']); | |
26 | $request = $apiRequest['params']; | |
cf8f0fff | 27 | $meta = civicrm_api3_generic_getfields(['action' => 'get'] + $apiRequest, FALSE); |
ebf2b57b | 28 | |
bc255d1d CW |
29 | // Hey api, would you like to provide default values? |
30 | $fnName = "_civicrm_api3_{$entity}_getlist_defaults"; | |
cf8f0fff | 31 | $defaults = function_exists($fnName) ? $fnName($request) : []; |
6b4c9564 | 32 | _civicrm_api3_generic_getList_defaults($entity, $request, $defaults, $meta['values']); |
ebf2b57b | 33 | |
fe43b7c9 CW |
34 | // Hey api, would you like to format the search params? |
35 | $fnName = "_civicrm_api3_{$entity}_getlist_params"; | |
36 | $fnName = function_exists($fnName) ? $fnName : '_civicrm_api3_generic_getlist_params'; | |
37 | $fnName($request); | |
79ae07d9 CW |
38 | |
39 | $request['params']['check_permissions'] = !empty($apiRequest['params']['check_permissions']); | |
fe43b7c9 | 40 | $result = civicrm_api3($entity, 'get', $request['params']); |
30420aa3 | 41 | if (!empty($request['input']) && !empty($defaults['search_field_fallback']) && $result['count'] < $request['params']['options']['limit']) { |
42 | // We support a field fallback. Note we don't do this as an OR query because that could easily | |
43 | // bypass an index & kill the server. We just 'pad' the results if needed with the second | |
44 | // query - this is effectively the same as what the old Ajax::getContactEmail function did. | |
45 | // Since these queries should be quick & often only one should be needed this is a simpler alternative | |
46 | // to constructing a UNION via the api. | |
47 | $request['params'][$defaults['search_field_fallback']] = $request['params'][$defaults['search_field']]; | |
48 | if ($request['params']['options']['sort'] === $defaults['search_field']) { | |
49 | // The way indexing works here is that the order by field will be chosen in preference to the | |
50 | // filter field. This can result in really bad performance so use the filter field for the sort. | |
51 | // See https://github.com/civicrm/civicrm-core/pull/16993 for performance test results. | |
52 | $request['params']['options']['sort'] = $defaults['search_field_fallback']; | |
53 | } | |
54 | // Exclude anything returned from the previous query since we are looking for additional rows in this | |
55 | // second query. | |
56 | $request['params'][$defaults['search_field']] = ['NOT LIKE' => $request['params'][$defaults['search_field_fallback']]['LIKE']]; | |
57 | $request['params']['options']['limit'] -= $result['count']; | |
58 | $result2 = civicrm_api3($entity, 'get', $request['params']); | |
59 | $result['values'] = array_merge($result['values'], $result2['values']); | |
60 | $result['count'] = count($result['values']); | |
61 | } | |
62 | else { | |
63 | // Re-index to sequential = 0. | |
64 | $result['values'] = array_merge($result['values']); | |
65 | } | |
fe43b7c9 CW |
66 | |
67 | // Hey api, would you like to format the output? | |
68 | $fnName = "_civicrm_api3_{$entity}_getlist_output"; | |
69 | $fnName = function_exists($fnName) ? $fnName : '_civicrm_api3_generic_getlist_output'; | |
6b4c9564 | 70 | $values = $fnName($result, $request, $entity, $meta['values']); |
fe43b7c9 | 71 | |
27d7d241 CW |
72 | _civicrm_api3_generic_getlist_postprocess($result, $request, $values); |
73 | ||
cf8f0fff | 74 | $output = ['page_num' => $request['page_num']]; |
78b203e5 CW |
75 | |
76 | // Limit is set for searching but not fetching by id | |
a6c6059d | 77 | if (!empty($request['params']['options']['limit'])) { |
fe43b7c9 | 78 | // If we have an extra result then this is not the last page |
a6c6059d | 79 | $last = $request['params']['options']['limit'] - 1; |
78b203e5 CW |
80 | $output['more_results'] = isset($values[$last]); |
81 | unset($values[$last]); | |
82 | } | |
fe43b7c9 CW |
83 | |
84 | return civicrm_api3_create_success($values, $request['params'], $entity, 'getlist', CRM_Core_DAO::$_nullObject, $output); | |
85 | } | |
86 | ||
87 | /** | |
211e2fca | 88 | * Set defaults for api.getlist. |
fe43b7c9 | 89 | * |
8c6b335b CW |
90 | * @param string $entity |
91 | * @param array $request | |
bc255d1d | 92 | * @param array $apiDefaults |
3bdf1f3a | 93 | * @param array $fields |
fe43b7c9 | 94 | */ |
6b4c9564 | 95 | function _civicrm_api3_generic_getList_defaults($entity, &$request, $apiDefaults, $fields) { |
cf8f0fff | 96 | $defaults = [ |
fe43b7c9 CW |
97 | 'page_num' => 1, |
98 | 'input' => '', | |
99 | 'image_field' => NULL, | |
4fcc9877 | 100 | 'color_field' => isset($fields['color']) ? 'color' : NULL, |
0724132d | 101 | 'id_field' => $entity == 'option_value' ? 'value' : 'id', |
cf8f0fff | 102 | 'description_field' => [], |
81b7bb6f | 103 | 'add_wildcard' => Civi::settings()->get('includeWildCardInName'), |
cf8f0fff CW |
104 | 'params' => [], |
105 | 'extra' => [], | |
106 | ]; | |
fe43b7c9 | 107 | // Find main field from meta |
cf8f0fff | 108 | foreach (['sort_name', 'title', 'label', 'name', 'subject'] as $field) { |
fe43b7c9 CW |
109 | if (isset($fields[$field])) { |
110 | $defaults['label_field'] = $defaults['search_field'] = $field; | |
111 | break; | |
112 | } | |
113 | } | |
8250601e | 114 | // Find fields to be used for the description |
cf8f0fff | 115 | foreach (['description'] as $field) { |
fe43b7c9 | 116 | if (isset($fields[$field])) { |
8250601e | 117 | $defaults['description_field'][] = $field; |
fe43b7c9 CW |
118 | } |
119 | } | |
89595c92 | 120 | $resultsPerPage = Civi::settings()->get('search_autocomplete_count'); |
6b4c9564 CW |
121 | if (isset($request['params']) && isset($apiDefaults['params'])) { |
122 | $request['params'] += $apiDefaults['params']; | |
123 | } | |
bc255d1d | 124 | $request += $apiDefaults + $defaults; |
fe43b7c9 | 125 | // Default api params |
cf8f0fff | 126 | $params = [ |
30420aa3 | 127 | 'sequential' => 0, |
cf8f0fff CW |
128 | 'options' => [], |
129 | ]; | |
76ec9ca7 | 130 | // When searching e.g. autocomplete |
fe43b7c9 | 131 | if ($request['input']) { |
cf8f0fff | 132 | $params[$request['search_field']] = ['LIKE' => ($request['add_wildcard'] ? '%' : '') . $request['input'] . '%']; |
fe43b7c9 | 133 | } |
76ec9ca7 CW |
134 | // When looking up a field e.g. displaying existing record |
135 | if (!empty($request['id'])) { | |
83e11123 CW |
136 | if (is_string($request['id']) && strpos($request['id'], ',')) { |
137 | $request['id'] = explode(',', trim($request['id'], ', ')); | |
76ec9ca7 | 138 | } |
78b203e5 | 139 | // Don't run into search limits when prefilling selection |
ba3b21c3 CW |
140 | $params['options']['limit'] = NULL; |
141 | unset($params['options']['offset'], $request['params']['options']['limit'], $request['params']['options']['offset']); | |
cf8f0fff | 142 | $params[$request['id_field']] = is_array($request['id']) ? ['IN' => $request['id']] : $request['id']; |
76ec9ca7 | 143 | } |
fe43b7c9 | 144 | $request['params'] += $params; |
deb188d8 | 145 | |
cf8f0fff | 146 | $request['params']['options'] += [ |
deb188d8 FW |
147 | // Add pagination parameters |
148 | 'sort' => $request['label_field'], | |
149 | // Adding one extra result allows us to see if there are any more | |
150 | 'limit' => $resultsPerPage + 1, | |
151 | // Because sql is zero-based | |
152 | 'offset' => ($request['page_num'] - 1) * $resultsPerPage, | |
cf8f0fff | 153 | ]; |
fe43b7c9 CW |
154 | } |
155 | ||
156 | /** | |
c1a920f1 | 157 | * Fallback implementation of getlist_params. May be overridden by individual apis. |
fe43b7c9 | 158 | * |
8c6b335b | 159 | * @param array $request |
fe43b7c9 CW |
160 | */ |
161 | function _civicrm_api3_generic_getlist_params(&$request) { | |
cf8f0fff | 162 | $fieldsToReturn = [$request['id_field'], $request['label_field']]; |
ff88d165 CW |
163 | if (!empty($request['image_field'])) { |
164 | $fieldsToReturn[] = $request['image_field']; | |
165 | } | |
4fcc9877 CW |
166 | if (!empty($request['color_field'])) { |
167 | $fieldsToReturn[] = $request['color_field']; | |
168 | } | |
ff88d165 | 169 | if (!empty($request['description_field'])) { |
8250601e | 170 | $fieldsToReturn = array_merge($fieldsToReturn, (array) $request['description_field']); |
ff88d165 | 171 | } |
8250601e | 172 | $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra'])); |
fe43b7c9 CW |
173 | } |
174 | ||
175 | /** | |
211e2fca | 176 | * Fallback implementation of getlist_output. May be overridden by individual api functions. |
fe43b7c9 | 177 | * |
8c6b335b CW |
178 | * @param array $result |
179 | * @param array $request | |
b233e1b4 | 180 | * @param string $entity |
6b4c9564 | 181 | * @param array $fields |
fe43b7c9 CW |
182 | * |
183 | * @return array | |
184 | */ | |
6b4c9564 | 185 | function _civicrm_api3_generic_getlist_output($result, $request, $entity, $fields) { |
cf8f0fff | 186 | $output = []; |
fe43b7c9 CW |
187 | if (!empty($result['values'])) { |
188 | foreach ($result['values'] as $row) { | |
cf8f0fff | 189 | $data = [ |
76ec9ca7 | 190 | 'id' => $row[$request['id_field']], |
fe43b7c9 | 191 | 'label' => $row[$request['label_field']], |
cf8f0fff | 192 | ]; |
ff88d165 | 193 | if (!empty($request['description_field'])) { |
cf8f0fff | 194 | $data['description'] = []; |
8250601e CW |
195 | foreach ((array) $request['description_field'] as $field) { |
196 | if (!empty($row[$field])) { | |
6b4c9564 CW |
197 | if (!isset($fields[$field]['pseudoconstant'])) { |
198 | $data['description'][] = $row[$field]; | |
199 | } | |
200 | else { | |
201 | $data['description'][] = CRM_Core_PseudoConstant::getLabel( | |
202 | _civicrm_api3_get_BAO($entity), | |
203 | $field, | |
204 | $row[$field] | |
205 | ); | |
206 | } | |
8250601e CW |
207 | } |
208 | } | |
ff88d165 CW |
209 | }; |
210 | if (!empty($request['image_field'])) { | |
2e1f50d6 | 211 | $data['image'] = $row[$request['image_field']] ?? ''; |
8250601e | 212 | } |
4fcc9877 CW |
213 | if (isset($row[$request['color_field']])) { |
214 | $data['color'] = $row[$request['color_field']]; | |
215 | } | |
ff88d165 | 216 | $output[] = $data; |
fe43b7c9 CW |
217 | } |
218 | } | |
219 | return $output; | |
220 | } | |
f7b2fef0 | 221 | |
27d7d241 CW |
222 | /** |
223 | * Common postprocess for getlist output | |
224 | * | |
225 | * @param $result | |
226 | * @param $request | |
227 | * @param $values | |
228 | */ | |
229 | function _civicrm_api3_generic_getlist_postprocess($result, $request, &$values) { | |
cf8f0fff | 230 | $chains = []; |
27d7d241 CW |
231 | foreach ($request['params'] as $field => $param) { |
232 | if (substr($field, 0, 4) === 'api.') { | |
233 | $chains[] = $field; | |
234 | } | |
235 | } | |
236 | if (!empty($result['values'])) { | |
237 | foreach (array_values($result['values']) as $num => $row) { | |
238 | foreach ($request['extra'] as $field) { | |
2e1f50d6 | 239 | $values[$num]['extra'][$field] = $row[$field] ?? NULL; |
27d7d241 CW |
240 | } |
241 | foreach ($chains as $chain) { | |
2e1f50d6 | 242 | $values[$num][$chain] = $row[$chain] ?? NULL; |
27d7d241 CW |
243 | } |
244 | } | |
245 | } | |
246 | } | |
247 | ||
f7b2fef0 CW |
248 | /** |
249 | * Provide metadata for this api | |
250 | * | |
251 | * @param array $params | |
252 | * @param array $apiRequest | |
253 | */ | |
254 | function _civicrm_api3_generic_getlist_spec(&$params, $apiRequest) { | |
cf8f0fff CW |
255 | $params += [ |
256 | 'page_num' => [ | |
f7b2fef0 CW |
257 | 'title' => 'Page Number', |
258 | 'description' => "Current page of a multi-page lookup", | |
259 | 'type' => CRM_Utils_Type::T_INT, | |
cf8f0fff CW |
260 | ], |
261 | 'input' => [ | |
f7b2fef0 CW |
262 | 'title' => 'Search Input', |
263 | 'description' => "String to search on", | |
264 | 'type' => CRM_Utils_Type::T_TEXT, | |
cf8f0fff CW |
265 | ], |
266 | 'params' => [ | |
f7b2fef0 CW |
267 | 'title' => 'API Params', |
268 | 'description' => "Additional filters to send to the {$apiRequest['entity']} API.", | |
cf8f0fff CW |
269 | ], |
270 | 'extra' => [ | |
f7b2fef0 CW |
271 | 'title' => 'Extra', |
272 | 'description' => 'Array of additional fields to return.', | |
cf8f0fff CW |
273 | ], |
274 | 'image_field' => [ | |
f7b2fef0 CW |
275 | 'title' => 'Image Field', |
276 | 'description' => "Field that this entity uses to store icons (usually automatic)", | |
277 | 'type' => CRM_Utils_Type::T_TEXT, | |
cf8f0fff CW |
278 | ], |
279 | 'id_field' => [ | |
f7b2fef0 CW |
280 | 'title' => 'ID Field', |
281 | 'description' => "Field that uniquely identifies this entity (usually automatic)", | |
282 | 'type' => CRM_Utils_Type::T_TEXT, | |
cf8f0fff CW |
283 | ], |
284 | 'description_field' => [ | |
f7b2fef0 CW |
285 | 'title' => 'Description Field', |
286 | 'description' => "Field that this entity uses to store summary text (usually automatic)", | |
287 | 'type' => CRM_Utils_Type::T_TEXT, | |
cf8f0fff CW |
288 | ], |
289 | 'label_field' => [ | |
fee3e3c3 | 290 | 'title' => 'Label Field', |
f7b2fef0 CW |
291 | 'description' => "Field to display as title of results (usually automatic)", |
292 | 'type' => CRM_Utils_Type::T_TEXT, | |
cf8f0fff CW |
293 | ], |
294 | 'search_field' => [ | |
f7b2fef0 CW |
295 | 'title' => 'Search Field', |
296 | 'description' => "Field to search on (assumed to be the same as label field unless otherwise specified)", | |
297 | 'type' => CRM_Utils_Type::T_TEXT, | |
cf8f0fff CW |
298 | ], |
299 | ]; | |
f7b2fef0 | 300 | } |