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