CRM-13966 - Wire up contact api to getlist api
[civicrm-core.git] / api / v3 / Generic / Getlist.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
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 */
34 function 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);
44
45 $result = civicrm_api3($entity, 'get', $request['params']);
46
47 // Hey api, would you like to format the output?
48 $fnName = "_civicrm_api3_{$entity}_getlist_output";
49 $fnName = function_exists($fnName) ? $fnName : '_civicrm_api3_generic_getlist_output';
50 $values = $fnName($result, $request);
51
52 $output = array(
53 // If we have an extra result then this is not the last page
54 'more_results' => isset($values[10]),
55 'page_num' => $request['page_num'],
56 );
57 unset($values[10]);
58
59 return civicrm_api3_create_success($values, $request['params'], $entity, 'getlist', CRM_Core_DAO::$_nullObject, $output);
60 }
61
62 /**
63 * Set defaults for api.getlist
64 *
65 * @param $entity string
66 * @param $request array
67 */
68 function _civicrm_api3_generic_getList_defaults($entity, &$request) {
69 $config = CRM_Core_Config::singleton();
70 $fields = _civicrm_api_get_fields($entity);
71 $defaults = array(
72 'page_num' => 1,
73 'input' => '',
74 'image_field' => NULL,
75 'id_field' => 'id',
76 'params' => array(),
77 );
78 // Find main field from meta
79 foreach (array('sort_name', 'title', 'label', 'name') as $field) {
80 if (isset($fields[$field])) {
81 $defaults['label_field'] = $defaults['search_field'] = $field;
82 break;
83 }
84 }
85 foreach (array('description') as $field) {
86 if (isset($fields[$field])) {
87 $defaults['description_field'] = $field;
88 break;
89 }
90 }
91 $resultsPerPage = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'search_autocomplete_count', NULL, 10);
92 $request += $defaults;
93 // Default api params
94 $params = array(
95 'options' => array(
96 // Adding one extra result allows us to see if there are any more
97 'limit' => $resultsPerPage + 1,
98 // Because sql is zero-based
99 'offset' => ($request['page_num'] - 1) * $resultsPerPage,
100 'sort' => $request['label_field'],
101 ),
102 'sequential' => 1,
103 );
104 // When searching e.g. autocomplete
105 if ($request['input']) {
106 $params[$request['search_field']] = array('LIKE' => ($config->includeWildCardInName ? '%' : '') . $request['input'] . '%');
107 }
108 // When looking up a field e.g. displaying existing record
109 if (!empty($request['id'])) {
110 if (is_string($request['id']) && strpos(',', $request['id'])) {
111 $request['id'] = explode(',', $request['id']);
112 }
113 $params[$request['id_field']] = is_array($request['id']) ? array('IN' => $request['id']) : $request['id'];
114 }
115 $request['params'] += $params;
116 }
117
118 /**
119 * Fallback implementation of getlist_params. May be overridden by individual apis
120 *
121 * @param $request array
122 */
123 function _civicrm_api3_generic_getlist_params(&$request) {
124 $fieldsToReturn = array($request['id_field'], $request['label_field']);
125 if (!empty($request['image_field'])) {
126 $fieldsToReturn[] = $request['image_field'];
127 }
128 if (!empty($request['description_field'])) {
129 $fieldsToReturn[] = $request['description_field'];
130 }
131 $request['params']['options']['return'] = $fieldsToReturn;
132 }
133
134 /**
135 * Fallback implementation of getlist_output. May be overridden by individual apis
136 *
137 * @param $result array
138 * @param $request array
139 *
140 * @return array
141 */
142 function _civicrm_api3_generic_getlist_output($result, $request) {
143 $output = array();
144 if (!empty($result['values'])) {
145 foreach ($result['values'] as $row) {
146 $data = array(
147 'id' => $row[$request['id_field']],
148 'label' => $row[$request['label_field']],
149 );
150 if (!empty($request['description_field'])) {
151 $data['description'] = isset($row[$request['description_field']]) ? $row[$request['description_field']] : '';
152 };
153 if (!empty($request['image_field'])) {
154 $data['image'] = isset($row[$request['image_field']]) ? $row[$request['image_field']] : '';
155 };
156 $output[] = $data;
157 }
158 }
159 return $output;
160 }