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