Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
731a0992 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | */ |
6a488035 TO |
27 | |
28 | /** | |
c28e1768 CW |
29 | * This api exposes CiviCRM contacts. |
30 | * Contacts are the main entity in CiviCRM and this api is more robust than most. | |
31 | * - Get action allows all params supported by advanced search. | |
b081365f | 32 | * - Create action allows creating several related entities at once (e.g. email). |
c28e1768 CW |
33 | * - Create allows checking for duplicate contacts. |
34 | * Use getfields to list the full range of parameters and options supported by each action. | |
6a488035 TO |
35 | * |
36 | * @package CiviCRM_APIv3 | |
6a488035 TO |
37 | */ |
38 | ||
39 | /** | |
c28e1768 | 40 | * Create or update a contact. |
6a488035 | 41 | * |
cf470720 TO |
42 | * @param array $params |
43 | * Input parameters. | |
6a488035 | 44 | * |
77b97be7 | 45 | * @throws API_Exception |
6a488035 | 46 | * |
a6c01b45 | 47 | * @return array |
72b3a70c | 48 | * API Result Array |
6a488035 TO |
49 | */ |
50 | function civicrm_api3_contact_create($params) { | |
51 | ||
52 | $contactID = CRM_Utils_Array::value('contact_id', $params, CRM_Utils_Array::value('id', $params)); | |
53 | $dupeCheck = CRM_Utils_Array::value('dupe_check', $params, FALSE); | |
54 | $values = _civicrm_api3_contact_check_params($params, $dupeCheck); | |
55 | if ($values) { | |
56 | return $values; | |
57 | } | |
58 | ||
59 | if (!$contactID) { | |
60 | // If we get here, we're ready to create a new contact | |
61 | if (($email = CRM_Utils_Array::value('email', $params)) && !is_array($params['email'])) { | |
62 | $defLocType = CRM_Core_BAO_LocationType::getDefault(); | |
63 | $params['email'] = array( | |
35671d00 | 64 | 1 => array( |
d5cc0fc2 | 65 | 'email' => $email, |
6a488035 TO |
66 | 'is_primary' => 1, |
67 | 'location_type_id' => ($defLocType->id) ? $defLocType->id : 1, | |
68 | ), | |
69 | ); | |
70 | } | |
71 | } | |
72 | ||
73 | if (!empty($params['home_url'])) { | |
cbf48754 | 74 | $websiteTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'); |
35671d00 | 75 | $params['website'] = array( |
d5cc0fc2 | 76 | 1 => array( |
77 | 'website_type_id' => key($websiteTypes), | |
6a488035 TO |
78 | 'url' => $params['home_url'], |
79 | ), | |
80 | ); | |
81 | } | |
82 | ||
6ecbca5b | 83 | _civicrm_api3_greeting_format_params($params); |
6a488035 TO |
84 | |
85 | $values = array(); | |
6a488035 | 86 | |
6ecbca5b | 87 | if (empty($params['contact_type']) && $contactID) { |
88 | $params['contact_type'] = CRM_Contact_BAO_Contact::getContactType($contactID); | |
6a488035 TO |
89 | } |
90 | ||
6ecbca5b | 91 | if (!isset($params['contact_sub_type']) && $contactID) { |
92 | $params['contact_sub_type'] = CRM_Contact_BAO_Contact::getContactSubType($contactID); | |
6a488035 TO |
93 | } |
94 | ||
6ecbca5b | 95 | _civicrm_api3_custom_format_params($params, $values, $params['contact_type'], $contactID); |
6a488035 TO |
96 | |
97 | $params = array_merge($params, $values); | |
6ecbca5b | 98 | //@todo we should just call basic_create here - but need to make contact:create accept 'id' on the bao |
6a488035 TO |
99 | $contact = _civicrm_api3_contact_update($params, $contactID); |
100 | ||
101 | if (is_a($contact, 'CRM_Core_Error')) { | |
6ecbca5b | 102 | throw new API_Exception($contact->_errors[0]['message']); |
6a488035 TO |
103 | } |
104 | else { | |
105 | $values = array(); | |
106 | _civicrm_api3_object_to_array_unique_fields($contact, $values[$contact->id]); | |
107 | } | |
108 | ||
109 | return civicrm_api3_create_success($values, $params, 'Contact', 'create'); | |
110 | } | |
111 | ||
11e09c59 | 112 | /** |
0aa0303c | 113 | * Adjust Metadata for Create action. |
6a488035 | 114 | * |
cf470720 | 115 | * @param array $params |
b081365f | 116 | * Array of parameters determined by getfields. |
6a488035 TO |
117 | */ |
118 | function _civicrm_api3_contact_create_spec(&$params) { | |
119 | $params['contact_type']['api.required'] = 1; | |
120 | $params['id']['api.aliases'] = array('contact_id'); | |
121 | $params['current_employer'] = array( | |
122 | 'title' => 'Current Employer', | |
123 | 'description' => 'Name of Current Employer', | |
9c5991b3 | 124 | 'type' => CRM_Utils_Type::T_STRING, |
6a488035 | 125 | ); |
0391dc25 | 126 | $params['dupe_check'] = array( |
127 | 'title' => 'Check for Duplicates', | |
128 | 'description' => 'Throw error if contact create matches dedupe rule', | |
129 | ); | |
6ecbca5b | 130 | $params['prefix_id']['api.aliases'] = array('individual_prefix', 'individual_prefix_id'); |
131 | $params['suffix_id']['api.aliases'] = array('individual_suffix', 'individual_suffix_id'); | |
6a488035 TO |
132 | } |
133 | ||
134 | /** | |
61fe4988 EM |
135 | * Retrieve one or more contacts, given a set of search params. |
136 | * | |
137 | * @param array $params | |
6a488035 | 138 | * |
a6c01b45 | 139 | * @return array |
72b3a70c | 140 | * API Result Array |
6a488035 TO |
141 | */ |
142 | function civicrm_api3_contact_get($params) { | |
143 | $options = array(); | |
144 | _civicrm_api3_contact_get_supportanomalies($params, $options); | |
145 | $contacts = _civicrm_api3_get_using_query_object('contact', $params, $options); | |
6ecbca5b | 146 | return civicrm_api3_create_success($contacts, $params, 'contact'); |
6a488035 TO |
147 | } |
148 | ||
aa1b1481 | 149 | /** |
61fe4988 EM |
150 | * Get count of contact. |
151 | * | |
c490a46a | 152 | * @param array $params |
aa1b1481 EM |
153 | * |
154 | * @return int | |
155 | */ | |
6a488035 TO |
156 | function civicrm_api3_contact_getcount($params) { |
157 | $options = array(); | |
158 | _civicrm_api3_contact_get_supportanomalies($params, $options); | |
35671d00 | 159 | $count = _civicrm_api3_get_using_query_object('contact', $params, $options, 1); |
972322c5 | 160 | return (int) $count; |
6a488035 | 161 | } |
11e09c59 TO |
162 | |
163 | /** | |
9d32e6f7 | 164 | * Adjust Metadata for Get action. |
6a488035 | 165 | * |
cf470720 | 166 | * @param array $params |
b081365f | 167 | * Array of parameters determined by getfields. |
6a488035 TO |
168 | */ |
169 | function _civicrm_api3_contact_get_spec(&$params) { | |
170 | $params['contact_is_deleted']['api.default'] = 0; | |
171 | ||
9d32e6f7 | 172 | // We declare all these pseudoFields as there are other undocumented fields accessible |
6a488035 TO |
173 | // via the api - but if check permissions is set we only allow declared fields |
174 | $params['address_id']['title'] = 'Primary Address ID'; | |
175 | $params['street_address']['title'] = 'Primary Address Street Address'; | |
176 | $params['supplemental_address_1']['title'] = 'Primary Address Supplemental Address 1'; | |
177 | $params['supplemental_address_2']['title'] = 'Primary Address Supplemental Address 2'; | |
23c2fe57 | 178 | $params['current_employer']['title'] = 'Current Employer'; |
6a488035 TO |
179 | $params['city']['title'] = 'Primary Address City'; |
180 | $params['postal_code_suffix']['title'] = 'Primary Address Post Code Suffix'; | |
181 | $params['postal_code']['title'] = 'Primary Address Post Code'; | |
182 | $params['geo_code_1']['title'] = 'Primary Address Latitude'; | |
183 | $params['geo_code_2']['title'] = 'Primary Address Longitude'; | |
184 | $params['state_province_id']['title'] = 'Primary Address State Province ID'; | |
185 | $params['state_province_name']['title'] = 'Primary Address State Province Name'; | |
186 | $params['state_province']['title'] = 'Primary Address State Province'; | |
41847fb4 | 187 | $params['country_id']['title'] = 'Primary Address Country ID'; |
6a488035 TO |
188 | $params['country']['title'] = 'Primary Address country'; |
189 | $params['worldregion_id']['title'] = 'Primary Address World Region ID'; | |
190 | $params['worldregion']['title'] = 'Primary Address World Region'; | |
191 | $params['phone_id']['title'] = 'Primary Phone ID'; | |
7d4ceb59 | 192 | $params['phone']['title'] = 'Primary Phone'; |
6a488035 TO |
193 | $params['phone_type_id']['title'] = 'Primary Phone Type ID'; |
194 | $params['provider_id']['title'] = 'Primary Phone Provider ID'; | |
195 | $params['email_id']['title'] = 'Primary Email ID'; | |
196 | $params['email']['title'] = 'Primary Email'; | |
c2280651 | 197 | $params['gender_id']['title'] = 'Gender ID'; |
198 | $params['gender']['title'] = 'Gender'; | |
6a488035 | 199 | $params['on_hold']['title'] = 'Primary Email On Hold'; |
c206647d EM |
200 | $params['im']['title'] = 'Primary Instant Messenger'; |
201 | $params['im_id']['title'] = 'Primary Instant Messenger ID'; | |
6a488035 TO |
202 | $params['group_id']['title'] = 'Group Memberships (filter)'; |
203 | $params['group']['title'] = 'Group Memberships (filter, array)'; | |
204 | $params['tag']['title'] = 'Assigned tags (filter, array)'; | |
c206647d EM |
205 | $params['birth_date_low'] = array('name' => 'birth_date_low', 'type' => CRM_Utils_Type::T_DATE, 'title' => ts('Birth Date is equal to or greater than')); |
206 | $params['birth_date_high'] = array('name' => 'birth_date_high', 'type' => CRM_Utils_Type::T_DATE, 'title' => ts('Birth Date is equal to or less than')); | |
35671d00 | 207 | $params['deceased_date_low'] = array('name' => 'deceased_date_low', 'type' => CRM_Utils_Type::T_DATE, 'title' => ts('Deceased Date is equal to or greater than')); |
9f60788a | 208 | $params['deceased_date_high'] = array('name' => 'deceased_date_high', 'type' => CRM_Utils_Type::T_DATE, 'title' => ts('Deceased Date is equal to or less than')); |
6a488035 TO |
209 | } |
210 | ||
11e09c59 | 211 | /** |
9d32e6f7 EM |
212 | * Support for historical oddities. |
213 | * | |
6a488035 TO |
214 | * We are supporting 'showAll' = 'all', 'trash' or 'active' for contact get |
215 | * and for getcount | |
216 | * - hopefully some day we'll come up with a std syntax for the 3-way-boolean of | |
217 | * 0, 1 or not set | |
218 | * | |
219 | * We also support 'filter_group_id' & 'filter.group_id' | |
220 | * | |
cf470720 TO |
221 | * @param array $params |
222 | * As passed into api get or getcount function. | |
223 | * @param array $options | |
224 | * Array of options (so we can modify the filter). | |
6a488035 TO |
225 | */ |
226 | function _civicrm_api3_contact_get_supportanomalies(&$params, &$options) { | |
227 | if (isset($params['showAll'])) { | |
228 | if (strtolower($params['showAll']) == "active") { | |
229 | $params['contact_is_deleted'] = 0; | |
230 | } | |
231 | if (strtolower($params['showAll']) == "trash") { | |
232 | $params['contact_is_deleted'] = 1; | |
233 | } | |
234 | if (strtolower($params['showAll']) == "all" && isset($params['contact_is_deleted'])) { | |
235 | unset($params['contact_is_deleted']); | |
236 | } | |
237 | } | |
238 | // support for group filters | |
239 | if (array_key_exists('filter_group_id', $params)) { | |
240 | $params['filter.group_id'] = $params['filter_group_id']; | |
241 | unset($params['filter_group_id']); | |
242 | } | |
243 | // filter.group_id works both for 1,2,3 and array (1,2,3) | |
244 | if (array_key_exists('filter.group_id', $params)) { | |
245 | if (is_array($params['filter.group_id'])) { | |
246 | $groups = $params['filter.group_id']; | |
247 | } | |
4f99ca55 TO |
248 | else { |
249 | $groups = explode(',', $params['filter.group_id']); | |
35671d00 | 250 | } |
6a488035 TO |
251 | unset($params['filter.group_id']); |
252 | $groups = array_flip($groups); | |
253 | $groups[key($groups)] = 1; | |
254 | $options['input_params']['group'] = $groups; | |
255 | } | |
256 | } | |
257 | ||
258 | /** | |
9d32e6f7 | 259 | * Delete a contact with given contact id. |
6a488035 | 260 | * |
cf470720 | 261 | * @param array $params |
c23f45d3 | 262 | * input parameters per getfields |
6a488035 | 263 | * |
a6c01b45 | 264 | * @return array |
72b3a70c | 265 | * API Result Array |
6a488035 TO |
266 | */ |
267 | function civicrm_api3_contact_delete($params) { | |
268 | ||
269 | $contactID = CRM_Utils_Array::value('id', $params); | |
270 | ||
271 | $session = CRM_Core_Session::singleton(); | |
272 | if ($contactID == $session->get('userID')) { | |
273 | return civicrm_api3_create_error('This contact record is linked to the currently logged in user account - and cannot be deleted.'); | |
274 | } | |
0d8afee2 CW |
275 | $restore = !empty($params['restore']) ? $params['restore'] : FALSE; |
276 | $skipUndelete = !empty($params['skip_undelete']) ? $params['skip_undelete'] : FALSE; | |
f182074e PN |
277 | |
278 | // CRM-12929 | |
279 | // restrict permanent delete if a contact has financial trxn associated with it | |
280 | $error = NULL; | |
281 | if ($skipUndelete && CRM_Financial_BAO_FinancialItem::checkContactPresent(array($contactID), $error)) { | |
ad3f841d | 282 | return civicrm_api3_create_error($error['_qf_default']); |
f182074e | 283 | } |
6a488035 TO |
284 | if (CRM_Contact_BAO_Contact::deleteContact($contactID, $restore, $skipUndelete)) { |
285 | return civicrm_api3_create_success(); | |
286 | } | |
287 | else { | |
288 | return civicrm_api3_create_error('Could not delete contact'); | |
289 | } | |
290 | } | |
291 | ||
292 | ||
aa1b1481 | 293 | /** |
9d32e6f7 EM |
294 | * Check parameters passed in. |
295 | * | |
296 | * This function is on it's way out. | |
297 | * | |
c490a46a | 298 | * @param array $params |
aa1b1481 | 299 | * @param bool $dupeCheck |
aa1b1481 EM |
300 | * |
301 | * @return null | |
302 | * @throws API_Exception | |
303 | * @throws CiviCRM_API3_Exception | |
304 | */ | |
c1d12cc0 | 305 | function _civicrm_api3_contact_check_params(&$params, $dupeCheck) { |
6a488035 TO |
306 | |
307 | switch (strtolower(CRM_Utils_Array::value('contact_type', $params))) { | |
308 | case 'household': | |
35671d00 | 309 | civicrm_api3_verify_mandatory($params, NULL, array('household_name')); |
6a488035 | 310 | break; |
35671d00 | 311 | |
6a488035 | 312 | case 'organization': |
35671d00 | 313 | civicrm_api3_verify_mandatory($params, NULL, array('organization_name')); |
6a488035 | 314 | break; |
35671d00 | 315 | |
6a488035 | 316 | case 'individual': |
35671d00 | 317 | civicrm_api3_verify_one_mandatory($params, NULL, array( |
6a488035 TO |
318 | 'first_name', |
319 | 'last_name', | |
320 | 'email', | |
321 | 'display_name', | |
322 | ) | |
35671d00 TO |
323 | ); |
324 | break; | |
6a488035 TO |
325 | } |
326 | ||
fe18a93c CW |
327 | // Fixme: This really needs to be handled at a lower level. @See CRM-13123 |
328 | if (isset($params['preferred_communication_method'])) { | |
329 | $params['preferred_communication_method'] = CRM_Utils_Array::implodePadded($params['preferred_communication_method']); | |
330 | } | |
331 | ||
8cc574cf | 332 | if (!empty($params['contact_sub_type']) && !empty($params['contact_type'])) { |
35671d00 TO |
333 | if (!(CRM_Contact_BAO_ContactType::isExtendsContactType($params['contact_sub_type'], $params['contact_type']))) { |
334 | throw new API_Exception("Invalid or Mismatched Contact Subtype: " . implode(', ', (array) $params['contact_sub_type'])); | |
6a488035 | 335 | } |
35671d00 | 336 | } |
6a488035 TO |
337 | |
338 | if ($dupeCheck) { | |
339 | // check for record already existing | |
340 | $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']); | |
341 | ||
342 | // CRM-6431 | |
343 | // setting 'check_permission' here means that the dedupe checking will be carried out even if the | |
344 | // person does not have permission to carry out de-dupes | |
345 | // this is similar to the front end form | |
346 | if (isset($params['check_permission'])) { | |
347 | $dedupeParams['check_permission'] = $params['check_permission']; | |
348 | } | |
349 | ||
67cfa333 | 350 | $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type'], 'Unsupervised', array()); |
6a488035 | 351 | |
35671d00 | 352 | if (count($ids) > 0) { |
86bfa4f6 | 353 | throw new API_Exception("Found matching contacts: " . implode(',', $ids), "duplicate", array("ids" => $ids)); |
6a488035 TO |
354 | } |
355 | } | |
356 | ||
8d99ab37 | 357 | // The BAO no longer supports the legacy param "current_employer" so here is a shim for api backward-compatability |
6a488035 | 358 | if (!empty($params['current_employer'])) { |
8d99ab37 CW |
359 | $organizationParams = array( |
360 | 'organization_name' => $params['current_employer'], | |
361 | ); | |
6a488035 TO |
362 | |
363 | $dedupParams = CRM_Dedupe_Finder::formatParams($organizationParams, 'Organization'); | |
364 | ||
365 | $dedupParams['check_permission'] = FALSE; | |
366 | $dupeIds = CRM_Dedupe_Finder::dupesByParams($dedupParams, 'Organization', 'Supervised'); | |
367 | ||
368 | // check for mismatch employer name and id | |
369 | if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds)) { | |
370 | throw new API_Exception('Employer name and Employer id Mismatch'); | |
371 | } | |
372 | ||
373 | // show error if multiple organisation with same name exist | |
374 | if (empty($params['employer_id']) && (count($dupeIds) > 1)) { | |
375 | throw new API_Exception('Found more than one Organisation with same Name.'); | |
376 | } | |
8d99ab37 CW |
377 | |
378 | if ($dupeIds) { | |
379 | $params['employer_id'] = $dupeIds[0]; | |
380 | } | |
381 | else { | |
382 | $result = civicrm_api3('contact', 'create', array( | |
383 | 'organization_name' => $params['current_employer'], | |
21dfd5f5 | 384 | 'contact_type' => 'Organization', |
8d99ab37 CW |
385 | )); |
386 | $params['employer_id'] = $result['id']; | |
387 | } | |
6a488035 TO |
388 | } |
389 | ||
390 | return NULL; | |
391 | } | |
392 | ||
393 | /** | |
9d32e6f7 | 394 | * Helper function for contact create. |
6a488035 | 395 | * |
cf470720 TO |
396 | * @param array $params |
397 | * (reference ) an assoc array of name/value pairs. | |
398 | * @param int $contactID | |
399 | * If present the contact with that ID is updated. | |
6a488035 | 400 | * |
54f1aa2a | 401 | * @return CRM_Contact_BAO_Contact|CRM_Core_Error |
6a488035 TO |
402 | */ |
403 | function _civicrm_api3_contact_update($params, $contactID = NULL) { | |
6ecbca5b | 404 | //@todo - doesn't contact create support 'id' which is already set- check & remove |
6a488035 TO |
405 | if ($contactID) { |
406 | $params['contact_id'] = $contactID; | |
407 | } | |
408 | ||
6ecbca5b | 409 | return CRM_Contact_BAO_Contact::create($params); |
6a488035 TO |
410 | } |
411 | ||
412 | /** | |
9d32e6f7 | 413 | * Validate the addressee or email or postal greetings. |
6a488035 | 414 | * |
cf470720 | 415 | * @param array $params |
9d32e6f7 | 416 | * Array per getfields metadata. |
6a488035 | 417 | * |
77b97be7 | 418 | * @throws API_Exception |
6a488035 TO |
419 | */ |
420 | function _civicrm_api3_greeting_format_params($params) { | |
421 | $greetingParams = array('', '_id', '_custom'); | |
422 | foreach (array('email', 'postal', 'addressee') as $key) { | |
423 | $greeting = '_greeting'; | |
424 | if ($key == 'addressee') { | |
425 | $greeting = ''; | |
426 | } | |
427 | ||
428 | $formatParams = FALSE; | |
22242c87 | 429 | // Unset display value from params. |
6a488035 TO |
430 | if (isset($params["{$key}{$greeting}_display"])) { |
431 | unset($params["{$key}{$greeting}_display"]); | |
432 | } | |
433 | ||
434 | // check if greetings are present in present | |
435 | foreach ($greetingParams as $greetingValues) { | |
436 | if (array_key_exists("{$key}{$greeting}{$greetingValues}", $params)) { | |
437 | $formatParams = TRUE; | |
438 | break; | |
439 | } | |
440 | } | |
441 | ||
442 | if (!$formatParams) { | |
443 | continue; | |
444 | } | |
445 | ||
446 | $nullValue = FALSE; | |
447 | $filter = array( | |
448 | 'contact_type' => $params['contact_type'], | |
449 | 'greeting_type' => "{$key}{$greeting}", | |
450 | ); | |
451 | ||
452 | $greetings = CRM_Core_PseudoConstant::greeting($filter); | |
453 | $greetingId = CRM_Utils_Array::value("{$key}{$greeting}_id", $params); | |
454 | $greetingVal = CRM_Utils_Array::value("{$key}{$greeting}", $params); | |
455 | $customGreeting = CRM_Utils_Array::value("{$key}{$greeting}_custom", $params); | |
456 | ||
457 | if (!$greetingId && $greetingVal) { | |
458 | $params["{$key}{$greeting}_id"] = CRM_Utils_Array::key($params["{$key}{$greeting}"], $greetings); | |
459 | } | |
460 | ||
461 | if ($customGreeting && $greetingId && | |
462 | ($greetingId != array_search('Customized', $greetings)) | |
463 | ) { | |
6ecbca5b | 464 | throw new API_Exception(ts('Provide either %1 greeting id and/or %1 greeting or custom %1 greeting', |
6a488035 TO |
465 | array(1 => $key) |
466 | )); | |
467 | } | |
468 | ||
469 | if ($greetingVal && $greetingId && | |
470 | ($greetingId != CRM_Utils_Array::key($greetingVal, $greetings)) | |
471 | ) { | |
6ecbca5b | 472 | throw new API_Exception(ts('Mismatch in %1 greeting id and %1 greeting', |
6a488035 TO |
473 | array(1 => $key) |
474 | )); | |
475 | } | |
476 | ||
477 | if ($greetingId) { | |
478 | ||
479 | if (!array_key_exists($greetingId, $greetings)) { | |
6ecbca5b | 480 | throw new API_Exception(ts('Invalid %1 greeting Id', array(1 => $key))); |
6a488035 TO |
481 | } |
482 | ||
483 | if (!$customGreeting && ($greetingId == array_search('Customized', $greetings))) { | |
6ecbca5b | 484 | throw new API_Exception(ts('Please provide a custom value for %1 greeting', |
6a488035 TO |
485 | array(1 => $key) |
486 | )); | |
487 | } | |
488 | } | |
489 | elseif ($greetingVal) { | |
490 | ||
491 | if (!in_array($greetingVal, $greetings)) { | |
6ecbca5b | 492 | throw new API_Exception(ts('Invalid %1 greeting', array(1 => $key))); |
6a488035 TO |
493 | } |
494 | ||
495 | $greetingId = CRM_Utils_Array::key($greetingVal, $greetings); | |
496 | } | |
497 | ||
498 | if ($customGreeting) { | |
499 | $greetingId = CRM_Utils_Array::key('Customized', $greetings); | |
500 | } | |
501 | ||
35671d00 | 502 | $customValue = isset($params['contact_id']) ? CRM_Core_DAO::getFieldValue( |
ad3f841d DL |
503 | 'CRM_Contact_DAO_Contact', |
504 | $params['contact_id'], | |
505 | "{$key}{$greeting}_custom" | |
35671d00 | 506 | ) : FALSE; |
6a488035 TO |
507 | |
508 | if (array_key_exists("{$key}{$greeting}_id", $params) && empty($params["{$key}{$greeting}_id"])) { | |
509 | $nullValue = TRUE; | |
510 | } | |
511 | elseif (array_key_exists("{$key}{$greeting}", $params) && empty($params["{$key}{$greeting}"])) { | |
512 | $nullValue = TRUE; | |
513 | } | |
514 | elseif ($customValue && array_key_exists("{$key}{$greeting}_custom", $params) | |
515 | && empty($params["{$key}{$greeting}_custom"]) | |
516 | ) { | |
517 | $nullValue = TRUE; | |
518 | } | |
519 | ||
520 | $params["{$key}{$greeting}_id"] = $greetingId; | |
521 | ||
522 | if (!$customValue && !$customGreeting && array_key_exists("{$key}{$greeting}_custom", $params)) { | |
523 | unset($params["{$key}{$greeting}_custom"]); | |
524 | } | |
525 | ||
526 | if ($nullValue) { | |
527 | $params["{$key}{$greeting}_id"] = ''; | |
528 | $params["{$key}{$greeting}_custom"] = ''; | |
529 | } | |
530 | ||
531 | if (isset($params["{$key}{$greeting}"])) { | |
532 | unset($params["{$key}{$greeting}"]); | |
533 | } | |
534 | } | |
535 | } | |
536 | ||
537 | /** | |
9d32e6f7 | 538 | * Old contact quick search api. |
6a488035 | 539 | * |
03f32517 | 540 | * @deprecated |
6a488035 | 541 | * |
d0997921 | 542 | * @param array $params |
9d32e6f7 | 543 | * |
645ee340 EM |
544 | * @return array |
545 | * @throws \API_Exception | |
6a488035 | 546 | */ |
6a488035 TO |
547 | function civicrm_api3_contact_getquick($params) { |
548 | civicrm_api3_verify_mandatory($params, NULL, array('name')); | |
aca85468 | 549 | $name = CRM_Utils_Type::escape(CRM_Utils_Array::value('name', $params), 'String'); |
6a488035 TO |
550 | |
551 | // get the autocomplete options from settings | |
552 | $acpref = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
553 | CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
554 | 'contact_autocomplete_options' | |
555 | ) | |
556 | ); | |
557 | ||
558 | // get the option values for contact autocomplete | |
559 | $acOptions = CRM_Core_OptionGroup::values('contact_autocomplete_options', FALSE, FALSE, FALSE, NULL, 'name'); | |
560 | ||
561 | $list = array(); | |
562 | foreach ($acpref as $value) { | |
8cc574cf | 563 | if ($value && !empty($acOptions[$value])) { |
6a488035 TO |
564 | $list[$value] = $acOptions[$value]; |
565 | } | |
566 | } | |
567 | // If we are doing quicksearch by a field other than name, make sure that field is added to results | |
568 | if (!empty($params['field_name'])) { | |
1aba4d9a | 569 | $field_name = CRM_Utils_String::munge($params['field_name']); |
5d8ba7a7 | 570 | // Unique name contact_id = id |
1aba4d9a CW |
571 | if ($field_name == 'contact_id') { |
572 | $field_name = 'id'; | |
5d8ba7a7 | 573 | } |
6a488035 | 574 | // phone_numeric should be phone |
1aba4d9a | 575 | $searchField = str_replace('_numeric', '', $field_name); |
22e263ad | 576 | if (!in_array($searchField, $list)) { |
6a488035 TO |
577 | $list[] = $searchField; |
578 | } | |
579 | } | |
580 | ||
581 | $select = $actualSelectElements = array('sort_name'); | |
582 | $where = ''; | |
583 | $from = array(); | |
584 | foreach ($list as $value) { | |
585 | $suffix = substr($value, 0, 2) . substr($value, -1); | |
586 | switch ($value) { | |
587 | case 'street_address': | |
588 | case 'city': | |
589 | case 'postal_code': | |
590 | $selectText = $value; | |
591 | $value = "address"; | |
592 | $suffix = 'sts'; | |
593 | case 'phone': | |
594 | case 'email': | |
595 | $actualSelectElements[] = $select[] = ($value == 'address') ? $selectText : $value; | |
aebdef3c JL |
596 | if ($value == 'phone') { |
597 | $actualSelectElements[] = $select[] = 'phone_ext'; | |
598 | } | |
6a488035 TO |
599 | $from[$value] = "LEFT JOIN civicrm_{$value} {$suffix} ON ( cc.id = {$suffix}.contact_id AND {$suffix}.is_primary = 1 ) "; |
600 | break; | |
601 | ||
602 | case 'country': | |
603 | case 'state_province': | |
604 | $select[] = "{$suffix}.name as {$value}"; | |
605 | $actualSelectElements[] = "{$suffix}.name"; | |
606 | if (!in_array('address', $from)) { | |
607 | $from['address'] = 'LEFT JOIN civicrm_address sts ON ( cc.id = sts.contact_id AND sts.is_primary = 1) '; | |
608 | } | |
609 | $from[$value] = " LEFT JOIN civicrm_{$value} {$suffix} ON ( sts.{$value}_id = {$suffix}.id ) "; | |
610 | break; | |
611 | ||
612 | default: | |
613 | if ($value != 'id') { | |
614 | $suffix = 'cc'; | |
615 | if (!empty($params['field_name']) && $params['field_name'] == 'value') { | |
133da98d | 616 | $suffix = CRM_Utils_String::munge(CRM_Utils_Array::value('table_name', $params, 'cc')); |
6a488035 TO |
617 | } |
618 | $actualSelectElements[] = $select[] = $suffix . '.' . $value; | |
619 | } | |
620 | break; | |
621 | } | |
622 | } | |
623 | ||
624 | $config = CRM_Core_Config::singleton(); | |
625 | $as = $select; | |
626 | $select = implode(', ', $select); | |
627 | if (!empty($select)) { | |
628 | $select = ", $select"; | |
629 | } | |
630 | $actualSelectElements = implode(', ', $actualSelectElements); | |
631 | $selectAliases = $from; | |
632 | unset($selectAliases['address']); | |
633 | $selectAliases = implode(', ', array_keys($selectAliases)); | |
634 | if (!empty($selectAliases)) { | |
635 | $selectAliases = ", $selectAliases"; | |
636 | } | |
637 | $from = implode(' ', $from); | |
133da98d CW |
638 | $limit = (int) CRM_Utils_Array::value('limit', $params); |
639 | $limit = $limit > 0 ? $limit : 10; | |
6a488035 TO |
640 | |
641 | // add acl clause here | |
642 | list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause('cc'); | |
643 | ||
644 | if ($aclWhere) { | |
645 | $where .= " AND $aclWhere "; | |
646 | } | |
647 | ||
a7488080 | 648 | if (!empty($params['org'])) { |
6a488035 TO |
649 | $where .= " AND contact_type = \"Organization\""; |
650 | ||
651 | // CRM-7157, hack: get current employer details when | |
652 | // employee_id is present. | |
653 | $currEmpDetails = array(); | |
a7488080 | 654 | if (!empty($params['employee_id'])) { |
6a488035 | 655 | if ($currentEmployer = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', |
133da98d | 656 | (int) $params['employee_id'], |
6a488035 TO |
657 | 'employer_id' |
658 | )) { | |
659 | if ($config->includeWildCardInName) { | |
660 | $strSearch = "%$name%"; | |
661 | } | |
662 | else { | |
663 | $strSearch = "$name%"; | |
664 | } | |
665 | ||
666 | // get current employer details | |
667 | $dao = CRM_Core_DAO::executeQuery("SELECT cc.id as id, CONCAT_WS( ' :: ', {$actualSelectElements} ) as data, sort_name | |
668 | FROM civicrm_contact cc {$from} WHERE cc.contact_type = \"Organization\" AND cc.id = {$currentEmployer} AND cc.sort_name LIKE '$strSearch'"); | |
669 | if ($dao->fetch()) { | |
670 | $currEmpDetails = array( | |
671 | 'id' => $dao->id, | |
672 | 'data' => $dao->data, | |
673 | ); | |
674 | } | |
675 | } | |
676 | } | |
677 | } | |
678 | ||
a7488080 | 679 | if (!empty($params['contact_sub_type'])) { |
69164898 N |
680 | $contactSubType = CRM_Utils_Type::escape($params['contact_sub_type'], 'String'); |
681 | $where .= " AND cc.contact_sub_type = '{$contactSubType}'"; | |
682 | } | |
683 | ||
e1b717cb P |
684 | if (!empty($params['contact_type'])) { |
685 | $contactType = CRM_Utils_Type::escape($params['contact_type'], 'String'); | |
686 | $where .= " AND cc.contact_type LIKE '{$contactType}'"; | |
687 | } | |
688 | ||
6a488035 | 689 | //set default for current_employer or return contact with particular id |
a7488080 | 690 | if (!empty($params['id'])) { |
1aba4d9a | 691 | $where .= " AND cc.id = " . (int) $params['id']; |
6a488035 TO |
692 | } |
693 | ||
a7488080 | 694 | if (!empty($params['cid'])) { |
1aba4d9a | 695 | $where .= " AND cc.id <> " . (int) $params['cid']; |
6a488035 TO |
696 | } |
697 | ||
698 | //contact's based of relationhip type | |
699 | $relType = NULL; | |
a7488080 | 700 | if (!empty($params['rel'])) { |
6a488035 TO |
701 | $relation = explode('_', CRM_Utils_Array::value('rel', $params)); |
702 | $relType = CRM_Utils_Type::escape($relation[0], 'Integer'); | |
703 | $rel = CRM_Utils_Type::escape($relation[2], 'String'); | |
704 | } | |
705 | ||
706 | if ($config->includeWildCardInName) { | |
707 | $strSearch = "%$name%"; | |
708 | } | |
709 | else { | |
710 | $strSearch = "$name%"; | |
711 | } | |
712 | $includeEmailFrom = $includeNickName = $exactIncludeNickName = ''; | |
713 | if ($config->includeNickNameInName) { | |
714 | $includeNickName = " OR nick_name LIKE '$strSearch'"; | |
715 | $exactIncludeNickName = " OR nick_name LIKE '$name'"; | |
716 | } | |
717 | ||
718 | //CRM-10687 | |
719 | if (!empty($params['field_name']) && !empty($params['table_name'])) { | |
1aba4d9a | 720 | $table_name = CRM_Utils_String::munge($params['table_name']); |
8d3b1aa6 PC |
721 | $whereClause = " WHERE ( $table_name.$field_name LIKE '$strSearch') {$where}"; |
722 | $exactWhereClause = " WHERE ( $table_name.$field_name = '$name') {$where}"; | |
6a488035 TO |
723 | // Search by id should be exact |
724 | if ($field_name == 'id' || $field_name == 'external_identifier') { | |
725 | $whereClause = $exactWhereClause; | |
726 | } | |
727 | } | |
728 | else { | |
729 | if ($config->includeEmailInName) { | |
730 | if (!in_array('email', $list)) { | |
731 | $includeEmailFrom = "LEFT JOIN civicrm_email eml ON ( cc.id = eml.contact_id AND eml.is_primary = 1 )"; | |
732 | } | |
733 | $whereClause = " WHERE ( email LIKE '$strSearch' OR sort_name LIKE '$strSearch' $includeNickName ) {$where} "; | |
734 | $exactWhereClause = " WHERE ( email LIKE '$name' OR sort_name LIKE '$name' $exactIncludeNickName ) {$where} "; | |
735 | } | |
736 | else { | |
737 | $whereClause = " WHERE ( sort_name LIKE '$strSearch' $includeNickName ) {$where} "; | |
738 | $exactWhereClause = " WHERE ( sort_name LIKE '$name' $exactIncludeNickName ) {$where} "; | |
739 | } | |
740 | } | |
741 | ||
742 | $additionalFrom = ''; | |
743 | if ($relType) { | |
744 | $additionalFrom = " | |
745 | INNER JOIN civicrm_relationship_type r ON ( | |
746 | r.id = {$relType} | |
747 | AND ( cc.contact_type = r.contact_type_{$rel} OR r.contact_type_{$rel} IS NULL ) | |
748 | AND ( cc.contact_sub_type = r.contact_sub_type_{$rel} OR r.contact_sub_type_{$rel} IS NULL ) | |
749 | )"; | |
750 | } | |
751 | ||
752 | // check if only CMS users are requested | |
a7488080 | 753 | if (!empty($params['cmsuser'])) { |
6a488035 TO |
754 | $additionalFrom = " |
755 | INNER JOIN civicrm_uf_match um ON (um.contact_id=cc.id) | |
756 | "; | |
757 | } | |
758 | ||
51e61eae ARW |
759 | $orderByInner = ""; |
760 | $orderByOuter = "ORDER BY exactFirst"; | |
761 | if ($config->includeOrderByClause) { | |
762 | $orderByInner = "ORDER BY sort_name"; | |
763 | $orderByOuter .= ", sort_name"; | |
764 | } | |
765 | ||
6a488035 TO |
766 | //CRM-5954 |
767 | $query = " | |
768 | SELECT DISTINCT(id), data, sort_name {$selectAliases} | |
769 | FROM ( | |
770 | ( SELECT 0 as exactFirst, cc.id as id, CONCAT_WS( ' :: ', {$actualSelectElements} ) as data {$select} | |
771 | FROM civicrm_contact cc {$from} | |
772 | {$aclFrom} | |
773 | {$additionalFrom} {$includeEmailFrom} | |
774 | {$exactWhereClause} | |
775 | LIMIT 0, {$limit} ) | |
776 | UNION | |
777 | ( SELECT 1 as exactFirst, cc.id as id, CONCAT_WS( ' :: ', {$actualSelectElements} ) as data {$select} | |
778 | FROM civicrm_contact cc {$from} | |
779 | {$aclFrom} | |
780 | {$additionalFrom} {$includeEmailFrom} | |
781 | {$whereClause} | |
51e61eae | 782 | {$orderByInner} |
6a488035 TO |
783 | LIMIT 0, {$limit} ) |
784 | ) t | |
51e61eae | 785 | {$orderByOuter} |
6a488035 TO |
786 | LIMIT 0, {$limit} |
787 | "; | |
788 | // send query to hook to be modified if needed | |
789 | CRM_Utils_Hook::contactListQuery($query, | |
790 | $name, | |
133da98d CW |
791 | empty($params['context']) ? NULL : CRM_Utils_Type::escape($params['context'], 'String'), |
792 | empty($params['id']) ? NULL : $params['id'] | |
6a488035 TO |
793 | ); |
794 | ||
795 | $dao = CRM_Core_DAO::executeQuery($query); | |
796 | ||
797 | $contactList = array(); | |
798 | $listCurrentEmployer = TRUE; | |
799 | while ($dao->fetch()) { | |
800 | $t = array('id' => $dao->id); | |
801 | foreach ($as as $k) { | |
35671d00 | 802 | $t[$k] = isset($dao->$k) ? $dao->$k : ''; |
6a488035 TO |
803 | } |
804 | $t['data'] = $dao->data; | |
805 | $contactList[] = $t; | |
a7488080 | 806 | if (!empty($params['org']) && |
6a488035 TO |
807 | !empty($currEmpDetails) && |
808 | $dao->id == $currEmpDetails['id'] | |
809 | ) { | |
810 | $listCurrentEmployer = FALSE; | |
811 | } | |
812 | } | |
813 | ||
814 | //return organization name if doesn't exist in db | |
815 | if (empty($contactList)) { | |
a7488080 | 816 | if (!empty($params['org'])) { |
6a488035 TO |
817 | if ($listCurrentEmployer && !empty($currEmpDetails)) { |
818 | $contactList = array( | |
819 | array( | |
d5cc0fc2 | 820 | 'data' => $currEmpDetails['data'], |
21dfd5f5 TO |
821 | 'id' => $currEmpDetails['id'], |
822 | ), | |
6a488035 TO |
823 | ); |
824 | } | |
825 | else { | |
826 | $contactList = array( | |
827 | array( | |
828 | 'data' => $name, | |
21dfd5f5 TO |
829 | 'id' => $name, |
830 | ), | |
6a488035 TO |
831 | ); |
832 | } | |
833 | } | |
834 | } | |
835 | ||
a14e9d08 CW |
836 | return civicrm_api3_create_success($contactList, $params, 'contact', 'getquick'); |
837 | } | |
838 | ||
839 | /** | |
35823763 EM |
840 | * Declare deprecated api functions. |
841 | * | |
a14e9d08 | 842 | * @deprecated api notice |
a6c01b45 | 843 | * @return array |
16b10e64 | 844 | * Array of deprecated actions |
a14e9d08 CW |
845 | */ |
846 | function _civicrm_api3_contact_deprecation() { | |
847 | return array('getquick' => 'The "getquick" action is deprecated in favor of "getlist".'); | |
6a488035 TO |
848 | } |
849 | ||
850 | /** | |
851 | * Merges given pair of duplicate contacts. | |
852 | * | |
cf470720 | 853 | * @param array $params |
b081365f CW |
854 | * Allowed array keys are: |
855 | * -int main_id: main contact id with whom merge has to happen | |
856 | * -int other_id: duplicate contact which would be deleted after merge operation | |
857 | * -string mode: "safe" skips the merge if there are no conflicts. Does a force merge otherwise. | |
858 | * -boolean auto_flip: whether to let api decide which contact to retain and which to delete. | |
6a488035 | 859 | * |
a6c01b45 | 860 | * @return array |
72b3a70c | 861 | * API Result Array |
6a488035 TO |
862 | */ |
863 | function civicrm_api3_contact_merge($params) { | |
864 | $mode = CRM_Utils_Array::value('mode', $params, 'safe'); | |
865 | $autoFlip = CRM_Utils_Array::value('auto_flip', $params, TRUE); | |
866 | ||
35671d00 TO |
867 | $dupePairs = array(array( |
868 | 'srcID' => CRM_Utils_Array::value('main_id', $params), | |
6a488035 TO |
869 | 'dstID' => CRM_Utils_Array::value('other_id', $params), |
870 | )); | |
871 | $result = CRM_Dedupe_Merger::merge($dupePairs, array(), $mode, $autoFlip); | |
872 | ||
873 | if ($result['is_error'] == 0) { | |
874 | return civicrm_api3_create_success(); | |
875 | } | |
876 | else { | |
877 | return civicrm_api3_create_error($result['messages']); | |
878 | } | |
879 | } | |
880 | ||
aa1b1481 | 881 | /** |
9d32e6f7 EM |
882 | * Adjust metadata for contact_proximity api function. |
883 | * | |
c490a46a | 884 | * @param array $params |
aa1b1481 | 885 | */ |
6a488035 TO |
886 | function _civicrm_api3_contact_proximity_spec(&$params) { |
887 | $params['latitude']['api.required'] = 1; | |
4c41ecb2 | 888 | $params['latitude']['title'] = 'Latitude'; |
6a488035 | 889 | $params['longitude']['api.required'] = 1; |
4c41ecb2 | 890 | $params['longitude']['title'] = 'Longitude'; |
6a488035 | 891 | $params['unit']['api.default'] = 'meter'; |
4c41ecb2 | 892 | $params['unit']['title'] = 'Unit of Measurement'; |
6a488035 TO |
893 | } |
894 | ||
aa1b1481 | 895 | /** |
9d32e6f7 EM |
896 | * Get contacts by proximity. |
897 | * | |
c490a46a | 898 | * @param array $params |
aa1b1481 EM |
899 | * |
900 | * @return array | |
901 | * @throws Exception | |
902 | */ | |
6a488035 TO |
903 | function civicrm_api3_contact_proximity($params) { |
904 | $latitude = CRM_Utils_Array::value('latitude', $params); | |
905 | $longitude = CRM_Utils_Array::value('longitude', $params); | |
906 | $distance = CRM_Utils_Array::value('distance', $params); | |
907 | ||
908 | $unit = CRM_Utils_Array::value('unit', $params); | |
909 | ||
910 | // check and ensure that lat/long and distance are floats | |
911 | if ( | |
912 | !CRM_Utils_Rule::numeric($latitude) || | |
913 | !CRM_Utils_Rule::numeric($longitude) || | |
914 | !CRM_Utils_Rule::numeric($distance) | |
915 | ) { | |
916 | throw new Exception(ts('Latitude, Longitude and Distance should exist and be numeric')); | |
917 | } | |
918 | ||
919 | if ($unit == "mile") { | |
920 | $conversionFactor = 1609.344; | |
921 | } | |
922 | else { | |
923 | $conversionFactor = 1000; | |
924 | } | |
925 | //Distance in meters | |
926 | $distance = $distance * $conversionFactor; | |
927 | ||
928 | $whereClause = CRM_Contact_BAO_ProximityQuery::where($latitude, $longitude, $distance); | |
929 | ||
930 | $query = " | |
931 | SELECT civicrm_contact.id as contact_id, | |
932 | civicrm_contact.display_name as display_name | |
933 | FROM civicrm_contact | |
934 | LEFT JOIN civicrm_address ON civicrm_contact.id = civicrm_address.contact_id | |
935 | WHERE $whereClause | |
936 | "; | |
937 | ||
938 | $dao = CRM_Core_DAO::executeQuery($query); | |
939 | $contacts = array(); | |
940 | while ($dao->fetch()) { | |
941 | $contacts[] = $dao->toArray(); | |
942 | } | |
943 | ||
944 | return civicrm_api3_create_success($contacts, $params, 'contact', 'get_by_location', $dao); | |
945 | } | |
946 | ||
ff88d165 CW |
947 | |
948 | /** | |
22242c87 EM |
949 | * Get parameters for getlist function. |
950 | * | |
a6c6059d | 951 | * @see _civicrm_api3_generic_getlist_params |
ff88d165 | 952 | * |
8c6b335b | 953 | * @param array $request |
ff88d165 CW |
954 | */ |
955 | function _civicrm_api3_contact_getlist_params(&$request) { | |
956 | // get the autocomplete options from settings | |
957 | $acpref = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
958 | CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
959 | 'contact_autocomplete_options' | |
960 | ) | |
961 | ); | |
962 | ||
963 | // get the option values for contact autocomplete | |
964 | $acOptions = CRM_Core_OptionGroup::values('contact_autocomplete_options', FALSE, FALSE, FALSE, NULL, 'name'); | |
965 | ||
966 | $list = array(); | |
967 | foreach ($acpref as $value) { | |
968 | if ($value && !empty($acOptions[$value])) { | |
969 | $list[] = $acOptions[$value]; | |
970 | } | |
971 | } | |
972 | // If we are doing quicksearch by a field other than name, make sure that field is added to results | |
973 | $field_name = CRM_Utils_String::munge($request['search_field']); | |
974 | // Unique name contact_id = id | |
975 | if ($field_name == 'contact_id') { | |
976 | $field_name = 'id'; | |
977 | } | |
978 | // phone_numeric should be phone | |
979 | $searchField = str_replace('_numeric', '', $field_name); | |
22e263ad | 980 | if (!in_array($searchField, $list)) { |
ff88d165 CW |
981 | $list[] = $searchField; |
982 | } | |
8250601e | 983 | $request['description_field'] = $list; |
54bee7df | 984 | $list[] = 'contact_type'; |
8250601e | 985 | $request['params']['return'] = array_unique(array_merge($list, $request['extra'])); |
ff88d165 CW |
986 | $request['params']['options']['sort'] = 'sort_name'; |
987 | // Contact api doesn't support array(LIKE => 'foo') syntax | |
609a8c53 | 988 | if (!empty($request['input'])) { |
fd816db5 | 989 | $request['params'][$request['search_field']] = $request['input']; |
609a8c53 | 990 | } |
ff88d165 CW |
991 | } |
992 | ||
993 | /** | |
22242c87 EM |
994 | * Get output for getlist function. |
995 | * | |
a6c6059d | 996 | * @see _civicrm_api3_generic_getlist_output |
ff88d165 | 997 | * |
8c6b335b CW |
998 | * @param array $result |
999 | * @param array $request | |
ff88d165 CW |
1000 | * |
1001 | * @return array | |
1002 | */ | |
1003 | function _civicrm_api3_contact_getlist_output($result, $request) { | |
1004 | $output = array(); | |
1005 | if (!empty($result['values'])) { | |
dc64d047 EM |
1006 | $addressFields = array_intersect(array( |
1007 | 'street_address', | |
1008 | 'city', | |
1009 | 'state_province', | |
1010 | 'country', | |
1011 | ), | |
1012 | $request['params']['return']); | |
ff88d165 CW |
1013 | foreach ($result['values'] as $row) { |
1014 | $data = array( | |
1015 | 'id' => $row[$request['id_field']], | |
1016 | 'label' => $row[$request['label_field']], | |
88881f79 | 1017 | 'description' => array(), |
ff88d165 | 1018 | ); |
8250601e CW |
1019 | foreach ($request['description_field'] as $item) { |
1020 | if (!strpos($item, '_name') && !in_array($item, $addressFields) && !empty($row[$item])) { | |
88881f79 | 1021 | $data['description'][] = $row[$item]; |
ff88d165 CW |
1022 | } |
1023 | } | |
88881f79 | 1024 | $address = array(); |
22e263ad | 1025 | foreach ($addressFields as $item) { |
88881f79 CW |
1026 | if (!empty($row[$item])) { |
1027 | $address[] = $row[$item]; | |
1028 | } | |
1029 | } | |
1030 | if ($address) { | |
1031 | $data['description'][] = implode(' ', $address); | |
1032 | } | |
ff88d165 CW |
1033 | if (!empty($request['image_field'])) { |
1034 | $data['image'] = isset($row[$request['image_field']]) ? $row[$request['image_field']] : ''; | |
54bee7df CW |
1035 | } |
1036 | else { | |
1037 | $data['icon_class'] = $row['contact_type']; | |
1038 | } | |
8250601e CW |
1039 | foreach ($request['extra'] as $field) { |
1040 | $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL; | |
1041 | } | |
ff88d165 CW |
1042 | $output[] = $data; |
1043 | } | |
1044 | } | |
1045 | return $output; | |
1046 | } |