| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | */ |
| 17 | |
| 18 | /* |
| 19 | * These functions have been deprecated out of API v3 Utils folder as they are not part of the |
| 20 | * API. Calling API functions directly is not supported & these functions are not called by any |
| 21 | * part of the API so are not really part of the api |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | require_once 'api/v3/utils.php'; |
| 26 | |
| 27 | /** |
| 28 | * Check duplicate contacts based on de-dupe parameters. |
| 29 | * |
| 30 | * @param array $params |
| 31 | * |
| 32 | * @return array |
| 33 | */ |
| 34 | function _civicrm_api3_deprecated_check_contact_dedupe($params) { |
| 35 | static $cIndieFields = NULL; |
| 36 | static $defaultLocationId = NULL; |
| 37 | |
| 38 | $contactType = $params['contact_type']; |
| 39 | if ($cIndieFields == NULL) { |
| 40 | require_once 'CRM/Contact/BAO/Contact.php'; |
| 41 | $cTempIndieFields = CRM_Contact_BAO_Contact::importableFields($contactType); |
| 42 | $cIndieFields = $cTempIndieFields; |
| 43 | |
| 44 | require_once "CRM/Core/BAO/LocationType.php"; |
| 45 | $defaultLocation = CRM_Core_BAO_LocationType::getDefault(); |
| 46 | |
| 47 | // set the value to default location id else set to 1 |
| 48 | if (!$defaultLocationId = (int) $defaultLocation->id) { |
| 49 | $defaultLocationId = 1; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | require_once 'CRM/Contact/BAO/Query.php'; |
| 54 | $locationFields = CRM_Contact_BAO_Query::$_locationSpecificFields; |
| 55 | |
| 56 | $contactFormatted = []; |
| 57 | foreach ($params as $key => $field) { |
| 58 | if ($field == NULL || $field === '') { |
| 59 | continue; |
| 60 | } |
| 61 | // CRM-17040, Considering only primary contact when importing contributions. So contribution inserts into primary contact |
| 62 | // instead of soft credit contact. |
| 63 | if (is_array($field) && $key != "soft_credit") { |
| 64 | foreach ($field as $value) { |
| 65 | $break = FALSE; |
| 66 | if (is_array($value)) { |
| 67 | foreach ($value as $name => $testForEmpty) { |
| 68 | if ($name !== 'phone_type' && |
| 69 | ($testForEmpty === '' || $testForEmpty == NULL) |
| 70 | ) { |
| 71 | $break = TRUE; |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | else { |
| 77 | $break = TRUE; |
| 78 | } |
| 79 | if (!$break) { |
| 80 | _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted); |
| 81 | } |
| 82 | } |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | $value = [$key => $field]; |
| 87 | |
| 88 | // check if location related field, then we need to add primary location type |
| 89 | if (in_array($key, $locationFields)) { |
| 90 | $value['location_type_id'] = $defaultLocationId; |
| 91 | } |
| 92 | elseif (array_key_exists($key, $cIndieFields)) { |
| 93 | $value['contact_type'] = $contactType; |
| 94 | } |
| 95 | |
| 96 | _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted); |
| 97 | } |
| 98 | |
| 99 | $contactFormatted['contact_type'] = $contactType; |
| 100 | |
| 101 | return _civicrm_api3_deprecated_duplicate_formatted_contact($contactFormatted); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * take the input parameter list as specified in the data model and |
| 106 | * convert it into the same format that we use in QF and BAO object |
| 107 | * |
| 108 | * @param array $params |
| 109 | * Associative array of property name/value. |
| 110 | * pairs to insert in new contact. |
| 111 | * @param array $values |
| 112 | * The reformatted properties that we can use internally. |
| 113 | * |
| 114 | * @param array|bool $create Is the formatted Values array going to |
| 115 | * be used for CRM_Activity_BAO_Activity::create() |
| 116 | * |
| 117 | * @return array|CRM_Error |
| 118 | */ |
| 119 | function _civicrm_api3_deprecated_activity_formatted_param(&$params, &$values, $create = FALSE) { |
| 120 | // copy all the activity fields as is |
| 121 | $fields = CRM_Activity_DAO_Activity::fields(); |
| 122 | _civicrm_api3_store_values($fields, $params, $values); |
| 123 | |
| 124 | require_once 'CRM/Core/OptionGroup.php'; |
| 125 | $customFields = CRM_Core_BAO_CustomField::getFields('Activity'); |
| 126 | |
| 127 | foreach ($params as $key => $value) { |
| 128 | // ignore empty values or empty arrays etc |
| 129 | if (CRM_Utils_System::isNull($value)) { |
| 130 | continue; |
| 131 | } |
| 132 | |
| 133 | //Handling Custom Data |
| 134 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { |
| 135 | $values[$key] = $value; |
| 136 | $type = $customFields[$customFieldID]['html_type']; |
| 137 | if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID])) { |
| 138 | $values[$key] = CRM_Import_Parser::unserializeCustomValue($customFieldID, $value, $type); |
| 139 | } |
| 140 | elseif ($type == 'Select' || $type == 'Radio') { |
| 141 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); |
| 142 | foreach ($customOption as $customFldID => $customValue) { |
| 143 | $val = $customValue['value'] ?? NULL; |
| 144 | $label = $customValue['label'] ?? NULL; |
| 145 | $label = strtolower($label); |
| 146 | $value = strtolower(trim($value)); |
| 147 | if (($value == $label) || ($value == strtolower($val))) { |
| 148 | $values[$key] = $val; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if ($key == 'target_contact_id') { |
| 155 | if (!CRM_Utils_Rule::integer($value)) { |
| 156 | return civicrm_api3_create_error("contact_id not valid: $value"); |
| 157 | } |
| 158 | $contactID = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value"); |
| 159 | if (!$contactID) { |
| 160 | return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value."); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | return NULL; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * This function adds the contact variable in $values to the |
| 169 | * parameter list $params. For most cases, $values should have length 1. If |
| 170 | * the variable being added is a child of Location, a location_type_id must |
| 171 | * also be included. If it is a child of phone, a phone_type must be included. |
| 172 | * |
| 173 | * @param array $values |
| 174 | * The variable(s) to be added. |
| 175 | * @param array $params |
| 176 | * The structured parameter list. |
| 177 | * |
| 178 | * @return bool|CRM_Utils_Error |
| 179 | */ |
| 180 | function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) { |
| 181 | // Crawl through the possible classes: |
| 182 | // Contact |
| 183 | // Individual |
| 184 | // Household |
| 185 | // Organization |
| 186 | // Location |
| 187 | // Address |
| 188 | // Email |
| 189 | // Phone |
| 190 | // IM |
| 191 | // Note |
| 192 | // Custom |
| 193 | |
| 194 | // Cache the various object fields |
| 195 | static $fields = NULL; |
| 196 | |
| 197 | if ($fields == NULL) { |
| 198 | $fields = []; |
| 199 | } |
| 200 | |
| 201 | // first add core contact values since for other Civi modules they are not added |
| 202 | require_once 'CRM/Contact/BAO/Contact.php'; |
| 203 | $contactFields = CRM_Contact_DAO_Contact::fields(); |
| 204 | _civicrm_api3_store_values($contactFields, $values, $params); |
| 205 | |
| 206 | if (isset($values['contact_type'])) { |
| 207 | // we're an individual/household/org property |
| 208 | |
| 209 | $fields[$values['contact_type']] = CRM_Contact_DAO_Contact::fields(); |
| 210 | |
| 211 | _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params); |
| 212 | return TRUE; |
| 213 | } |
| 214 | |
| 215 | if (isset($values['individual_prefix'])) { |
| 216 | if (!empty($params['prefix_id'])) { |
| 217 | $prefixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'); |
| 218 | $params['prefix'] = $prefixes[$params['prefix_id']]; |
| 219 | } |
| 220 | else { |
| 221 | $params['prefix'] = $values['individual_prefix']; |
| 222 | } |
| 223 | return TRUE; |
| 224 | } |
| 225 | |
| 226 | if (isset($values['individual_suffix'])) { |
| 227 | if (!empty($params['suffix_id'])) { |
| 228 | $suffixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'); |
| 229 | $params['suffix'] = $suffixes[$params['suffix_id']]; |
| 230 | } |
| 231 | else { |
| 232 | $params['suffix'] = $values['individual_suffix']; |
| 233 | } |
| 234 | return TRUE; |
| 235 | } |
| 236 | |
| 237 | // CRM-4575 |
| 238 | if (isset($values['email_greeting'])) { |
| 239 | if (!empty($params['email_greeting_id'])) { |
| 240 | $emailGreetingFilter = [ |
| 241 | 'contact_type' => $params['contact_type'] ?? NULL, |
| 242 | 'greeting_type' => 'email_greeting', |
| 243 | ]; |
| 244 | $emailGreetings = CRM_Core_PseudoConstant::greeting($emailGreetingFilter); |
| 245 | $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']]; |
| 246 | } |
| 247 | else { |
| 248 | $params['email_greeting'] = $values['email_greeting']; |
| 249 | } |
| 250 | |
| 251 | return TRUE; |
| 252 | } |
| 253 | |
| 254 | if (isset($values['postal_greeting'])) { |
| 255 | if (!empty($params['postal_greeting_id'])) { |
| 256 | $postalGreetingFilter = [ |
| 257 | 'contact_type' => $params['contact_type'] ?? NULL, |
| 258 | 'greeting_type' => 'postal_greeting', |
| 259 | ]; |
| 260 | $postalGreetings = CRM_Core_PseudoConstant::greeting($postalGreetingFilter); |
| 261 | $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']]; |
| 262 | } |
| 263 | else { |
| 264 | $params['postal_greeting'] = $values['postal_greeting']; |
| 265 | } |
| 266 | return TRUE; |
| 267 | } |
| 268 | |
| 269 | if (isset($values['addressee'])) { |
| 270 | if (!empty($params['addressee_id'])) { |
| 271 | $addresseeFilter = [ |
| 272 | 'contact_type' => $params['contact_type'] ?? NULL, |
| 273 | 'greeting_type' => 'addressee', |
| 274 | ]; |
| 275 | $addressee = CRM_Core_PseudoConstant::addressee($addresseeFilter); |
| 276 | $params['addressee'] = $addressee[$params['addressee_id']]; |
| 277 | } |
| 278 | else { |
| 279 | $params['addressee'] = $values['addressee']; |
| 280 | } |
| 281 | return TRUE; |
| 282 | } |
| 283 | |
| 284 | if (isset($values['gender'])) { |
| 285 | if (!empty($params['gender_id'])) { |
| 286 | $genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); |
| 287 | $params['gender'] = $genders[$params['gender_id']]; |
| 288 | } |
| 289 | else { |
| 290 | $params['gender'] = $values['gender']; |
| 291 | } |
| 292 | return TRUE; |
| 293 | } |
| 294 | |
| 295 | if (!empty($values['preferred_communication_method'])) { |
| 296 | $comm = []; |
| 297 | $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER); |
| 298 | |
| 299 | $preffComm = explode(',', $values['preferred_communication_method']); |
| 300 | foreach ($preffComm as $v) { |
| 301 | $v = strtolower(trim($v)); |
| 302 | if (array_key_exists($v, $pcm)) { |
| 303 | $comm[$pcm[$v]] = 1; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | $params['preferred_communication_method'] = $comm; |
| 308 | return TRUE; |
| 309 | } |
| 310 | |
| 311 | // format the website params. |
| 312 | if (!empty($values['url'])) { |
| 313 | static $websiteFields; |
| 314 | if (!is_array($websiteFields)) { |
| 315 | require_once 'CRM/Core/DAO/Website.php'; |
| 316 | $websiteFields = CRM_Core_DAO_Website::fields(); |
| 317 | } |
| 318 | if (!array_key_exists('website', $params) || |
| 319 | !is_array($params['website']) |
| 320 | ) { |
| 321 | $params['website'] = []; |
| 322 | } |
| 323 | |
| 324 | $websiteCount = count($params['website']); |
| 325 | _civicrm_api3_store_values($websiteFields, $values, |
| 326 | $params['website'][++$websiteCount] |
| 327 | ); |
| 328 | |
| 329 | return TRUE; |
| 330 | } |
| 331 | |
| 332 | // get the formatted location blocks into params - w/ 3.0 format, CRM-4605 |
| 333 | if (!empty($values['location_type_id'])) { |
| 334 | static $fields = NULL; |
| 335 | if ($fields == NULL) { |
| 336 | $fields = []; |
| 337 | } |
| 338 | |
| 339 | foreach ([ |
| 340 | 'Phone', |
| 341 | 'Email', |
| 342 | 'IM', |
| 343 | 'OpenID', |
| 344 | 'Phone_Ext', |
| 345 | ] as $block) { |
| 346 | $name = strtolower($block); |
| 347 | if (!array_key_exists($name, $values)) { |
| 348 | continue; |
| 349 | } |
| 350 | |
| 351 | if ($name == 'phone_ext') { |
| 352 | $block = 'Phone'; |
| 353 | } |
| 354 | |
| 355 | // block present in value array. |
| 356 | if (!array_key_exists($name, $params) || !is_array($params[$name])) { |
| 357 | $params[$name] = []; |
| 358 | } |
| 359 | |
| 360 | if (!array_key_exists($block, $fields)) { |
| 361 | $className = "CRM_Core_DAO_$block"; |
| 362 | $fields[$block] =& $className::fields(); |
| 363 | } |
| 364 | |
| 365 | $blockCnt = count($params[$name]); |
| 366 | |
| 367 | // copy value to dao field name. |
| 368 | if ($name == 'im') { |
| 369 | $values['name'] = $values[$name]; |
| 370 | } |
| 371 | |
| 372 | _civicrm_api3_store_values($fields[$block], $values, |
| 373 | $params[$name][++$blockCnt] |
| 374 | ); |
| 375 | |
| 376 | if (empty($params['id']) && ($blockCnt == 1)) { |
| 377 | $params[$name][$blockCnt]['is_primary'] = TRUE; |
| 378 | } |
| 379 | |
| 380 | // we only process single block at a time. |
| 381 | return TRUE; |
| 382 | } |
| 383 | |
| 384 | // handle address fields. |
| 385 | if (!array_key_exists('address', $params) || !is_array($params['address'])) { |
| 386 | $params['address'] = []; |
| 387 | } |
| 388 | |
| 389 | $addressCnt = 1; |
| 390 | foreach ($params['address'] as $cnt => $addressBlock) { |
| 391 | if (CRM_Utils_Array::value('location_type_id', $values) == |
| 392 | CRM_Utils_Array::value('location_type_id', $addressBlock) |
| 393 | ) { |
| 394 | $addressCnt = $cnt; |
| 395 | break; |
| 396 | } |
| 397 | $addressCnt++; |
| 398 | } |
| 399 | |
| 400 | if (!array_key_exists('Address', $fields)) { |
| 401 | $fields['Address'] = CRM_Core_DAO_Address::fields(); |
| 402 | } |
| 403 | |
| 404 | // Note: we doing multiple value formatting here for address custom fields, plus putting into right format. |
| 405 | // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving |
| 406 | // the address in CRM_Core_BAO_Address::create method |
| 407 | if (!empty($values['location_type_id'])) { |
| 408 | static $customFields = []; |
| 409 | if (empty($customFields)) { |
| 410 | $customFields = CRM_Core_BAO_CustomField::getFields('Address'); |
| 411 | } |
| 412 | // make a copy of values, as we going to make changes |
| 413 | $newValues = $values; |
| 414 | foreach ($values as $key => $val) { |
| 415 | $customFieldID = CRM_Core_BAO_CustomField::getKeyID($key); |
| 416 | if ($customFieldID && array_key_exists($customFieldID, $customFields)) { |
| 417 | // mark an entry in fields array since we want the value of custom field to be copied |
| 418 | $fields['Address'][$key] = NULL; |
| 419 | |
| 420 | $htmlType = $customFields[$customFieldID]['html_type'] ?? NULL; |
| 421 | if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID]) && $val) { |
| 422 | $mulValues = explode(',', $val); |
| 423 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); |
| 424 | $newValues[$key] = []; |
| 425 | foreach ($mulValues as $v1) { |
| 426 | foreach ($customOption as $v2) { |
| 427 | if ((strtolower($v2['label']) == strtolower(trim($v1))) || |
| 428 | (strtolower($v2['value']) == strtolower(trim($v1))) |
| 429 | ) { |
| 430 | if ($htmlType == 'CheckBox') { |
| 431 | $newValues[$key][$v2['value']] = 1; |
| 432 | } |
| 433 | else { |
| 434 | $newValues[$key][] = $v2['value']; |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | // consider new values |
| 443 | $values = $newValues; |
| 444 | } |
| 445 | |
| 446 | _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$addressCnt]); |
| 447 | |
| 448 | $addressFields = [ |
| 449 | 'county', |
| 450 | 'country', |
| 451 | 'state_province', |
| 452 | 'supplemental_address_1', |
| 453 | 'supplemental_address_2', |
| 454 | 'supplemental_address_3', |
| 455 | 'StateProvince.name', |
| 456 | ]; |
| 457 | |
| 458 | foreach ($addressFields as $field) { |
| 459 | if (array_key_exists($field, $values)) { |
| 460 | if (!array_key_exists('address', $params)) { |
| 461 | $params['address'] = []; |
| 462 | } |
| 463 | $params['address'][$addressCnt][$field] = $values[$field]; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | if ($addressCnt == 1) { |
| 468 | |
| 469 | $params['address'][$addressCnt]['is_primary'] = TRUE; |
| 470 | } |
| 471 | return TRUE; |
| 472 | } |
| 473 | |
| 474 | if (isset($values['note'])) { |
| 475 | // add a note field |
| 476 | if (!isset($params['note'])) { |
| 477 | $params['note'] = []; |
| 478 | } |
| 479 | $noteBlock = count($params['note']) + 1; |
| 480 | |
| 481 | $params['note'][$noteBlock] = []; |
| 482 | if (!isset($fields['Note'])) { |
| 483 | $fields['Note'] = CRM_Core_DAO_Note::fields(); |
| 484 | } |
| 485 | |
| 486 | // get the current logged in civicrm user |
| 487 | $session = CRM_Core_Session::singleton(); |
| 488 | $userID = $session->get('userID'); |
| 489 | |
| 490 | if ($userID) { |
| 491 | $values['contact_id'] = $userID; |
| 492 | } |
| 493 | |
| 494 | _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]); |
| 495 | |
| 496 | return TRUE; |
| 497 | } |
| 498 | |
| 499 | // Check for custom field values |
| 500 | |
| 501 | if (empty($fields['custom'])) { |
| 502 | $fields['custom'] = &CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $values), |
| 503 | FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE |
| 504 | ); |
| 505 | } |
| 506 | |
| 507 | foreach ($values as $key => $value) { |
| 508 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { |
| 509 | // check if it's a valid custom field id |
| 510 | |
| 511 | if (!array_key_exists($customFieldID, $fields['custom'])) { |
| 512 | return civicrm_api3_create_error('Invalid custom field ID'); |
| 513 | } |
| 514 | else { |
| 515 | $params[$key] = $value; |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * |
| 523 | * @param array $params |
| 524 | * |
| 525 | * @return array |
| 526 | * <type> |
| 527 | */ |
| 528 | function _civicrm_api3_deprecated_duplicate_formatted_contact($params) { |
| 529 | $id = $params['id'] ?? NULL; |
| 530 | $externalId = $params['external_identifier'] ?? NULL; |
| 531 | if ($id || $externalId) { |
| 532 | $contact = new CRM_Contact_DAO_Contact(); |
| 533 | |
| 534 | $contact->id = $id; |
| 535 | $contact->external_identifier = $externalId; |
| 536 | |
| 537 | if ($contact->find(TRUE)) { |
| 538 | if ($params['contact_type'] != $contact->contact_type) { |
| 539 | return civicrm_api3_create_error("Mismatched contact IDs OR Mismatched contact Types"); |
| 540 | } |
| 541 | |
| 542 | $error = CRM_Core_Error::createError("Found matching contacts: $contact->id", |
| 543 | CRM_Core_Error::DUPLICATE_CONTACT, |
| 544 | 'Fatal', $contact->id |
| 545 | ); |
| 546 | return civicrm_api3_create_error($error->pop()); |
| 547 | } |
| 548 | } |
| 549 | else { |
| 550 | $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised'); |
| 551 | |
| 552 | if (!empty($ids)) { |
| 553 | $ids = implode(',', $ids); |
| 554 | $error = CRM_Core_Error::createError("Found matching contacts: $ids", |
| 555 | CRM_Core_Error::DUPLICATE_CONTACT, |
| 556 | 'Fatal', $ids |
| 557 | ); |
| 558 | return civicrm_api3_create_error($error->pop()); |
| 559 | } |
| 560 | } |
| 561 | return civicrm_api3_create_success(TRUE); |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Validate a formatted contact parameter list. |
| 566 | * |
| 567 | * @param array $params |
| 568 | * Structured parameter list (as in crm_format_params). |
| 569 | * |
| 570 | * @return bool|CRM_Core_Error |
| 571 | */ |
| 572 | function _civicrm_api3_deprecated_validate_formatted_contact(&$params) { |
| 573 | // Look for offending email addresses |
| 574 | |
| 575 | if (array_key_exists('email', $params)) { |
| 576 | foreach ($params['email'] as $count => $values) { |
| 577 | if (!is_array($values)) { |
| 578 | continue; |
| 579 | } |
| 580 | if ($email = CRM_Utils_Array::value('email', $values)) { |
| 581 | // validate each email |
| 582 | if (!CRM_Utils_Rule::email($email)) { |
| 583 | return civicrm_api3_create_error('No valid email address'); |
| 584 | } |
| 585 | |
| 586 | // check for loc type id. |
| 587 | if (empty($values['location_type_id'])) { |
| 588 | return civicrm_api3_create_error('Location Type Id missing.'); |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | // Validate custom data fields |
| 595 | if (array_key_exists('custom', $params) && is_array($params['custom'])) { |
| 596 | foreach ($params['custom'] as $key => $custom) { |
| 597 | if (is_array($custom)) { |
| 598 | foreach ($custom as $fieldId => $value) { |
| 599 | $valid = CRM_Core_BAO_CustomValue::typecheck(CRM_Utils_Array::value('type', $value), |
| 600 | CRM_Utils_Array::value('value', $value) |
| 601 | ); |
| 602 | if (!$valid && $value['is_required']) { |
| 603 | return civicrm_api3_create_error('Invalid value for custom field \'' . |
| 604 | CRM_Utils_Array::value('name', $custom) . '\'' |
| 605 | ); |
| 606 | } |
| 607 | if (CRM_Utils_Array::value('type', $custom) == 'Date') { |
| 608 | $params['custom'][$key][$fieldId]['value'] = str_replace('-', '', $params['custom'][$key][$fieldId]['value']); |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | return civicrm_api3_create_success(TRUE); |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * @deprecated - this is part of the import parser not the API & needs to be moved on out |
| 620 | * |
| 621 | * @param array $params |
| 622 | * @param $onDuplicate |
| 623 | * |
| 624 | * @return array|bool |
| 625 | * <type> |
| 626 | */ |
| 627 | function _civicrm_api3_deprecated_create_participant_formatted($params, $onDuplicate) { |
| 628 | require_once 'CRM/Event/Import/Parser.php'; |
| 629 | if ($onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK) { |
| 630 | CRM_Core_Error::reset(); |
| 631 | $error = _civicrm_api3_deprecated_participant_check_params($params, TRUE); |
| 632 | if (civicrm_error($error)) { |
| 633 | return $error; |
| 634 | } |
| 635 | } |
| 636 | require_once "api/v3/Participant.php"; |
| 637 | return civicrm_api3_participant_create($params); |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * |
| 642 | * @param array $params |
| 643 | * |
| 644 | * @param bool $checkDuplicate |
| 645 | * |
| 646 | * @return array|bool |
| 647 | * <type> |
| 648 | */ |
| 649 | function _civicrm_api3_deprecated_participant_check_params($params, $checkDuplicate = FALSE) { |
| 650 | |
| 651 | // check if participant id is valid or not |
| 652 | if (!empty($params['id'])) { |
| 653 | $participant = new CRM_Event_BAO_Participant(); |
| 654 | $participant->id = $params['id']; |
| 655 | if (!$participant->find(TRUE)) { |
| 656 | return civicrm_api3_create_error(ts('Participant id is not valid')); |
| 657 | } |
| 658 | } |
| 659 | require_once 'CRM/Contact/BAO/Contact.php'; |
| 660 | // check if contact id is valid or not |
| 661 | if (!empty($params['contact_id'])) { |
| 662 | $contact = new CRM_Contact_BAO_Contact(); |
| 663 | $contact->id = $params['contact_id']; |
| 664 | if (!$contact->find(TRUE)) { |
| 665 | return civicrm_api3_create_error(ts('Contact id is not valid')); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | // check that event id is not an template |
| 670 | if (!empty($params['event_id'])) { |
| 671 | $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template'); |
| 672 | if (!empty($isTemplate)) { |
| 673 | return civicrm_api3_create_error(ts('Event templates are not meant to be registered.')); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | $result = []; |
| 678 | if ($checkDuplicate) { |
| 679 | if (CRM_Event_BAO_Participant::checkDuplicate($params, $result)) { |
| 680 | $participantID = array_pop($result); |
| 681 | |
| 682 | $error = CRM_Core_Error::createError("Found matching participant record.", |
| 683 | CRM_Core_Error::DUPLICATE_PARTICIPANT, |
| 684 | 'Fatal', $participantID |
| 685 | ); |
| 686 | |
| 687 | return civicrm_api3_create_error($error->pop(), |
| 688 | [ |
| 689 | 'contactID' => $params['contact_id'], |
| 690 | 'participantID' => $participantID, |
| 691 | ] |
| 692 | ); |
| 693 | } |
| 694 | } |
| 695 | return TRUE; |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * @param array $params |
| 700 | * @param bool $dupeCheck |
| 701 | * @param int $dedupeRuleGroupID |
| 702 | * |
| 703 | * @return array|null |
| 704 | */ |
| 705 | function _civicrm_api3_deprecated_contact_check_params( |
| 706 | &$params, |
| 707 | $dupeCheck = TRUE, |
| 708 | $dedupeRuleGroupID = NULL) { |
| 709 | |
| 710 | $requiredCheck = TRUE; |
| 711 | |
| 712 | if (isset($params['id']) && is_numeric($params['id'])) { |
| 713 | $requiredCheck = FALSE; |
| 714 | } |
| 715 | if ($requiredCheck) { |
| 716 | if (isset($params['id'])) { |
| 717 | $required = ['Individual', 'Household', 'Organization']; |
| 718 | } |
| 719 | $required = [ |
| 720 | 'Individual' => [ |
| 721 | ['first_name', 'last_name'], |
| 722 | 'email', |
| 723 | ], |
| 724 | 'Household' => [ |
| 725 | 'household_name', |
| 726 | ], |
| 727 | 'Organization' => [ |
| 728 | 'organization_name', |
| 729 | ], |
| 730 | ]; |
| 731 | |
| 732 | // contact_type has a limited number of valid values |
| 733 | if (empty($params['contact_type'])) { |
| 734 | return civicrm_api3_create_error("No Contact Type"); |
| 735 | } |
| 736 | $fields = $required[$params['contact_type']] ?? NULL; |
| 737 | if ($fields == NULL) { |
| 738 | return civicrm_api3_create_error("Invalid Contact Type: {$params['contact_type']}"); |
| 739 | } |
| 740 | |
| 741 | if ($csType = CRM_Utils_Array::value('contact_sub_type', $params)) { |
| 742 | if (!(CRM_Contact_BAO_ContactType::isExtendsContactType($csType, $params['contact_type']))) { |
| 743 | return civicrm_api3_create_error("Invalid or Mismatched Contact Subtype: " . implode(', ', (array) $csType)); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | if (empty($params['contact_id']) && !empty($params['id'])) { |
| 748 | $valid = FALSE; |
| 749 | $error = ''; |
| 750 | foreach ($fields as $field) { |
| 751 | if (is_array($field)) { |
| 752 | $valid = TRUE; |
| 753 | foreach ($field as $element) { |
| 754 | if (empty($params[$element])) { |
| 755 | $valid = FALSE; |
| 756 | $error .= $element; |
| 757 | break; |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | else { |
| 762 | if (!empty($params[$field])) { |
| 763 | $valid = TRUE; |
| 764 | } |
| 765 | } |
| 766 | if ($valid) { |
| 767 | break; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | if (!$valid) { |
| 772 | return civicrm_api3_create_error("Required fields not found for {$params['contact_type']} : $error"); |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | if ($dupeCheck) { |
| 778 | // @todo switch to using api version |
| 779 | // $dupes = civicrm_api3('Contact', 'duplicatecheck', (array('match' => $params, 'dedupe_rule_id' => $dedupeRuleGroupID))); |
| 780 | // $ids = $dupes['count'] ? implode(',', array_keys($dupes['values'])) : NULL; |
| 781 | $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised', [], CRM_Utils_Array::value('check_permissions', $params), $dedupeRuleGroupID); |
| 782 | if ($ids != NULL) { |
| 783 | $error = CRM_Core_Error::createError("Found matching contacts: " . implode(',', $ids), |
| 784 | CRM_Core_Error::DUPLICATE_CONTACT, |
| 785 | 'Fatal', $ids |
| 786 | ); |
| 787 | return civicrm_api3_create_error($error->pop()); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | // check for organisations with same name |
| 792 | if (!empty($params['current_employer'])) { |
| 793 | $organizationParams = ['organization_name' => $params['current_employer']]; |
| 794 | $dupeIds = CRM_Contact_BAO_Contact::getDuplicateContacts($organizationParams, 'Organization', 'Supervised', [], FALSE); |
| 795 | |
| 796 | // check for mismatch employer name and id |
| 797 | if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds) |
| 798 | ) { |
| 799 | return civicrm_api3_create_error('Employer name and Employer id Mismatch'); |
| 800 | } |
| 801 | |
| 802 | // show error if multiple organisation with same name exist |
| 803 | if (empty($params['employer_id']) && (count($dupeIds) > 1) |
| 804 | ) { |
| 805 | return civicrm_api3_create_error('Found more than one Organisation with same Name.'); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | return NULL; |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * @param $result |
| 814 | * @param int $activityTypeID |
| 815 | * |
| 816 | * @return array |
| 817 | * <type> $params |
| 818 | */ |
| 819 | function _civicrm_api3_deprecated_activity_buildmailparams($result, $activityTypeID) { |
| 820 | // get ready for collecting data about activity to be created |
| 821 | $params = []; |
| 822 | |
| 823 | $params['activity_type_id'] = $activityTypeID; |
| 824 | |
| 825 | $params['status_id'] = 'Completed'; |
| 826 | if (!empty($result['from']['id'])) { |
| 827 | $params['source_contact_id'] = $params['assignee_contact_id'] = $result['from']['id']; |
| 828 | } |
| 829 | $params['target_contact_id'] = []; |
| 830 | $keys = ['to', 'cc', 'bcc']; |
| 831 | foreach ($keys as $key) { |
| 832 | if (is_array($result[$key])) { |
| 833 | foreach ($result[$key] as $key => $keyValue) { |
| 834 | if (!empty($keyValue['id'])) { |
| 835 | $params['target_contact_id'][] = $keyValue['id']; |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | $params['subject'] = $result['subject']; |
| 841 | $params['activity_date_time'] = $result['date']; |
| 842 | $params['details'] = $result['body']; |
| 843 | |
| 844 | $numAttachments = Civi::settings()->get('max_attachments_backend') ?? CRM_Core_BAO_File::DEFAULT_MAX_ATTACHMENTS_BACKEND; |
| 845 | for ($i = 1; $i <= $numAttachments; $i++) { |
| 846 | if (isset($result["attachFile_$i"])) { |
| 847 | $params["attachFile_$i"] = $result["attachFile_$i"]; |
| 848 | } |
| 849 | else { |
| 850 | // No point looping 100 times if there's only one attachment |
| 851 | break; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | return $params; |
| 856 | } |