comment fixes
[civicrm-core.git] / api / v3 / Generic / Getlist.php
CommitLineData
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 */
39function 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 85function _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,
0724132d 91 'id_field' => $entity == 'option_value' ? 'value' : 'id',
8250601e 92 'description_field' => array(),
fe43b7c9 93 'params' => array(),
8250601e 94 'extra' => array(),
fe43b7c9
CW
95 );
96 // Find main field from meta
dabf9814 97 foreach (array('sort_name', 'title', 'label', 'name', 'subject') as $field) {
fe43b7c9
CW
98 if (isset($fields[$field])) {
99 $defaults['label_field'] = $defaults['search_field'] = $field;
100 break;
101 }
102 }
8250601e 103 // Find fields to be used for the description
fe43b7c9
CW
104 foreach (array('description') as $field) {
105 if (isset($fields[$field])) {
8250601e 106 $defaults['description_field'][] = $field;
fe43b7c9
CW
107 }
108 }
89595c92 109 $resultsPerPage = Civi::settings()->get('search_autocomplete_count');
6b4c9564
CW
110 if (isset($request['params']) && isset($apiDefaults['params'])) {
111 $request['params'] += $apiDefaults['params'];
112 }
bc255d1d 113 $request += $apiDefaults + $defaults;
fe43b7c9
CW
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 );
76ec9ca7 125 // When searching e.g. autocomplete
fe43b7c9
CW
126 if ($request['input']) {
127 $params[$request['search_field']] = array('LIKE' => ($config->includeWildCardInName ? '%' : '') . $request['input'] . '%');
128 }
76ec9ca7
CW
129 // When looking up a field e.g. displaying existing record
130 if (!empty($request['id'])) {
83e11123
CW
131 if (is_string($request['id']) && strpos($request['id'], ',')) {
132 $request['id'] = explode(',', trim($request['id'], ', '));
76ec9ca7 133 }
78b203e5 134 // Don't run into search limits when prefilling selection
ba3b21c3
CW
135 $params['options']['limit'] = NULL;
136 unset($params['options']['offset'], $request['params']['options']['limit'], $request['params']['options']['offset']);
76ec9ca7
CW
137 $params[$request['id_field']] = is_array($request['id']) ? array('IN' => $request['id']) : $request['id'];
138 }
fe43b7c9
CW
139 $request['params'] += $params;
140}
141
142/**
c1a920f1 143 * Fallback implementation of getlist_params. May be overridden by individual apis.
fe43b7c9 144 *
8c6b335b 145 * @param array $request
fe43b7c9
CW
146 */
147function _civicrm_api3_generic_getlist_params(&$request) {
76ec9ca7 148 $fieldsToReturn = array($request['id_field'], $request['label_field']);
ff88d165
CW
149 if (!empty($request['image_field'])) {
150 $fieldsToReturn[] = $request['image_field'];
151 }
152 if (!empty($request['description_field'])) {
8250601e 153 $fieldsToReturn = array_merge($fieldsToReturn, (array) $request['description_field']);
ff88d165 154 }
8250601e 155 $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
fe43b7c9
CW
156}
157
158/**
211e2fca 159 * Fallback implementation of getlist_output. May be overridden by individual api functions.
fe43b7c9 160 *
8c6b335b
CW
161 * @param array $result
162 * @param array $request
b233e1b4 163 * @param string $entity
6b4c9564 164 * @param array $fields
fe43b7c9
CW
165 *
166 * @return array
167 */
6b4c9564 168function _civicrm_api3_generic_getlist_output($result, $request, $entity, $fields) {
fe43b7c9
CW
169 $output = array();
170 if (!empty($result['values'])) {
171 foreach ($result['values'] as $row) {
ff88d165 172 $data = array(
76ec9ca7 173 'id' => $row[$request['id_field']],
fe43b7c9 174 'label' => $row[$request['label_field']],
fe43b7c9 175 );
ff88d165 176 if (!empty($request['description_field'])) {
8250601e
CW
177 $data['description'] = array();
178 foreach ((array) $request['description_field'] as $field) {
179 if (!empty($row[$field])) {
6b4c9564
CW
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 }
8250601e
CW
190 }
191 }
ff88d165
CW
192 };
193 if (!empty($request['image_field'])) {
194 $data['image'] = isset($row[$request['image_field']]) ? $row[$request['image_field']] : '';
8250601e 195 }
ff88d165 196 $output[] = $data;
fe43b7c9
CW
197 }
198 }
199 return $output;
200}
f7b2fef0 201
27d7d241
CW
202/**
203 * Common postprocess for getlist output
204 *
205 * @param $result
206 * @param $request
207 * @param $values
208 */
209function _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
f7b2fef0
CW
228/**
229 * Provide metadata for this api
230 *
231 * @param array $params
232 * @param array $apiRequest
233 */
234function _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}