Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 | 2 | /* |
a30c801b TO |
3 | +--------------------------------------------------------------------+ |
4 | | Copyright CiviCRM LLC. All rights reserved. | | |
5 | | | | |
6 | | This work is published under the GNU AGPLv3 license with some | | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
9 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
c28e1768 | 13 | * This api exposes CiviCRM contacts. |
244bbdd8 | 14 | * |
c28e1768 CW |
15 | * Contacts are the main entity in CiviCRM and this api is more robust than most. |
16 | * - Get action allows all params supported by advanced search. | |
b081365f | 17 | * - Create action allows creating several related entities at once (e.g. email). |
c28e1768 CW |
18 | * - Create allows checking for duplicate contacts. |
19 | * Use getfields to list the full range of parameters and options supported by each action. | |
6a488035 TO |
20 | * |
21 | * @package CiviCRM_APIv3 | |
6a488035 TO |
22 | */ |
23 | ||
24 | /** | |
244bbdd8 | 25 | * Create or update a Contact. |
6a488035 | 26 | * |
cf470720 TO |
27 | * @param array $params |
28 | * Input parameters. | |
6a488035 | 29 | * |
77b97be7 | 30 | * @throws API_Exception |
6a488035 | 31 | * |
a6c01b45 | 32 | * @return array |
72b3a70c | 33 | * API Result Array |
6a488035 TO |
34 | */ |
35 | function civicrm_api3_contact_create($params) { | |
6a488035 | 36 | $contactID = CRM_Utils_Array::value('contact_id', $params, CRM_Utils_Array::value('id', $params)); |
c16ed19b CW |
37 | |
38 | if ($contactID && !empty($params['check_permissions']) && !CRM_Contact_BAO_Contact_Permission::allow($contactID, CRM_Core_Permission::EDIT)) { | |
39 | throw new \Civi\API\Exception\UnauthorizedException('Permission denied to modify contact record'); | |
40 | } | |
41 | ||
fedc3428 | 42 | if (!empty($params['dupe_check'])) { |
59ca5f92 | 43 | $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised', [], $params['check_permission']); |
fedc3428 | 44 | if (count($ids) > 0) { |
59ca5f92 | 45 | throw new API_Exception("Found matching contacts: " . implode(',', $ids), "duplicate", ["ids" => $ids]); |
fedc3428 | 46 | } |
47 | } | |
48 | ||
49 | $values = _civicrm_api3_contact_check_params($params); | |
6a488035 TO |
50 | if ($values) { |
51 | return $values; | |
52 | } | |
53 | ||
54 | if (!$contactID) { | |
55 | // If we get here, we're ready to create a new contact | |
56 | if (($email = CRM_Utils_Array::value('email', $params)) && !is_array($params['email'])) { | |
57 | $defLocType = CRM_Core_BAO_LocationType::getDefault(); | |
59ca5f92 | 58 | $params['email'] = [ |
59 | 1 => [ | |
d5cc0fc2 | 60 | 'email' => $email, |
6a488035 TO |
61 | 'is_primary' => 1, |
62 | 'location_type_id' => ($defLocType->id) ? $defLocType->id : 1, | |
59ca5f92 | 63 | ], |
64 | ]; | |
6a488035 TO |
65 | } |
66 | } | |
67 | ||
68 | if (!empty($params['home_url'])) { | |
cbf48754 | 69 | $websiteTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'); |
59ca5f92 | 70 | $params['website'] = [ |
71 | 1 => [ | |
d5cc0fc2 | 72 | 'website_type_id' => key($websiteTypes), |
6a488035 | 73 | 'url' => $params['home_url'], |
59ca5f92 | 74 | ], |
75 | ]; | |
6a488035 TO |
76 | } |
77 | ||
6ecbca5b | 78 | _civicrm_api3_greeting_format_params($params); |
6a488035 | 79 | |
59ca5f92 | 80 | $values = []; |
6a488035 | 81 | |
6ecbca5b | 82 | if (empty($params['contact_type']) && $contactID) { |
83 | $params['contact_type'] = CRM_Contact_BAO_Contact::getContactType($contactID); | |
6a488035 TO |
84 | } |
85 | ||
6ecbca5b | 86 | if (!isset($params['contact_sub_type']) && $contactID) { |
87 | $params['contact_sub_type'] = CRM_Contact_BAO_Contact::getContactSubType($contactID); | |
6a488035 TO |
88 | } |
89 | ||
6ecbca5b | 90 | _civicrm_api3_custom_format_params($params, $values, $params['contact_type'], $contactID); |
6a488035 TO |
91 | |
92 | $params = array_merge($params, $values); | |
6ecbca5b | 93 | //@todo we should just call basic_create here - but need to make contact:create accept 'id' on the bao |
6a488035 TO |
94 | $contact = _civicrm_api3_contact_update($params, $contactID); |
95 | ||
96 | if (is_a($contact, 'CRM_Core_Error')) { | |
6ecbca5b | 97 | throw new API_Exception($contact->_errors[0]['message']); |
6a488035 TO |
98 | } |
99 | else { | |
59ca5f92 | 100 | $values = []; |
6a488035 TO |
101 | _civicrm_api3_object_to_array_unique_fields($contact, $values[$contact->id]); |
102 | } | |
103 | ||
104 | return civicrm_api3_create_success($values, $params, 'Contact', 'create'); | |
105 | } | |
106 | ||
11e09c59 | 107 | /** |
0aa0303c | 108 | * Adjust Metadata for Create action. |
6a488035 | 109 | * |
cf470720 | 110 | * @param array $params |
b081365f | 111 | * Array of parameters determined by getfields. |
6a488035 TO |
112 | */ |
113 | function _civicrm_api3_contact_create_spec(&$params) { | |
114 | $params['contact_type']['api.required'] = 1; | |
59ca5f92 | 115 | $params['id']['api.aliases'] = ['contact_id']; |
116 | $params['current_employer'] = [ | |
6a488035 TO |
117 | 'title' => 'Current Employer', |
118 | 'description' => 'Name of Current Employer', | |
9c5991b3 | 119 | 'type' => CRM_Utils_Type::T_STRING, |
59ca5f92 | 120 | ]; |
121 | $params['dupe_check'] = [ | |
0391dc25 | 122 | 'title' => 'Check for Duplicates', |
123 | 'description' => 'Throw error if contact create matches dedupe rule', | |
d142432b | 124 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
59ca5f92 | 125 | ]; |
126 | $params['skip_greeting_processing'] = [ | |
d29b0fe7 | 127 | 'title' => 'Skip Greeting processing', |
128 | 'description' => 'Do not process greetings, (these can be done by scheduled job and there may be a preference to do so for performance reasons)', | |
129 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
130 | 'api.default' => 0, | |
59ca5f92 | 131 | ]; |
132 | $params['prefix_id']['api.aliases'] = [ | |
133 | 'individual_prefix', | |
134 | 'individual_prefix_id', | |
135 | ]; | |
136 | $params['suffix_id']['api.aliases'] = [ | |
137 | 'individual_suffix', | |
138 | 'individual_suffix_id', | |
139 | ]; | |
140 | $params['gender_id']['api.aliases'] = ['gender']; | |
6a488035 TO |
141 | } |
142 | ||
143 | /** | |
61fe4988 EM |
144 | * Retrieve one or more contacts, given a set of search params. |
145 | * | |
146 | * @param array $params | |
6a488035 | 147 | * |
a6c01b45 | 148 | * @return array |
72b3a70c | 149 | * API Result Array |
b69793be | 150 | * |
151 | * @throws \API_Exception | |
6a488035 TO |
152 | */ |
153 | function civicrm_api3_contact_get($params) { | |
59ca5f92 | 154 | $options = []; |
6a488035 | 155 | _civicrm_api3_contact_get_supportanomalies($params, $options); |
244bbdd8 | 156 | $contacts = _civicrm_api3_get_using_query_object('Contact', $params, $options); |
dc2add4f SL |
157 | if (!empty($params['check_permissions'])) { |
158 | CRM_Contact_BAO_Contact::unsetProtectedFields($contacts); | |
c10bca20 | 159 | } |
dc2add4f | 160 | return civicrm_api3_create_success($contacts, $params, 'Contact'); |
c10bca20 TO |
161 | } |
162 | ||
aa1b1481 | 163 | /** |
244bbdd8 | 164 | * Get number of contacts matching the supplied criteria. |
61fe4988 | 165 | * |
c490a46a | 166 | * @param array $params |
aa1b1481 EM |
167 | * |
168 | * @return int | |
169 | */ | |
6a488035 | 170 | function civicrm_api3_contact_getcount($params) { |
59ca5f92 | 171 | $options = []; |
6a488035 | 172 | _civicrm_api3_contact_get_supportanomalies($params, $options); |
244bbdd8 | 173 | $count = _civicrm_api3_get_using_query_object('Contact', $params, $options, 1); |
972322c5 | 174 | return (int) $count; |
6a488035 | 175 | } |
11e09c59 TO |
176 | |
177 | /** | |
9d32e6f7 | 178 | * Adjust Metadata for Get action. |
6a488035 | 179 | * |
cf470720 | 180 | * @param array $params |
b081365f | 181 | * Array of parameters determined by getfields. |
6a488035 TO |
182 | */ |
183 | function _civicrm_api3_contact_get_spec(&$params) { | |
184 | $params['contact_is_deleted']['api.default'] = 0; | |
185 | ||
9d32e6f7 | 186 | // We declare all these pseudoFields as there are other undocumented fields accessible |
6a488035 | 187 | // via the api - but if check permissions is set we only allow declared fields |
59ca5f92 | 188 | $params['address_id'] = [ |
d142432b EM |
189 | 'title' => 'Primary Address ID', |
190 | 'type' => CRM_Utils_Type::T_INT, | |
59ca5f92 | 191 | ]; |
192 | $params['street_address'] = [ | |
d142432b EM |
193 | 'title' => 'Primary Address Street Address', |
194 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 195 | ]; |
196 | $params['supplemental_address_1'] = [ | |
d142432b EM |
197 | 'title' => 'Primary Address Supplemental Address 1', |
198 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 199 | ]; |
200 | $params['supplemental_address_2'] = [ | |
d142432b EM |
201 | 'title' => 'Primary Address Supplemental Address 2', |
202 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 203 | ]; |
204 | $params['supplemental_address_3'] = [ | |
207f62c6 AS |
205 | 'title' => 'Primary Address Supplemental Address 3', |
206 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 207 | ]; |
208 | $params['current_employer'] = [ | |
d142432b EM |
209 | 'title' => 'Current Employer', |
210 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 211 | ]; |
212 | $params['city'] = [ | |
d142432b EM |
213 | 'title' => 'Primary Address City', |
214 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 215 | ]; |
216 | $params['postal_code_suffix'] = [ | |
d142432b EM |
217 | 'title' => 'Primary Address Post Code Suffix', |
218 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 219 | ]; |
220 | $params['postal_code'] = [ | |
d142432b EM |
221 | 'title' => 'Primary Address Post Code', |
222 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 223 | ]; |
224 | $params['geo_code_1'] = [ | |
d142432b EM |
225 | 'title' => 'Primary Address Latitude', |
226 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 227 | ]; |
228 | $params['geo_code_2'] = [ | |
d142432b EM |
229 | 'title' => 'Primary Address Longitude', |
230 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 231 | ]; |
232 | $params['state_province_id'] = [ | |
d142432b EM |
233 | 'title' => 'Primary Address State Province ID', |
234 | 'type' => CRM_Utils_Type::T_INT, | |
59ca5f92 | 235 | 'pseudoconstant' => [ |
3493947a | 236 | 'table' => 'civicrm_state_province', |
59ca5f92 | 237 | ], |
238 | ]; | |
239 | $params['state_province_name'] = [ | |
d142432b EM |
240 | 'title' => 'Primary Address State Province Name', |
241 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 242 | 'pseudoconstant' => [ |
3493947a | 243 | 'table' => 'civicrm_state_province', |
59ca5f92 | 244 | ], |
245 | ]; | |
246 | $params['state_province'] = [ | |
d142432b EM |
247 | 'title' => 'Primary Address State Province', |
248 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 249 | 'pseudoconstant' => [ |
3493947a | 250 | 'table' => 'civicrm_state_province', |
59ca5f92 | 251 | ], |
252 | ]; | |
253 | $params['country_id'] = [ | |
d142432b EM |
254 | 'title' => 'Primary Address Country ID', |
255 | 'type' => CRM_Utils_Type::T_INT, | |
59ca5f92 | 256 | 'pseudoconstant' => [ |
3493947a | 257 | 'table' => 'civicrm_country', |
59ca5f92 | 258 | ], |
259 | ]; | |
260 | $params['country'] = [ | |
d142432b EM |
261 | 'title' => 'Primary Address country', |
262 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 263 | 'pseudoconstant' => [ |
3493947a | 264 | 'table' => 'civicrm_country', |
59ca5f92 | 265 | ], |
266 | ]; | |
267 | $params['worldregion_id'] = [ | |
d142432b EM |
268 | 'title' => 'Primary Address World Region ID', |
269 | 'type' => CRM_Utils_Type::T_INT, | |
59ca5f92 | 270 | 'pseudoconstant' => [ |
3493947a | 271 | 'table' => 'civicrm_world_region', |
59ca5f92 | 272 | ], |
273 | ]; | |
274 | $params['worldregion'] = [ | |
d142432b EM |
275 | 'title' => 'Primary Address World Region', |
276 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 277 | 'pseudoconstant' => [ |
3493947a | 278 | 'table' => 'civicrm_world_region', |
59ca5f92 | 279 | ], |
280 | ]; | |
281 | $params['phone_id'] = [ | |
d142432b EM |
282 | 'title' => 'Primary Phone ID', |
283 | 'type' => CRM_Utils_Type::T_INT, | |
59ca5f92 | 284 | ]; |
285 | $params['phone'] = [ | |
d142432b EM |
286 | 'title' => 'Primary Phone', |
287 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 288 | ]; |
289 | $params['phone_type_id'] = [ | |
d142432b EM |
290 | 'title' => 'Primary Phone Type ID', |
291 | 'type' => CRM_Utils_Type::T_INT, | |
59ca5f92 | 292 | ]; |
293 | $params['provider_id'] = [ | |
d142432b EM |
294 | 'title' => 'Primary Phone Provider ID', |
295 | 'type' => CRM_Utils_Type::T_INT, | |
59ca5f92 | 296 | ]; |
297 | $params['email_id'] = [ | |
d142432b EM |
298 | 'title' => 'Primary Email ID', |
299 | 'type' => CRM_Utils_Type::T_INT, | |
59ca5f92 | 300 | ]; |
301 | $params['email'] = [ | |
d142432b EM |
302 | 'title' => 'Primary Email', |
303 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 304 | ]; |
305 | $params['on_hold'] = [ | |
d142432b EM |
306 | 'title' => 'Primary Email On Hold', |
307 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
59ca5f92 | 308 | ]; |
309 | $params['im'] = [ | |
d142432b EM |
310 | 'title' => 'Primary Instant Messenger', |
311 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 312 | ]; |
313 | $params['im_id'] = [ | |
d142432b EM |
314 | 'title' => 'Primary Instant Messenger ID', |
315 | 'type' => CRM_Utils_Type::T_INT, | |
59ca5f92 | 316 | ]; |
317 | $params['group'] = [ | |
985f4890 | 318 | 'title' => 'Group', |
59ca5f92 | 319 | 'pseudoconstant' => [ |
985f4890 | 320 | 'table' => 'civicrm_group', |
59ca5f92 | 321 | ], |
322 | ]; | |
323 | $params['tag'] = [ | |
985f4890 | 324 | 'title' => 'Tags', |
59ca5f92 | 325 | 'pseudoconstant' => [ |
985f4890 | 326 | 'table' => 'civicrm_tag', |
59ca5f92 | 327 | ], |
328 | ]; | |
329 | $params['uf_user'] = [ | |
8d475ce9 CW |
330 | 'title' => 'CMS User', |
331 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
59ca5f92 | 332 | ]; |
333 | $params['birth_date_low'] = [ | |
334 | 'name' => 'birth_date_low', | |
335 | 'type' => CRM_Utils_Type::T_DATE, | |
336 | 'title' => ts('Birth Date is equal to or greater than'), | |
337 | ]; | |
338 | $params['birth_date_high'] = [ | |
339 | 'name' => 'birth_date_high', | |
340 | 'type' => CRM_Utils_Type::T_DATE, | |
341 | 'title' => ts('Birth Date is equal to or less than'), | |
342 | ]; | |
343 | $params['deceased_date_low'] = [ | |
344 | 'name' => 'deceased_date_low', | |
345 | 'type' => CRM_Utils_Type::T_DATE, | |
346 | 'title' => ts('Deceased Date is equal to or greater than'), | |
347 | ]; | |
348 | $params['deceased_date_high'] = [ | |
349 | 'name' => 'deceased_date_high', | |
350 | 'type' => CRM_Utils_Type::T_DATE, | |
351 | 'title' => ts('Deceased Date is equal to or less than'), | |
352 | ]; | |
6a488035 TO |
353 | } |
354 | ||
11e09c59 | 355 | /** |
9d32e6f7 EM |
356 | * Support for historical oddities. |
357 | * | |
244bbdd8 | 358 | * We are supporting 'showAll' = 'all', 'trash' or 'active' for Contact get |
6a488035 TO |
359 | * and for getcount |
360 | * - hopefully some day we'll come up with a std syntax for the 3-way-boolean of | |
361 | * 0, 1 or not set | |
362 | * | |
363 | * We also support 'filter_group_id' & 'filter.group_id' | |
364 | * | |
cf470720 TO |
365 | * @param array $params |
366 | * As passed into api get or getcount function. | |
367 | * @param array $options | |
368 | * Array of options (so we can modify the filter). | |
6a488035 TO |
369 | */ |
370 | function _civicrm_api3_contact_get_supportanomalies(&$params, &$options) { | |
b69793be | 371 | if (!empty($params['email']) && !is_array($params['email'])) { |
372 | // Fix this to be in array format so the query object does not add LIKE | |
373 | // I think there is a better fix that I will do for master. | |
374 | $params['email'] = ['=' => $params['email']]; | |
375 | } | |
6a488035 TO |
376 | if (isset($params['showAll'])) { |
377 | if (strtolower($params['showAll']) == "active") { | |
378 | $params['contact_is_deleted'] = 0; | |
379 | } | |
380 | if (strtolower($params['showAll']) == "trash") { | |
381 | $params['contact_is_deleted'] = 1; | |
382 | } | |
383 | if (strtolower($params['showAll']) == "all" && isset($params['contact_is_deleted'])) { | |
384 | unset($params['contact_is_deleted']); | |
385 | } | |
386 | } | |
387 | // support for group filters | |
388 | if (array_key_exists('filter_group_id', $params)) { | |
389 | $params['filter.group_id'] = $params['filter_group_id']; | |
390 | unset($params['filter_group_id']); | |
391 | } | |
392 | // filter.group_id works both for 1,2,3 and array (1,2,3) | |
393 | if (array_key_exists('filter.group_id', $params)) { | |
394 | if (is_array($params['filter.group_id'])) { | |
395 | $groups = $params['filter.group_id']; | |
396 | } | |
4f99ca55 TO |
397 | else { |
398 | $groups = explode(',', $params['filter.group_id']); | |
35671d00 | 399 | } |
6a488035 | 400 | unset($params['filter.group_id']); |
6a488035 | 401 | $options['input_params']['group'] = $groups; |
51c4748e SL |
402 | } |
403 | if (isset($params['group'])) { | |
404 | $groups = $params['group']; | |
f639bf2f PN |
405 | $groupsByTitle = CRM_Core_PseudoConstant::group(); |
406 | $groupsByName = CRM_Contact_BAO_GroupContact::buildOptions('group_id', 'validate'); | |
407 | $allGroups = array_merge(array_flip($groupsByTitle), array_flip($groupsByName)); | |
51c4748e | 408 | if (is_array($groups) && in_array(key($groups), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { |
0c66b30c | 409 | // Get the groups array. |
51c4748e | 410 | $groupsArray = $groups[key($groups)]; |
0c66b30c | 411 | foreach ($groupsArray as &$group) { |
f639bf2f PN |
412 | if (!is_numeric($group) && !empty($allGroups[$group])) { |
413 | $group = $allGroups[$group]; | |
51c4748e SL |
414 | } |
415 | } | |
0c66b30c | 416 | // Now reset the $groups array with the ids not the titles. |
56272bee | 417 | $groups[key($groups)] = $groupsArray; |
51c4748e | 418 | } |
0c66b30c | 419 | // handle format like 'group' => array('title1', 'title2'). |
51c4748e SL |
420 | elseif (is_array($groups)) { |
421 | foreach ($groups as $k => &$group) { | |
f639bf2f PN |
422 | if (!is_numeric($group) && !empty($allGroups[$group])) { |
423 | $group = $allGroups[$group]; | |
51c4748e | 424 | } |
f639bf2f | 425 | if (!is_numeric($k) && !empty($allGroups[$k])) { |
b6b28d93 | 426 | unset($groups[$k]); |
f639bf2f | 427 | $groups[$allGroups[$k]] = $group; |
b6b28d93 | 428 | } |
51c4748e SL |
429 | } |
430 | } | |
59ca5f92 | 431 | elseif (!is_numeric($groups) && !empty($allGroups[$groups])) { |
f639bf2f | 432 | $groups = $allGroups[$groups]; |
51c4748e SL |
433 | } |
434 | $params['group'] = $groups; | |
6a488035 TO |
435 | } |
436 | } | |
437 | ||
438 | /** | |
244bbdd8 | 439 | * Delete a Contact with given contact_id. |
6a488035 | 440 | * |
cf470720 | 441 | * @param array $params |
c23f45d3 | 442 | * input parameters per getfields |
6a488035 | 443 | * |
c16ed19b | 444 | * @throws \Civi\API\Exception\UnauthorizedException |
a6c01b45 | 445 | * @return array |
72b3a70c | 446 | * API Result Array |
6a488035 TO |
447 | */ |
448 | function civicrm_api3_contact_delete($params) { | |
6a488035 TO |
449 | $contactID = CRM_Utils_Array::value('id', $params); |
450 | ||
c16ed19b CW |
451 | if (!empty($params['check_permissions']) && !CRM_Contact_BAO_Contact_Permission::allow($contactID, CRM_Core_Permission::DELETE)) { |
452 | throw new \Civi\API\Exception\UnauthorizedException('Permission denied to modify contact record'); | |
453 | } | |
454 | ||
6a488035 TO |
455 | $session = CRM_Core_Session::singleton(); |
456 | if ($contactID == $session->get('userID')) { | |
457 | return civicrm_api3_create_error('This contact record is linked to the currently logged in user account - and cannot be deleted.'); | |
458 | } | |
0d8afee2 CW |
459 | $restore = !empty($params['restore']) ? $params['restore'] : FALSE; |
460 | $skipUndelete = !empty($params['skip_undelete']) ? $params['skip_undelete'] : FALSE; | |
f182074e PN |
461 | |
462 | // CRM-12929 | |
463 | // restrict permanent delete if a contact has financial trxn associated with it | |
464 | $error = NULL; | |
59ca5f92 | 465 | if ($skipUndelete && CRM_Financial_BAO_FinancialItem::checkContactPresent([$contactID], $error)) { |
ad3f841d | 466 | return civicrm_api3_create_error($error['_qf_default']); |
f182074e | 467 | } |
fa6448fa EM |
468 | if (CRM_Contact_BAO_Contact::deleteContact($contactID, $restore, $skipUndelete, |
469 | CRM_Utils_Array::value('check_permissions', $params))) { | |
6a488035 TO |
470 | return civicrm_api3_create_success(); |
471 | } | |
472 | else { | |
473 | return civicrm_api3_create_error('Could not delete contact'); | |
474 | } | |
475 | } | |
476 | ||
aa1b1481 | 477 | /** |
9d32e6f7 EM |
478 | * Check parameters passed in. |
479 | * | |
480 | * This function is on it's way out. | |
481 | * | |
c490a46a | 482 | * @param array $params |
aa1b1481 EM |
483 | * |
484 | * @return null | |
485 | * @throws API_Exception | |
486 | * @throws CiviCRM_API3_Exception | |
487 | */ | |
fedc3428 | 488 | function _civicrm_api3_contact_check_params(&$params) { |
6a488035 TO |
489 | |
490 | switch (strtolower(CRM_Utils_Array::value('contact_type', $params))) { | |
491 | case 'household': | |
59ca5f92 | 492 | civicrm_api3_verify_mandatory($params, NULL, ['household_name']); |
6a488035 | 493 | break; |
35671d00 | 494 | |
6a488035 | 495 | case 'organization': |
59ca5f92 | 496 | civicrm_api3_verify_mandatory($params, NULL, ['organization_name']); |
6a488035 | 497 | break; |
35671d00 | 498 | |
6a488035 | 499 | case 'individual': |
59ca5f92 | 500 | civicrm_api3_verify_one_mandatory($params, NULL, [ |
7c31ae57 SL |
501 | 'first_name', |
502 | 'last_name', | |
503 | 'email', | |
504 | 'display_name', | |
505 | ]); | |
35671d00 | 506 | break; |
6a488035 TO |
507 | } |
508 | ||
8cc574cf | 509 | if (!empty($params['contact_sub_type']) && !empty($params['contact_type'])) { |
35671d00 TO |
510 | if (!(CRM_Contact_BAO_ContactType::isExtendsContactType($params['contact_sub_type'], $params['contact_type']))) { |
511 | throw new API_Exception("Invalid or Mismatched Contact Subtype: " . implode(', ', (array) $params['contact_sub_type'])); | |
6a488035 | 512 | } |
35671d00 | 513 | } |
6a488035 | 514 | |
8d99ab37 | 515 | // The BAO no longer supports the legacy param "current_employer" so here is a shim for api backward-compatability |
6a488035 | 516 | if (!empty($params['current_employer'])) { |
59ca5f92 | 517 | $organizationParams = [ |
8d99ab37 | 518 | 'organization_name' => $params['current_employer'], |
59ca5f92 | 519 | ]; |
6a488035 | 520 | |
59ca5f92 | 521 | $dupeIds = CRM_Contact_BAO_Contact::getDuplicateContacts($organizationParams, 'Organization', 'Supervised', [], FALSE); |
6a488035 TO |
522 | |
523 | // check for mismatch employer name and id | |
524 | if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds)) { | |
525 | throw new API_Exception('Employer name and Employer id Mismatch'); | |
526 | } | |
527 | ||
528 | // show error if multiple organisation with same name exist | |
529 | if (empty($params['employer_id']) && (count($dupeIds) > 1)) { | |
530 | throw new API_Exception('Found more than one Organisation with same Name.'); | |
531 | } | |
8d99ab37 CW |
532 | |
533 | if ($dupeIds) { | |
534 | $params['employer_id'] = $dupeIds[0]; | |
535 | } | |
536 | else { | |
59ca5f92 | 537 | $result = civicrm_api3('Contact', 'create', [ |
8d99ab37 | 538 | 'organization_name' => $params['current_employer'], |
21dfd5f5 | 539 | 'contact_type' => 'Organization', |
59ca5f92 | 540 | ]); |
8d99ab37 CW |
541 | $params['employer_id'] = $result['id']; |
542 | } | |
6a488035 TO |
543 | } |
544 | ||
545 | return NULL; | |
546 | } | |
547 | ||
548 | /** | |
244bbdd8 | 549 | * Helper function for Contact create. |
6a488035 | 550 | * |
cf470720 TO |
551 | * @param array $params |
552 | * (reference ) an assoc array of name/value pairs. | |
553 | * @param int $contactID | |
554 | * If present the contact with that ID is updated. | |
6a488035 | 555 | * |
54f1aa2a | 556 | * @return CRM_Contact_BAO_Contact|CRM_Core_Error |
6a488035 TO |
557 | */ |
558 | function _civicrm_api3_contact_update($params, $contactID = NULL) { | |
6ecbca5b | 559 | //@todo - doesn't contact create support 'id' which is already set- check & remove |
6a488035 TO |
560 | if ($contactID) { |
561 | $params['contact_id'] = $contactID; | |
562 | } | |
563 | ||
6ecbca5b | 564 | return CRM_Contact_BAO_Contact::create($params); |
6a488035 TO |
565 | } |
566 | ||
567 | /** | |
9d32e6f7 | 568 | * Validate the addressee or email or postal greetings. |
6a488035 | 569 | * |
cf470720 | 570 | * @param array $params |
9d32e6f7 | 571 | * Array per getfields metadata. |
6a488035 | 572 | * |
77b97be7 | 573 | * @throws API_Exception |
6a488035 TO |
574 | */ |
575 | function _civicrm_api3_greeting_format_params($params) { | |
59ca5f92 | 576 | $greetingParams = ['', '_id', '_custom']; |
577 | foreach (['email', 'postal', 'addressee'] as $key) { | |
6a488035 TO |
578 | $greeting = '_greeting'; |
579 | if ($key == 'addressee') { | |
580 | $greeting = ''; | |
581 | } | |
582 | ||
583 | $formatParams = FALSE; | |
22242c87 | 584 | // Unset display value from params. |
6a488035 TO |
585 | if (isset($params["{$key}{$greeting}_display"])) { |
586 | unset($params["{$key}{$greeting}_display"]); | |
587 | } | |
588 | ||
589 | // check if greetings are present in present | |
590 | foreach ($greetingParams as $greetingValues) { | |
591 | if (array_key_exists("{$key}{$greeting}{$greetingValues}", $params)) { | |
592 | $formatParams = TRUE; | |
593 | break; | |
594 | } | |
595 | } | |
596 | ||
597 | if (!$formatParams) { | |
598 | continue; | |
599 | } | |
600 | ||
601 | $nullValue = FALSE; | |
59ca5f92 | 602 | $filter = [ |
6a488035 | 603 | 'greeting_type' => "{$key}{$greeting}", |
59ca5f92 | 604 | ]; |
6a488035 | 605 | |
59ca5f92 | 606 | $greetings = CRM_Core_PseudoConstant::greeting($filter); |
607 | $greetingId = CRM_Utils_Array::value("{$key}{$greeting}_id", $params); | |
608 | $greetingVal = CRM_Utils_Array::value("{$key}{$greeting}", $params); | |
6a488035 TO |
609 | $customGreeting = CRM_Utils_Array::value("{$key}{$greeting}_custom", $params); |
610 | ||
611 | if (!$greetingId && $greetingVal) { | |
612 | $params["{$key}{$greeting}_id"] = CRM_Utils_Array::key($params["{$key}{$greeting}"], $greetings); | |
613 | } | |
614 | ||
615 | if ($customGreeting && $greetingId && | |
616 | ($greetingId != array_search('Customized', $greetings)) | |
617 | ) { | |
6ecbca5b | 618 | throw new API_Exception(ts('Provide either %1 greeting id and/or %1 greeting or custom %1 greeting', |
59ca5f92 | 619 | [1 => $key] |
620 | )); | |
6a488035 TO |
621 | } |
622 | ||
623 | if ($greetingVal && $greetingId && | |
624 | ($greetingId != CRM_Utils_Array::key($greetingVal, $greetings)) | |
625 | ) { | |
6ecbca5b | 626 | throw new API_Exception(ts('Mismatch in %1 greeting id and %1 greeting', |
59ca5f92 | 627 | [1 => $key] |
628 | )); | |
6a488035 TO |
629 | } |
630 | ||
631 | if ($greetingId) { | |
6a488035 | 632 | if (!$customGreeting && ($greetingId == array_search('Customized', $greetings))) { |
6ecbca5b | 633 | throw new API_Exception(ts('Please provide a custom value for %1 greeting', |
59ca5f92 | 634 | [1 => $key] |
635 | )); | |
6a488035 TO |
636 | } |
637 | } | |
638 | elseif ($greetingVal) { | |
639 | ||
640 | if (!in_array($greetingVal, $greetings)) { | |
59ca5f92 | 641 | throw new API_Exception(ts('Invalid %1 greeting', [1 => $key])); |
6a488035 TO |
642 | } |
643 | ||
644 | $greetingId = CRM_Utils_Array::key($greetingVal, $greetings); | |
645 | } | |
646 | ||
647 | if ($customGreeting) { | |
648 | $greetingId = CRM_Utils_Array::key('Customized', $greetings); | |
649 | } | |
650 | ||
35671d00 | 651 | $customValue = isset($params['contact_id']) ? CRM_Core_DAO::getFieldValue( |
59ca5f92 | 652 | 'CRM_Contact_DAO_Contact', |
653 | $params['contact_id'], | |
654 | "{$key}{$greeting}_custom" | |
655 | ) : FALSE; | |
6a488035 TO |
656 | |
657 | if (array_key_exists("{$key}{$greeting}_id", $params) && empty($params["{$key}{$greeting}_id"])) { | |
658 | $nullValue = TRUE; | |
659 | } | |
660 | elseif (array_key_exists("{$key}{$greeting}", $params) && empty($params["{$key}{$greeting}"])) { | |
661 | $nullValue = TRUE; | |
662 | } | |
663 | elseif ($customValue && array_key_exists("{$key}{$greeting}_custom", $params) | |
664 | && empty($params["{$key}{$greeting}_custom"]) | |
665 | ) { | |
666 | $nullValue = TRUE; | |
667 | } | |
668 | ||
669 | $params["{$key}{$greeting}_id"] = $greetingId; | |
670 | ||
671 | if (!$customValue && !$customGreeting && array_key_exists("{$key}{$greeting}_custom", $params)) { | |
672 | unset($params["{$key}{$greeting}_custom"]); | |
673 | } | |
674 | ||
675 | if ($nullValue) { | |
676 | $params["{$key}{$greeting}_id"] = ''; | |
677 | $params["{$key}{$greeting}_custom"] = ''; | |
678 | } | |
679 | ||
680 | if (isset($params["{$key}{$greeting}"])) { | |
681 | unset($params["{$key}{$greeting}"]); | |
682 | } | |
683 | } | |
684 | } | |
685 | ||
2baa1e00 | 686 | /** |
687 | * Adjust Metadata for Get action. | |
688 | * | |
689 | * @param array $params | |
690 | * Array of parameters determined by getfields. | |
691 | */ | |
692 | function _civicrm_api3_contact_getquick_spec(&$params) { | |
693 | $params['name']['api.required'] = TRUE; | |
694 | $params['name']['title'] = ts('String to search on'); | |
695 | $params['name']['type'] = CRM_Utils_Type::T_STRING; | |
696 | $params['field']['type'] = CRM_Utils_Type::T_STRING; | |
697 | $params['field']['title'] = ts('Field to search on'); | |
59ca5f92 | 698 | $params['field']['options'] = [ |
2baa1e00 | 699 | '', |
700 | 'id', | |
701 | 'contact_id', | |
702 | 'external_identifier', | |
703 | 'first_name', | |
704 | 'last_name', | |
705 | 'job_title', | |
706 | 'postal_code', | |
707 | 'street_address', | |
708 | 'email', | |
709 | 'city', | |
710 | 'phone_numeric', | |
59ca5f92 | 711 | ]; |
2baa1e00 | 712 | $params['table_name']['type'] = CRM_Utils_Type::T_STRING; |
713 | $params['table_name']['title'] = ts('Table alias to search on'); | |
714 | $params['table_name']['api.default'] = 'cc'; | |
715 | } | |
716 | ||
6a488035 | 717 | /** |
244bbdd8 | 718 | * Old Contact quick search api. |
6a488035 | 719 | * |
03f32517 | 720 | * @deprecated |
6a488035 | 721 | * |
d0997921 | 722 | * @param array $params |
9d32e6f7 | 723 | * |
645ee340 EM |
724 | * @return array |
725 | * @throws \API_Exception | |
6a488035 | 726 | */ |
6a488035 | 727 | function civicrm_api3_contact_getquick($params) { |
aca85468 | 728 | $name = CRM_Utils_Type::escape(CRM_Utils_Array::value('name', $params), 'String'); |
2baa1e00 | 729 | $table_name = CRM_Utils_String::munge($params['table_name']); |
6a488035 TO |
730 | // get the autocomplete options from settings |
731 | $acpref = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
732 | CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
733 | 'contact_autocomplete_options' | |
734 | ) | |
735 | ); | |
736 | ||
4235341b CW |
737 | $table_names = [ |
738 | 'email' => 'eml', | |
739 | 'phone_numeric' => 'phe', | |
740 | 'street_address' => 'sts', | |
741 | 'city' => 'sts', | |
742 | 'postal_code' => 'sts', | |
743 | ]; | |
744 | ||
6a488035 TO |
745 | // get the option values for contact autocomplete |
746 | $acOptions = CRM_Core_OptionGroup::values('contact_autocomplete_options', FALSE, FALSE, FALSE, NULL, 'name'); | |
747 | ||
59ca5f92 | 748 | $list = $from = []; |
6a488035 | 749 | foreach ($acpref as $value) { |
8cc574cf | 750 | if ($value && !empty($acOptions[$value])) { |
6a488035 TO |
751 | $list[$value] = $acOptions[$value]; |
752 | } | |
753 | } | |
754 | // If we are doing quicksearch by a field other than name, make sure that field is added to results | |
755 | if (!empty($params['field_name'])) { | |
1aba4d9a | 756 | $field_name = CRM_Utils_String::munge($params['field_name']); |
5d8ba7a7 | 757 | // Unique name contact_id = id |
1aba4d9a CW |
758 | if ($field_name == 'contact_id') { |
759 | $field_name = 'id'; | |
5d8ba7a7 | 760 | } |
4235341b CW |
761 | if (isset($table_names[$field_name])) { |
762 | $table_name = $table_names[$field_name]; | |
763 | } | |
764 | elseif (strpos($field_name, 'custom_') === 0) { | |
1f8ac3d2 | 765 | $customField = civicrm_api3('CustomField', 'getsingle', [ |
4235341b | 766 | 'id' => substr($field_name, 7), |
59ca5f92 | 767 | 'return' => [ |
768 | 'custom_group_id.table_name', | |
769 | 'column_name', | |
770 | 'data_type', | |
771 | 'option_group_id', | |
772 | 'html_type', | |
773 | ], | |
4235341b | 774 | ]); |
1f8ac3d2 CW |
775 | $field_name = $customField['column_name']; |
776 | $table_name = CRM_Utils_String::munge($customField['custom_group_id.table_name']); | |
4235341b | 777 | $from[$field_name] = "LEFT JOIN `$table_name` ON cc.id = `$table_name`.entity_id"; |
1f8ac3d2 CW |
778 | if (CRM_Core_BAO_CustomField::hasOptions($customField)) { |
779 | $customOptionsWhere = []; | |
780 | $customFieldOptions = CRM_Contact_BAO_Contact::buildOptions('custom_' . $customField['id'], 'search'); | |
781 | $isMultivalueField = CRM_Core_BAO_CustomField::isSerialized($customField); | |
782 | $sep = CRM_Core_DAO::VALUE_SEPARATOR; | |
783 | foreach ($customFieldOptions as $optionKey => $optionLabel) { | |
784 | if (mb_stripos($optionLabel, $name) !== FALSE) { | |
785 | $customOptionsWhere[$optionKey] = "$table_name.$field_name " . ($isMultivalueField ? "LIKE '%{$sep}{$optionKey}{$sep}%'" : "= '$optionKey'"); | |
786 | } | |
787 | } | |
788 | } | |
4235341b | 789 | } |
6a488035 | 790 | // phone_numeric should be phone |
1aba4d9a | 791 | $searchField = str_replace('_numeric', '', $field_name); |
22e263ad | 792 | if (!in_array($searchField, $list)) { |
6a488035 TO |
793 | $list[] = $searchField; |
794 | } | |
795 | } | |
2baa1e00 | 796 | else { |
797 | // Set field name to first name for exact match checking. | |
798 | $field_name = 'sort_name'; | |
799 | } | |
6a488035 | 800 | |
59ca5f92 | 801 | $select = $actualSelectElements = ['sort_name']; |
802 | $where = ''; | |
6a488035 TO |
803 | foreach ($list as $value) { |
804 | $suffix = substr($value, 0, 2) . substr($value, -1); | |
805 | switch ($value) { | |
806 | case 'street_address': | |
807 | case 'city': | |
808 | case 'postal_code': | |
809 | $selectText = $value; | |
59ca5f92 | 810 | $value = "address"; |
811 | $suffix = 'sts'; | |
6a488035 TO |
812 | case 'phone': |
813 | case 'email': | |
814 | $actualSelectElements[] = $select[] = ($value == 'address') ? $selectText : $value; | |
aebdef3c JL |
815 | if ($value == 'phone') { |
816 | $actualSelectElements[] = $select[] = 'phone_ext'; | |
817 | } | |
6a488035 TO |
818 | $from[$value] = "LEFT JOIN civicrm_{$value} {$suffix} ON ( cc.id = {$suffix}.contact_id AND {$suffix}.is_primary = 1 ) "; |
819 | break; | |
820 | ||
821 | case 'country': | |
822 | case 'state_province': | |
823 | $select[] = "{$suffix}.name as {$value}"; | |
824 | $actualSelectElements[] = "{$suffix}.name"; | |
825 | if (!in_array('address', $from)) { | |
826 | $from['address'] = 'LEFT JOIN civicrm_address sts ON ( cc.id = sts.contact_id AND sts.is_primary = 1) '; | |
827 | } | |
828 | $from[$value] = " LEFT JOIN civicrm_{$value} {$suffix} ON ( sts.{$value}_id = {$suffix}.id ) "; | |
829 | break; | |
830 | ||
831 | default: | |
4235341b CW |
832 | if ($value == 'id') { |
833 | $actualSelectElements[] = 'cc.id'; | |
834 | } | |
835 | elseif ($value != 'sort_name') { | |
6a488035 | 836 | $suffix = 'cc'; |
4235341b CW |
837 | if ($field_name == $value) { |
838 | $suffix = $table_name; | |
6a488035 TO |
839 | } |
840 | $actualSelectElements[] = $select[] = $suffix . '.' . $value; | |
841 | } | |
842 | break; | |
843 | } | |
844 | } | |
845 | ||
846 | $config = CRM_Core_Config::singleton(); | |
59ca5f92 | 847 | $as = $select; |
6a488035 TO |
848 | $select = implode(', ', $select); |
849 | if (!empty($select)) { | |
850 | $select = ", $select"; | |
851 | } | |
852 | $actualSelectElements = implode(', ', $actualSelectElements); | |
6a488035 | 853 | $from = implode(' ', $from); |
133da98d | 854 | $limit = (int) CRM_Utils_Array::value('limit', $params); |
89595c92 | 855 | $limit = $limit > 0 ? $limit : Civi::settings()->get('search_autocomplete_count'); |
6a488035 TO |
856 | |
857 | // add acl clause here | |
858 | list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause('cc'); | |
859 | ||
860 | if ($aclWhere) { | |
861 | $where .= " AND $aclWhere "; | |
862 | } | |
613643e0 | 863 | $isPrependWildcard = \Civi::settings()->get('includeWildCardInName'); |
6a488035 | 864 | |
a7488080 | 865 | if (!empty($params['org'])) { |
6a488035 TO |
866 | $where .= " AND contact_type = \"Organization\""; |
867 | ||
868 | // CRM-7157, hack: get current employer details when | |
869 | // employee_id is present. | |
59ca5f92 | 870 | $currEmpDetails = []; |
a7488080 | 871 | if (!empty($params['employee_id'])) { |
6a488035 | 872 | if ($currentEmployer = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', |
59ca5f92 | 873 | (int) $params['employee_id'], |
874 | 'employer_id' | |
875 | )) { | |
613643e0 | 876 | if ($isPrependWildcard) { |
6a488035 TO |
877 | $strSearch = "%$name%"; |
878 | } | |
879 | else { | |
880 | $strSearch = "$name%"; | |
881 | } | |
882 | ||
883 | // get current employer details | |
884 | $dao = CRM_Core_DAO::executeQuery("SELECT cc.id as id, CONCAT_WS( ' :: ', {$actualSelectElements} ) as data, sort_name | |
885 | FROM civicrm_contact cc {$from} WHERE cc.contact_type = \"Organization\" AND cc.id = {$currentEmployer} AND cc.sort_name LIKE '$strSearch'"); | |
886 | if ($dao->fetch()) { | |
59ca5f92 | 887 | $currEmpDetails = [ |
6a488035 TO |
888 | 'id' => $dao->id, |
889 | 'data' => $dao->data, | |
59ca5f92 | 890 | ]; |
6a488035 TO |
891 | } |
892 | } | |
893 | } | |
894 | } | |
895 | ||
a7488080 | 896 | if (!empty($params['contact_sub_type'])) { |
69164898 N |
897 | $contactSubType = CRM_Utils_Type::escape($params['contact_sub_type'], 'String'); |
898 | $where .= " AND cc.contact_sub_type = '{$contactSubType}'"; | |
899 | } | |
900 | ||
e1b717cb P |
901 | if (!empty($params['contact_type'])) { |
902 | $contactType = CRM_Utils_Type::escape($params['contact_type'], 'String'); | |
903 | $where .= " AND cc.contact_type LIKE '{$contactType}'"; | |
904 | } | |
905 | ||
244bbdd8 | 906 | // Set default for current_employer or return contact with particular id |
a7488080 | 907 | if (!empty($params['id'])) { |
1aba4d9a | 908 | $where .= " AND cc.id = " . (int) $params['id']; |
6a488035 TO |
909 | } |
910 | ||
a7488080 | 911 | if (!empty($params['cid'])) { |
1aba4d9a | 912 | $where .= " AND cc.id <> " . (int) $params['cid']; |
6a488035 TO |
913 | } |
914 | ||
244bbdd8 | 915 | // Contact's based of relationhip type |
6a488035 | 916 | $relType = NULL; |
a7488080 | 917 | if (!empty($params['rel'])) { |
6a488035 | 918 | $relation = explode('_', CRM_Utils_Array::value('rel', $params)); |
59ca5f92 | 919 | $relType = CRM_Utils_Type::escape($relation[0], 'Integer'); |
920 | $rel = CRM_Utils_Type::escape($relation[2], 'String'); | |
6a488035 TO |
921 | } |
922 | ||
613643e0 | 923 | if ($isPrependWildcard) { |
6a488035 TO |
924 | $strSearch = "%$name%"; |
925 | } | |
926 | else { | |
927 | $strSearch = "$name%"; | |
928 | } | |
1f8ac3d2 | 929 | $includeEmailFrom = $includeNickName = ''; |
6a488035 TO |
930 | if ($config->includeNickNameInName) { |
931 | $includeNickName = " OR nick_name LIKE '$strSearch'"; | |
6a488035 TO |
932 | } |
933 | ||
1f8ac3d2 CW |
934 | if (isset($customOptionsWhere)) { |
935 | $customOptionsWhere = $customOptionsWhere ?: [0]; | |
936 | $whereClause = " WHERE (" . implode(' OR ', $customOptionsWhere) . ") $where"; | |
937 | } | |
d976e590 | 938 | elseif (!empty($params['field_name']) && !empty($params['table_name']) && $params['field_name'] != 'sort_name') { |
8d3b1aa6 | 939 | $whereClause = " WHERE ( $table_name.$field_name LIKE '$strSearch') {$where}"; |
6a488035 TO |
940 | // Search by id should be exact |
941 | if ($field_name == 'id' || $field_name == 'external_identifier') { | |
613643e0 | 942 | $whereClause = " WHERE ( $table_name.$field_name = '$name') {$where}"; |
6a488035 TO |
943 | } |
944 | } | |
945 | else { | |
a5728a28 | 946 | $whereClause = " WHERE ( sort_name LIKE '$strSearch' $includeNickName ) {$where} "; |
6a488035 TO |
947 | if ($config->includeEmailInName) { |
948 | if (!in_array('email', $list)) { | |
949 | $includeEmailFrom = "LEFT JOIN civicrm_email eml ON ( cc.id = eml.contact_id AND eml.is_primary = 1 )"; | |
950 | } | |
36575b09 | 951 | $emailWhere = " WHERE email LIKE '$strSearch'"; |
6a488035 TO |
952 | } |
953 | } | |
954 | ||
955 | $additionalFrom = ''; | |
956 | if ($relType) { | |
957 | $additionalFrom = " | |
958 | INNER JOIN civicrm_relationship_type r ON ( | |
959 | r.id = {$relType} | |
960 | AND ( cc.contact_type = r.contact_type_{$rel} OR r.contact_type_{$rel} IS NULL ) | |
961 | AND ( cc.contact_sub_type = r.contact_sub_type_{$rel} OR r.contact_sub_type_{$rel} IS NULL ) | |
962 | )"; | |
963 | } | |
964 | ||
965 | // check if only CMS users are requested | |
a7488080 | 966 | if (!empty($params['cmsuser'])) { |
6a488035 TO |
967 | $additionalFrom = " |
968 | INNER JOIN civicrm_uf_match um ON (um.contact_id=cc.id) | |
969 | "; | |
970 | } | |
613643e0 | 971 | $orderBy = _civicrm_api3_quicksearch_get_order_by($name, $isPrependWildcard, $field_name); |
51e61eae | 972 | |
6a488035 TO |
973 | //CRM-5954 |
974 | $query = " | |
1f8ac3d2 | 975 | SELECT DISTINCT(id), data, sort_name, exactFirst |
6a488035 | 976 | FROM ( |
2baa1e00 | 977 | ( SELECT IF($table_name.$field_name = '{$name}', 0, 1) as exactFirst, cc.id as id, CONCAT_WS( ' :: ', |
978 | {$actualSelectElements} ) | |
979 | as data | |
980 | {$select} | |
6a488035 TO |
981 | FROM civicrm_contact cc {$from} |
982 | {$aclFrom} | |
a5728a28 | 983 | {$additionalFrom} |
6a488035 | 984 | {$whereClause} |
613643e0 | 985 | {$orderBy} |
6a488035 | 986 | LIMIT 0, {$limit} ) |
6a488035 | 987 | "; |
a5728a28 | 988 | |
989 | if (!empty($emailWhere)) { | |
990 | $query .= " | |
991 | UNION ( | |
992 | SELECT IF($table_name.$field_name = '{$name}', 0, 1) as exactFirst, cc.id as id, CONCAT_WS( ' :: ', | |
993 | {$actualSelectElements} ) | |
994 | as data | |
995 | {$select} | |
996 | FROM civicrm_contact cc {$from} | |
997 | {$aclFrom} | |
998 | {$additionalFrom} {$includeEmailFrom} | |
c37a2d66 | 999 | {$emailWhere} AND cc.is_deleted = 0 " . ($aclWhere ? " AND $aclWhere " : '') . " |
613643e0 | 1000 | {$orderBy} |
a5728a28 | 1001 | LIMIT 0, {$limit} |
1002 | ) | |
1003 | "; | |
1004 | } | |
36575b09 | 1005 | $query .= ") t |
613643e0 | 1006 | {$orderBy} |
2baa1e00 | 1007 | LIMIT 0, {$limit} |
1008 | "; | |
1009 | ||
6a488035 TO |
1010 | // send query to hook to be modified if needed |
1011 | CRM_Utils_Hook::contactListQuery($query, | |
1012 | $name, | |
133da98d CW |
1013 | empty($params['context']) ? NULL : CRM_Utils_Type::escape($params['context'], 'String'), |
1014 | empty($params['id']) ? NULL : $params['id'] | |
6a488035 TO |
1015 | ); |
1016 | ||
1017 | $dao = CRM_Core_DAO::executeQuery($query); | |
1018 | ||
59ca5f92 | 1019 | $contactList = []; |
6a488035 TO |
1020 | $listCurrentEmployer = TRUE; |
1021 | while ($dao->fetch()) { | |
59ca5f92 | 1022 | $t = ['id' => $dao->id]; |
6a488035 | 1023 | foreach ($as as $k) { |
35671d00 | 1024 | $t[$k] = isset($dao->$k) ? $dao->$k : ''; |
6a488035 TO |
1025 | } |
1026 | $t['data'] = $dao->data; | |
1f8ac3d2 CW |
1027 | // Replace keys with values when displaying fields from an option list |
1028 | if (!empty($customOptionsWhere)) { | |
1029 | $data = explode(' :: ', $dao->data); | |
1030 | $pos = count($data) - 1; | |
1031 | $customValue = array_intersect(CRM_Utils_Array::explodePadded($data[$pos]), array_keys($customOptionsWhere)); | |
1032 | $data[$pos] = implode(', ', array_intersect_key($customFieldOptions, array_flip($customValue))); | |
1033 | $t['data'] = implode(' :: ', $data); | |
1034 | } | |
6a488035 | 1035 | $contactList[] = $t; |
a7488080 | 1036 | if (!empty($params['org']) && |
6a488035 TO |
1037 | !empty($currEmpDetails) && |
1038 | $dao->id == $currEmpDetails['id'] | |
1039 | ) { | |
1040 | $listCurrentEmployer = FALSE; | |
1041 | } | |
1042 | } | |
1043 | ||
1044 | //return organization name if doesn't exist in db | |
1045 | if (empty($contactList)) { | |
a7488080 | 1046 | if (!empty($params['org'])) { |
6a488035 | 1047 | if ($listCurrentEmployer && !empty($currEmpDetails)) { |
59ca5f92 | 1048 | $contactList = [ |
1049 | [ | |
d5cc0fc2 | 1050 | 'data' => $currEmpDetails['data'], |
59ca5f92 | 1051 | 'id' => $currEmpDetails['id'], |
1052 | ], | |
1053 | ]; | |
6a488035 TO |
1054 | } |
1055 | else { | |
59ca5f92 | 1056 | $contactList = [ |
1057 | [ | |
6a488035 | 1058 | 'data' => $name, |
59ca5f92 | 1059 | 'id' => $name, |
1060 | ], | |
1061 | ]; | |
6a488035 TO |
1062 | } |
1063 | } | |
1064 | } | |
1065 | ||
244bbdd8 | 1066 | return civicrm_api3_create_success($contactList, $params, 'Contact', 'getquick'); |
a14e9d08 CW |
1067 | } |
1068 | ||
613643e0 | 1069 | /** |
1070 | * Get the order by string for the quicksearch query. | |
1071 | * | |
1072 | * Get the order by string. The string might be | |
1073 | * - sort name if there is no search value provided and the site is configured | |
1074 | * to search by sort name | |
1075 | * - empty if there is no search value provided and the site is not configured | |
1076 | * to search by sort name | |
1077 | * - exactFirst and then sort name if a search value is provided and the site is configured | |
1078 | * to search by sort name | |
1079 | * - exactFirst if a search value is provided and the site is not configured | |
1080 | * to search by sort name | |
1081 | * | |
1082 | * exactFirst means 'yes if the search value exactly matches the searched field. else no'. | |
1083 | * It is intended to prioritise exact matches for the entered string so on a first name search | |
1084 | * for 'kath' contacts with a first name of exactly Kath rise to the top. | |
1085 | * | |
1086 | * On short strings it is expensive. Per CRM-19547 there is still an open question | |
1087 | * as to whether we should only do exactMatch on a minimum length or on certain fields. | |
1088 | * | |
1089 | * However, we have mitigated this somewhat by not doing an exact match search on | |
1090 | * empty strings, non-wildcard sort-name searches and email searches where there is | |
1091 | * no @ after the first character. | |
1092 | * | |
1093 | * For the user it is further mitigated by the fact they just don't know the | |
1094 | * slower queries are firing. If they type 'smit' slowly enough 4 queries will trigger | |
1095 | * but if the first 3 are slow the first result they see may be off the 4th query. | |
1096 | * | |
1097 | * @param string $name | |
1098 | * @param bool $isPrependWildcard | |
1099 | * @param string $field_name | |
1100 | * | |
1101 | * @return string | |
1102 | */ | |
1103 | function _civicrm_api3_quicksearch_get_order_by($name, $isPrependWildcard, $field_name) { | |
1104 | $skipExactMatch = ($name === '%'); | |
1105 | if ($field_name === 'email' && !strpos('@', $name)) { | |
1106 | $skipExactMatch = TRUE; | |
1107 | } | |
1108 | ||
1109 | if (!\Civi::settings()->get('includeOrderByClause')) { | |
1110 | return $skipExactMatch ? '' : "ORDER BY exactFirst"; | |
1111 | } | |
1112 | if ($skipExactMatch || (!$isPrependWildcard && $field_name === 'sort_name')) { | |
1113 | // If there is no wildcard then sorting by exactFirst would have the same | |
1114 | // effect as just a sort_name search, but slower. | |
1115 | return "ORDER BY sort_name"; | |
1116 | } | |
1117 | ||
1118 | return "ORDER BY exactFirst, sort_name"; | |
1119 | } | |
1120 | ||
a14e9d08 | 1121 | /** |
35823763 EM |
1122 | * Declare deprecated api functions. |
1123 | * | |
a14e9d08 | 1124 | * @deprecated api notice |
a6c01b45 | 1125 | * @return array |
16b10e64 | 1126 | * Array of deprecated actions |
a14e9d08 CW |
1127 | */ |
1128 | function _civicrm_api3_contact_deprecation() { | |
59ca5f92 | 1129 | return ['getquick' => 'The "getquick" action is deprecated in favor of "getlist".']; |
6a488035 TO |
1130 | } |
1131 | ||
1132 | /** | |
1133 | * Merges given pair of duplicate contacts. | |
1134 | * | |
cf470720 | 1135 | * @param array $params |
b081365f CW |
1136 | * Allowed array keys are: |
1137 | * -int main_id: main contact id with whom merge has to happen | |
1138 | * -int other_id: duplicate contact which would be deleted after merge operation | |
1139 | * -string mode: "safe" skips the merge if there are no conflicts. Does a force merge otherwise. | |
6a488035 | 1140 | * |
a6c01b45 | 1141 | * @return array |
72b3a70c | 1142 | * API Result Array |
fedc3428 | 1143 | * @throws API_Exception |
6a488035 TO |
1144 | */ |
1145 | function civicrm_api3_contact_merge($params) { | |
8b5445c8 | 1146 | if (($result = CRM_Dedupe_Merger::merge( |
1147 | [['srcID' => $params['to_remove_id'], 'dstID' => $params['to_keep_id']]], | |
1148 | [], | |
1149 | $params['mode'], | |
1150 | FALSE, | |
1151 | CRM_Utils_Array::value('check_permissions', $params) | |
1152 | )) != FALSE) { | |
1153 | ||
12d73bba | 1154 | return civicrm_api3_create_success($result, $params); |
6a488035 | 1155 | } |
fedc3428 | 1156 | throw new API_Exception('Merge failed'); |
12d73bba | 1157 | } |
1158 | ||
1159 | /** | |
5ea06a7b | 1160 | * Adjust metadata for contact_merge api function. |
12d73bba | 1161 | * |
1162 | * @param array $params | |
1163 | */ | |
1164 | function _civicrm_api3_contact_merge_spec(&$params) { | |
59ca5f92 | 1165 | $params['to_remove_id'] = [ |
aefc291e | 1166 | 'title' => ts('ID of the contact to merge & remove'), |
1167 | 'description' => ts('Wow - these 2 aliased params are the logical reverse of what I expect - but what to do?'), | |
12d73bba | 1168 | 'api.required' => 1, |
1169 | 'type' => CRM_Utils_Type::T_INT, | |
59ca5f92 | 1170 | 'api.aliases' => ['main_id'], |
1171 | ]; | |
1172 | $params['to_keep_id'] = [ | |
aefc291e | 1173 | 'title' => ts('ID of the contact to keep'), |
1174 | 'description' => ts('Wow - these 2 aliased params are the logical reverse of what I expect - but what to do?'), | |
12d73bba | 1175 | 'api.required' => 1, |
1176 | 'type' => CRM_Utils_Type::T_INT, | |
59ca5f92 | 1177 | 'api.aliases' => ['other_id'], |
1178 | ]; | |
1179 | $params['mode'] = [ | |
aefc291e | 1180 | 'title' => ts('Dedupe mode'), |
1181 | 'description' => ts("In 'safe' mode conflicts will result in no merge. In 'aggressive' mode the merge will still proceed (hook dependent)"), | |
6f2c4a24 | 1182 | 'api.default' => 'safe', |
712ee28f | 1183 | 'options' => ['safe' => ts('Abort on unhandled conflict'), 'aggressive' => ts('Proceed on unhandled conflict. Note hooks may change handling here.')], |
aefc291e | 1184 | ]; |
1185 | } | |
1186 | ||
1187 | /** | |
1188 | * Determines if given pair of contaacts have conflicts that would affect merging them. | |
1189 | * | |
1190 | * @param array $params | |
1191 | * Allowed array keys are: | |
1192 | * -int main_id: main contact id with whom merge has to happen | |
1193 | * -int other_id: duplicate contact which would be deleted after merge operation | |
1194 | * -string mode: "safe" skips the merge if there are no conflicts. Does a force merge otherwise. | |
1195 | * | |
1196 | * @return array | |
1197 | * API Result Array | |
1198 | * | |
1199 | * @throws \CRM_Core_Exception | |
1200 | * @throws \CiviCRM_API3_Exception | |
712ee28f | 1201 | * @throws \API_Exception |
aefc291e | 1202 | */ |
1203 | function civicrm_api3_contact_get_merge_conflicts($params) { | |
1204 | $migrationInfo = []; | |
ffa59d18 | 1205 | $result = []; |
712ee28f | 1206 | foreach ((array) $params['mode'] as $mode) { |
ffa59d18 | 1207 | $result[$mode]['conflicts'] = CRM_Dedupe_Merger::getConflicts( |
712ee28f | 1208 | $migrationInfo, |
aefc291e | 1209 | $params['to_remove_id'], $params['to_keep_id'], |
ffa59d18 | 1210 | $mode |
712ee28f | 1211 | ); |
aefc291e | 1212 | } |
ffa59d18 | 1213 | return civicrm_api3_create_success($result, $params); |
aefc291e | 1214 | } |
1215 | ||
1216 | /** | |
1217 | * Adjust metadata for contact_merge api function. | |
1218 | * | |
1219 | * @param array $params | |
1220 | */ | |
1221 | function _civicrm_api3_contact_get_merge_conflicts_spec(&$params) { | |
1222 | $params['to_remove_id'] = [ | |
1223 | 'title' => ts('ID of the contact to merge & remove'), | |
1224 | 'api.required' => 1, | |
1225 | 'type' => CRM_Utils_Type::T_INT, | |
1226 | ]; | |
1227 | $params['to_keep_id'] = [ | |
1228 | 'title' => ts('ID of the contact to keep'), | |
1229 | 'api.required' => 1, | |
1230 | 'type' => CRM_Utils_Type::T_INT, | |
1231 | ]; | |
1232 | $params['mode'] = [ | |
1233 | 'title' => ts('Dedupe mode'), | |
1234 | 'description' => ts("'safe' or 'aggressive' - these modes map to the merge actions & may affect resolution done by hooks "), | |
ffa59d18 | 1235 | 'api.default' => 'safe', |
59ca5f92 | 1236 | ]; |
6a488035 TO |
1237 | } |
1238 | ||
734cd0d5 | 1239 | /** |
1240 | * Get the ultimate contact a contact was merged to. | |
1241 | * | |
1242 | * @param array $params | |
1243 | * | |
1244 | * @return array | |
1245 | * API Result Array | |
1246 | * @throws API_Exception | |
1247 | */ | |
1248 | function civicrm_api3_contact_getmergedto($params) { | |
1249 | $contactID = _civicrm_api3_contact_getmergedto($params); | |
1250 | if ($contactID) { | |
1251 | $values = [$contactID => ['id' => $contactID]]; | |
1252 | } | |
1253 | else { | |
1254 | $values = []; | |
1255 | } | |
1256 | return civicrm_api3_create_success($values, $params); | |
1257 | } | |
1258 | ||
1259 | /** | |
1260 | * Get the contact our contact was finally merged to. | |
1261 | * | |
1262 | * If the contact has been merged multiple times the crucial parent activity will have | |
1263 | * wound up on the ultimate contact so we can figure out the final resting place of the | |
1264 | * contact with only 2 activities even if 50 merges took place. | |
1265 | * | |
1266 | * @param array $params | |
1267 | * | |
1268 | * @return int|false | |
1269 | */ | |
1270 | function _civicrm_api3_contact_getmergedto($params) { | |
1271 | $contactID = FALSE; | |
1272 | $deleteActivity = civicrm_api3('ActivityContact', 'get', [ | |
1273 | 'contact_id' => $params['contact_id'], | |
1274 | 'activity_id.activity_type_id' => 'Contact Deleted By Merge', | |
1275 | 'is_deleted' => 0, | |
1276 | 'is_test' => $params['is_test'], | |
1277 | 'record_type_id' => 'Activity Targets', | |
1278 | 'return' => ['activity_id.parent_id'], | |
1279 | 'sequential' => 1, | |
1280 | 'options' => [ | |
1281 | 'limit' => 1, | |
59ca5f92 | 1282 | 'sort' => 'activity_id.activity_date_time DESC', |
734cd0d5 | 1283 | ], |
1284 | ])['values']; | |
1285 | if (!empty($deleteActivity)) { | |
1286 | $contactID = civicrm_api3('ActivityContact', 'getvalue', [ | |
1287 | 'activity_id' => $deleteActivity[0]['activity_id.parent_id'], | |
1288 | 'record_type_id' => 'Activity Targets', | |
1289 | 'return' => 'contact_id', | |
1290 | ]); | |
1291 | } | |
1292 | return $contactID; | |
1293 | } | |
1294 | ||
1295 | /** | |
1296 | * Adjust metadata for contact_merge api function. | |
1297 | * | |
1298 | * @param array $params | |
1299 | */ | |
1300 | function _civicrm_api3_contact_getmergedto_spec(&$params) { | |
1301 | $params['contact_id'] = [ | |
1302 | 'title' => ts('ID of contact to find ultimate contact for'), | |
1303 | 'type' => CRM_Utils_Type::T_INT, | |
1304 | 'api.required' => TRUE, | |
1305 | ]; | |
1306 | $params['is_test'] = [ | |
1307 | 'title' => ts('Get test deletions rather than live?'), | |
1308 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
1309 | 'api.default' => 0, | |
1310 | ]; | |
1311 | } | |
1312 | ||
1313 | /** | |
1314 | * Get the ultimate contact a contact was merged to. | |
1315 | * | |
1316 | * @param array $params | |
1317 | * | |
1318 | * @return array | |
1319 | * API Result Array | |
1320 | * @throws API_Exception | |
1321 | */ | |
1322 | function civicrm_api3_contact_getmergedfrom($params) { | |
1323 | $contacts = _civicrm_api3_contact_getmergedfrom($params); | |
1324 | return civicrm_api3_create_success($contacts, $params); | |
1325 | } | |
1326 | ||
1327 | /** | |
1328 | * Get all the contacts merged into our contact. | |
1329 | * | |
1330 | * @param array $params | |
1331 | * | |
1332 | * @return array | |
1333 | */ | |
1334 | function _civicrm_api3_contact_getmergedfrom($params) { | |
1335 | $activities = []; | |
1336 | $deleteActivities = civicrm_api3('ActivityContact', 'get', [ | |
1337 | 'contact_id' => $params['contact_id'], | |
1338 | 'activity_id.activity_type_id' => 'Contact Merged', | |
1339 | 'is_deleted' => 0, | |
1340 | 'is_test' => $params['is_test'], | |
1341 | 'record_type_id' => 'Activity Targets', | |
1342 | 'return' => 'activity_id', | |
1343 | ])['values']; | |
1344 | ||
1345 | foreach ($deleteActivities as $deleteActivity) { | |
1346 | $activities[] = $deleteActivity['activity_id']; | |
1347 | } | |
1348 | if (empty($activities)) { | |
1349 | return []; | |
1350 | } | |
1351 | ||
1352 | $activityContacts = civicrm_api3('ActivityContact', 'get', [ | |
1353 | 'activity_id.parent_id' => ['IN' => $activities], | |
1354 | 'record_type_id' => 'Activity Targets', | |
1355 | 'return' => 'contact_id', | |
1356 | ])['values']; | |
1357 | $contacts = []; | |
1358 | foreach ($activityContacts as $activityContact) { | |
1359 | $contacts[$activityContact['contact_id']] = ['id' => $activityContact['contact_id']]; | |
1360 | } | |
1361 | return $contacts; | |
1362 | } | |
1363 | ||
1364 | /** | |
1365 | * Adjust metadata for contact_merge api function. | |
1366 | * | |
1367 | * @param array $params | |
1368 | */ | |
1369 | function _civicrm_api3_contact_getmergedfrom_spec(&$params) { | |
1370 | $params['contact_id'] = [ | |
1371 | 'title' => ts('ID of contact to find ultimate contact for'), | |
1372 | 'type' => CRM_Utils_Type::T_INT, | |
1373 | 'api.required' => TRUE, | |
1374 | ]; | |
1375 | $params['is_test'] = [ | |
1376 | 'title' => ts('Get test deletions rather than live?'), | |
1377 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
1378 | 'api.default' => 0, | |
1379 | ]; | |
1380 | } | |
1381 | ||
aa1b1481 | 1382 | /** |
9d32e6f7 EM |
1383 | * Adjust metadata for contact_proximity api function. |
1384 | * | |
c490a46a | 1385 | * @param array $params |
aa1b1481 | 1386 | */ |
6a488035 | 1387 | function _civicrm_api3_contact_proximity_spec(&$params) { |
59ca5f92 | 1388 | $params['latitude'] = [ |
d142432b EM |
1389 | 'title' => 'Latitude', |
1390 | 'api.required' => 1, | |
1391 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 1392 | ]; |
1393 | $params['longitude'] = [ | |
d142432b EM |
1394 | 'title' => 'Longitude', |
1395 | 'api.required' => 1, | |
1396 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 1397 | ]; |
d142432b | 1398 | |
59ca5f92 | 1399 | $params['unit'] = [ |
d142432b EM |
1400 | 'title' => 'Unit of Measurement', |
1401 | 'api.default' => 'meter', | |
1402 | 'type' => CRM_Utils_Type::T_STRING, | |
59ca5f92 | 1403 | ]; |
6a488035 TO |
1404 | } |
1405 | ||
aa1b1481 | 1406 | /** |
9d32e6f7 EM |
1407 | * Get contacts by proximity. |
1408 | * | |
c490a46a | 1409 | * @param array $params |
aa1b1481 EM |
1410 | * |
1411 | * @return array | |
1412 | * @throws Exception | |
1413 | */ | |
6a488035 | 1414 | function civicrm_api3_contact_proximity($params) { |
59ca5f92 | 1415 | $latitude = CRM_Utils_Array::value('latitude', $params); |
6a488035 | 1416 | $longitude = CRM_Utils_Array::value('longitude', $params); |
59ca5f92 | 1417 | $distance = CRM_Utils_Array::value('distance', $params); |
6a488035 TO |
1418 | |
1419 | $unit = CRM_Utils_Array::value('unit', $params); | |
1420 | ||
1421 | // check and ensure that lat/long and distance are floats | |
1422 | if ( | |
1423 | !CRM_Utils_Rule::numeric($latitude) || | |
1424 | !CRM_Utils_Rule::numeric($longitude) || | |
1425 | !CRM_Utils_Rule::numeric($distance) | |
1426 | ) { | |
1427 | throw new Exception(ts('Latitude, Longitude and Distance should exist and be numeric')); | |
1428 | } | |
1429 | ||
1430 | if ($unit == "mile") { | |
1431 | $conversionFactor = 1609.344; | |
1432 | } | |
1433 | else { | |
1434 | $conversionFactor = 1000; | |
1435 | } | |
1436 | //Distance in meters | |
1437 | $distance = $distance * $conversionFactor; | |
1438 | ||
1439 | $whereClause = CRM_Contact_BAO_ProximityQuery::where($latitude, $longitude, $distance); | |
1440 | ||
1441 | $query = " | |
1442 | SELECT civicrm_contact.id as contact_id, | |
1443 | civicrm_contact.display_name as display_name | |
1444 | FROM civicrm_contact | |
1445 | LEFT JOIN civicrm_address ON civicrm_contact.id = civicrm_address.contact_id | |
1446 | WHERE $whereClause | |
1447 | "; | |
1448 | ||
1449 | $dao = CRM_Core_DAO::executeQuery($query); | |
59ca5f92 | 1450 | $contacts = []; |
6a488035 TO |
1451 | while ($dao->fetch()) { |
1452 | $contacts[] = $dao->toArray(); | |
1453 | } | |
1454 | ||
244bbdd8 | 1455 | return civicrm_api3_create_success($contacts, $params, 'Contact', 'get_by_location', $dao); |
6a488035 TO |
1456 | } |
1457 | ||
ff88d165 | 1458 | /** |
22242c87 EM |
1459 | * Get parameters for getlist function. |
1460 | * | |
a6c6059d | 1461 | * @see _civicrm_api3_generic_getlist_params |
ff88d165 | 1462 | * |
8c6b335b | 1463 | * @param array $request |
ff88d165 CW |
1464 | */ |
1465 | function _civicrm_api3_contact_getlist_params(&$request) { | |
1466 | // get the autocomplete options from settings | |
1467 | $acpref = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
1468 | CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
1469 | 'contact_autocomplete_options' | |
1470 | ) | |
1471 | ); | |
1472 | ||
1473 | // get the option values for contact autocomplete | |
1474 | $acOptions = CRM_Core_OptionGroup::values('contact_autocomplete_options', FALSE, FALSE, FALSE, NULL, 'name'); | |
1475 | ||
59ca5f92 | 1476 | $list = []; |
ff88d165 CW |
1477 | foreach ($acpref as $value) { |
1478 | if ($value && !empty($acOptions[$value])) { | |
1479 | $list[] = $acOptions[$value]; | |
1480 | } | |
1481 | } | |
1482 | // If we are doing quicksearch by a field other than name, make sure that field is added to results | |
1483 | $field_name = CRM_Utils_String::munge($request['search_field']); | |
1484 | // Unique name contact_id = id | |
1485 | if ($field_name == 'contact_id') { | |
1486 | $field_name = 'id'; | |
1487 | } | |
1488 | // phone_numeric should be phone | |
1489 | $searchField = str_replace('_numeric', '', $field_name); | |
22e263ad | 1490 | if (!in_array($searchField, $list)) { |
ff88d165 CW |
1491 | $list[] = $searchField; |
1492 | } | |
8250601e | 1493 | $request['description_field'] = $list; |
54bee7df | 1494 | $list[] = 'contact_type'; |
8250601e | 1495 | $request['params']['return'] = array_unique(array_merge($list, $request['extra'])); |
ff88d165 CW |
1496 | $request['params']['options']['sort'] = 'sort_name'; |
1497 | // Contact api doesn't support array(LIKE => 'foo') syntax | |
609a8c53 | 1498 | if (!empty($request['input'])) { |
fd816db5 | 1499 | $request['params'][$request['search_field']] = $request['input']; |
81b7bb6f CW |
1500 | // Temporarily override wildcard setting |
1501 | if (Civi::settings()->get('includeWildCardInName') != $request['add_wildcard']) { | |
1502 | Civi::$statics['civicrm_api3_contact_getlist']['override_wildcard'] = !$request['add_wildcard']; | |
1503 | Civi::settings()->set('includeWildCardInName', $request['add_wildcard']); | |
1504 | } | |
609a8c53 | 1505 | } |
ff88d165 CW |
1506 | } |
1507 | ||
1508 | /** | |
22242c87 EM |
1509 | * Get output for getlist function. |
1510 | * | |
a6c6059d | 1511 | * @see _civicrm_api3_generic_getlist_output |
ff88d165 | 1512 | * |
8c6b335b CW |
1513 | * @param array $result |
1514 | * @param array $request | |
ff88d165 CW |
1515 | * |
1516 | * @return array | |
1517 | */ | |
1518 | function _civicrm_api3_contact_getlist_output($result, $request) { | |
59ca5f92 | 1519 | $output = []; |
ff88d165 | 1520 | if (!empty($result['values'])) { |
59ca5f92 | 1521 | $addressFields = array_intersect([ |
1522 | 'street_address', | |
1523 | 'city', | |
1524 | 'state_province', | |
1525 | 'country', | |
1526 | ], | |
dc64d047 | 1527 | $request['params']['return']); |
ff88d165 | 1528 | foreach ($result['values'] as $row) { |
59ca5f92 | 1529 | $data = [ |
ff88d165 CW |
1530 | 'id' => $row[$request['id_field']], |
1531 | 'label' => $row[$request['label_field']], | |
59ca5f92 | 1532 | 'description' => [], |
1533 | ]; | |
8250601e CW |
1534 | foreach ($request['description_field'] as $item) { |
1535 | if (!strpos($item, '_name') && !in_array($item, $addressFields) && !empty($row[$item])) { | |
88881f79 | 1536 | $data['description'][] = $row[$item]; |
ff88d165 CW |
1537 | } |
1538 | } | |
59ca5f92 | 1539 | $address = []; |
22e263ad | 1540 | foreach ($addressFields as $item) { |
88881f79 CW |
1541 | if (!empty($row[$item])) { |
1542 | $address[] = $row[$item]; | |
1543 | } | |
1544 | } | |
1545 | if ($address) { | |
1546 | $data['description'][] = implode(' ', $address); | |
1547 | } | |
ff88d165 CW |
1548 | if (!empty($request['image_field'])) { |
1549 | $data['image'] = isset($row[$request['image_field']]) ? $row[$request['image_field']] : ''; | |
54bee7df CW |
1550 | } |
1551 | else { | |
1552 | $data['icon_class'] = $row['contact_type']; | |
1553 | } | |
ff88d165 CW |
1554 | $output[] = $data; |
1555 | } | |
1556 | } | |
81b7bb6f CW |
1557 | // Restore wildcard override by _civicrm_api3_contact_getlist_params |
1558 | if (isset(Civi::$statics['civicrm_api3_contact_getlist']['override_wildcard'])) { | |
1559 | Civi::settings()->set('includeWildCardInName', Civi::$statics['civicrm_api3_contact_getlist']['override_wildcard']); | |
1560 | unset(Civi::$statics['civicrm_api3_contact_getlist']['override_wildcard']); | |
1561 | } | |
ff88d165 CW |
1562 | return $output; |
1563 | } | |
eb5f7260 | 1564 | |
1565 | /** | |
1566 | * Check for duplicate contacts. | |
1567 | * | |
1568 | * @param array $params | |
1569 | * Params per getfields metadata. | |
1570 | * | |
1571 | * @return array | |
1572 | * API formatted array | |
1573 | */ | |
1574 | function civicrm_api3_contact_duplicatecheck($params) { | |
fedc3428 | 1575 | $dupes = CRM_Contact_BAO_Contact::getDuplicateContacts( |
1576 | $params['match'], | |
1577 | $params['match']['contact_type'], | |
d6def514 | 1578 | $params['rule_type'], |
1e8be62b | 1579 | CRM_Utils_Array::value('exclude', $params, []), |
fedc3428 | 1580 | CRM_Utils_Array::value('check_permissions', $params), |
1581 | CRM_Utils_Array::value('dedupe_rule_id', $params) | |
1582 | ); | |
59ca5f92 | 1583 | $values = []; |
d6def514 | 1584 | if ($dupes && !empty($params['return'])) { |
59ca5f92 | 1585 | return civicrm_api3('Contact', 'get', [ |
d6def514 | 1586 | 'return' => $params['return'], |
59ca5f92 | 1587 | 'id' => ['IN' => $dupes], |
d6def514 CW |
1588 | 'options' => CRM_Utils_Array::value('options', $params), |
1589 | 'sequential' => CRM_Utils_Array::value('sequential', $params), | |
1590 | 'check_permissions' => CRM_Utils_Array::value('check_permissions', $params), | |
59ca5f92 | 1591 | ]); |
d6def514 CW |
1592 | } |
1593 | foreach ($dupes as $dupe) { | |
59ca5f92 | 1594 | $values[$dupe] = ['id' => $dupe]; |
d6def514 | 1595 | } |
eb5f7260 | 1596 | return civicrm_api3_create_success($values, $params, 'Contact', 'duplicatecheck'); |
1597 | } | |
1598 | ||
1599 | /** | |
1600 | * Declare metadata for contact dedupe function. | |
1601 | * | |
1602 | * @param $params | |
1603 | */ | |
1604 | function _civicrm_api3_contact_duplicatecheck_spec(&$params) { | |
59ca5f92 | 1605 | $params['dedupe_rule_id'] = [ |
eb5f7260 | 1606 | 'title' => 'Dedupe Rule ID (optional)', |
1607 | 'description' => 'This will default to the built in unsupervised rule', | |
1608 | 'type' => CRM_Utils_Type::T_INT, | |
59ca5f92 | 1609 | ]; |
1610 | $params['rule_type'] = [ | |
d6def514 CW |
1611 | 'title' => 'Dedupe Rule Type', |
1612 | 'description' => 'If no rule id specified, pass "Unsupervised" or "Supervised"', | |
1613 | 'type' => CRM_Utils_Type::T_STRING, | |
1614 | 'api.default' => 'Unsupervised', | |
59ca5f92 | 1615 | ]; |
eb5f7260 | 1616 | // @todo declare 'match' parameter. We don't have a standard for type = array yet. |
1617 | } |