Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
731a0992 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
731a0992 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
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 | |
731a0992 | 35 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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', | |
9c5991b3 | 126 | 'type' => CRM_Utils_Type::T_STRING, |
6a488035 | 127 | ); |
0391dc25 | 128 | $params['dupe_check'] = array( |
129 | 'title' => 'Check for Duplicates', | |
130 | 'description' => 'Throw error if contact create matches dedupe rule', | |
131 | ); | |
6ecbca5b | 132 | $params['prefix_id']['api.aliases'] = array('individual_prefix', 'individual_prefix_id'); |
133 | $params['suffix_id']['api.aliases'] = array('individual_suffix', 'individual_suffix_id'); | |
6a488035 TO |
134 | } |
135 | ||
136 | /** | |
137 | * Retrieve one or more contacts, given a set of search params | |
138 | * | |
139 | * @param array input parameters | |
140 | * | |
141 | * @return array API Result Array | |
142 | * (@getfields contact_get} | |
143 | * @static void | |
144 | * @access public | |
145 | * | |
146 | * @example ContactGet.php Standard GET example | |
147 | * | |
148 | */ | |
149 | function civicrm_api3_contact_get($params) { | |
150 | $options = array(); | |
151 | _civicrm_api3_contact_get_supportanomalies($params, $options); | |
152 | $contacts = _civicrm_api3_get_using_query_object('contact', $params, $options); | |
6ecbca5b | 153 | return civicrm_api3_create_success($contacts, $params, 'contact'); |
6a488035 TO |
154 | } |
155 | ||
156 | function civicrm_api3_contact_getcount($params) { | |
157 | $options = array(); | |
158 | _civicrm_api3_contact_get_supportanomalies($params, $options); | |
159 | $count = _civicrm_api3_get_using_query_object('contact', $params, $options,1); | |
972322c5 | 160 | return (int) $count; |
6a488035 | 161 | } |
11e09c59 TO |
162 | |
163 | /** | |
6a488035 TO |
164 | * Adjust Metadata for Get action |
165 | * | |
166 | * @param array $params array or parameters determined by getfields | |
167 | */ | |
168 | function _civicrm_api3_contact_get_spec(&$params) { | |
169 | $params['contact_is_deleted']['api.default'] = 0; | |
170 | ||
171 | //we declare all these pseudofields as there are other undocumented fields accessible | |
172 | // via the api - but if check permissions is set we only allow declared fields | |
173 | $params['address_id']['title'] = 'Primary Address ID'; | |
174 | $params['street_address']['title'] = 'Primary Address Street Address'; | |
175 | $params['supplemental_address_1']['title'] = 'Primary Address Supplemental Address 1'; | |
176 | $params['supplemental_address_2']['title'] = 'Primary Address Supplemental Address 2'; | |
23c2fe57 | 177 | $params['current_employer']['title'] = 'Current Employer'; |
6a488035 TO |
178 | $params['city']['title'] = 'Primary Address City'; |
179 | $params['postal_code_suffix']['title'] = 'Primary Address Post Code Suffix'; | |
180 | $params['postal_code']['title'] = 'Primary Address Post Code'; | |
181 | $params['geo_code_1']['title'] = 'Primary Address Latitude'; | |
182 | $params['geo_code_2']['title'] = 'Primary Address Longitude'; | |
183 | $params['state_province_id']['title'] = 'Primary Address State Province ID'; | |
184 | $params['state_province_name']['title'] = 'Primary Address State Province Name'; | |
185 | $params['state_province']['title'] = 'Primary Address State Province'; | |
41847fb4 | 186 | $params['country_id']['title'] = 'Primary Address Country ID'; |
6a488035 TO |
187 | $params['country']['title'] = 'Primary Address country'; |
188 | $params['worldregion_id']['title'] = 'Primary Address World Region ID'; | |
189 | $params['worldregion']['title'] = 'Primary Address World Region'; | |
190 | $params['phone_id']['title'] = 'Primary Phone ID'; | |
7d4ceb59 | 191 | $params['phone']['title'] = 'Primary Phone'; |
6a488035 TO |
192 | $params['phone_type_id']['title'] = 'Primary Phone Type ID'; |
193 | $params['provider_id']['title'] = 'Primary Phone Provider ID'; | |
194 | $params['email_id']['title'] = 'Primary Email ID'; | |
195 | $params['email']['title'] = 'Primary Email'; | |
c2280651 | 196 | $params['gender_id']['title'] = 'Gender ID'; |
197 | $params['gender']['title'] = 'Gender'; | |
6a488035 TO |
198 | $params['on_hold']['title'] = 'Primary Email On Hold'; |
199 | $params['im']['title'] = 'Primary Instant Messanger'; | |
200 | $params['im_id']['title'] = 'Primary Instant Messanger ID'; | |
201 | $params['group_id']['title'] = 'Group Memberships (filter)'; | |
202 | $params['group']['title'] = 'Group Memberships (filter, array)'; | |
203 | $params['tag']['title'] = 'Assigned tags (filter, array)'; | |
9f60788a EM |
204 | $params['birth_date_low'] = array('name' => 'birth_date_low', 'type' => CRM_Utils_Type::T_DATE, 'title' => ts('Birthdate is equal to or greater than')); |
205 | $params['birth_date_high'] = array('name' => 'birth_date_high', 'type' => CRM_Utils_Type::T_DATE, 'title' => ts('Birthdate is equal to or less than')); | |
206 | $params['deceased_date_low'] = array('name' => 'deceased_date_low','type' => CRM_Utils_Type::T_DATE, 'title' => ts('Deceased Date is equal to or greater than')); | |
207 | $params['deceased_date_high'] = array('name' => 'deceased_date_high', 'type' => CRM_Utils_Type::T_DATE, 'title' => ts('Deceased Date is equal to or less than')); | |
6a488035 TO |
208 | } |
209 | ||
11e09c59 | 210 | /** |
6a488035 TO |
211 | * We are supporting 'showAll' = 'all', 'trash' or 'active' for contact get |
212 | * and for getcount | |
213 | * - hopefully some day we'll come up with a std syntax for the 3-way-boolean of | |
214 | * 0, 1 or not set | |
215 | * | |
216 | * We also support 'filter_group_id' & 'filter.group_id' | |
217 | * | |
218 | * @param array $params as passed into api get or getcount function | |
219 | * @param array $options array of options (so we can modify the filter) | |
220 | */ | |
221 | function _civicrm_api3_contact_get_supportanomalies(&$params, &$options) { | |
222 | if (isset($params['showAll'])) { | |
223 | if (strtolower($params['showAll']) == "active") { | |
224 | $params['contact_is_deleted'] = 0; | |
225 | } | |
226 | if (strtolower($params['showAll']) == "trash") { | |
227 | $params['contact_is_deleted'] = 1; | |
228 | } | |
229 | if (strtolower($params['showAll']) == "all" && isset($params['contact_is_deleted'])) { | |
230 | unset($params['contact_is_deleted']); | |
231 | } | |
232 | } | |
233 | // support for group filters | |
234 | if (array_key_exists('filter_group_id', $params)) { | |
235 | $params['filter.group_id'] = $params['filter_group_id']; | |
236 | unset($params['filter_group_id']); | |
237 | } | |
238 | // filter.group_id works both for 1,2,3 and array (1,2,3) | |
239 | if (array_key_exists('filter.group_id', $params)) { | |
240 | if (is_array($params['filter.group_id'])) { | |
241 | $groups = $params['filter.group_id']; | |
242 | } | |
243 | else $groups = explode(',', $params['filter.group_id']); | |
244 | unset($params['filter.group_id']); | |
245 | $groups = array_flip($groups); | |
246 | $groups[key($groups)] = 1; | |
247 | $options['input_params']['group'] = $groups; | |
248 | } | |
249 | } | |
250 | ||
251 | /** | |
252 | * Delete a contact with given contact id | |
253 | * | |
254 | * @param array $params (reference ) input parameters, contact_id element required | |
255 | * | |
256 | * @return array API Result Array | |
257 | * @access public | |
258 | * | |
259 | * @example ContactDelete.php | |
260 | * {@getfields contact_delete} | |
261 | */ | |
262 | function civicrm_api3_contact_delete($params) { | |
263 | ||
264 | $contactID = CRM_Utils_Array::value('id', $params); | |
265 | ||
266 | $session = CRM_Core_Session::singleton(); | |
267 | if ($contactID == $session->get('userID')) { | |
268 | return civicrm_api3_create_error('This contact record is linked to the currently logged in user account - and cannot be deleted.'); | |
269 | } | |
0d8afee2 CW |
270 | $restore = !empty($params['restore']) ? $params['restore'] : FALSE; |
271 | $skipUndelete = !empty($params['skip_undelete']) ? $params['skip_undelete'] : FALSE; | |
f182074e PN |
272 | |
273 | // CRM-12929 | |
274 | // restrict permanent delete if a contact has financial trxn associated with it | |
275 | $error = NULL; | |
276 | if ($skipUndelete && CRM_Financial_BAO_FinancialItem::checkContactPresent(array($contactID), $error)) { | |
ad3f841d | 277 | return civicrm_api3_create_error($error['_qf_default']); |
f182074e | 278 | } |
6a488035 TO |
279 | if (CRM_Contact_BAO_Contact::deleteContact($contactID, $restore, $skipUndelete)) { |
280 | return civicrm_api3_create_success(); | |
281 | } | |
282 | else { | |
283 | return civicrm_api3_create_error('Could not delete contact'); | |
284 | } | |
285 | } | |
286 | ||
287 | ||
288 | function _civicrm_api3_contact_check_params( &$params, $dupeCheck = true, $dupeErrorArray = false, $obsoletevalue = true, $dedupeRuleGroupID = null ) | |
289 | { | |
290 | ||
291 | switch (strtolower(CRM_Utils_Array::value('contact_type', $params))) { | |
292 | case 'household': | |
293 | civicrm_api3_verify_mandatory($params, null, array('household_name')); | |
294 | break; | |
295 | case 'organization': | |
296 | civicrm_api3_verify_mandatory($params, null, array('organization_name')); | |
297 | break; | |
298 | case 'individual': | |
299 | civicrm_api3_verify_one_mandatory($params, null, array( | |
300 | 'first_name', | |
301 | 'last_name', | |
302 | 'email', | |
303 | 'display_name', | |
304 | ) | |
305 | ); | |
306 | break; | |
307 | } | |
308 | ||
fe18a93c CW |
309 | // Fixme: This really needs to be handled at a lower level. @See CRM-13123 |
310 | if (isset($params['preferred_communication_method'])) { | |
311 | $params['preferred_communication_method'] = CRM_Utils_Array::implodePadded($params['preferred_communication_method']); | |
312 | } | |
313 | ||
8cc574cf | 314 | if (!empty($params['contact_sub_type']) && !empty($params['contact_type'])) { |
6a488035 | 315 | if (!(CRM_Contact_BAO_ContactType::isExtendsContactType($params['contact_sub_type'], $params['contact_type']))) { |
6ecbca5b | 316 | throw new API_Exception("Invalid or Mismatched Contact SubType: " . implode(', ', (array)$params['contact_sub_type'])); |
6a488035 TO |
317 | } |
318 | } | |
319 | ||
320 | if ($dupeCheck) { | |
321 | // check for record already existing | |
322 | $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']); | |
323 | ||
324 | // CRM-6431 | |
325 | // setting 'check_permission' here means that the dedupe checking will be carried out even if the | |
326 | // person does not have permission to carry out de-dupes | |
327 | // this is similar to the front end form | |
328 | if (isset($params['check_permission'])) { | |
329 | $dedupeParams['check_permission'] = $params['check_permission']; | |
330 | } | |
331 | ||
67cfa333 | 332 | $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type'], 'Unsupervised', array()); |
6a488035 TO |
333 | |
334 | if (count($ids) >0) { | |
335 | throw new API_Exception("Found matching contacts: ". implode(',',$ids),"duplicate",array("ids"=>$ids)); | |
336 | } | |
337 | } | |
338 | ||
8d99ab37 | 339 | // The BAO no longer supports the legacy param "current_employer" so here is a shim for api backward-compatability |
6a488035 | 340 | if (!empty($params['current_employer'])) { |
8d99ab37 CW |
341 | $organizationParams = array( |
342 | 'organization_name' => $params['current_employer'], | |
343 | ); | |
6a488035 TO |
344 | |
345 | $dedupParams = CRM_Dedupe_Finder::formatParams($organizationParams, 'Organization'); | |
346 | ||
347 | $dedupParams['check_permission'] = FALSE; | |
348 | $dupeIds = CRM_Dedupe_Finder::dupesByParams($dedupParams, 'Organization', 'Supervised'); | |
349 | ||
350 | // check for mismatch employer name and id | |
351 | if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds)) { | |
352 | throw new API_Exception('Employer name and Employer id Mismatch'); | |
353 | } | |
354 | ||
355 | // show error if multiple organisation with same name exist | |
356 | if (empty($params['employer_id']) && (count($dupeIds) > 1)) { | |
357 | throw new API_Exception('Found more than one Organisation with same Name.'); | |
358 | } | |
8d99ab37 CW |
359 | |
360 | if ($dupeIds) { | |
361 | $params['employer_id'] = $dupeIds[0]; | |
362 | } | |
363 | else { | |
364 | $result = civicrm_api3('contact', 'create', array( | |
365 | 'organization_name' => $params['current_employer'], | |
366 | 'contact_type' => 'Organization' | |
367 | )); | |
368 | $params['employer_id'] = $result['id']; | |
369 | } | |
6a488035 TO |
370 | } |
371 | ||
372 | return NULL; | |
373 | } | |
374 | ||
375 | /** | |
376 | * Takes an associative array and creates a contact object and all the associated | |
377 | * derived objects (i.e. individual, location, email, phone etc) | |
378 | * | |
379 | * @param array $params (reference ) an assoc array of name/value pairs | |
380 | * @param int $contactID if present the contact with that ID is updated | |
381 | * | |
382 | * @return object CRM_Contact_BAO_Contact object | |
383 | * @access public | |
384 | * @static | |
385 | */ | |
386 | function _civicrm_api3_contact_update($params, $contactID = NULL) { | |
6ecbca5b | 387 | //@todo - doesn't contact create support 'id' which is already set- check & remove |
6a488035 TO |
388 | if ($contactID) { |
389 | $params['contact_id'] = $contactID; | |
390 | } | |
391 | ||
6ecbca5b | 392 | return CRM_Contact_BAO_Contact::create($params); |
6a488035 TO |
393 | } |
394 | ||
395 | /** | |
396 | * Validate the addressee or email or postal greetings | |
397 | * | |
398 | * @param $params Associative array of property name/value | |
399 | * pairs to insert in new contact. | |
400 | * | |
401 | * @return array (reference ) null on success, error message otherwise | |
402 | * | |
403 | * @access public | |
404 | */ | |
405 | function _civicrm_api3_greeting_format_params($params) { | |
406 | $greetingParams = array('', '_id', '_custom'); | |
407 | foreach (array('email', 'postal', 'addressee') as $key) { | |
408 | $greeting = '_greeting'; | |
409 | if ($key == 'addressee') { | |
410 | $greeting = ''; | |
411 | } | |
412 | ||
413 | $formatParams = FALSE; | |
414 | // unset display value from params. | |
415 | if (isset($params["{$key}{$greeting}_display"])) { | |
416 | unset($params["{$key}{$greeting}_display"]); | |
417 | } | |
418 | ||
419 | // check if greetings are present in present | |
420 | foreach ($greetingParams as $greetingValues) { | |
421 | if (array_key_exists("{$key}{$greeting}{$greetingValues}", $params)) { | |
422 | $formatParams = TRUE; | |
423 | break; | |
424 | } | |
425 | } | |
426 | ||
427 | if (!$formatParams) { | |
428 | continue; | |
429 | } | |
430 | ||
431 | $nullValue = FALSE; | |
432 | $filter = array( | |
433 | 'contact_type' => $params['contact_type'], | |
434 | 'greeting_type' => "{$key}{$greeting}", | |
435 | ); | |
436 | ||
437 | $greetings = CRM_Core_PseudoConstant::greeting($filter); | |
438 | $greetingId = CRM_Utils_Array::value("{$key}{$greeting}_id", $params); | |
439 | $greetingVal = CRM_Utils_Array::value("{$key}{$greeting}", $params); | |
440 | $customGreeting = CRM_Utils_Array::value("{$key}{$greeting}_custom", $params); | |
441 | ||
442 | if (!$greetingId && $greetingVal) { | |
443 | $params["{$key}{$greeting}_id"] = CRM_Utils_Array::key($params["{$key}{$greeting}"], $greetings); | |
444 | } | |
445 | ||
446 | if ($customGreeting && $greetingId && | |
447 | ($greetingId != array_search('Customized', $greetings)) | |
448 | ) { | |
6ecbca5b | 449 | throw new API_Exception(ts('Provide either %1 greeting id and/or %1 greeting or custom %1 greeting', |
6a488035 TO |
450 | array(1 => $key) |
451 | )); | |
452 | } | |
453 | ||
454 | if ($greetingVal && $greetingId && | |
455 | ($greetingId != CRM_Utils_Array::key($greetingVal, $greetings)) | |
456 | ) { | |
6ecbca5b | 457 | throw new API_Exception(ts('Mismatch in %1 greeting id and %1 greeting', |
6a488035 TO |
458 | array(1 => $key) |
459 | )); | |
460 | } | |
461 | ||
462 | if ($greetingId) { | |
463 | ||
464 | if (!array_key_exists($greetingId, $greetings)) { | |
6ecbca5b | 465 | throw new API_Exception(ts('Invalid %1 greeting Id', array(1 => $key))); |
6a488035 TO |
466 | } |
467 | ||
468 | if (!$customGreeting && ($greetingId == array_search('Customized', $greetings))) { | |
6ecbca5b | 469 | throw new API_Exception(ts('Please provide a custom value for %1 greeting', |
6a488035 TO |
470 | array(1 => $key) |
471 | )); | |
472 | } | |
473 | } | |
474 | elseif ($greetingVal) { | |
475 | ||
476 | if (!in_array($greetingVal, $greetings)) { | |
6ecbca5b | 477 | throw new API_Exception(ts('Invalid %1 greeting', array(1 => $key))); |
6a488035 TO |
478 | } |
479 | ||
480 | $greetingId = CRM_Utils_Array::key($greetingVal, $greetings); | |
481 | } | |
482 | ||
483 | if ($customGreeting) { | |
484 | $greetingId = CRM_Utils_Array::key('Customized', $greetings); | |
485 | } | |
486 | ||
ad3f841d DL |
487 | $customValue = isset($params['contact_id']) ? |
488 | CRM_Core_DAO::getFieldValue( | |
489 | 'CRM_Contact_DAO_Contact', | |
490 | $params['contact_id'], | |
491 | "{$key}{$greeting}_custom" | |
492 | ) : | |
493 | FALSE; | |
6a488035 TO |
494 | |
495 | if (array_key_exists("{$key}{$greeting}_id", $params) && empty($params["{$key}{$greeting}_id"])) { | |
496 | $nullValue = TRUE; | |
497 | } | |
498 | elseif (array_key_exists("{$key}{$greeting}", $params) && empty($params["{$key}{$greeting}"])) { | |
499 | $nullValue = TRUE; | |
500 | } | |
501 | elseif ($customValue && array_key_exists("{$key}{$greeting}_custom", $params) | |
502 | && empty($params["{$key}{$greeting}_custom"]) | |
503 | ) { | |
504 | $nullValue = TRUE; | |
505 | } | |
506 | ||
507 | $params["{$key}{$greeting}_id"] = $greetingId; | |
508 | ||
509 | if (!$customValue && !$customGreeting && array_key_exists("{$key}{$greeting}_custom", $params)) { | |
510 | unset($params["{$key}{$greeting}_custom"]); | |
511 | } | |
512 | ||
513 | if ($nullValue) { | |
514 | $params["{$key}{$greeting}_id"] = ''; | |
515 | $params["{$key}{$greeting}_custom"] = ''; | |
516 | } | |
517 | ||
518 | if (isset($params["{$key}{$greeting}"])) { | |
519 | unset($params["{$key}{$greeting}"]); | |
520 | } | |
521 | } | |
522 | } | |
523 | ||
524 | /** | |
03f32517 | 525 | * Old contact quick search api |
6a488035 | 526 | * |
03f32517 | 527 | * @deprecated |
6a488035 TO |
528 | * |
529 | * {@example ContactGetquick.php 0} | |
530 | * | |
531 | */ | |
6a488035 TO |
532 | function civicrm_api3_contact_getquick($params) { |
533 | civicrm_api3_verify_mandatory($params, NULL, array('name')); | |
aca85468 | 534 | $name = CRM_Utils_Type::escape(CRM_Utils_Array::value('name', $params), 'String'); |
6a488035 TO |
535 | |
536 | // get the autocomplete options from settings | |
537 | $acpref = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
538 | CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
539 | 'contact_autocomplete_options' | |
540 | ) | |
541 | ); | |
542 | ||
543 | // get the option values for contact autocomplete | |
544 | $acOptions = CRM_Core_OptionGroup::values('contact_autocomplete_options', FALSE, FALSE, FALSE, NULL, 'name'); | |
545 | ||
546 | $list = array(); | |
547 | foreach ($acpref as $value) { | |
8cc574cf | 548 | if ($value && !empty($acOptions[$value])) { |
6a488035 TO |
549 | $list[$value] = $acOptions[$value]; |
550 | } | |
551 | } | |
552 | // If we are doing quicksearch by a field other than name, make sure that field is added to results | |
553 | if (!empty($params['field_name'])) { | |
1aba4d9a | 554 | $field_name = CRM_Utils_String::munge($params['field_name']); |
5d8ba7a7 | 555 | // Unique name contact_id = id |
1aba4d9a CW |
556 | if ($field_name == 'contact_id') { |
557 | $field_name = 'id'; | |
5d8ba7a7 | 558 | } |
6a488035 | 559 | // phone_numeric should be phone |
1aba4d9a | 560 | $searchField = str_replace('_numeric', '', $field_name); |
6a488035 TO |
561 | if(!in_array($searchField, $list)) { |
562 | $list[] = $searchField; | |
563 | } | |
564 | } | |
565 | ||
566 | $select = $actualSelectElements = array('sort_name'); | |
567 | $where = ''; | |
568 | $from = array(); | |
569 | foreach ($list as $value) { | |
570 | $suffix = substr($value, 0, 2) . substr($value, -1); | |
571 | switch ($value) { | |
572 | case 'street_address': | |
573 | case 'city': | |
574 | case 'postal_code': | |
575 | $selectText = $value; | |
576 | $value = "address"; | |
577 | $suffix = 'sts'; | |
578 | case 'phone': | |
579 | case 'email': | |
580 | $actualSelectElements[] = $select[] = ($value == 'address') ? $selectText : $value; | |
aebdef3c JL |
581 | if ($value == 'phone') { |
582 | $actualSelectElements[] = $select[] = 'phone_ext'; | |
583 | } | |
6a488035 TO |
584 | $from[$value] = "LEFT JOIN civicrm_{$value} {$suffix} ON ( cc.id = {$suffix}.contact_id AND {$suffix}.is_primary = 1 ) "; |
585 | break; | |
586 | ||
587 | case 'country': | |
588 | case 'state_province': | |
589 | $select[] = "{$suffix}.name as {$value}"; | |
590 | $actualSelectElements[] = "{$suffix}.name"; | |
591 | if (!in_array('address', $from)) { | |
592 | $from['address'] = 'LEFT JOIN civicrm_address sts ON ( cc.id = sts.contact_id AND sts.is_primary = 1) '; | |
593 | } | |
594 | $from[$value] = " LEFT JOIN civicrm_{$value} {$suffix} ON ( sts.{$value}_id = {$suffix}.id ) "; | |
595 | break; | |
596 | ||
597 | default: | |
598 | if ($value != 'id') { | |
599 | $suffix = 'cc'; | |
600 | if (!empty($params['field_name']) && $params['field_name'] == 'value') { | |
133da98d | 601 | $suffix = CRM_Utils_String::munge(CRM_Utils_Array::value('table_name', $params, 'cc')); |
6a488035 TO |
602 | } |
603 | $actualSelectElements[] = $select[] = $suffix . '.' . $value; | |
604 | } | |
605 | break; | |
606 | } | |
607 | } | |
608 | ||
609 | $config = CRM_Core_Config::singleton(); | |
610 | $as = $select; | |
611 | $select = implode(', ', $select); | |
612 | if (!empty($select)) { | |
613 | $select = ", $select"; | |
614 | } | |
615 | $actualSelectElements = implode(', ', $actualSelectElements); | |
616 | $selectAliases = $from; | |
617 | unset($selectAliases['address']); | |
618 | $selectAliases = implode(', ', array_keys($selectAliases)); | |
619 | if (!empty($selectAliases)) { | |
620 | $selectAliases = ", $selectAliases"; | |
621 | } | |
622 | $from = implode(' ', $from); | |
133da98d CW |
623 | $limit = (int) CRM_Utils_Array::value('limit', $params); |
624 | $limit = $limit > 0 ? $limit : 10; | |
6a488035 TO |
625 | |
626 | // add acl clause here | |
627 | list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause('cc'); | |
628 | ||
629 | if ($aclWhere) { | |
630 | $where .= " AND $aclWhere "; | |
631 | } | |
632 | ||
a7488080 | 633 | if (!empty($params['org'])) { |
6a488035 TO |
634 | $where .= " AND contact_type = \"Organization\""; |
635 | ||
636 | // CRM-7157, hack: get current employer details when | |
637 | // employee_id is present. | |
638 | $currEmpDetails = array(); | |
a7488080 | 639 | if (!empty($params['employee_id'])) { |
6a488035 | 640 | if ($currentEmployer = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', |
133da98d | 641 | (int) $params['employee_id'], |
6a488035 TO |
642 | 'employer_id' |
643 | )) { | |
644 | if ($config->includeWildCardInName) { | |
645 | $strSearch = "%$name%"; | |
646 | } | |
647 | else { | |
648 | $strSearch = "$name%"; | |
649 | } | |
650 | ||
651 | // get current employer details | |
652 | $dao = CRM_Core_DAO::executeQuery("SELECT cc.id as id, CONCAT_WS( ' :: ', {$actualSelectElements} ) as data, sort_name | |
653 | FROM civicrm_contact cc {$from} WHERE cc.contact_type = \"Organization\" AND cc.id = {$currentEmployer} AND cc.sort_name LIKE '$strSearch'"); | |
654 | if ($dao->fetch()) { | |
655 | $currEmpDetails = array( | |
656 | 'id' => $dao->id, | |
657 | 'data' => $dao->data, | |
658 | ); | |
659 | } | |
660 | } | |
661 | } | |
662 | } | |
663 | ||
a7488080 | 664 | if (!empty($params['contact_sub_type'])) { |
69164898 N |
665 | $contactSubType = CRM_Utils_Type::escape($params['contact_sub_type'], 'String'); |
666 | $where .= " AND cc.contact_sub_type = '{$contactSubType}'"; | |
667 | } | |
668 | ||
e1b717cb P |
669 | if (!empty($params['contact_type'])) { |
670 | $contactType = CRM_Utils_Type::escape($params['contact_type'], 'String'); | |
671 | $where .= " AND cc.contact_type LIKE '{$contactType}'"; | |
672 | } | |
673 | ||
6a488035 | 674 | //set default for current_employer or return contact with particular id |
a7488080 | 675 | if (!empty($params['id'])) { |
1aba4d9a | 676 | $where .= " AND cc.id = " . (int) $params['id']; |
6a488035 TO |
677 | } |
678 | ||
a7488080 | 679 | if (!empty($params['cid'])) { |
1aba4d9a | 680 | $where .= " AND cc.id <> " . (int) $params['cid']; |
6a488035 TO |
681 | } |
682 | ||
683 | //contact's based of relationhip type | |
684 | $relType = NULL; | |
a7488080 | 685 | if (!empty($params['rel'])) { |
6a488035 TO |
686 | $relation = explode('_', CRM_Utils_Array::value('rel', $params)); |
687 | $relType = CRM_Utils_Type::escape($relation[0], 'Integer'); | |
688 | $rel = CRM_Utils_Type::escape($relation[2], 'String'); | |
689 | } | |
690 | ||
691 | if ($config->includeWildCardInName) { | |
692 | $strSearch = "%$name%"; | |
693 | } | |
694 | else { | |
695 | $strSearch = "$name%"; | |
696 | } | |
697 | $includeEmailFrom = $includeNickName = $exactIncludeNickName = ''; | |
698 | if ($config->includeNickNameInName) { | |
699 | $includeNickName = " OR nick_name LIKE '$strSearch'"; | |
700 | $exactIncludeNickName = " OR nick_name LIKE '$name'"; | |
701 | } | |
702 | ||
703 | //CRM-10687 | |
704 | if (!empty($params['field_name']) && !empty($params['table_name'])) { | |
1aba4d9a | 705 | $table_name = CRM_Utils_String::munge($params['table_name']); |
6a488035 TO |
706 | $whereClause = " WHERE ( $table_name.$field_name LIKE '$strSearch')"; |
707 | $exactWhereClause = " WHERE ( $table_name.$field_name = '$name')"; | |
708 | // Search by id should be exact | |
709 | if ($field_name == 'id' || $field_name == 'external_identifier') { | |
710 | $whereClause = $exactWhereClause; | |
711 | } | |
712 | } | |
713 | else { | |
714 | if ($config->includeEmailInName) { | |
715 | if (!in_array('email', $list)) { | |
716 | $includeEmailFrom = "LEFT JOIN civicrm_email eml ON ( cc.id = eml.contact_id AND eml.is_primary = 1 )"; | |
717 | } | |
718 | $whereClause = " WHERE ( email LIKE '$strSearch' OR sort_name LIKE '$strSearch' $includeNickName ) {$where} "; | |
719 | $exactWhereClause = " WHERE ( email LIKE '$name' OR sort_name LIKE '$name' $exactIncludeNickName ) {$where} "; | |
720 | } | |
721 | else { | |
722 | $whereClause = " WHERE ( sort_name LIKE '$strSearch' $includeNickName ) {$where} "; | |
723 | $exactWhereClause = " WHERE ( sort_name LIKE '$name' $exactIncludeNickName ) {$where} "; | |
724 | } | |
725 | } | |
726 | ||
727 | $additionalFrom = ''; | |
728 | if ($relType) { | |
729 | $additionalFrom = " | |
730 | INNER JOIN civicrm_relationship_type r ON ( | |
731 | r.id = {$relType} | |
732 | AND ( cc.contact_type = r.contact_type_{$rel} OR r.contact_type_{$rel} IS NULL ) | |
733 | AND ( cc.contact_sub_type = r.contact_sub_type_{$rel} OR r.contact_sub_type_{$rel} IS NULL ) | |
734 | )"; | |
735 | } | |
736 | ||
737 | // check if only CMS users are requested | |
a7488080 | 738 | if (!empty($params['cmsuser'])) { |
6a488035 TO |
739 | $additionalFrom = " |
740 | INNER JOIN civicrm_uf_match um ON (um.contact_id=cc.id) | |
741 | "; | |
742 | } | |
743 | ||
51e61eae ARW |
744 | $orderByInner = ""; |
745 | $orderByOuter = "ORDER BY exactFirst"; | |
746 | if ($config->includeOrderByClause) { | |
747 | $orderByInner = "ORDER BY sort_name"; | |
748 | $orderByOuter .= ", sort_name"; | |
749 | } | |
750 | ||
6a488035 TO |
751 | //CRM-5954 |
752 | $query = " | |
753 | SELECT DISTINCT(id), data, sort_name {$selectAliases} | |
754 | FROM ( | |
755 | ( SELECT 0 as exactFirst, cc.id as id, CONCAT_WS( ' :: ', {$actualSelectElements} ) as data {$select} | |
756 | FROM civicrm_contact cc {$from} | |
757 | {$aclFrom} | |
758 | {$additionalFrom} {$includeEmailFrom} | |
759 | {$exactWhereClause} | |
760 | LIMIT 0, {$limit} ) | |
761 | UNION | |
762 | ( SELECT 1 as exactFirst, cc.id as id, CONCAT_WS( ' :: ', {$actualSelectElements} ) as data {$select} | |
763 | FROM civicrm_contact cc {$from} | |
764 | {$aclFrom} | |
765 | {$additionalFrom} {$includeEmailFrom} | |
766 | {$whereClause} | |
51e61eae | 767 | {$orderByInner} |
6a488035 TO |
768 | LIMIT 0, {$limit} ) |
769 | ) t | |
51e61eae | 770 | {$orderByOuter} |
6a488035 TO |
771 | LIMIT 0, {$limit} |
772 | "; | |
773 | // send query to hook to be modified if needed | |
774 | CRM_Utils_Hook::contactListQuery($query, | |
775 | $name, | |
133da98d CW |
776 | empty($params['context']) ? NULL : CRM_Utils_Type::escape($params['context'], 'String'), |
777 | empty($params['id']) ? NULL : $params['id'] | |
6a488035 TO |
778 | ); |
779 | ||
780 | $dao = CRM_Core_DAO::executeQuery($query); | |
781 | ||
782 | $contactList = array(); | |
783 | $listCurrentEmployer = TRUE; | |
784 | while ($dao->fetch()) { | |
785 | $t = array('id' => $dao->id); | |
786 | foreach ($as as $k) { | |
787 | $t[$k] = isset($dao->$k)? $dao->$k: ''; | |
788 | } | |
789 | $t['data'] = $dao->data; | |
790 | $contactList[] = $t; | |
a7488080 | 791 | if (!empty($params['org']) && |
6a488035 TO |
792 | !empty($currEmpDetails) && |
793 | $dao->id == $currEmpDetails['id'] | |
794 | ) { | |
795 | $listCurrentEmployer = FALSE; | |
796 | } | |
797 | } | |
798 | ||
799 | //return organization name if doesn't exist in db | |
800 | if (empty($contactList)) { | |
a7488080 | 801 | if (!empty($params['org'])) { |
6a488035 TO |
802 | if ($listCurrentEmployer && !empty($currEmpDetails)) { |
803 | $contactList = array( | |
804 | array( | |
805 | 'data' => $currEmpDetails['data'], | |
806 | 'id' => $currEmpDetails['id'] | |
807 | ) | |
808 | ); | |
809 | } | |
810 | else { | |
811 | $contactList = array( | |
812 | array( | |
813 | 'data' => $name, | |
814 | 'id' => $name | |
815 | ) | |
816 | ); | |
817 | } | |
818 | } | |
819 | } | |
820 | ||
821 | return civicrm_api3_create_success($contactList, $params); | |
822 | } | |
823 | ||
824 | /** | |
825 | * Merges given pair of duplicate contacts. | |
826 | * | |
827 | * @param array $params input parameters | |
828 | * | |
829 | * Allowed @params array keys are: | |
830 | * {int main_id main contact id with whom merge has to happen} | |
831 | * {int other_id duplicate contact which would be deleted after merge operation} | |
832 | * {string mode helps decide how to behave when there are conflicts. | |
833 | * A 'safe' value skips the merge if there are no conflicts. Does a force merge otherwise.} | |
834 | * {boolean auto_flip wether to let api decide which contact to retain and which to delete.} | |
835 | * | |
836 | * @return array API Result Array | |
837 | * | |
838 | * @static void | |
839 | * @access public | |
840 | */ | |
841 | function civicrm_api3_contact_merge($params) { | |
842 | $mode = CRM_Utils_Array::value('mode', $params, 'safe'); | |
843 | $autoFlip = CRM_Utils_Array::value('auto_flip', $params, TRUE); | |
844 | ||
845 | $dupePairs = array(array('srcID' => CRM_Utils_Array::value('main_id', $params), | |
846 | 'dstID' => CRM_Utils_Array::value('other_id', $params), | |
847 | )); | |
848 | $result = CRM_Dedupe_Merger::merge($dupePairs, array(), $mode, $autoFlip); | |
849 | ||
850 | if ($result['is_error'] == 0) { | |
851 | return civicrm_api3_create_success(); | |
852 | } | |
853 | else { | |
854 | return civicrm_api3_create_error($result['messages']); | |
855 | } | |
856 | } | |
857 | ||
858 | function _civicrm_api3_contact_proximity_spec(&$params) { | |
859 | $params['latitude']['api.required'] = 1; | |
860 | $params['longitude']['api.required'] = 1; | |
861 | $params['unit']['api.default'] = 'meter'; | |
862 | } | |
863 | ||
864 | function civicrm_api3_contact_proximity($params) { | |
865 | $latitude = CRM_Utils_Array::value('latitude', $params); | |
866 | $longitude = CRM_Utils_Array::value('longitude', $params); | |
867 | $distance = CRM_Utils_Array::value('distance', $params); | |
868 | ||
869 | $unit = CRM_Utils_Array::value('unit', $params); | |
870 | ||
871 | // check and ensure that lat/long and distance are floats | |
872 | if ( | |
873 | !CRM_Utils_Rule::numeric($latitude) || | |
874 | !CRM_Utils_Rule::numeric($longitude) || | |
875 | !CRM_Utils_Rule::numeric($distance) | |
876 | ) { | |
877 | throw new Exception(ts('Latitude, Longitude and Distance should exist and be numeric')); | |
878 | } | |
879 | ||
880 | if ($unit == "mile") { | |
881 | $conversionFactor = 1609.344; | |
882 | } | |
883 | else { | |
884 | $conversionFactor = 1000; | |
885 | } | |
886 | //Distance in meters | |
887 | $distance = $distance * $conversionFactor; | |
888 | ||
889 | $whereClause = CRM_Contact_BAO_ProximityQuery::where($latitude, $longitude, $distance); | |
890 | ||
891 | $query = " | |
892 | SELECT civicrm_contact.id as contact_id, | |
893 | civicrm_contact.display_name as display_name | |
894 | FROM civicrm_contact | |
895 | LEFT JOIN civicrm_address ON civicrm_contact.id = civicrm_address.contact_id | |
896 | WHERE $whereClause | |
897 | "; | |
898 | ||
899 | $dao = CRM_Core_DAO::executeQuery($query); | |
900 | $contacts = array(); | |
901 | while ($dao->fetch()) { | |
902 | $contacts[] = $dao->toArray(); | |
903 | } | |
904 | ||
905 | return civicrm_api3_create_success($contacts, $params, 'contact', 'get_by_location', $dao); | |
906 | } | |
907 | ||
ff88d165 CW |
908 | |
909 | /** | |
a6c6059d | 910 | * @see _civicrm_api3_generic_getlist_params |
ff88d165 CW |
911 | * |
912 | * @param $request array | |
913 | */ | |
914 | function _civicrm_api3_contact_getlist_params(&$request) { | |
915 | // get the autocomplete options from settings | |
916 | $acpref = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
917 | CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
918 | 'contact_autocomplete_options' | |
919 | ) | |
920 | ); | |
921 | ||
922 | // get the option values for contact autocomplete | |
923 | $acOptions = CRM_Core_OptionGroup::values('contact_autocomplete_options', FALSE, FALSE, FALSE, NULL, 'name'); | |
924 | ||
925 | $list = array(); | |
926 | foreach ($acpref as $value) { | |
927 | if ($value && !empty($acOptions[$value])) { | |
928 | $list[] = $acOptions[$value]; | |
929 | } | |
930 | } | |
931 | // If we are doing quicksearch by a field other than name, make sure that field is added to results | |
932 | $field_name = CRM_Utils_String::munge($request['search_field']); | |
933 | // Unique name contact_id = id | |
934 | if ($field_name == 'contact_id') { | |
935 | $field_name = 'id'; | |
936 | } | |
937 | // phone_numeric should be phone | |
938 | $searchField = str_replace('_numeric', '', $field_name); | |
939 | if(!in_array($searchField, $list)) { | |
940 | $list[] = $searchField; | |
941 | } | |
8250601e | 942 | $request['description_field'] = $list; |
54bee7df | 943 | $list[] = 'contact_type'; |
8250601e | 944 | $request['params']['return'] = array_unique(array_merge($list, $request['extra'])); |
ff88d165 CW |
945 | $request['params']['options']['sort'] = 'sort_name'; |
946 | // Contact api doesn't support array(LIKE => 'foo') syntax | |
609a8c53 CW |
947 | if (!empty($request['input'])) { |
948 | $request['params'][$request['search_field']] = $request['input']; | |
949 | } | |
ff88d165 CW |
950 | } |
951 | ||
952 | /** | |
a6c6059d | 953 | * @see _civicrm_api3_generic_getlist_output |
ff88d165 CW |
954 | * |
955 | * @param $result array | |
956 | * @param $request array | |
957 | * | |
958 | * @return array | |
959 | */ | |
960 | function _civicrm_api3_contact_getlist_output($result, $request) { | |
961 | $output = array(); | |
962 | if (!empty($result['values'])) { | |
88881f79 | 963 | $addressFields = array_intersect(array('street_address', 'city', 'state_province', 'country'), $request['params']['return']); |
ff88d165 CW |
964 | foreach ($result['values'] as $row) { |
965 | $data = array( | |
966 | 'id' => $row[$request['id_field']], | |
967 | 'label' => $row[$request['label_field']], | |
88881f79 | 968 | 'description' => array(), |
ff88d165 | 969 | ); |
8250601e CW |
970 | foreach ($request['description_field'] as $item) { |
971 | if (!strpos($item, '_name') && !in_array($item, $addressFields) && !empty($row[$item])) { | |
88881f79 | 972 | $data['description'][] = $row[$item]; |
ff88d165 CW |
973 | } |
974 | } | |
88881f79 CW |
975 | $address = array(); |
976 | foreach($addressFields as $item) { | |
977 | if (!empty($row[$item])) { | |
978 | $address[] = $row[$item]; | |
979 | } | |
980 | } | |
981 | if ($address) { | |
982 | $data['description'][] = implode(' ', $address); | |
983 | } | |
ff88d165 CW |
984 | if (!empty($request['image_field'])) { |
985 | $data['image'] = isset($row[$request['image_field']]) ? $row[$request['image_field']] : ''; | |
54bee7df CW |
986 | } |
987 | else { | |
988 | $data['icon_class'] = $row['contact_type']; | |
989 | } | |
8250601e CW |
990 | foreach ($request['extra'] as $field) { |
991 | $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL; | |
992 | } | |
ff88d165 CW |
993 | $output[] = $data; |
994 | } | |
995 | } | |
996 | return $output; | |
997 | } |