Commit | Line | Data |
---|---|---|
fe43b7c9 | 1 | <?php |
fe43b7c9 CW |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
b081365f CW |
4 | | CiviCRM version 4.6 | |
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']; | |
ebf2b57b | 42 | |
bc255d1d CW |
43 | // Hey api, would you like to provide default values? |
44 | $fnName = "_civicrm_api3_{$entity}_getlist_defaults"; | |
45 | $defaults = function_exists($fnName) ? $fnName($request) : array(); | |
46 | _civicrm_api3_generic_getList_defaults($entity, $request, $defaults); | |
ebf2b57b | 47 | |
fe43b7c9 CW |
48 | // Hey api, would you like to format the search params? |
49 | $fnName = "_civicrm_api3_{$entity}_getlist_params"; | |
50 | $fnName = function_exists($fnName) ? $fnName : '_civicrm_api3_generic_getlist_params'; | |
51 | $fnName($request); | |
79ae07d9 CW |
52 | |
53 | $request['params']['check_permissions'] = !empty($apiRequest['params']['check_permissions']); | |
fe43b7c9 CW |
54 | $result = civicrm_api3($entity, 'get', $request['params']); |
55 | ||
56 | // Hey api, would you like to format the output? | |
57 | $fnName = "_civicrm_api3_{$entity}_getlist_output"; | |
58 | $fnName = function_exists($fnName) ? $fnName : '_civicrm_api3_generic_getlist_output'; | |
59 | $values = $fnName($result, $request); | |
60 | ||
78b203e5 CW |
61 | $output = array('page_num' => $request['page_num']); |
62 | ||
63 | // Limit is set for searching but not fetching by id | |
a6c6059d | 64 | if (!empty($request['params']['options']['limit'])) { |
fe43b7c9 | 65 | // If we have an extra result then this is not the last page |
a6c6059d | 66 | $last = $request['params']['options']['limit'] - 1; |
78b203e5 CW |
67 | $output['more_results'] = isset($values[$last]); |
68 | unset($values[$last]); | |
69 | } | |
fe43b7c9 CW |
70 | |
71 | return civicrm_api3_create_success($values, $request['params'], $entity, 'getlist', CRM_Core_DAO::$_nullObject, $output); | |
72 | } | |
73 | ||
74 | /** | |
211e2fca | 75 | * Set defaults for api.getlist. |
fe43b7c9 | 76 | * |
8c6b335b CW |
77 | * @param string $entity |
78 | * @param array $request | |
bc255d1d | 79 | * @param array $apiDefaults |
fe43b7c9 | 80 | */ |
bc255d1d | 81 | function _civicrm_api3_generic_getList_defaults($entity, &$request, $apiDefaults) { |
fe43b7c9 CW |
82 | $config = CRM_Core_Config::singleton(); |
83 | $fields = _civicrm_api_get_fields($entity); | |
84 | $defaults = array( | |
85 | 'page_num' => 1, | |
86 | 'input' => '', | |
87 | 'image_field' => NULL, | |
0724132d | 88 | 'id_field' => $entity == 'option_value' ? 'value' : 'id', |
8250601e | 89 | 'description_field' => array(), |
fe43b7c9 | 90 | 'params' => array(), |
8250601e | 91 | 'extra' => array(), |
fe43b7c9 CW |
92 | ); |
93 | // Find main field from meta | |
dabf9814 | 94 | foreach (array('sort_name', 'title', 'label', 'name', 'subject') as $field) { |
fe43b7c9 CW |
95 | if (isset($fields[$field])) { |
96 | $defaults['label_field'] = $defaults['search_field'] = $field; | |
97 | break; | |
98 | } | |
99 | } | |
8250601e | 100 | // Find fields to be used for the description |
fe43b7c9 CW |
101 | foreach (array('description') as $field) { |
102 | if (isset($fields[$field])) { | |
8250601e | 103 | $defaults['description_field'][] = $field; |
fe43b7c9 CW |
104 | } |
105 | } | |
106 | $resultsPerPage = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'search_autocomplete_count', NULL, 10); | |
bc255d1d | 107 | $request += $apiDefaults + $defaults; |
fe43b7c9 CW |
108 | // Default api params |
109 | $params = array( | |
110 | 'options' => array( | |
111 | // Adding one extra result allows us to see if there are any more | |
112 | 'limit' => $resultsPerPage + 1, | |
113 | // Because sql is zero-based | |
114 | 'offset' => ($request['page_num'] - 1) * $resultsPerPage, | |
115 | 'sort' => $request['label_field'], | |
116 | ), | |
117 | 'sequential' => 1, | |
118 | ); | |
76ec9ca7 | 119 | // When searching e.g. autocomplete |
fe43b7c9 CW |
120 | if ($request['input']) { |
121 | $params[$request['search_field']] = array('LIKE' => ($config->includeWildCardInName ? '%' : '') . $request['input'] . '%'); | |
122 | } | |
76ec9ca7 CW |
123 | // When looking up a field e.g. displaying existing record |
124 | if (!empty($request['id'])) { | |
83e11123 CW |
125 | if (is_string($request['id']) && strpos($request['id'], ',')) { |
126 | $request['id'] = explode(',', trim($request['id'], ', ')); | |
76ec9ca7 | 127 | } |
78b203e5 | 128 | // Don't run into search limits when prefilling selection |
ba3b21c3 CW |
129 | $params['options']['limit'] = NULL; |
130 | unset($params['options']['offset'], $request['params']['options']['limit'], $request['params']['options']['offset']); | |
76ec9ca7 CW |
131 | $params[$request['id_field']] = is_array($request['id']) ? array('IN' => $request['id']) : $request['id']; |
132 | } | |
fe43b7c9 CW |
133 | $request['params'] += $params; |
134 | } | |
135 | ||
136 | /** | |
c1a920f1 | 137 | * Fallback implementation of getlist_params. May be overridden by individual apis. |
fe43b7c9 | 138 | * |
8c6b335b | 139 | * @param array $request |
fe43b7c9 CW |
140 | */ |
141 | function _civicrm_api3_generic_getlist_params(&$request) { | |
76ec9ca7 | 142 | $fieldsToReturn = array($request['id_field'], $request['label_field']); |
ff88d165 CW |
143 | if (!empty($request['image_field'])) { |
144 | $fieldsToReturn[] = $request['image_field']; | |
145 | } | |
146 | if (!empty($request['description_field'])) { | |
8250601e | 147 | $fieldsToReturn = array_merge($fieldsToReturn, (array) $request['description_field']); |
ff88d165 | 148 | } |
8250601e | 149 | $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra'])); |
fe43b7c9 CW |
150 | } |
151 | ||
152 | /** | |
211e2fca | 153 | * Fallback implementation of getlist_output. May be overridden by individual api functions. |
fe43b7c9 | 154 | * |
8c6b335b CW |
155 | * @param array $result |
156 | * @param array $request | |
fe43b7c9 CW |
157 | * |
158 | * @return array | |
159 | */ | |
160 | function _civicrm_api3_generic_getlist_output($result, $request) { | |
161 | $output = array(); | |
162 | if (!empty($result['values'])) { | |
163 | foreach ($result['values'] as $row) { | |
ff88d165 | 164 | $data = array( |
76ec9ca7 | 165 | 'id' => $row[$request['id_field']], |
fe43b7c9 | 166 | 'label' => $row[$request['label_field']], |
fe43b7c9 | 167 | ); |
ff88d165 | 168 | if (!empty($request['description_field'])) { |
8250601e CW |
169 | $data['description'] = array(); |
170 | foreach ((array) $request['description_field'] as $field) { | |
171 | if (!empty($row[$field])) { | |
172 | $data['description'][] = $row[$field]; | |
173 | } | |
174 | } | |
ff88d165 CW |
175 | }; |
176 | if (!empty($request['image_field'])) { | |
177 | $data['image'] = isset($row[$request['image_field']]) ? $row[$request['image_field']] : ''; | |
8250601e CW |
178 | } |
179 | foreach ($request['extra'] as $field) { | |
180 | $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL; | |
181 | } | |
ff88d165 | 182 | $output[] = $data; |
fe43b7c9 CW |
183 | } |
184 | } | |
185 | return $output; | |
186 | } | |
f7b2fef0 CW |
187 | |
188 | /** | |
189 | * Provide metadata for this api | |
190 | * | |
191 | * @param array $params | |
192 | * @param array $apiRequest | |
193 | */ | |
194 | function _civicrm_api3_generic_getlist_spec(&$params, $apiRequest) { | |
195 | $params += array( | |
196 | 'page_num' => array( | |
197 | 'title' => 'Page Number', | |
198 | 'description' => "Current page of a multi-page lookup", | |
199 | 'type' => CRM_Utils_Type::T_INT, | |
200 | ), | |
201 | 'input' => array( | |
202 | 'title' => 'Search Input', | |
203 | 'description' => "String to search on", | |
204 | 'type' => CRM_Utils_Type::T_TEXT, | |
205 | ), | |
206 | 'params' => array( | |
207 | 'title' => 'API Params', | |
208 | 'description' => "Additional filters to send to the {$apiRequest['entity']} API.", | |
209 | ), | |
210 | 'extra' => array( | |
211 | 'title' => 'Extra', | |
212 | 'description' => 'Array of additional fields to return.', | |
213 | ), | |
214 | 'image_field' => array( | |
215 | 'title' => 'Image Field', | |
216 | 'description' => "Field that this entity uses to store icons (usually automatic)", | |
217 | 'type' => CRM_Utils_Type::T_TEXT, | |
218 | ), | |
219 | 'id_field' => array( | |
220 | 'title' => 'ID Field', | |
221 | 'description' => "Field that uniquely identifies this entity (usually automatic)", | |
222 | 'type' => CRM_Utils_Type::T_TEXT, | |
223 | ), | |
224 | 'description_field' => array( | |
225 | 'title' => 'Description Field', | |
226 | 'description' => "Field that this entity uses to store summary text (usually automatic)", | |
227 | 'type' => CRM_Utils_Type::T_TEXT, | |
228 | ), | |
229 | 'label_field' => array( | |
230 | 'title' => 'Search Field', | |
231 | 'description' => "Field to display as title of results (usually automatic)", | |
232 | 'type' => CRM_Utils_Type::T_TEXT, | |
233 | ), | |
234 | 'search_field' => array( | |
235 | 'title' => 'Search Field', | |
236 | 'description' => "Field to search on (assumed to be the same as label field unless otherwise specified)", | |
237 | 'type' => CRM_Utils_Type::T_TEXT, | |
238 | ), | |
239 | ); | |
240 | } |