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