Merge pull request #2792 from deepak-srivastava/dedupe-hooks
[civicrm-core.git] / api / v3 / Generic / Getlist.php
CommitLineData
fe43b7c9
CW
1<?php
2
3/*
4 +--------------------------------------------------------------------+
731a0992 5| CiviCRM version 4.5 |
fe43b7c9 6+--------------------------------------------------------------------+
731a0992 7| Copyright CiviCRM LLC (c) 2004-2014 |
fe43b7c9
CW
8+--------------------------------------------------------------------+
9| This file is a part of CiviCRM. |
10| |
11| CiviCRM is free software; you can copy, modify, and distribute it |
12| under the terms of the GNU Affero General Public License |
13| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14| |
15| CiviCRM is distributed in the hope that it will be useful, but |
16| WITHOUT ANY WARRANTY; without even the implied warranty of |
17| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18| See the GNU Affero General Public License for more details. |
19| |
20| You should have received a copy of the GNU Affero General Public |
21| License and the CiviCRM Licensing Exception along |
22| with this program; if not, contact CiviCRM LLC |
23| at info[AT]civicrm[DOT]org. If you have questions about the |
24| GNU Affero General Public License or the licensing of CiviCRM, |
25| see the CiviCRM license FAQ at http://civicrm.org/licensing |
26+--------------------------------------------------------------------+
27*/
28/**
29 * Generic api wrapper used for quicksearch and autocomplete
30 *
31 * @param $apiRequest array
32 * @return mixed
33 */
34function civicrm_api3_generic_getList($apiRequest) {
35 $entity = _civicrm_api_get_entity_name_from_camel($apiRequest['entity']);
36 $request = $apiRequest['params'];
37
38 _civicrm_api3_generic_getList_defaults($entity, $request);
39
40 // Hey api, would you like to format the search params?
41 $fnName = "_civicrm_api3_{$entity}_getlist_params";
42 $fnName = function_exists($fnName) ? $fnName : '_civicrm_api3_generic_getlist_params';
43 $fnName($request);
79ae07d9
CW
44
45 $request['params']['check_permissions'] = !empty($apiRequest['params']['check_permissions']);
fe43b7c9
CW
46 $result = civicrm_api3($entity, 'get', $request['params']);
47
48 // Hey api, would you like to format the output?
49 $fnName = "_civicrm_api3_{$entity}_getlist_output";
50 $fnName = function_exists($fnName) ? $fnName : '_civicrm_api3_generic_getlist_output';
51 $values = $fnName($result, $request);
52
78b203e5
CW
53 $output = array('page_num' => $request['page_num']);
54
55 // Limit is set for searching but not fetching by id
a6c6059d 56 if (!empty($request['params']['options']['limit'])) {
fe43b7c9 57 // If we have an extra result then this is not the last page
a6c6059d 58 $last = $request['params']['options']['limit'] - 1;
78b203e5
CW
59 $output['more_results'] = isset($values[$last]);
60 unset($values[$last]);
61 }
fe43b7c9
CW
62
63 return civicrm_api3_create_success($values, $request['params'], $entity, 'getlist', CRM_Core_DAO::$_nullObject, $output);
64}
65
66/**
67 * Set defaults for api.getlist
68 *
69 * @param $entity string
70 * @param $request array
71 */
72function _civicrm_api3_generic_getList_defaults($entity, &$request) {
73 $config = CRM_Core_Config::singleton();
74 $fields = _civicrm_api_get_fields($entity);
75 $defaults = array(
76 'page_num' => 1,
77 'input' => '',
78 'image_field' => NULL,
0724132d 79 'id_field' => $entity == 'option_value' ? 'value' : 'id',
8250601e 80 'description_field' => array(),
fe43b7c9 81 'params' => array(),
8250601e 82 'extra' => array(),
fe43b7c9
CW
83 );
84 // Find main field from meta
85 foreach (array('sort_name', 'title', 'label', 'name') as $field) {
86 if (isset($fields[$field])) {
87 $defaults['label_field'] = $defaults['search_field'] = $field;
88 break;
89 }
90 }
8250601e 91 // Find fields to be used for the description
fe43b7c9
CW
92 foreach (array('description') as $field) {
93 if (isset($fields[$field])) {
8250601e 94 $defaults['description_field'][] = $field;
fe43b7c9
CW
95 }
96 }
97 $resultsPerPage = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'search_autocomplete_count', NULL, 10);
98 $request += $defaults;
99 // Default api params
100 $params = array(
101 'options' => array(
102 // Adding one extra result allows us to see if there are any more
103 'limit' => $resultsPerPage + 1,
104 // Because sql is zero-based
105 'offset' => ($request['page_num'] - 1) * $resultsPerPage,
106 'sort' => $request['label_field'],
107 ),
108 'sequential' => 1,
109 );
76ec9ca7 110 // When searching e.g. autocomplete
fe43b7c9
CW
111 if ($request['input']) {
112 $params[$request['search_field']] = array('LIKE' => ($config->includeWildCardInName ? '%' : '') . $request['input'] . '%');
113 }
76ec9ca7
CW
114 // When looking up a field e.g. displaying existing record
115 if (!empty($request['id'])) {
83e11123
CW
116 if (is_string($request['id']) && strpos($request['id'], ',')) {
117 $request['id'] = explode(',', trim($request['id'], ', '));
76ec9ca7 118 }
78b203e5 119 // Don't run into search limits when prefilling selection
a6c6059d 120 unset($params['options']['limit'], $params['options']['offset'], $request['params']['options']['limit'], $request['params']['options']['offset']);
76ec9ca7
CW
121 $params[$request['id_field']] = is_array($request['id']) ? array('IN' => $request['id']) : $request['id'];
122 }
fe43b7c9
CW
123 $request['params'] += $params;
124}
125
126/**
127 * Fallback implementation of getlist_params. May be overridden by individual apis
128 *
129 * @param $request array
130 */
131function _civicrm_api3_generic_getlist_params(&$request) {
76ec9ca7 132 $fieldsToReturn = array($request['id_field'], $request['label_field']);
ff88d165
CW
133 if (!empty($request['image_field'])) {
134 $fieldsToReturn[] = $request['image_field'];
135 }
136 if (!empty($request['description_field'])) {
8250601e 137 $fieldsToReturn = array_merge($fieldsToReturn, (array) $request['description_field']);
ff88d165 138 }
8250601e 139 $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
fe43b7c9
CW
140}
141
142/**
143 * Fallback implementation of getlist_output. May be overridden by individual apis
144 *
145 * @param $result array
146 * @param $request array
147 *
148 * @return array
149 */
150function _civicrm_api3_generic_getlist_output($result, $request) {
151 $output = array();
152 if (!empty($result['values'])) {
153 foreach ($result['values'] as $row) {
ff88d165 154 $data = array(
76ec9ca7 155 'id' => $row[$request['id_field']],
fe43b7c9 156 'label' => $row[$request['label_field']],
fe43b7c9 157 );
ff88d165 158 if (!empty($request['description_field'])) {
8250601e
CW
159 $data['description'] = array();
160 foreach ((array) $request['description_field'] as $field) {
161 if (!empty($row[$field])) {
162 $data['description'][] = $row[$field];
163 }
164 }
ff88d165
CW
165 };
166 if (!empty($request['image_field'])) {
167 $data['image'] = isset($row[$request['image_field']]) ? $row[$request['image_field']] : '';
8250601e
CW
168 }
169 foreach ($request['extra'] as $field) {
170 $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL;
171 }
ff88d165 172 $output[] = $data;
fe43b7c9
CW
173 }
174 }
175 return $output;
176}