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