Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * new version of civicrm apis. See blog post at | |
30 | * http://civicrm.org/node/131 | |
31 | * @todo Write sth | |
32 | * | |
33 | * @package CiviCRM_APIv3 | |
34 | * @subpackage API_Contact | |
35 | * @copyright CiviCRM LLC (c) 2004-2013 | |
36 | * $Id: Contact.php 30879 2010-11-22 15:45:55Z shot $ | |
37 | * | |
38 | */ | |
39 | ||
40 | /** | |
41 | * Create or update a contact (note you should always call this via civicrm_api() & never directly) | |
42 | * | |
43 | * @param array $params input parameters | |
44 | * | |
45 | * Allowed @params array keys are: | |
46 | * {@getfields contact_create} | |
47 | * | |
48 | * | |
49 | * @example ContactCreate.php Example of Create Call | |
50 | * | |
51 | * @return array API Result Array | |
52 | * | |
53 | * @static void | |
54 | * @access public | |
55 | */ | |
56 | function civicrm_api3_contact_create($params) { | |
57 | ||
58 | $contactID = CRM_Utils_Array::value('contact_id', $params, CRM_Utils_Array::value('id', $params)); | |
59 | $dupeCheck = CRM_Utils_Array::value('dupe_check', $params, FALSE); | |
60 | $values = _civicrm_api3_contact_check_params($params, $dupeCheck); | |
61 | if ($values) { | |
62 | return $values; | |
63 | } | |
64 | ||
65 | if (!$contactID) { | |
66 | // If we get here, we're ready to create a new contact | |
67 | if (($email = CRM_Utils_Array::value('email', $params)) && !is_array($params['email'])) { | |
68 | $defLocType = CRM_Core_BAO_LocationType::getDefault(); | |
69 | $params['email'] = array( | |
70 | 1 => array('email' => $email, | |
71 | 'is_primary' => 1, | |
72 | 'location_type_id' => ($defLocType->id) ? $defLocType->id : 1, | |
73 | ), | |
74 | ); | |
75 | } | |
76 | } | |
77 | ||
78 | if (!empty($params['home_url'])) { | |
cbf48754 | 79 | $websiteTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'); |
6a488035 TO |
80 | $params['website'] = array(1 => array('website_type_id' => key($websiteTypes), |
81 | 'url' => $params['home_url'], | |
82 | ), | |
83 | ); | |
84 | } | |
85 | ||
6ecbca5b | 86 | _civicrm_api3_greeting_format_params($params); |
6a488035 TO |
87 | |
88 | $values = array(); | |
6a488035 | 89 | |
6ecbca5b | 90 | if (empty($params['contact_type']) && $contactID) { |
91 | $params['contact_type'] = CRM_Contact_BAO_Contact::getContactType($contactID); | |
6a488035 TO |
92 | } |
93 | ||
6ecbca5b | 94 | if (!isset($params['contact_sub_type']) && $contactID) { |
95 | $params['contact_sub_type'] = CRM_Contact_BAO_Contact::getContactSubType($contactID); | |
6a488035 TO |
96 | } |
97 | ||
6ecbca5b | 98 | _civicrm_api3_custom_format_params($params, $values, $params['contact_type'], $contactID); |
6a488035 TO |
99 | |
100 | $params = array_merge($params, $values); | |
6ecbca5b | 101 | //@todo we should just call basic_create here - but need to make contact:create accept 'id' on the bao |
6a488035 TO |
102 | $contact = _civicrm_api3_contact_update($params, $contactID); |
103 | ||
104 | if (is_a($contact, 'CRM_Core_Error')) { | |
6ecbca5b | 105 | throw new API_Exception($contact->_errors[0]['message']); |
6a488035 TO |
106 | } |
107 | else { | |
108 | $values = array(); | |
109 | _civicrm_api3_object_to_array_unique_fields($contact, $values[$contact->id]); | |
110 | } | |
111 | ||
112 | return civicrm_api3_create_success($values, $params, 'Contact', 'create'); | |
113 | } | |
114 | ||
11e09c59 | 115 | /** |
6a488035 TO |
116 | * Adjust Metadata for Create action |
117 | * | |
118 | * @param array $params array or parameters determined by getfields | |
119 | */ | |
120 | function _civicrm_api3_contact_create_spec(&$params) { | |
121 | $params['contact_type']['api.required'] = 1; | |
122 | $params['id']['api.aliases'] = array('contact_id'); | |
123 | $params['current_employer'] = array( | |
124 | 'title' => 'Current Employer', | |
125 | 'description' => 'Name of Current Employer', | |
126 | ); | |
0391dc25 | 127 | $params['dupe_check'] = array( |
128 | 'title' => 'Check for Duplicates', | |
129 | 'description' => 'Throw error if contact create matches dedupe rule', | |
130 | ); | |
6ecbca5b | 131 | $params['prefix_id']['api.aliases'] = array('individual_prefix', 'individual_prefix_id'); |
132 | $params['suffix_id']['api.aliases'] = array('individual_suffix', 'individual_suffix_id'); | |
6a488035 TO |
133 | } |
134 | ||
135 | /** | |
136 | * Retrieve one or more contacts, given a set of search params | |
137 | * | |
138 | * @param array input parameters | |
139 | * | |
140 | * @return array API Result Array | |
141 | * (@getfields contact_get} | |
142 | * @static void | |
143 | * @access public | |
144 | * | |
145 | * @example ContactGet.php Standard GET example | |
146 | * | |
147 | */ | |
148 | function civicrm_api3_contact_get($params) { | |
149 | $options = array(); | |
150 | _civicrm_api3_contact_get_supportanomalies($params, $options); | |
151 | $contacts = _civicrm_api3_get_using_query_object('contact', $params, $options); | |
6ecbca5b | 152 | return civicrm_api3_create_success($contacts, $params, 'contact'); |
6a488035 TO |
153 | } |
154 | ||
155 | function civicrm_api3_contact_getcount($params) { | |
156 | $options = array(); | |
157 | _civicrm_api3_contact_get_supportanomalies($params, $options); | |
158 | $count = _civicrm_api3_get_using_query_object('contact', $params, $options,1); | |
972322c5 | 159 | return (int) $count; |
6a488035 | 160 | } |
11e09c59 TO |
161 | |
162 | /** | |
6a488035 TO |
163 | * Adjust Metadata for Get action |
164 | * | |
165 | * @param array $params array or parameters determined by getfields | |
166 | */ | |
167 | function _civicrm_api3_contact_get_spec(&$params) { | |
168 | $params['contact_is_deleted']['api.default'] = 0; | |
169 | ||
170 | //we declare all these pseudofields as there are other undocumented fields accessible | |
171 | // via the api - but if check permissions is set we only allow declared fields | |
172 | $params['address_id']['title'] = 'Primary Address ID'; | |
173 | $params['street_address']['title'] = 'Primary Address Street Address'; | |
174 | $params['supplemental_address_1']['title'] = 'Primary Address Supplemental Address 1'; | |
175 | $params['supplemental_address_2']['title'] = 'Primary Address Supplemental Address 2'; | |
176 | $params['city']['title'] = 'Primary Address City'; | |
177 | $params['postal_code_suffix']['title'] = 'Primary Address Post Code Suffix'; | |
178 | $params['postal_code']['title'] = 'Primary Address Post Code'; | |
179 | $params['geo_code_1']['title'] = 'Primary Address Latitude'; | |
180 | $params['geo_code_2']['title'] = 'Primary Address Longitude'; | |
181 | $params['state_province_id']['title'] = 'Primary Address State Province ID'; | |
182 | $params['state_province_name']['title'] = 'Primary Address State Province Name'; | |
183 | $params['state_province']['title'] = 'Primary Address State Province'; | |
184 | $params['country_id']['title'] = 'Primary Address State Province ID'; | |
185 | $params['country']['title'] = 'Primary Address country'; | |
186 | $params['worldregion_id']['title'] = 'Primary Address World Region ID'; | |
187 | $params['worldregion']['title'] = 'Primary Address World Region'; | |
188 | $params['phone_id']['title'] = 'Primary Phone ID'; | |
7d4ceb59 | 189 | $params['phone']['title'] = 'Primary Phone'; |
6a488035 TO |
190 | $params['phone_type_id']['title'] = 'Primary Phone Type ID'; |
191 | $params['provider_id']['title'] = 'Primary Phone Provider ID'; | |
192 | $params['email_id']['title'] = 'Primary Email ID'; | |
193 | $params['email']['title'] = 'Primary Email'; | |
194 | $params['on_hold']['title'] = 'Primary Email On Hold'; | |
195 | $params['im']['title'] = 'Primary Instant Messanger'; | |
196 | $params['im_id']['title'] = 'Primary Instant Messanger ID'; | |
197 | $params['group_id']['title'] = 'Group Memberships (filter)'; | |
198 | $params['group']['title'] = 'Group Memberships (filter, array)'; | |
199 | $params['tag']['title'] = 'Assigned tags (filter, array)'; | |
200 | } | |
201 | ||
11e09c59 | 202 | /** |
6a488035 TO |
203 | * We are supporting 'showAll' = 'all', 'trash' or 'active' for contact get |
204 | * and for getcount | |
205 | * - hopefully some day we'll come up with a std syntax for the 3-way-boolean of | |
206 | * 0, 1 or not set | |
207 | * | |
208 | * We also support 'filter_group_id' & 'filter.group_id' | |
209 | * | |
210 | * @param array $params as passed into api get or getcount function | |
211 | * @param array $options array of options (so we can modify the filter) | |
212 | */ | |
213 | function _civicrm_api3_contact_get_supportanomalies(&$params, &$options) { | |
214 | if (isset($params['showAll'])) { | |
215 | if (strtolower($params['showAll']) == "active") { | |
216 | $params['contact_is_deleted'] = 0; | |
217 | } | |
218 | if (strtolower($params['showAll']) == "trash") { | |
219 | $params['contact_is_deleted'] = 1; | |
220 | } | |
221 | if (strtolower($params['showAll']) == "all" && isset($params['contact_is_deleted'])) { | |
222 | unset($params['contact_is_deleted']); | |
223 | } | |
224 | } | |
225 | // support for group filters | |
226 | if (array_key_exists('filter_group_id', $params)) { | |
227 | $params['filter.group_id'] = $params['filter_group_id']; | |
228 | unset($params['filter_group_id']); | |
229 | } | |
230 | // filter.group_id works both for 1,2,3 and array (1,2,3) | |
231 | if (array_key_exists('filter.group_id', $params)) { | |
232 | if (is_array($params['filter.group_id'])) { | |
233 | $groups = $params['filter.group_id']; | |
234 | } | |
235 | else $groups = explode(',', $params['filter.group_id']); | |
236 | unset($params['filter.group_id']); | |
237 | $groups = array_flip($groups); | |
238 | $groups[key($groups)] = 1; | |
239 | $options['input_params']['group'] = $groups; | |
240 | } | |
241 | } | |
242 | ||
243 | /** | |
244 | * Delete a contact with given contact id | |
245 | * | |
246 | * @param array $params (reference ) input parameters, contact_id element required | |
247 | * | |
248 | * @return array API Result Array | |
249 | * @access public | |
250 | * | |
251 | * @example ContactDelete.php | |
252 | * {@getfields contact_delete} | |
253 | */ | |
254 | function civicrm_api3_contact_delete($params) { | |
255 | ||
256 | $contactID = CRM_Utils_Array::value('id', $params); | |
257 | ||
258 | $session = CRM_Core_Session::singleton(); | |
259 | if ($contactID == $session->get('userID')) { | |
260 | return civicrm_api3_create_error('This contact record is linked to the currently logged in user account - and cannot be deleted.'); | |
261 | } | |
262 | $restore = CRM_Utils_Array::value('restore', $params) ? $params['restore'] : FALSE; | |
263 | $skipUndelete = CRM_Utils_Array::value('skip_undelete', $params) ? $params['skip_undelete'] : FALSE; | |
f182074e PN |
264 | |
265 | // CRM-12929 | |
266 | // restrict permanent delete if a contact has financial trxn associated with it | |
267 | $error = NULL; | |
268 | if ($skipUndelete && CRM_Financial_BAO_FinancialItem::checkContactPresent(array($contactID), $error)) { | |
ad3f841d | 269 | return civicrm_api3_create_error($error['_qf_default']); |
f182074e | 270 | } |
6a488035 TO |
271 | if (CRM_Contact_BAO_Contact::deleteContact($contactID, $restore, $skipUndelete)) { |
272 | return civicrm_api3_create_success(); | |
273 | } | |
274 | else { | |
275 | return civicrm_api3_create_error('Could not delete contact'); | |
276 | } | |
277 | } | |
278 | ||
279 | ||
280 | function _civicrm_api3_contact_check_params( &$params, $dupeCheck = true, $dupeErrorArray = false, $obsoletevalue = true, $dedupeRuleGroupID = null ) | |
281 | { | |
282 | ||
283 | switch (strtolower(CRM_Utils_Array::value('contact_type', $params))) { | |
284 | case 'household': | |
285 | civicrm_api3_verify_mandatory($params, null, array('household_name')); | |
286 | break; | |
287 | case 'organization': | |
288 | civicrm_api3_verify_mandatory($params, null, array('organization_name')); | |
289 | break; | |
290 | case 'individual': | |
291 | civicrm_api3_verify_one_mandatory($params, null, array( | |
292 | 'first_name', | |
293 | 'last_name', | |
294 | 'email', | |
295 | 'display_name', | |
296 | ) | |
297 | ); | |
298 | break; | |
299 | } | |
300 | ||
fe18a93c CW |
301 | // Fixme: This really needs to be handled at a lower level. @See CRM-13123 |
302 | if (isset($params['preferred_communication_method'])) { | |
303 | $params['preferred_communication_method'] = CRM_Utils_Array::implodePadded($params['preferred_communication_method']); | |
304 | } | |
305 | ||
6a488035 TO |
306 | if (CRM_Utils_Array::value('contact_sub_type', $params) && CRM_Utils_Array::value('contact_type', $params)) { |
307 | if (!(CRM_Contact_BAO_ContactType::isExtendsContactType($params['contact_sub_type'], $params['contact_type']))) { | |
6ecbca5b | 308 | throw new API_Exception("Invalid or Mismatched Contact SubType: " . implode(', ', (array)$params['contact_sub_type'])); |
6a488035 TO |
309 | } |
310 | } | |
311 | ||
312 | if ($dupeCheck) { | |
313 | // check for record already existing | |
314 | $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']); | |
315 | ||
316 | // CRM-6431 | |
317 | // setting 'check_permission' here means that the dedupe checking will be carried out even if the | |
318 | // person does not have permission to carry out de-dupes | |
319 | // this is similar to the front end form | |
320 | if (isset($params['check_permission'])) { | |
321 | $dedupeParams['check_permission'] = $params['check_permission']; | |
322 | } | |
323 | ||
324 | $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type'], 'Strict', array()); | |
325 | ||
326 | if (count($ids) >0) { | |
327 | throw new API_Exception("Found matching contacts: ". implode(',',$ids),"duplicate",array("ids"=>$ids)); | |
328 | } | |
329 | } | |
330 | ||
331 | //check for organisations with same name | |
332 | if (!empty($params['current_employer'])) { | |
333 | $organizationParams = array(); | |
334 | $organizationParams['organization_name'] = $params['current_employer']; | |
335 | ||
336 | $dedupParams = CRM_Dedupe_Finder::formatParams($organizationParams, 'Organization'); | |
337 | ||
338 | $dedupParams['check_permission'] = FALSE; | |
339 | $dupeIds = CRM_Dedupe_Finder::dupesByParams($dedupParams, 'Organization', 'Supervised'); | |
340 | ||
341 | // check for mismatch employer name and id | |
342 | if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds)) { | |
343 | throw new API_Exception('Employer name and Employer id Mismatch'); | |
344 | } | |
345 | ||
346 | // show error if multiple organisation with same name exist | |
347 | if (empty($params['employer_id']) && (count($dupeIds) > 1)) { | |
348 | throw new API_Exception('Found more than one Organisation with same Name.'); | |
349 | } | |
350 | } | |
351 | ||
352 | return NULL; | |
353 | } | |
354 | ||
355 | /** | |
356 | * Takes an associative array and creates a contact object and all the associated | |
357 | * derived objects (i.e. individual, location, email, phone etc) | |
358 | * | |
359 | * @param array $params (reference ) an assoc array of name/value pairs | |
360 | * @param int $contactID if present the contact with that ID is updated | |
361 | * | |
362 | * @return object CRM_Contact_BAO_Contact object | |
363 | * @access public | |
364 | * @static | |
365 | */ | |
366 | function _civicrm_api3_contact_update($params, $contactID = NULL) { | |
6ecbca5b | 367 | //@todo - doesn't contact create support 'id' which is already set- check & remove |
6a488035 TO |
368 | if ($contactID) { |
369 | $params['contact_id'] = $contactID; | |
370 | } | |
371 | ||
6ecbca5b | 372 | return CRM_Contact_BAO_Contact::create($params); |
6a488035 TO |
373 | } |
374 | ||
375 | /** | |
376 | * Validate the addressee or email or postal greetings | |
377 | * | |
378 | * @param $params Associative array of property name/value | |
379 | * pairs to insert in new contact. | |
380 | * | |
381 | * @return array (reference ) null on success, error message otherwise | |
382 | * | |
383 | * @access public | |
384 | */ | |
385 | function _civicrm_api3_greeting_format_params($params) { | |
386 | $greetingParams = array('', '_id', '_custom'); | |
387 | foreach (array('email', 'postal', 'addressee') as $key) { | |
388 | $greeting = '_greeting'; | |
389 | if ($key == 'addressee') { | |
390 | $greeting = ''; | |
391 | } | |
392 | ||
393 | $formatParams = FALSE; | |
394 | // unset display value from params. | |
395 | if (isset($params["{$key}{$greeting}_display"])) { | |
396 | unset($params["{$key}{$greeting}_display"]); | |
397 | } | |
398 | ||
399 | // check if greetings are present in present | |
400 | foreach ($greetingParams as $greetingValues) { | |
401 | if (array_key_exists("{$key}{$greeting}{$greetingValues}", $params)) { | |
402 | $formatParams = TRUE; | |
403 | break; | |
404 | } | |
405 | } | |
406 | ||
407 | if (!$formatParams) { | |
408 | continue; | |
409 | } | |
410 | ||
411 | $nullValue = FALSE; | |
412 | $filter = array( | |
413 | 'contact_type' => $params['contact_type'], | |
414 | 'greeting_type' => "{$key}{$greeting}", | |
415 | ); | |
416 | ||
417 | $greetings = CRM_Core_PseudoConstant::greeting($filter); | |
418 | $greetingId = CRM_Utils_Array::value("{$key}{$greeting}_id", $params); | |
419 | $greetingVal = CRM_Utils_Array::value("{$key}{$greeting}", $params); | |
420 | $customGreeting = CRM_Utils_Array::value("{$key}{$greeting}_custom", $params); | |
421 | ||
422 | if (!$greetingId && $greetingVal) { | |
423 | $params["{$key}{$greeting}_id"] = CRM_Utils_Array::key($params["{$key}{$greeting}"], $greetings); | |
424 | } | |
425 | ||
426 | if ($customGreeting && $greetingId && | |
427 | ($greetingId != array_search('Customized', $greetings)) | |
428 | ) { | |
6ecbca5b | 429 | throw new API_Exception(ts('Provide either %1 greeting id and/or %1 greeting or custom %1 greeting', |
6a488035 TO |
430 | array(1 => $key) |
431 | )); | |
432 | } | |
433 | ||
434 | if ($greetingVal && $greetingId && | |
435 | ($greetingId != CRM_Utils_Array::key($greetingVal, $greetings)) | |
436 | ) { | |
6ecbca5b | 437 | throw new API_Exception(ts('Mismatch in %1 greeting id and %1 greeting', |
6a488035 TO |
438 | array(1 => $key) |
439 | )); | |
440 | } | |
441 | ||
442 | if ($greetingId) { | |
443 | ||
444 | if (!array_key_exists($greetingId, $greetings)) { | |
6ecbca5b | 445 | throw new API_Exception(ts('Invalid %1 greeting Id', array(1 => $key))); |
6a488035 TO |
446 | } |
447 | ||
448 | if (!$customGreeting && ($greetingId == array_search('Customized', $greetings))) { | |
6ecbca5b | 449 | throw new API_Exception(ts('Please provide a custom value for %1 greeting', |
6a488035 TO |
450 | array(1 => $key) |
451 | )); | |
452 | } | |
453 | } | |
454 | elseif ($greetingVal) { | |
455 | ||
456 | if (!in_array($greetingVal, $greetings)) { | |
6ecbca5b | 457 | throw new API_Exception(ts('Invalid %1 greeting', array(1 => $key))); |
6a488035 TO |
458 | } |
459 | ||
460 | $greetingId = CRM_Utils_Array::key($greetingVal, $greetings); | |
461 | } | |
462 | ||
463 | if ($customGreeting) { | |
464 | $greetingId = CRM_Utils_Array::key('Customized', $greetings); | |
465 | } | |
466 | ||
ad3f841d DL |
467 | $customValue = isset($params['contact_id']) ? |
468 | CRM_Core_DAO::getFieldValue( | |
469 | 'CRM_Contact_DAO_Contact', | |
470 | $params['contact_id'], | |
471 | "{$key}{$greeting}_custom" | |
472 | ) : | |
473 | FALSE; | |
6a488035 TO |
474 | |
475 | if (array_key_exists("{$key}{$greeting}_id", $params) && empty($params["{$key}{$greeting}_id"])) { | |
476 | $nullValue = TRUE; | |
477 | } | |
478 | elseif (array_key_exists("{$key}{$greeting}", $params) && empty($params["{$key}{$greeting}"])) { | |
479 | $nullValue = TRUE; | |
480 | } | |
481 | elseif ($customValue && array_key_exists("{$key}{$greeting}_custom", $params) | |
482 | && empty($params["{$key}{$greeting}_custom"]) | |
483 | ) { | |
484 | $nullValue = TRUE; | |
485 | } | |
486 | ||
487 | $params["{$key}{$greeting}_id"] = $greetingId; | |
488 | ||
489 | if (!$customValue && !$customGreeting && array_key_exists("{$key}{$greeting}_custom", $params)) { | |
490 | unset($params["{$key}{$greeting}_custom"]); | |
491 | } | |
492 | ||
493 | if ($nullValue) { | |
494 | $params["{$key}{$greeting}_id"] = ''; | |
495 | $params["{$key}{$greeting}_custom"] = ''; | |
496 | } | |
497 | ||
498 | if (isset($params["{$key}{$greeting}"])) { | |
499 | unset($params["{$key}{$greeting}"]); | |
500 | } | |
501 | } | |
502 | } | |
503 | ||
504 | /** | |
505 | * Contact quick search api | |
506 | * | |
507 | * @access public | |
508 | * | |
509 | * {@example ContactGetquick.php 0} | |
510 | * | |
511 | */ | |
512 | function civicrm_api3_contact_quicksearch($params) { | |
513 | // kept as an alias for compatibility reasons. CRM-11136 | |
514 | return civicrm_api3_contact_getquick($params); | |
515 | } | |
516 | ||
517 | function civicrm_api3_contact_getquick($params) { | |
518 | civicrm_api3_verify_mandatory($params, NULL, array('name')); | |
1aba4d9a | 519 | $name = CRM_Utils_Type::escape($params['name'], 'String'); |
6a488035 TO |
520 | |
521 | // get the autocomplete options from settings | |
522 | $acpref = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
523 | CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
524 | 'contact_autocomplete_options' | |
525 | ) | |
526 | ); | |
527 | ||
528 | // get the option values for contact autocomplete | |
529 | $acOptions = CRM_Core_OptionGroup::values('contact_autocomplete_options', FALSE, FALSE, FALSE, NULL, 'name'); | |
530 | ||
531 | $list = array(); | |
532 | foreach ($acpref as $value) { | |
533 | if ($value && CRM_Utils_Array::value($value, $acOptions)) { | |
534 | $list[$value] = $acOptions[$value]; | |
535 | } | |
536 | } | |
537 | // If we are doing quicksearch by a field other than name, make sure that field is added to results | |
538 | if (!empty($params['field_name'])) { | |
1aba4d9a | 539 | $field_name = CRM_Utils_String::munge($params['field_name']); |
5d8ba7a7 | 540 | // Unique name contact_id = id |
1aba4d9a CW |
541 | if ($field_name == 'contact_id') { |
542 | $field_name = 'id'; | |
5d8ba7a7 | 543 | } |
6a488035 | 544 | // phone_numeric should be phone |
1aba4d9a | 545 | $searchField = str_replace('_numeric', '', $field_name); |
6a488035 TO |
546 | if(!in_array($searchField, $list)) { |
547 | $list[] = $searchField; | |
548 | } | |
549 | } | |
550 | ||
551 | $select = $actualSelectElements = array('sort_name'); | |
552 | $where = ''; | |
553 | $from = array(); | |
554 | foreach ($list as $value) { | |
555 | $suffix = substr($value, 0, 2) . substr($value, -1); | |
556 | switch ($value) { | |
557 | case 'street_address': | |
558 | case 'city': | |
559 | case 'postal_code': | |
560 | $selectText = $value; | |
561 | $value = "address"; | |
562 | $suffix = 'sts'; | |
563 | case 'phone': | |
564 | case 'email': | |
565 | $actualSelectElements[] = $select[] = ($value == 'address') ? $selectText : $value; | |
566 | $from[$value] = "LEFT JOIN civicrm_{$value} {$suffix} ON ( cc.id = {$suffix}.contact_id AND {$suffix}.is_primary = 1 ) "; | |
567 | break; | |
568 | ||
569 | case 'country': | |
570 | case 'state_province': | |
571 | $select[] = "{$suffix}.name as {$value}"; | |
572 | $actualSelectElements[] = "{$suffix}.name"; | |
573 | if (!in_array('address', $from)) { | |
574 | $from['address'] = 'LEFT JOIN civicrm_address sts ON ( cc.id = sts.contact_id AND sts.is_primary = 1) '; | |
575 | } | |
576 | $from[$value] = " LEFT JOIN civicrm_{$value} {$suffix} ON ( sts.{$value}_id = {$suffix}.id ) "; | |
577 | break; | |
578 | ||
579 | default: | |
580 | if ($value != 'id') { | |
581 | $suffix = 'cc'; | |
582 | if (!empty($params['field_name']) && $params['field_name'] == 'value') { | |
133da98d | 583 | $suffix = CRM_Utils_String::munge(CRM_Utils_Array::value('table_name', $params, 'cc')); |
6a488035 TO |
584 | } |
585 | $actualSelectElements[] = $select[] = $suffix . '.' . $value; | |
586 | } | |
587 | break; | |
588 | } | |
589 | } | |
590 | ||
591 | $config = CRM_Core_Config::singleton(); | |
592 | $as = $select; | |
593 | $select = implode(', ', $select); | |
594 | if (!empty($select)) { | |
595 | $select = ", $select"; | |
596 | } | |
597 | $actualSelectElements = implode(', ', $actualSelectElements); | |
598 | $selectAliases = $from; | |
599 | unset($selectAliases['address']); | |
600 | $selectAliases = implode(', ', array_keys($selectAliases)); | |
601 | if (!empty($selectAliases)) { | |
602 | $selectAliases = ", $selectAliases"; | |
603 | } | |
604 | $from = implode(' ', $from); | |
133da98d CW |
605 | $limit = (int) CRM_Utils_Array::value('limit', $params); |
606 | $limit = $limit > 0 ? $limit : 10; | |
6a488035 TO |
607 | |
608 | // add acl clause here | |
609 | list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause('cc'); | |
610 | ||
611 | if ($aclWhere) { | |
612 | $where .= " AND $aclWhere "; | |
613 | } | |
614 | ||
615 | if (CRM_Utils_Array::value('org', $params)) { | |
616 | $where .= " AND contact_type = \"Organization\""; | |
617 | ||
618 | // CRM-7157, hack: get current employer details when | |
619 | // employee_id is present. | |
620 | $currEmpDetails = array(); | |
621 | if (CRM_Utils_Array::value('employee_id', $params)) { | |
622 | if ($currentEmployer = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
133da98d | 623 | (int) $params['employee_id'], |
6a488035 TO |
624 | 'employer_id' |
625 | )) { | |
626 | if ($config->includeWildCardInName) { | |
627 | $strSearch = "%$name%"; | |
628 | } | |
629 | else { | |
630 | $strSearch = "$name%"; | |
631 | } | |
632 | ||
633 | // get current employer details | |
634 | $dao = CRM_Core_DAO::executeQuery("SELECT cc.id as id, CONCAT_WS( ' :: ', {$actualSelectElements} ) as data, sort_name | |
635 | FROM civicrm_contact cc {$from} WHERE cc.contact_type = \"Organization\" AND cc.id = {$currentEmployer} AND cc.sort_name LIKE '$strSearch'"); | |
636 | if ($dao->fetch()) { | |
637 | $currEmpDetails = array( | |
638 | 'id' => $dao->id, | |
639 | 'data' => $dao->data, | |
640 | ); | |
641 | } | |
642 | } | |
643 | } | |
644 | } | |
645 | ||
646 | //set default for current_employer or return contact with particular id | |
647 | if (CRM_Utils_Array::value('id', $params)) { | |
1aba4d9a | 648 | $where .= " AND cc.id = " . (int) $params['id']; |
6a488035 TO |
649 | } |
650 | ||
651 | if (CRM_Utils_Array::value('cid', $params)) { | |
1aba4d9a | 652 | $where .= " AND cc.id <> " . (int) $params['cid']; |
6a488035 TO |
653 | } |
654 | ||
655 | //contact's based of relationhip type | |
656 | $relType = NULL; | |
657 | if (CRM_Utils_Array::value('rel', $params)) { | |
658 | $relation = explode('_', CRM_Utils_Array::value('rel', $params)); | |
659 | $relType = CRM_Utils_Type::escape($relation[0], 'Integer'); | |
660 | $rel = CRM_Utils_Type::escape($relation[2], 'String'); | |
661 | } | |
662 | ||
663 | if ($config->includeWildCardInName) { | |
664 | $strSearch = "%$name%"; | |
665 | } | |
666 | else { | |
667 | $strSearch = "$name%"; | |
668 | } | |
669 | $includeEmailFrom = $includeNickName = $exactIncludeNickName = ''; | |
670 | if ($config->includeNickNameInName) { | |
671 | $includeNickName = " OR nick_name LIKE '$strSearch'"; | |
672 | $exactIncludeNickName = " OR nick_name LIKE '$name'"; | |
673 | } | |
674 | ||
675 | //CRM-10687 | |
676 | if (!empty($params['field_name']) && !empty($params['table_name'])) { | |
1aba4d9a | 677 | $table_name = CRM_Utils_String::munge($params['table_name']); |
6a488035 TO |
678 | $whereClause = " WHERE ( $table_name.$field_name LIKE '$strSearch')"; |
679 | $exactWhereClause = " WHERE ( $table_name.$field_name = '$name')"; | |
680 | // Search by id should be exact | |
681 | if ($field_name == 'id' || $field_name == 'external_identifier') { | |
682 | $whereClause = $exactWhereClause; | |
683 | } | |
684 | } | |
685 | else { | |
686 | if ($config->includeEmailInName) { | |
687 | if (!in_array('email', $list)) { | |
688 | $includeEmailFrom = "LEFT JOIN civicrm_email eml ON ( cc.id = eml.contact_id AND eml.is_primary = 1 )"; | |
689 | } | |
690 | $whereClause = " WHERE ( email LIKE '$strSearch' OR sort_name LIKE '$strSearch' $includeNickName ) {$where} "; | |
691 | $exactWhereClause = " WHERE ( email LIKE '$name' OR sort_name LIKE '$name' $exactIncludeNickName ) {$where} "; | |
692 | } | |
693 | else { | |
694 | $whereClause = " WHERE ( sort_name LIKE '$strSearch' $includeNickName ) {$where} "; | |
695 | $exactWhereClause = " WHERE ( sort_name LIKE '$name' $exactIncludeNickName ) {$where} "; | |
696 | } | |
697 | } | |
698 | ||
699 | $additionalFrom = ''; | |
700 | if ($relType) { | |
701 | $additionalFrom = " | |
702 | INNER JOIN civicrm_relationship_type r ON ( | |
703 | r.id = {$relType} | |
704 | AND ( cc.contact_type = r.contact_type_{$rel} OR r.contact_type_{$rel} IS NULL ) | |
705 | AND ( cc.contact_sub_type = r.contact_sub_type_{$rel} OR r.contact_sub_type_{$rel} IS NULL ) | |
706 | )"; | |
707 | } | |
708 | ||
709 | // check if only CMS users are requested | |
710 | if (CRM_Utils_Array::value('cmsuser', $params)) { | |
711 | $additionalFrom = " | |
712 | INNER JOIN civicrm_uf_match um ON (um.contact_id=cc.id) | |
713 | "; | |
714 | } | |
715 | ||
51e61eae ARW |
716 | $orderByInner = ""; |
717 | $orderByOuter = "ORDER BY exactFirst"; | |
718 | if ($config->includeOrderByClause) { | |
719 | $orderByInner = "ORDER BY sort_name"; | |
720 | $orderByOuter .= ", sort_name"; | |
721 | } | |
722 | ||
6a488035 TO |
723 | //CRM-5954 |
724 | $query = " | |
725 | SELECT DISTINCT(id), data, sort_name {$selectAliases} | |
726 | FROM ( | |
727 | ( SELECT 0 as exactFirst, cc.id as id, CONCAT_WS( ' :: ', {$actualSelectElements} ) as data {$select} | |
728 | FROM civicrm_contact cc {$from} | |
729 | {$aclFrom} | |
730 | {$additionalFrom} {$includeEmailFrom} | |
731 | {$exactWhereClause} | |
732 | LIMIT 0, {$limit} ) | |
733 | UNION | |
734 | ( SELECT 1 as exactFirst, cc.id as id, CONCAT_WS( ' :: ', {$actualSelectElements} ) as data {$select} | |
735 | FROM civicrm_contact cc {$from} | |
736 | {$aclFrom} | |
737 | {$additionalFrom} {$includeEmailFrom} | |
738 | {$whereClause} | |
51e61eae | 739 | {$orderByInner} |
6a488035 TO |
740 | LIMIT 0, {$limit} ) |
741 | ) t | |
51e61eae | 742 | {$orderByOuter} |
6a488035 TO |
743 | LIMIT 0, {$limit} |
744 | "; | |
745 | // send query to hook to be modified if needed | |
746 | CRM_Utils_Hook::contactListQuery($query, | |
747 | $name, | |
133da98d CW |
748 | empty($params['context']) ? NULL : CRM_Utils_Type::escape($params['context'], 'String'), |
749 | empty($params['id']) ? NULL : $params['id'] | |
6a488035 TO |
750 | ); |
751 | ||
752 | $dao = CRM_Core_DAO::executeQuery($query); | |
753 | ||
754 | $contactList = array(); | |
755 | $listCurrentEmployer = TRUE; | |
756 | while ($dao->fetch()) { | |
757 | $t = array('id' => $dao->id); | |
758 | foreach ($as as $k) { | |
759 | $t[$k] = isset($dao->$k)? $dao->$k: ''; | |
760 | } | |
761 | $t['data'] = $dao->data; | |
762 | $contactList[] = $t; | |
763 | if (CRM_Utils_Array::value('org', $params) && | |
764 | !empty($currEmpDetails) && | |
765 | $dao->id == $currEmpDetails['id'] | |
766 | ) { | |
767 | $listCurrentEmployer = FALSE; | |
768 | } | |
769 | } | |
770 | ||
771 | //return organization name if doesn't exist in db | |
772 | if (empty($contactList)) { | |
773 | if (CRM_Utils_Array::value('org', $params)) { | |
774 | if ($listCurrentEmployer && !empty($currEmpDetails)) { | |
775 | $contactList = array( | |
776 | array( | |
777 | 'data' => $currEmpDetails['data'], | |
778 | 'id' => $currEmpDetails['id'] | |
779 | ) | |
780 | ); | |
781 | } | |
782 | else { | |
783 | $contactList = array( | |
784 | array( | |
785 | 'data' => $name, | |
786 | 'id' => $name | |
787 | ) | |
788 | ); | |
789 | } | |
790 | } | |
791 | } | |
792 | ||
793 | return civicrm_api3_create_success($contactList, $params); | |
794 | } | |
795 | ||
796 | /** | |
797 | * Merges given pair of duplicate contacts. | |
798 | * | |
799 | * @param array $params input parameters | |
800 | * | |
801 | * Allowed @params array keys are: | |
802 | * {int main_id main contact id with whom merge has to happen} | |
803 | * {int other_id duplicate contact which would be deleted after merge operation} | |
804 | * {string mode helps decide how to behave when there are conflicts. | |
805 | * A 'safe' value skips the merge if there are no conflicts. Does a force merge otherwise.} | |
806 | * {boolean auto_flip wether to let api decide which contact to retain and which to delete.} | |
807 | * | |
808 | * @return array API Result Array | |
809 | * | |
810 | * @static void | |
811 | * @access public | |
812 | */ | |
813 | function civicrm_api3_contact_merge($params) { | |
814 | $mode = CRM_Utils_Array::value('mode', $params, 'safe'); | |
815 | $autoFlip = CRM_Utils_Array::value('auto_flip', $params, TRUE); | |
816 | ||
817 | $dupePairs = array(array('srcID' => CRM_Utils_Array::value('main_id', $params), | |
818 | 'dstID' => CRM_Utils_Array::value('other_id', $params), | |
819 | )); | |
820 | $result = CRM_Dedupe_Merger::merge($dupePairs, array(), $mode, $autoFlip); | |
821 | ||
822 | if ($result['is_error'] == 0) { | |
823 | return civicrm_api3_create_success(); | |
824 | } | |
825 | else { | |
826 | return civicrm_api3_create_error($result['messages']); | |
827 | } | |
828 | } | |
829 | ||
830 | function _civicrm_api3_contact_proximity_spec(&$params) { | |
831 | $params['latitude']['api.required'] = 1; | |
832 | $params['longitude']['api.required'] = 1; | |
833 | $params['unit']['api.default'] = 'meter'; | |
834 | } | |
835 | ||
836 | function civicrm_api3_contact_proximity($params) { | |
837 | $latitude = CRM_Utils_Array::value('latitude', $params); | |
838 | $longitude = CRM_Utils_Array::value('longitude', $params); | |
839 | $distance = CRM_Utils_Array::value('distance', $params); | |
840 | ||
841 | $unit = CRM_Utils_Array::value('unit', $params); | |
842 | ||
843 | // check and ensure that lat/long and distance are floats | |
844 | if ( | |
845 | !CRM_Utils_Rule::numeric($latitude) || | |
846 | !CRM_Utils_Rule::numeric($longitude) || | |
847 | !CRM_Utils_Rule::numeric($distance) | |
848 | ) { | |
849 | throw new Exception(ts('Latitude, Longitude and Distance should exist and be numeric')); | |
850 | } | |
851 | ||
852 | if ($unit == "mile") { | |
853 | $conversionFactor = 1609.344; | |
854 | } | |
855 | else { | |
856 | $conversionFactor = 1000; | |
857 | } | |
858 | //Distance in meters | |
859 | $distance = $distance * $conversionFactor; | |
860 | ||
861 | $whereClause = CRM_Contact_BAO_ProximityQuery::where($latitude, $longitude, $distance); | |
862 | ||
863 | $query = " | |
864 | SELECT civicrm_contact.id as contact_id, | |
865 | civicrm_contact.display_name as display_name | |
866 | FROM civicrm_contact | |
867 | LEFT JOIN civicrm_address ON civicrm_contact.id = civicrm_address.contact_id | |
868 | WHERE $whereClause | |
869 | "; | |
870 | ||
871 | $dao = CRM_Core_DAO::executeQuery($query); | |
872 | $contacts = array(); | |
873 | while ($dao->fetch()) { | |
874 | $contacts[] = $dao->toArray(); | |
875 | } | |
876 | ||
877 | return civicrm_api3_create_success($contacts, $params, 'contact', 'get_by_location', $dao); | |
878 | } | |
879 |