3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 * Generic api wrapper used for quicksearch and autocomplete.
30 * @param array $apiRequest
34 function civicrm_api3_generic_getList($apiRequest) {
35 $entity = _civicrm_api_get_entity_name_from_camel($apiRequest['entity']);
36 $request = $apiRequest['params'];
38 // Hey api, would you like to provide default values?
39 $fnName = "_civicrm_api3_{$entity}_getlist_defaults";
40 $defaults = function_exists($fnName) ?
$fnName($request) : array();
41 _civicrm_api3_generic_getList_defaults($entity, $request, $defaults);
43 // Hey api, would you like to format the search params?
44 $fnName = "_civicrm_api3_{$entity}_getlist_params";
45 $fnName = function_exists($fnName) ?
$fnName : '_civicrm_api3_generic_getlist_params';
48 $request['params']['check_permissions'] = !empty($apiRequest['params']['check_permissions']);
49 $result = civicrm_api3($entity, 'get', $request['params']);
51 // Hey api, would you like to format the output?
52 $fnName = "_civicrm_api3_{$entity}_getlist_output";
53 $fnName = function_exists($fnName) ?
$fnName : '_civicrm_api3_generic_getlist_output';
54 $values = $fnName($result, $request);
56 $output = array('page_num' => $request['page_num']);
58 // Limit is set for searching but not fetching by id
59 if (!empty($request['params']['options']['limit'])) {
60 // If we have an extra result then this is not the last page
61 $last = $request['params']['options']['limit'] - 1;
62 $output['more_results'] = isset($values[$last]);
63 unset($values[$last]);
66 return civicrm_api3_create_success($values, $request['params'], $entity, 'getlist', CRM_Core_DAO
::$_nullObject, $output);
70 * Set defaults for api.getlist
72 * @param string $entity
73 * @param array $request
74 * @param array $apiDefaults
76 function _civicrm_api3_generic_getList_defaults($entity, &$request, $apiDefaults) {
77 $config = CRM_Core_Config
::singleton();
78 $fields = _civicrm_api_get_fields($entity);
82 'image_field' => NULL,
83 'id_field' => $entity == 'option_value' ?
'value' : 'id',
84 'description_field' => array(),
88 // Find main field from meta
89 foreach (array('sort_name', 'title', 'label', 'name', 'subject') as $field) {
90 if (isset($fields[$field])) {
91 $defaults['label_field'] = $defaults['search_field'] = $field;
95 // Find fields to be used for the description
96 foreach (array('description') as $field) {
97 if (isset($fields[$field])) {
98 $defaults['description_field'][] = $field;
101 $resultsPerPage = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'search_autocomplete_count', NULL, 10);
102 $request +
= $apiDefaults +
$defaults;
103 // Default api params
106 // Adding one extra result allows us to see if there are any more
107 'limit' => $resultsPerPage +
1,
108 // Because sql is zero-based
109 'offset' => ($request['page_num'] - 1) * $resultsPerPage,
110 'sort' => $request['label_field'],
114 // When searching e.g. autocomplete
115 if ($request['input']) {
116 $params[$request['search_field']] = array('LIKE' => ($config->includeWildCardInName ?
'%' : '') . $request['input'] . '%');
118 // When looking up a field e.g. displaying existing record
119 if (!empty($request['id'])) {
120 if (is_string($request['id']) && strpos($request['id'], ',')) {
121 $request['id'] = explode(',', trim($request['id'], ', '));
123 // Don't run into search limits when prefilling selection
124 $params['options']['limit'] = NULL;
125 unset($params['options']['offset'], $request['params']['options']['limit'], $request['params']['options']['offset']);
126 $params[$request['id_field']] = is_array($request['id']) ?
array('IN' => $request['id']) : $request['id'];
128 $request['params'] +
= $params;
132 * Fallback implementation of getlist_params. May be overridden by individual apis
134 * @param array $request
136 function _civicrm_api3_generic_getlist_params(&$request) {
137 $fieldsToReturn = array($request['id_field'], $request['label_field']);
138 if (!empty($request['image_field'])) {
139 $fieldsToReturn[] = $request['image_field'];
141 if (!empty($request['description_field'])) {
142 $fieldsToReturn = array_merge($fieldsToReturn, (array) $request['description_field']);
144 $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
148 * Fallback implementation of getlist_output. May be overridden by individual apis
150 * @param array $result
151 * @param array $request
155 function _civicrm_api3_generic_getlist_output($result, $request) {
157 if (!empty($result['values'])) {
158 foreach ($result['values'] as $row) {
160 'id' => $row[$request['id_field']],
161 'label' => $row[$request['label_field']],
163 if (!empty($request['description_field'])) {
164 $data['description'] = array();
165 foreach ((array) $request['description_field'] as $field) {
166 if (!empty($row[$field])) {
167 $data['description'][] = $row[$field];
171 if (!empty($request['image_field'])) {
172 $data['image'] = isset($row[$request['image_field']]) ?
$row[$request['image_field']] : '';
174 foreach ($request['extra'] as $field) {
175 $data['extra'][$field] = isset($row[$field]) ?
$row[$field] : NULL;