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