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