Commit | Line | Data |
---|---|---|
fe43b7c9 | 1 | <?php |
fe43b7c9 CW |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
b081365f | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
b081365f CW |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 26 | */ |
b081365f CW |
27 | |
28 | /** | |
29 | * @package CiviCRM_APIv3 | |
30 | */ | |
31 | ||
fe43b7c9 | 32 | /** |
2fb1dd66 | 33 | * Generic api wrapper used for quicksearch and autocomplete. |
fe43b7c9 | 34 | * |
72b3a70c | 35 | * @param array $apiRequest |
2fb1dd66 | 36 | * |
fe43b7c9 CW |
37 | * @return mixed |
38 | */ | |
39 | function civicrm_api3_generic_getList($apiRequest) { | |
40 | $entity = _civicrm_api_get_entity_name_from_camel($apiRequest['entity']); | |
41 | $request = $apiRequest['params']; | |
6b4c9564 | 42 | $meta = civicrm_api3_generic_getfields(array('action' => 'get') + $apiRequest, FALSE); |
ebf2b57b | 43 | |
bc255d1d CW |
44 | // Hey api, would you like to provide default values? |
45 | $fnName = "_civicrm_api3_{$entity}_getlist_defaults"; | |
46 | $defaults = function_exists($fnName) ? $fnName($request) : array(); | |
6b4c9564 | 47 | _civicrm_api3_generic_getList_defaults($entity, $request, $defaults, $meta['values']); |
ebf2b57b | 48 | |
fe43b7c9 CW |
49 | // Hey api, would you like to format the search params? |
50 | $fnName = "_civicrm_api3_{$entity}_getlist_params"; | |
51 | $fnName = function_exists($fnName) ? $fnName : '_civicrm_api3_generic_getlist_params'; | |
52 | $fnName($request); | |
79ae07d9 CW |
53 | |
54 | $request['params']['check_permissions'] = !empty($apiRequest['params']['check_permissions']); | |
fe43b7c9 CW |
55 | $result = civicrm_api3($entity, 'get', $request['params']); |
56 | ||
57 | // Hey api, would you like to format the output? | |
58 | $fnName = "_civicrm_api3_{$entity}_getlist_output"; | |
59 | $fnName = function_exists($fnName) ? $fnName : '_civicrm_api3_generic_getlist_output'; | |
6b4c9564 | 60 | $values = $fnName($result, $request, $entity, $meta['values']); |
fe43b7c9 | 61 | |
27d7d241 CW |
62 | _civicrm_api3_generic_getlist_postprocess($result, $request, $values); |
63 | ||
78b203e5 CW |
64 | $output = array('page_num' => $request['page_num']); |
65 | ||
66 | // Limit is set for searching but not fetching by id | |
a6c6059d | 67 | if (!empty($request['params']['options']['limit'])) { |
fe43b7c9 | 68 | // If we have an extra result then this is not the last page |
a6c6059d | 69 | $last = $request['params']['options']['limit'] - 1; |
78b203e5 CW |
70 | $output['more_results'] = isset($values[$last]); |
71 | unset($values[$last]); | |
72 | } | |
fe43b7c9 CW |
73 | |
74 | return civicrm_api3_create_success($values, $request['params'], $entity, 'getlist', CRM_Core_DAO::$_nullObject, $output); | |
75 | } | |
76 | ||
77 | /** | |
211e2fca | 78 | * Set defaults for api.getlist. |
fe43b7c9 | 79 | * |
8c6b335b CW |
80 | * @param string $entity |
81 | * @param array $request | |
bc255d1d | 82 | * @param array $apiDefaults |
3bdf1f3a | 83 | * @param array $fields |
fe43b7c9 | 84 | */ |
6b4c9564 | 85 | function _civicrm_api3_generic_getList_defaults($entity, &$request, $apiDefaults, $fields) { |
fe43b7c9 | 86 | $config = CRM_Core_Config::singleton(); |
fe43b7c9 CW |
87 | $defaults = array( |
88 | 'page_num' => 1, | |
89 | 'input' => '', | |
90 | 'image_field' => NULL, | |
4fcc9877 | 91 | 'color_field' => isset($fields['color']) ? 'color' : NULL, |
0724132d | 92 | 'id_field' => $entity == 'option_value' ? 'value' : 'id', |
8250601e | 93 | 'description_field' => array(), |
fe43b7c9 | 94 | 'params' => array(), |
8250601e | 95 | 'extra' => array(), |
fe43b7c9 CW |
96 | ); |
97 | // Find main field from meta | |
dabf9814 | 98 | foreach (array('sort_name', 'title', 'label', 'name', 'subject') as $field) { |
fe43b7c9 CW |
99 | if (isset($fields[$field])) { |
100 | $defaults['label_field'] = $defaults['search_field'] = $field; | |
101 | break; | |
102 | } | |
103 | } | |
8250601e | 104 | // Find fields to be used for the description |
fe43b7c9 CW |
105 | foreach (array('description') as $field) { |
106 | if (isset($fields[$field])) { | |
8250601e | 107 | $defaults['description_field'][] = $field; |
fe43b7c9 CW |
108 | } |
109 | } | |
89595c92 | 110 | $resultsPerPage = Civi::settings()->get('search_autocomplete_count'); |
6b4c9564 CW |
111 | if (isset($request['params']) && isset($apiDefaults['params'])) { |
112 | $request['params'] += $apiDefaults['params']; | |
113 | } | |
bc255d1d | 114 | $request += $apiDefaults + $defaults; |
fe43b7c9 CW |
115 | // Default api params |
116 | $params = array( | |
117 | 'options' => array( | |
118 | // Adding one extra result allows us to see if there are any more | |
119 | 'limit' => $resultsPerPage + 1, | |
120 | // Because sql is zero-based | |
121 | 'offset' => ($request['page_num'] - 1) * $resultsPerPage, | |
122 | 'sort' => $request['label_field'], | |
123 | ), | |
124 | 'sequential' => 1, | |
125 | ); | |
76ec9ca7 | 126 | // When searching e.g. autocomplete |
fe43b7c9 CW |
127 | if ($request['input']) { |
128 | $params[$request['search_field']] = array('LIKE' => ($config->includeWildCardInName ? '%' : '') . $request['input'] . '%'); | |
129 | } | |
76ec9ca7 CW |
130 | // When looking up a field e.g. displaying existing record |
131 | if (!empty($request['id'])) { | |
83e11123 CW |
132 | if (is_string($request['id']) && strpos($request['id'], ',')) { |
133 | $request['id'] = explode(',', trim($request['id'], ', ')); | |
76ec9ca7 | 134 | } |
78b203e5 | 135 | // Don't run into search limits when prefilling selection |
ba3b21c3 CW |
136 | $params['options']['limit'] = NULL; |
137 | unset($params['options']['offset'], $request['params']['options']['limit'], $request['params']['options']['offset']); | |
76ec9ca7 CW |
138 | $params[$request['id_field']] = is_array($request['id']) ? array('IN' => $request['id']) : $request['id']; |
139 | } | |
fe43b7c9 CW |
140 | $request['params'] += $params; |
141 | } | |
142 | ||
143 | /** | |
c1a920f1 | 144 | * Fallback implementation of getlist_params. May be overridden by individual apis. |
fe43b7c9 | 145 | * |
8c6b335b | 146 | * @param array $request |
fe43b7c9 CW |
147 | */ |
148 | function _civicrm_api3_generic_getlist_params(&$request) { | |
76ec9ca7 | 149 | $fieldsToReturn = array($request['id_field'], $request['label_field']); |
ff88d165 CW |
150 | if (!empty($request['image_field'])) { |
151 | $fieldsToReturn[] = $request['image_field']; | |
152 | } | |
4fcc9877 CW |
153 | if (!empty($request['color_field'])) { |
154 | $fieldsToReturn[] = $request['color_field']; | |
155 | } | |
ff88d165 | 156 | if (!empty($request['description_field'])) { |
8250601e | 157 | $fieldsToReturn = array_merge($fieldsToReturn, (array) $request['description_field']); |
ff88d165 | 158 | } |
8250601e | 159 | $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra'])); |
fe43b7c9 CW |
160 | } |
161 | ||
162 | /** | |
211e2fca | 163 | * Fallback implementation of getlist_output. May be overridden by individual api functions. |
fe43b7c9 | 164 | * |
8c6b335b CW |
165 | * @param array $result |
166 | * @param array $request | |
b233e1b4 | 167 | * @param string $entity |
6b4c9564 | 168 | * @param array $fields |
fe43b7c9 CW |
169 | * |
170 | * @return array | |
171 | */ | |
6b4c9564 | 172 | function _civicrm_api3_generic_getlist_output($result, $request, $entity, $fields) { |
fe43b7c9 CW |
173 | $output = array(); |
174 | if (!empty($result['values'])) { | |
175 | foreach ($result['values'] as $row) { | |
ff88d165 | 176 | $data = array( |
76ec9ca7 | 177 | 'id' => $row[$request['id_field']], |
fe43b7c9 | 178 | 'label' => $row[$request['label_field']], |
fe43b7c9 | 179 | ); |
ff88d165 | 180 | if (!empty($request['description_field'])) { |
8250601e CW |
181 | $data['description'] = array(); |
182 | foreach ((array) $request['description_field'] as $field) { | |
183 | if (!empty($row[$field])) { | |
6b4c9564 CW |
184 | if (!isset($fields[$field]['pseudoconstant'])) { |
185 | $data['description'][] = $row[$field]; | |
186 | } | |
187 | else { | |
188 | $data['description'][] = CRM_Core_PseudoConstant::getLabel( | |
189 | _civicrm_api3_get_BAO($entity), | |
190 | $field, | |
191 | $row[$field] | |
192 | ); | |
193 | } | |
8250601e CW |
194 | } |
195 | } | |
ff88d165 CW |
196 | }; |
197 | if (!empty($request['image_field'])) { | |
198 | $data['image'] = isset($row[$request['image_field']]) ? $row[$request['image_field']] : ''; | |
8250601e | 199 | } |
4fcc9877 CW |
200 | if (isset($row[$request['color_field']])) { |
201 | $data['color'] = $row[$request['color_field']]; | |
202 | } | |
ff88d165 | 203 | $output[] = $data; |
fe43b7c9 CW |
204 | } |
205 | } | |
206 | return $output; | |
207 | } | |
f7b2fef0 | 208 | |
27d7d241 CW |
209 | /** |
210 | * Common postprocess for getlist output | |
211 | * | |
212 | * @param $result | |
213 | * @param $request | |
214 | * @param $values | |
215 | */ | |
216 | function _civicrm_api3_generic_getlist_postprocess($result, $request, &$values) { | |
217 | $chains = array(); | |
218 | foreach ($request['params'] as $field => $param) { | |
219 | if (substr($field, 0, 4) === 'api.') { | |
220 | $chains[] = $field; | |
221 | } | |
222 | } | |
223 | if (!empty($result['values'])) { | |
224 | foreach (array_values($result['values']) as $num => $row) { | |
225 | foreach ($request['extra'] as $field) { | |
226 | $values[$num]['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL; | |
227 | } | |
228 | foreach ($chains as $chain) { | |
229 | $values[$num][$chain] = isset($row[$chain]) ? $row[$chain] : NULL; | |
230 | } | |
231 | } | |
232 | } | |
233 | } | |
234 | ||
f7b2fef0 CW |
235 | /** |
236 | * Provide metadata for this api | |
237 | * | |
238 | * @param array $params | |
239 | * @param array $apiRequest | |
240 | */ | |
241 | function _civicrm_api3_generic_getlist_spec(&$params, $apiRequest) { | |
242 | $params += array( | |
243 | 'page_num' => array( | |
244 | 'title' => 'Page Number', | |
245 | 'description' => "Current page of a multi-page lookup", | |
246 | 'type' => CRM_Utils_Type::T_INT, | |
247 | ), | |
248 | 'input' => array( | |
249 | 'title' => 'Search Input', | |
250 | 'description' => "String to search on", | |
251 | 'type' => CRM_Utils_Type::T_TEXT, | |
252 | ), | |
253 | 'params' => array( | |
254 | 'title' => 'API Params', | |
255 | 'description' => "Additional filters to send to the {$apiRequest['entity']} API.", | |
256 | ), | |
257 | 'extra' => array( | |
258 | 'title' => 'Extra', | |
259 | 'description' => 'Array of additional fields to return.', | |
260 | ), | |
261 | 'image_field' => array( | |
262 | 'title' => 'Image Field', | |
263 | 'description' => "Field that this entity uses to store icons (usually automatic)", | |
264 | 'type' => CRM_Utils_Type::T_TEXT, | |
265 | ), | |
266 | 'id_field' => array( | |
267 | 'title' => 'ID Field', | |
268 | 'description' => "Field that uniquely identifies this entity (usually automatic)", | |
269 | 'type' => CRM_Utils_Type::T_TEXT, | |
270 | ), | |
271 | 'description_field' => array( | |
272 | 'title' => 'Description Field', | |
273 | 'description' => "Field that this entity uses to store summary text (usually automatic)", | |
274 | 'type' => CRM_Utils_Type::T_TEXT, | |
275 | ), | |
276 | 'label_field' => array( | |
fee3e3c3 | 277 | 'title' => 'Label Field', |
f7b2fef0 CW |
278 | 'description' => "Field to display as title of results (usually automatic)", |
279 | 'type' => CRM_Utils_Type::T_TEXT, | |
280 | ), | |
281 | 'search_field' => array( | |
282 | 'title' => 'Search Field', | |
283 | 'description' => "Field to search on (assumed to be the same as label field unless otherwise specified)", | |
284 | 'type' => CRM_Utils_Type::T_TEXT, | |
285 | ), | |
286 | ); | |
287 | } |