Commit | Line | Data |
---|---|---|
fe43b7c9 | 1 | <?php |
fe43b7c9 CW |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
b081365f | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 |
fe43b7c9 | 83 | */ |
6b4c9564 | 84 | function _civicrm_api3_generic_getList_defaults($entity, &$request, $apiDefaults, $fields) { |
fe43b7c9 | 85 | $config = CRM_Core_Config::singleton(); |
fe43b7c9 CW |
86 | $defaults = array( |
87 | 'page_num' => 1, | |
88 | 'input' => '', | |
89 | 'image_field' => NULL, | |
0724132d | 90 | 'id_field' => $entity == 'option_value' ? 'value' : 'id', |
8250601e | 91 | 'description_field' => array(), |
fe43b7c9 | 92 | 'params' => array(), |
8250601e | 93 | 'extra' => array(), |
fe43b7c9 CW |
94 | ); |
95 | // Find main field from meta | |
dabf9814 | 96 | foreach (array('sort_name', 'title', 'label', 'name', 'subject') as $field) { |
fe43b7c9 CW |
97 | if (isset($fields[$field])) { |
98 | $defaults['label_field'] = $defaults['search_field'] = $field; | |
99 | break; | |
100 | } | |
101 | } | |
8250601e | 102 | // Find fields to be used for the description |
fe43b7c9 CW |
103 | foreach (array('description') as $field) { |
104 | if (isset($fields[$field])) { | |
8250601e | 105 | $defaults['description_field'][] = $field; |
fe43b7c9 CW |
106 | } |
107 | } | |
89595c92 | 108 | $resultsPerPage = Civi::settings()->get('search_autocomplete_count'); |
6b4c9564 CW |
109 | if (isset($request['params']) && isset($apiDefaults['params'])) { |
110 | $request['params'] += $apiDefaults['params']; | |
111 | } | |
bc255d1d | 112 | $request += $apiDefaults + $defaults; |
fe43b7c9 CW |
113 | // Default api params |
114 | $params = array( | |
115 | 'options' => array( | |
116 | // Adding one extra result allows us to see if there are any more | |
117 | 'limit' => $resultsPerPage + 1, | |
118 | // Because sql is zero-based | |
119 | 'offset' => ($request['page_num'] - 1) * $resultsPerPage, | |
120 | 'sort' => $request['label_field'], | |
121 | ), | |
122 | 'sequential' => 1, | |
123 | ); | |
76ec9ca7 | 124 | // When searching e.g. autocomplete |
fe43b7c9 CW |
125 | if ($request['input']) { |
126 | $params[$request['search_field']] = array('LIKE' => ($config->includeWildCardInName ? '%' : '') . $request['input'] . '%'); | |
127 | } | |
76ec9ca7 CW |
128 | // When looking up a field e.g. displaying existing record |
129 | if (!empty($request['id'])) { | |
83e11123 CW |
130 | if (is_string($request['id']) && strpos($request['id'], ',')) { |
131 | $request['id'] = explode(',', trim($request['id'], ', ')); | |
76ec9ca7 | 132 | } |
78b203e5 | 133 | // Don't run into search limits when prefilling selection |
ba3b21c3 CW |
134 | $params['options']['limit'] = NULL; |
135 | unset($params['options']['offset'], $request['params']['options']['limit'], $request['params']['options']['offset']); | |
76ec9ca7 CW |
136 | $params[$request['id_field']] = is_array($request['id']) ? array('IN' => $request['id']) : $request['id']; |
137 | } | |
fe43b7c9 CW |
138 | $request['params'] += $params; |
139 | } | |
140 | ||
141 | /** | |
c1a920f1 | 142 | * Fallback implementation of getlist_params. May be overridden by individual apis. |
fe43b7c9 | 143 | * |
8c6b335b | 144 | * @param array $request |
fe43b7c9 CW |
145 | */ |
146 | function _civicrm_api3_generic_getlist_params(&$request) { | |
76ec9ca7 | 147 | $fieldsToReturn = array($request['id_field'], $request['label_field']); |
ff88d165 CW |
148 | if (!empty($request['image_field'])) { |
149 | $fieldsToReturn[] = $request['image_field']; | |
150 | } | |
151 | if (!empty($request['description_field'])) { | |
8250601e | 152 | $fieldsToReturn = array_merge($fieldsToReturn, (array) $request['description_field']); |
ff88d165 | 153 | } |
8250601e | 154 | $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra'])); |
fe43b7c9 CW |
155 | } |
156 | ||
157 | /** | |
211e2fca | 158 | * Fallback implementation of getlist_output. May be overridden by individual api functions. |
fe43b7c9 | 159 | * |
8c6b335b CW |
160 | * @param array $result |
161 | * @param array $request | |
b233e1b4 | 162 | * @param string $entity |
6b4c9564 | 163 | * @param array $fields |
fe43b7c9 CW |
164 | * |
165 | * @return array | |
166 | */ | |
6b4c9564 | 167 | function _civicrm_api3_generic_getlist_output($result, $request, $entity, $fields) { |
fe43b7c9 CW |
168 | $output = array(); |
169 | if (!empty($result['values'])) { | |
170 | foreach ($result['values'] as $row) { | |
ff88d165 | 171 | $data = array( |
76ec9ca7 | 172 | 'id' => $row[$request['id_field']], |
fe43b7c9 | 173 | 'label' => $row[$request['label_field']], |
fe43b7c9 | 174 | ); |
ff88d165 | 175 | if (!empty($request['description_field'])) { |
8250601e CW |
176 | $data['description'] = array(); |
177 | foreach ((array) $request['description_field'] as $field) { | |
178 | if (!empty($row[$field])) { | |
6b4c9564 CW |
179 | if (!isset($fields[$field]['pseudoconstant'])) { |
180 | $data['description'][] = $row[$field]; | |
181 | } | |
182 | else { | |
183 | $data['description'][] = CRM_Core_PseudoConstant::getLabel( | |
184 | _civicrm_api3_get_BAO($entity), | |
185 | $field, | |
186 | $row[$field] | |
187 | ); | |
188 | } | |
8250601e CW |
189 | } |
190 | } | |
ff88d165 CW |
191 | }; |
192 | if (!empty($request['image_field'])) { | |
193 | $data['image'] = isset($row[$request['image_field']]) ? $row[$request['image_field']] : ''; | |
8250601e | 194 | } |
ff88d165 | 195 | $output[] = $data; |
fe43b7c9 CW |
196 | } |
197 | } | |
198 | return $output; | |
199 | } | |
f7b2fef0 | 200 | |
27d7d241 CW |
201 | /** |
202 | * Common postprocess for getlist output | |
203 | * | |
204 | * @param $result | |
205 | * @param $request | |
206 | * @param $values | |
207 | */ | |
208 | function _civicrm_api3_generic_getlist_postprocess($result, $request, &$values) { | |
209 | $chains = array(); | |
210 | foreach ($request['params'] as $field => $param) { | |
211 | if (substr($field, 0, 4) === 'api.') { | |
212 | $chains[] = $field; | |
213 | } | |
214 | } | |
215 | if (!empty($result['values'])) { | |
216 | foreach (array_values($result['values']) as $num => $row) { | |
217 | foreach ($request['extra'] as $field) { | |
218 | $values[$num]['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL; | |
219 | } | |
220 | foreach ($chains as $chain) { | |
221 | $values[$num][$chain] = isset($row[$chain]) ? $row[$chain] : NULL; | |
222 | } | |
223 | } | |
224 | } | |
225 | } | |
226 | ||
f7b2fef0 CW |
227 | /** |
228 | * Provide metadata for this api | |
229 | * | |
230 | * @param array $params | |
231 | * @param array $apiRequest | |
232 | */ | |
233 | function _civicrm_api3_generic_getlist_spec(&$params, $apiRequest) { | |
234 | $params += array( | |
235 | 'page_num' => array( | |
236 | 'title' => 'Page Number', | |
237 | 'description' => "Current page of a multi-page lookup", | |
238 | 'type' => CRM_Utils_Type::T_INT, | |
239 | ), | |
240 | 'input' => array( | |
241 | 'title' => 'Search Input', | |
242 | 'description' => "String to search on", | |
243 | 'type' => CRM_Utils_Type::T_TEXT, | |
244 | ), | |
245 | 'params' => array( | |
246 | 'title' => 'API Params', | |
247 | 'description' => "Additional filters to send to the {$apiRequest['entity']} API.", | |
248 | ), | |
249 | 'extra' => array( | |
250 | 'title' => 'Extra', | |
251 | 'description' => 'Array of additional fields to return.', | |
252 | ), | |
253 | 'image_field' => array( | |
254 | 'title' => 'Image Field', | |
255 | 'description' => "Field that this entity uses to store icons (usually automatic)", | |
256 | 'type' => CRM_Utils_Type::T_TEXT, | |
257 | ), | |
258 | 'id_field' => array( | |
259 | 'title' => 'ID Field', | |
260 | 'description' => "Field that uniquely identifies this entity (usually automatic)", | |
261 | 'type' => CRM_Utils_Type::T_TEXT, | |
262 | ), | |
263 | 'description_field' => array( | |
264 | 'title' => 'Description Field', | |
265 | 'description' => "Field that this entity uses to store summary text (usually automatic)", | |
266 | 'type' => CRM_Utils_Type::T_TEXT, | |
267 | ), | |
268 | 'label_field' => array( | |
269 | 'title' => 'Search Field', | |
270 | 'description' => "Field to display as title of results (usually automatic)", | |
271 | 'type' => CRM_Utils_Type::T_TEXT, | |
272 | ), | |
273 | 'search_field' => array( | |
274 | 'title' => 'Search Field', | |
275 | 'description' => "Field to search on (assumed to be the same as label field unless otherwise specified)", | |
276 | 'type' => CRM_Utils_Type::T_TEXT, | |
277 | ), | |
278 | ); | |
279 | } |