Commit | Line | Data |
---|---|---|
fe43b7c9 CW |
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); | |
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 | ||
53 | $output = array( | |
54 | // If we have an extra result then this is not the last page | |
55 | 'more_results' => isset($values[10]), | |
56 | 'page_num' => $request['page_num'], | |
57 | ); | |
58 | unset($values[10]); | |
59 | ||
60 | return civicrm_api3_create_success($values, $request['params'], $entity, 'getlist', CRM_Core_DAO::$_nullObject, $output); | |
61 | } | |
62 | ||
63 | /** | |
64 | * Set defaults for api.getlist | |
65 | * | |
66 | * @param $entity string | |
67 | * @param $request array | |
68 | */ | |
69 | function _civicrm_api3_generic_getList_defaults($entity, &$request) { | |
70 | $config = CRM_Core_Config::singleton(); | |
71 | $fields = _civicrm_api_get_fields($entity); | |
72 | $defaults = array( | |
73 | 'page_num' => 1, | |
74 | 'input' => '', | |
75 | 'image_field' => NULL, | |
0724132d | 76 | 'id_field' => $entity == 'option_value' ? 'value' : 'id', |
8250601e | 77 | 'description_field' => array(), |
fe43b7c9 | 78 | 'params' => array(), |
8250601e | 79 | 'extra' => array(), |
fe43b7c9 CW |
80 | ); |
81 | // Find main field from meta | |
82 | foreach (array('sort_name', 'title', 'label', 'name') as $field) { | |
83 | if (isset($fields[$field])) { | |
84 | $defaults['label_field'] = $defaults['search_field'] = $field; | |
85 | break; | |
86 | } | |
87 | } | |
8250601e | 88 | // Find fields to be used for the description |
fe43b7c9 CW |
89 | foreach (array('description') as $field) { |
90 | if (isset($fields[$field])) { | |
8250601e | 91 | $defaults['description_field'][] = $field; |
fe43b7c9 CW |
92 | } |
93 | } | |
94 | $resultsPerPage = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'search_autocomplete_count', NULL, 10); | |
95 | $request += $defaults; | |
96 | // Default api params | |
97 | $params = array( | |
98 | 'options' => array( | |
99 | // Adding one extra result allows us to see if there are any more | |
100 | 'limit' => $resultsPerPage + 1, | |
101 | // Because sql is zero-based | |
102 | 'offset' => ($request['page_num'] - 1) * $resultsPerPage, | |
103 | 'sort' => $request['label_field'], | |
104 | ), | |
105 | 'sequential' => 1, | |
106 | ); | |
76ec9ca7 | 107 | // When searching e.g. autocomplete |
fe43b7c9 CW |
108 | if ($request['input']) { |
109 | $params[$request['search_field']] = array('LIKE' => ($config->includeWildCardInName ? '%' : '') . $request['input'] . '%'); | |
110 | } | |
76ec9ca7 CW |
111 | // When looking up a field e.g. displaying existing record |
112 | if (!empty($request['id'])) { | |
113 | if (is_string($request['id']) && strpos(',', $request['id'])) { | |
114 | $request['id'] = explode(',', $request['id']); | |
115 | } | |
116 | $params[$request['id_field']] = is_array($request['id']) ? array('IN' => $request['id']) : $request['id']; | |
117 | } | |
fe43b7c9 CW |
118 | $request['params'] += $params; |
119 | } | |
120 | ||
121 | /** | |
122 | * Fallback implementation of getlist_params. May be overridden by individual apis | |
123 | * | |
124 | * @param $request array | |
125 | */ | |
126 | function _civicrm_api3_generic_getlist_params(&$request) { | |
76ec9ca7 | 127 | $fieldsToReturn = array($request['id_field'], $request['label_field']); |
ff88d165 CW |
128 | if (!empty($request['image_field'])) { |
129 | $fieldsToReturn[] = $request['image_field']; | |
130 | } | |
131 | if (!empty($request['description_field'])) { | |
8250601e | 132 | $fieldsToReturn = array_merge($fieldsToReturn, (array) $request['description_field']); |
ff88d165 | 133 | } |
8250601e | 134 | $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra'])); |
fe43b7c9 CW |
135 | } |
136 | ||
137 | /** | |
138 | * Fallback implementation of getlist_output. May be overridden by individual apis | |
139 | * | |
140 | * @param $result array | |
141 | * @param $request array | |
142 | * | |
143 | * @return array | |
144 | */ | |
145 | function _civicrm_api3_generic_getlist_output($result, $request) { | |
146 | $output = array(); | |
147 | if (!empty($result['values'])) { | |
148 | foreach ($result['values'] as $row) { | |
ff88d165 | 149 | $data = array( |
76ec9ca7 | 150 | 'id' => $row[$request['id_field']], |
fe43b7c9 | 151 | 'label' => $row[$request['label_field']], |
fe43b7c9 | 152 | ); |
ff88d165 | 153 | if (!empty($request['description_field'])) { |
8250601e CW |
154 | $data['description'] = array(); |
155 | foreach ((array) $request['description_field'] as $field) { | |
156 | if (!empty($row[$field])) { | |
157 | $data['description'][] = $row[$field]; | |
158 | } | |
159 | } | |
ff88d165 CW |
160 | }; |
161 | if (!empty($request['image_field'])) { | |
162 | $data['image'] = isset($row[$request['image_field']]) ? $row[$request['image_field']] : ''; | |
8250601e CW |
163 | } |
164 | foreach ($request['extra'] as $field) { | |
165 | $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL; | |
166 | } | |
ff88d165 | 167 | $output[] = $data; |
fe43b7c9 CW |
168 | } |
169 | } | |
170 | return $output; | |
171 | } |