3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
12 use Civi\Api4\Contact
;
13 use Civi\Api4\RelationshipType
;
15 require_once 'CRM/Utils/DeprecatedUtils.php';
16 require_once 'api/v3/utils.php';
21 * @copyright CiviCRM LLC https://civicrm.org/licensing
25 * class to parse contact csv files
27 class CRM_Contact_Import_Parser_Contact
extends CRM_Import_Parser
{
29 use CRM_Contact_Import_MetadataTrait
;
31 protected $_mapperKeys = [];
32 protected $_relationships;
35 * Is update only permitted on an id match.
37 * Note this historically was true for when id or external identifier was
38 * present. However, CRM-17275 determined that a dedupe-match could over-ride
39 * external identifier.
43 protected $_updateWithId;
46 protected $_externalIdentifierIndex;
47 protected $_allExternalIdentifiers = [];
48 protected $_parseStreetAddress;
51 * Array of successfully imported contact id's
55 protected $_newContacts;
62 protected $_lineCount;
65 * Array of successfully imported related contact id's
69 protected $_newRelatedContacts;
71 protected $_tableName;
74 * Total number of lines in file
80 protected $_primaryKeyName;
81 protected $_statusFieldName;
83 protected $fieldMetadata = [];
86 * Relationship labels.
88 * Temporary cache of labels to reduce queries in getRelationshipLabels.
91 * e.g ['5a_b' => 'Employer', '5b_a' => 'Employee']
93 protected $relationshipLabels = [];
100 public $_onDuplicate;
103 * Dedupe rule group id to use if set
107 public $_dedupeRuleGroupID = NULL;
112 * @param array $mapperKeys
114 public function __construct($mapperKeys = []) {
115 parent
::__construct();
116 $this->_mapperKeys
= $mapperKeys;
120 * The initializer code, called before processing.
122 public function init() {
123 $this->setFieldMetadata();
124 foreach ($this->getImportableFieldsMetadata() as $name => $field) {
125 $this->addField($name, $field['title'], CRM_Utils_Array
::value('type', $field), CRM_Utils_Array
::value('headerPattern', $field), CRM_Utils_Array
::value('dataPattern', $field), CRM_Utils_Array
::value('hasLocationType', $field));
127 $this->_newContacts
= [];
129 $this->setActiveFields($this->_mapperKeys
);
131 $this->_externalIdentifierIndex
= -1;
134 foreach ($this->_mapperKeys
as $key) {
135 if ($key == 'external_identifier') {
136 $this->_externalIdentifierIndex
= $index;
141 $this->_updateWithId
= FALSE;
142 if (in_array('id', $this->_mapperKeys
) ||
($this->_externalIdentifierIndex
>= 0 && $this->isUpdateExistingContacts())) {
143 $this->_updateWithId
= TRUE;
146 $this->_parseStreetAddress
= CRM_Utils_Array
::value('street_address_parsing', CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'address_options'), FALSE);
150 * Is this a case where the user has opted to update existing contacts.
154 * @throws \API_Exception
156 private function isUpdateExistingContacts(): bool {
157 return in_array((int) $this->getSubmittedValue('onDuplicate'), [
158 CRM_Import_Parser
::DUPLICATE_UPDATE
,
159 CRM_Import_Parser
::DUPLICATE_FILL
,
164 * Gets the fields available for importing in a key-name, title format.
167 * eg. ['first_name' => 'First Name'.....]
169 * @throws \API_Exception
171 * @todo - we are constructing the metadata before we
172 * have set the contact type so we re-do it here.
174 * Once we have cleaned up the way the mapper is handled
175 * we can ditch all the existing _construct parameters in favour
176 * of just the userJobID - there are current open PRs towards this end.
178 public function getAvailableFields(): array {
179 $this->setFieldMetadata();
181 foreach ($this->getImportableFieldsMetadata() as $name => $field) {
182 if ($name === 'id' && $this->isSkipDuplicates()) {
183 // Duplicates are being skipped so id matching is not availble.
186 $return[$name] = $field['title'];
192 * Did the user specify duplicates should be skipped and not imported.
196 * @throws \API_Exception
198 private function isSkipDuplicates(): bool {
199 return ((int) $this->getSubmittedValue('onDuplicate')) === CRM_Import_Parser
::DUPLICATE_SKIP
;
203 * Handle the values in preview mode.
205 * Function will be deprecated in favour of validateValues.
207 * @param array $values
208 * The array of values belonging to this line.
211 * the result of this processing
212 * CRM_Import_Parser::ERROR or CRM_Import_Parser::VALID
214 public function preview(&$values) {
215 return $this->summary($values);
219 * Handle the values in summary mode.
221 * Function will be deprecated in favour of validateValues.
223 * @param array $values
224 * The array of values belonging to this line.
227 * the result of this processing
228 * CRM_Import_Parser::ERROR or CRM_Import_Parser::VALID
230 public function summary(&$values): int {
231 $rowNumber = (int) ($values[count($values) - 1]);
233 $this->validateValues($values);
235 catch (CRM_Core_Exception
$e) {
236 $this->setImportStatus($rowNumber, 'ERROR', $e->getMessage());
237 array_unshift($values, $e->getMessage());
238 return CRM_Import_Parser
::ERROR
;
240 $this->setImportStatus($rowNumber, 'NEW', '');
242 return CRM_Import_Parser
::VALID
;
246 * Get Array of all the fields that could potentially be part
251 public function getAllFields() {
252 return $this->_fields
;
256 * Handle the values in import mode.
258 * @param int $onDuplicate
259 * The code for what action to take on duplicates.
260 * @param array $values
261 * The array of values belonging to this line.
264 * the result of this processing
266 * @throws \CiviCRM_API3_Exception
267 * @throws \CRM_Core_Exception
268 * @throws \API_Exception
270 public function import($onDuplicate, &$values) {
271 $this->_unparsedStreetAddressContacts
= [];
272 if (!$this->getSubmittedValue('doGeocodeAddress')) {
273 // CRM-5854, reset the geocode method to null to prevent geocoding
274 CRM_Utils_GeocodeProvider
::disableForSession();
277 // first make sure this is a valid line
278 //$this->_updateWithId = false;
279 $response = $this->summary($values);
280 $statusFieldName = $this->_statusFieldName
;
282 if ($response != CRM_Import_Parser
::VALID
) {
283 $this->setImportStatus((int) $values[count($values) - 1], 'Invalid', "Invalid (Error Code: $response)");
287 $params = $this->getMappedRow($values);
289 'contact_type' => $this->_contactType
,
292 $contactFields = CRM_Contact_DAO_Contact
::import();
294 //check if external identifier exists in database
295 if (!empty($params['external_identifier']) && (!empty($params['id']) ||
in_array($onDuplicate, [
296 CRM_Import_Parser
::DUPLICATE_SKIP
,
297 CRM_Import_Parser
::DUPLICATE_NOCHECK
,
300 $extIDResult = civicrm_api3('Contact', 'get', [
301 'external_identifier' => $params['external_identifier'],
303 'return' => ['id', 'contact_is_deleted'],
305 if (isset($extIDResult['id'])) {
306 // record with matching external identifier does exist.
307 $internalCid = $extIDResult['id'];
308 if ($internalCid != CRM_Utils_Array
::value('id', $params)) {
309 if ($extIDResult['values'][$internalCid]['contact_is_deleted'] == 1) {
310 // And it is deleted. What to do? If we skip it, they user
311 // will be under the impression that the record exists in
312 // the database, yet they won't be able to find it. If we
313 // don't skip it, the database will try to insert a new record
314 // with an external_identifier that is non-unique. So...
315 // we will update this contact to remove the external_identifier
316 // and let a new record be created.
317 $update_params = ['id' => $internalCid, 'external_identifier' => ''];
318 civicrm_api3('Contact', 'create', $update_params);
321 $errorMessage = ts('External ID already exists in Database.');
322 array_unshift($values, $errorMessage);
323 $this->setImportStatus((int) $values[count($values) - 1], 'ERROR', $errorMessage);
324 return CRM_Import_Parser
::DUPLICATE
;
330 if (!empty($this->_contactSubType
)) {
331 $params['contact_sub_type'] = $this->_contactSubType
;
334 if ($subType = CRM_Utils_Array
::value('contact_sub_type', $params)) {
335 if (CRM_Contact_BAO_ContactType
::isExtendsContactType($subType, $this->_contactType
, FALSE, 'label')) {
336 $subTypes = CRM_Contact_BAO_ContactType
::subTypePairs($this->_contactType
, FALSE, NULL);
337 $params['contact_sub_type'] = array_search($subType, $subTypes);
339 elseif (!CRM_Contact_BAO_ContactType
::isExtendsContactType($subType, $this->_contactType
)) {
340 $message = "Mismatched or Invalid Contact Subtype.";
341 array_unshift($values, $message);
342 return CRM_Import_Parser
::NO_MATCH
;
346 // Get contact id to format common data in update/fill mode,
347 // prioritising a dedupe rule check over an external_identifier check, but falling back on ext id.
348 if ($this->_updateWithId
&& empty($params['id'])) {
350 $possibleMatches = $this->getPossibleContactMatches($params);
352 catch (CRM_Core_Exception
$e) {
353 $errorMessage = $e->getMessage();
354 array_unshift($values, $errorMessage);
356 $this->setImportStatus((int) $values[count($values) - 1], 'ERROR', $errorMessage);
357 return CRM_Import_Parser
::ERROR
;
359 foreach ($possibleMatches as $possibleID) {
360 $params['id'] = $formatted['id'] = $possibleID;
363 //format common data, CRM-4062
364 $this->formatCommonData($params, $formatted, $contactFields);
366 $relationship = FALSE;
367 $createNewContact = TRUE;
368 // Support Match and Update Via Contact ID
369 if ($this->_updateWithId
&& isset($params['id'])) {
370 $createNewContact = FALSE;
371 // @todo - it feels like all the rows from here to the end of the IF
372 // could be removed in favour of a simple check for whether the contact_type & id match
373 $matchedIDs = $this->getIdsOfMatchingContacts($formatted);
374 if (!empty($matchedIDs)) {
375 if (count($matchedIDs) >= 1) {
377 foreach ($matchedIDs as $contactId) {
378 if ($params['id'] == $contactId) {
379 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_type');
380 if ($formatted['contact_type'] == $contactType) {
381 //validation of subtype for update mode
383 $contactSubType = NULL;
384 if (!empty($params['contact_sub_type'])) {
385 $contactSubType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_sub_type');
388 if (!empty($contactSubType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($params['id'], $contactSubType) && $contactSubType != CRM_Utils_Array
::value('contact_sub_type', $formatted))) {
390 $message = "Mismatched contact SubTypes :";
391 array_unshift($values, $message);
393 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
397 $this->_retCode
= CRM_Import_Parser
::VALID
;
401 $message = "Mismatched contact Types :";
402 array_unshift($values, $message);
404 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
409 $message = "Mismatched contact IDs OR Mismatched contact Types :";
410 array_unshift($values, $message);
411 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
417 if (!empty($params['id'])) {
418 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_type');
420 if ($formatted['contact_type'] == $contactType) {
421 //validation of subtype for update mode
423 $contactSubType = NULL;
424 if (!empty($params['contact_sub_type'])) {
425 $contactSubType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_sub_type');
428 if (!empty($contactSubType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($params['id'], $contactSubType) && $contactSubType != CRM_Utils_Array
::value('contact_sub_type', $formatted))) {
430 $message = "Mismatched contact SubTypes :";
431 array_unshift($values, $message);
432 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
435 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $params['id'], FALSE, $this->_dedupeRuleGroupID
);
436 $this->_retCode
= CRM_Import_Parser
::VALID
;
440 $message = "Mismatched contact Types :";
441 array_unshift($values, $message);
442 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
446 // we should avoid multiple errors for single record
447 // since we have already retCode and we trying to force again.
448 if ($this->_retCode
!= CRM_Import_Parser
::NO_MATCH
) {
449 $message = "No contact found for this contact ID:" . $params['id'];
450 array_unshift($values, $message);
451 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
457 //now we want to create new contact on update/fill also.
458 $createNewContact = TRUE;
462 if (isset($newContact) && is_a($newContact, 'CRM_Contact_BAO_Contact')) {
463 $relationship = TRUE;
468 //now we create new contact in update/fill mode also.
470 if ($createNewContact ||
($this->_retCode
!= CRM_Import_Parser
::NO_MATCH
&& $this->_updateWithId
)) {
471 // @todo - there are multiple places where formatting is done that need consolidation.
472 // This handles where the label has been passed in and it has gotten this far.
473 // probably a bunch of hard-coded stuff could be removed to rely on this.
474 $fields = Contact
::getFields(FALSE)
475 ->addWhere('options', '=', TRUE)
476 ->setLoadOptions(TRUE)
477 ->execute()->indexBy('name');
478 foreach ($fields as $fieldName => $fieldSpec) {
479 if (isset($formatted[$fieldName]) && is_array($formatted[$fieldName])) {
480 // If we have an array at this stage, it's probably a multi-select
481 // field that has already been parsed properly into the value that
482 // should be inserted into the database.
485 if (!empty($formatted[$fieldName])
486 && empty($fieldSpec['options'][$formatted[$fieldName]])) {
487 $formatted[$fieldName] = array_search($formatted[$fieldName], $fieldSpec['options'], TRUE) ??
$formatted[$fieldName];
490 //CRM-4430, don't carry if not submitted.
491 if ($this->_updateWithId
&& !empty($params['id'])) {
492 $contactID = $params['id'];
494 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactID, TRUE, $this->_dedupeRuleGroupID
);
497 if (isset($newContact) && is_object($newContact) && ($newContact instanceof CRM_Contact_BAO_Contact
)) {
498 $relationship = TRUE;
499 $newContact = clone($newContact);
500 $contactID = $newContact->id
;
501 $this->_newContacts
[] = $contactID;
503 //get return code if we create new contact in update mode, CRM-4148
504 if ($this->_updateWithId
) {
505 $this->_retCode
= CRM_Import_Parser
::VALID
;
508 elseif (isset($newContact) && CRM_Core_Error
::isAPIError($newContact, CRM_Core_Error
::DUPLICATE_CONTACT
)) {
509 // if duplicate, no need of further processing
510 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_SKIP
) {
511 $errorMessage = "Skipping duplicate record";
512 array_unshift($values, $errorMessage);
513 $this->setImportStatus((int) $values[count($values) - 1], 'DUPLICATE', $errorMessage);
514 return CRM_Import_Parser
::DUPLICATE
;
517 $relationship = TRUE;
518 // CRM-10433/CRM-20739 - IDs could be string or array; handle accordingly
519 if (!is_array($dupeContactIDs = $newContact['error_message']['params'][0])) {
520 $dupeContactIDs = explode(',', $dupeContactIDs);
522 $dupeCount = count($dupeContactIDs);
523 $contactID = array_pop($dupeContactIDs);
524 // check to see if we had more than one duplicate contact id.
525 // if we have more than one, the record will be rejected below
526 if ($dupeCount == 1) {
527 // there was only one dupe, we will continue normally...
528 if (!in_array($contactID, $this->_newContacts
)) {
529 $this->_newContacts
[] = $contactID;
536 $currentImportID = end($values);
539 'contactID' => $contactID,
540 'importID' => $currentImportID,
541 'importTempTable' => $this->_tableName
,
542 'fieldHeaders' => $this->_mapperKeys
,
543 'fields' => $this->_activeFields
,
546 CRM_Utils_Hook
::import('Contact', 'process', $this, $hookParams);
550 $primaryContactId = NULL;
551 if (CRM_Core_Error
::isAPIError($newContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
552 if ($dupeCount == 1 && CRM_Utils_Rule
::integer($contactID)) {
553 $primaryContactId = $contactID;
557 $primaryContactId = $newContact->id
;
560 if ((CRM_Core_Error
::isAPIError($newContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
) ||
is_a($newContact, 'CRM_Contact_BAO_Contact')) && $primaryContactId) {
562 //relationship contact insert
563 foreach ($params as $key => $field) {
564 [$id, $first, $second] = CRM_Utils_System
::explode('_', $key, 3);
565 if (!($first == 'a' && $second == 'b') && !($first == 'b' && $second == 'a')) {
569 $relationType = new CRM_Contact_DAO_RelationshipType();
570 $relationType->id
= $id;
571 $relationType->find(TRUE);
572 $direction = "contact_sub_type_$second";
575 'contact_type' => $params[$key]['contact_type'],
578 //set subtype for related contact CRM-5125
579 if (isset($relationType->$direction)) {
580 //validation of related contact subtype for update mode
581 if ($relCsType = CRM_Utils_Array
::value('contact_sub_type', $params[$key]) && $relCsType != $relationType->$direction) {
582 $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact.");
583 array_unshift($values, $errorMessage);
584 return CRM_Import_Parser
::NO_MATCH
;
587 $formatting['contact_sub_type'] = $relationType->$direction;
591 $contactFields = NULL;
592 $contactFields = CRM_Contact_DAO_Contact
::import();
594 //Relation on the basis of External Identifier.
595 if (empty($params[$key]['id']) && !empty($params[$key]['external_identifier'])) {
596 $params[$key]['id'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['external_identifier'], 'id', 'external_identifier');
598 // check for valid related contact id in update/fill mode, CRM-4424
599 if (in_array($onDuplicate, [
600 CRM_Import_Parser
::DUPLICATE_UPDATE
,
601 CRM_Import_Parser
::DUPLICATE_FILL
,
602 ]) && !empty($params[$key]['id'])) {
603 $relatedContactType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['id'], 'contact_type');
604 if (!$relatedContactType) {
605 $errorMessage = ts("No contact found for this related contact ID: %1", [1 => $params[$key]['id']]);
606 array_unshift($values, $errorMessage);
607 return CRM_Import_Parser
::NO_MATCH
;
610 //validation of related contact subtype for update mode
612 $relatedCsType = NULL;
613 if (!empty($formatting['contact_sub_type'])) {
614 $relatedCsType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['id'], 'contact_sub_type');
617 if (!empty($relatedCsType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($params[$key]['id'], $relatedCsType) &&
618 $relatedCsType != CRM_Utils_Array
::value('contact_sub_type', $formatting))
620 $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact.") . ' ' . ts("ID: %1", [1 => $params[$key]['id']]);
621 array_unshift($values, $errorMessage);
622 return CRM_Import_Parser
::NO_MATCH
;
624 // get related contact id to format data in update/fill mode,
625 //if external identifier is present, CRM-4423
626 $formatting['id'] = $params[$key]['id'];
629 //format common data, CRM-4062
630 $this->formatCommonData($field, $formatting, $contactFields);
633 if (!empty($params[$key]['id'])) {
635 'contact_id' => $params[$key]['id'],
638 $relatedNewContact = CRM_Contact_BAO_Contact
::retrieve($contact, $defaults);
641 $relatedNewContact = $this->createContact($formatting, $contactFields, $onDuplicate, NULL, FALSE);
644 if (is_object($relatedNewContact) ||
($relatedNewContact instanceof CRM_Contact_BAO_Contact
)) {
645 $relatedNewContact = clone($relatedNewContact);
649 // To update/fill contact, get the matching contact Ids if duplicate contact found
650 // otherwise get contact Id from object of related contact
651 if (is_array($relatedNewContact) && civicrm_error($relatedNewContact)) {
652 if (CRM_Core_Error
::isAPIError($relatedNewContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
653 $matchedIDs = $relatedNewContact['error_message']['params'][0];
654 if (!is_array($matchedIDs)) {
655 $matchedIDs = explode(',', $matchedIDs);
659 $errorMessage = $relatedNewContact['error_message'];
660 array_unshift($values, $errorMessage);
661 $this->setImportStatus((int) $values[count($values) - 1], 'ERROR', $errorMessage);
662 return CRM_Import_Parser
::ERROR
;
666 $matchedIDs[] = $relatedNewContact->id
;
668 // update/fill related contact after getting matching Contact Ids, CRM-4424
669 if (in_array($onDuplicate, [
670 CRM_Import_Parser
::DUPLICATE_UPDATE
,
671 CRM_Import_Parser
::DUPLICATE_FILL
,
673 //validation of related contact subtype for update mode
675 $relatedCsType = NULL;
676 if (!empty($formatting['contact_sub_type'])) {
677 $relatedCsType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $matchedIDs[0], 'contact_sub_type');
680 if (!empty($relatedCsType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($matchedIDs[0], $relatedCsType) && $relatedCsType != CRM_Utils_Array
::value('contact_sub_type', $formatting))) {
681 $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact.");
682 array_unshift($values, $errorMessage);
683 return CRM_Import_Parser
::NO_MATCH
;
686 $updatedContact = $this->createContact($formatting, $contactFields, $onDuplicate, $matchedIDs[0]);
689 static $relativeContact = [];
690 if (CRM_Core_Error
::isAPIError($relatedNewContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
691 if (count($matchedIDs) >= 1) {
692 $relContactId = $matchedIDs[0];
693 //add relative contact to count during update & fill mode.
694 //logic to make count distinct by contact id.
695 if ($this->_newRelatedContacts ||
!empty($relativeContact)) {
696 $reContact = array_keys($relativeContact, $relContactId);
698 if (empty($reContact)) {
699 $this->_newRelatedContacts
[] = $relativeContact[] = $relContactId;
703 $this->_newRelatedContacts
[] = $relativeContact[] = $relContactId;
708 $relContactId = $relatedNewContact->id
;
709 $this->_newRelatedContacts
[] = $relativeContact[] = $relContactId;
712 if (CRM_Core_Error
::isAPIError($relatedNewContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
) ||
($relatedNewContact instanceof CRM_Contact_BAO_Contact
)) {
713 //fix for CRM-1993.Checks for duplicate related contacts
714 if (count($matchedIDs) >= 1) {
715 //if more than one duplicate contact
716 //found, create relationship with first contact
717 // now create the relationship record
719 'relationship_type_id' => $key,
724 'skipRecentView' => TRUE,
727 // we only handle related contact success, we ignore failures for now
728 // at some point wold be nice to have related counts as separate
730 'contact' => $primaryContactId,
733 [$valid, $duplicate] = self
::legacyCreateMultiple($relationParams, $relationIds);
735 if ($valid ||
$duplicate) {
736 $relationIds['contactTarget'] = $relContactId;
737 $action = ($duplicate) ? CRM_Core_Action
::UPDATE
: CRM_Core_Action
::ADD
;
738 CRM_Contact_BAO_Relationship
::relatedMemberships($primaryContactId, $relationParams, $relationIds, $action);
741 //handle current employer, CRM-3532
743 $allRelationships = CRM_Core_PseudoConstant
::relationshipType('name');
744 $relationshipTypeId = str_replace([
751 $relationshipType = str_replace($relationshipTypeId . '_', '', $key);
752 $orgId = $individualId = NULL;
753 if ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employee of') {
754 $orgId = $relContactId;
755 $individualId = $primaryContactId;
757 elseif ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employer of') {
758 $orgId = $primaryContactId;
759 $individualId = $relContactId;
761 if ($orgId && $individualId) {
762 $currentEmpParams[$individualId] = $orgId;
763 CRM_Contact_BAO_Contact_Utils
::setCurrentEmployer($currentEmpParams);
771 if ($this->_updateWithId
) {
772 //return warning if street address is unparsed, CRM-5886
773 return $this->processMessage($values, $this->_retCode
);
776 if (is_array($newContact) && civicrm_error($newContact)) {
779 if (($code = CRM_Utils_Array
::value('code', $newContact['error_message'])) && ($code == CRM_Core_Error
::DUPLICATE_CONTACT
)) {
780 return $this->handleDuplicateError($newContact, $statusFieldName, $values, $onDuplicate, $formatted, $contactFields);
782 // Not a dupe, so we had an error
783 $errorMessage = $newContact['error_message'];
784 array_unshift($values, $errorMessage);
785 $this->setImportStatus((int) $values[count($values) - 1], 'ERROR', $errorMessage);
786 return CRM_Import_Parser
::ERROR
;
790 if (empty($this->_unparsedStreetAddressContacts
)) {
791 $this->setImportStatus((int) ($values[count($values) - 1]), 'IMPORTED', '', $contactID);
792 return CRM_Import_Parser
::VALID
;
795 // @todo - record unparsed address as 'imported' but the presence of a message is meaningful?
796 return $this->processMessage($values, CRM_Import_Parser
::VALID
);
800 * Only called from import now... plus one place outside of core & tests.
802 * @todo - deprecate more aggressively - will involve copying to the import
803 * class, adding a deprecation notice here & removing from tests.
805 * Takes an associative array and creates a relationship object.
807 * @deprecated For single creates use the api instead (it's tested).
808 * For multiple a new variant of this function needs to be written and migrated to as this is a bit
811 * @param array $params
812 * (reference ) an assoc array of name/value pairs.
814 * The array that holds all the db ids.
815 * per http://wiki.civicrm.org/confluence/display/CRM/Database+layer
816 * "we are moving away from the $ids param "
819 * @throws \CRM_Core_Exception
821 private static function legacyCreateMultiple($params, $ids = []) {
822 // clarify that the only key ever pass in the ids array is 'contact'
823 // There is legacy handling for other keys but a universe search on
824 // calls to this function (not supported to be called from outside core)
825 // only returns 2 calls - one in CRM_Contact_Import_Parser_Contact
826 // and the other in jma grant applications (CRM_Grant_Form_Grant_Confirm)
827 // both only pass in contact as a key here.
828 $contactID = $ids['contact'];
830 // There is only ever one value passed in from the 2 places above that call
831 // this - by clarifying here like this we can cleanup within this
832 // function without having to do more universe searches.
833 $relatedContactID = key($params['contact_check']);
835 // check if the relationship is valid between contacts.
836 // step 1: check if the relationship is valid if not valid skip and keep the count
837 // step 2: check the if two contacts already have a relationship if yes skip and keep the count
838 // step 3: if valid relationship then add the relation and keep the count
841 [$contactFields['relationship_type_id'], $firstLetter, $secondLetter] = explode('_', $params['relationship_type_id']);
842 $contactFields['contact_id_' . $firstLetter] = $contactID;
843 $contactFields['contact_id_' . $secondLetter] = $relatedContactID;
844 if (!CRM_Contact_BAO_Relationship
::checkRelationshipType($contactFields['contact_id_a'], $contactFields['contact_id_b'],
845 $contactFields['relationship_type_id'])) {
850 CRM_Contact_BAO_Relationship
::checkDuplicateRelationship(
860 $singleInstanceParams = array_merge($params, $contactFields);
861 CRM_Contact_BAO_Relationship
::add($singleInstanceParams);
866 * Format common params data to proper format to store.
868 * @param array $params
869 * Contain record values.
870 * @param array $formatted
871 * Array of formatted data.
872 * @param array $contactFields
873 * Contact DAO fields.
875 private function formatCommonData($params, &$formatted, $contactFields) {
877 CRM_Utils_Array
::value('contact_type', $formatted),
881 //add custom fields for contact sub type
882 if (!empty($this->_contactSubType
)) {
883 $csType = $this->_contactSubType
;
886 if ($relCsType = CRM_Utils_Array
::value('contact_sub_type', $formatted)) {
887 $csType = $relCsType;
890 $customFields = CRM_Core_BAO_CustomField
::getFields($formatted['contact_type'], FALSE, FALSE, $csType);
892 $addressCustomFields = CRM_Core_BAO_CustomField
::getFields('Address');
893 $customFields = $customFields +
$addressCustomFields;
895 //if a Custom Email Greeting, Custom Postal Greeting or Custom Addressee is mapped, and no "Greeting / Addressee Type ID" is provided, then automatically set the type = Customized, CRM-4575
897 'email_greeting_custom' => 'email_greeting',
898 'postal_greeting_custom' => 'postal_greeting',
899 'addressee_custom' => 'addressee',
901 foreach ($elements as $k => $v) {
902 if (array_key_exists($k, $params) && !(array_key_exists($v, $params))) {
903 $label = key(CRM_Core_OptionGroup
::values($v, TRUE, NULL, NULL, 'AND v.name = "Customized"'));
904 $params[$v] = $label;
909 $session = CRM_Core_Session
::singleton();
910 $dateType = $session->get("dateTypes");
911 foreach ($params as $key => $val) {
912 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
913 if ($customFieldID &&
914 !array_key_exists($customFieldID, $addressCustomFields)
916 //we should not update Date to null, CRM-4062
917 if ($val && ($customFields[$customFieldID]['data_type'] == 'Date')) {
919 CRM_Contact_Import_Parser_Contact
::formatCustomDate($params, $formatted, $dateType, $key);
921 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
922 if (empty($val) && !is_numeric($val) && $this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_FILL
) {
923 //retain earlier value when Import mode is `Fill`
924 unset($params[$key]);
927 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
932 if ($key == 'birth_date' && $val) {
933 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key);
935 elseif ($key == 'deceased_date' && $val) {
936 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key);
937 $params['is_deceased'] = 1;
939 elseif ($key == 'is_deceased' && $val) {
940 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
944 //now format custom data.
945 foreach ($params as $key => $field) {
946 if (is_array($field)) {
947 $isAddressCustomField = FALSE;
948 foreach ($field as $value) {
950 if (is_array($value)) {
951 foreach ($value as $name => $testForEmpty) {
952 if ($addressCustomFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
953 $isAddressCustomField = TRUE;
956 // check if $value does not contain IM provider or phoneType
957 if (($name !== 'phone_type_id' ||
$name !== 'provider_id') && ($testForEmpty === '' ||
$testForEmpty == NULL)) {
968 if (!empty($value['location_type_id'])) {
969 $this->formatLocationBlock($value, $formatted);
972 // @todo - this is still reachable - e.g. import with related contact info like firstname,lastname,spouse-first-name,spouse-last-name,spouse-home-phone
973 CRM_Core_Error
::deprecatedFunctionWarning('this is not expected to be reachable now');
974 $this->formatContactParameters($value, $formatted);
978 if (!$isAddressCustomField) {
987 if (($key !== 'preferred_communication_method') && (array_key_exists($key, $contactFields))) {
988 // due to merging of individual table and
989 // contact table, we need to avoid
990 // preferred_communication_method forcefully
991 $formatValues['contact_type'] = $formatted['contact_type'];
994 if ($key == 'id' && isset($field)) {
995 $formatted[$key] = $field;
997 $this->formatContactParameters($formatValues, $formatted);
999 //Handling Custom Data
1000 // note: Address custom fields will be handled separately inside formatContactParameters
1001 if (($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) &&
1002 array_key_exists($customFieldID, $customFields) &&
1003 !array_key_exists($customFieldID, $addressCustomFields)
1006 $extends = $customFields[$customFieldID]['extends'] ??
NULL;
1007 $htmlType = $customFields[$customFieldID]['html_type'] ??
NULL;
1008 $dataType = $customFields[$customFieldID]['data_type'] ??
NULL;
1009 $serialized = CRM_Core_BAO_CustomField
::isSerialized($customFields[$customFieldID]);
1011 if (!$serialized && in_array($htmlType, ['Select', 'Radio', 'Autocomplete-Select']) && in_array($dataType, ['String', 'Int'])) {
1012 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1013 foreach ($customOption as $customValue) {
1014 $val = $customValue['value'] ??
NULL;
1015 $label = strtolower($customValue['label'] ??
'');
1016 $value = strtolower(trim($formatted[$key]));
1017 if (($value == $label) ||
($value == strtolower($val))) {
1018 $params[$key] = $formatted[$key] = $val;
1022 elseif ($serialized && !empty($formatted[$key]) && !empty($params[$key])) {
1023 $mulValues = explode(',', $formatted[$key]);
1024 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1025 $formatted[$key] = [];
1027 foreach ($mulValues as $v1) {
1028 foreach ($customOption as $v2) {
1029 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
1030 (strtolower($v2['value']) == strtolower(trim($v1)))
1032 if ($htmlType == 'CheckBox') {
1033 $params[$key][$v2['value']] = $formatted[$key][$v2['value']] = 1;
1036 $params[$key][] = $formatted[$key][] = $v2['value'];
1045 if (!empty($key) && ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) && array_key_exists($customFieldID, $customFields) &&
1046 !array_key_exists($customFieldID, $addressCustomFields)
1048 // @todo calling api functions directly is not supported
1049 _civicrm_api3_custom_format_params($params, $formatted, $extends);
1052 // to check if not update mode and unset the fields with empty value.
1053 if (!$this->_updateWithId
&& array_key_exists('custom', $formatted)) {
1054 foreach ($formatted['custom'] as $customKey => $customvalue) {
1055 if (empty($formatted['custom'][$customKey][-1]['is_required'])) {
1056 $formatted['custom'][$customKey][-1]['is_required'] = $customFields[$customKey]['is_required'];
1058 $emptyValue = $customvalue[-1]['value'] ??
NULL;
1059 if (!isset($emptyValue)) {
1060 unset($formatted['custom'][$customKey]);
1065 // parse street address, CRM-5450
1066 if ($this->_parseStreetAddress
) {
1067 if (array_key_exists('address', $formatted) && is_array($formatted['address'])) {
1068 foreach ($formatted['address'] as $instance => & $address) {
1069 $streetAddress = $address['street_address'] ??
NULL;
1070 if (empty($streetAddress)) {
1073 // parse address field.
1074 $parsedFields = CRM_Core_BAO_Address
::parseStreetAddress($streetAddress);
1076 //street address consider to be parsed properly,
1077 //If we get street_name and street_number.
1078 if (empty($parsedFields['street_name']) ||
empty($parsedFields['street_number'])) {
1079 $parsedFields = array_fill_keys(array_keys($parsedFields), '');
1082 // merge parse address w/ main address block.
1083 $address = array_merge($address, $parsedFields);
1090 * Get the array of successfully imported contact id's
1094 public function getImportedContacts() {
1095 return $this->_newContacts
;
1099 * Get the array of successfully imported related contact id's
1103 public function &getRelatedImportedContacts() {
1104 return $this->_newRelatedContacts
;
1108 * Check if an error in custom data.
1110 * @param array $params
1111 * @param string $errorMessage
1112 * A string containing all the error-fields.
1114 * @param null $csType
1115 * @param null $relationships
1117 public static function isErrorInCustomData($params, &$errorMessage, $csType = NULL, $relationships = NULL) {
1118 $dateType = CRM_Core_Session
::singleton()->get("dateTypes");
1121 if (!empty($params['contact_sub_type'])) {
1122 $csType = $params['contact_sub_type'] ??
NULL;
1125 if (empty($params['contact_type'])) {
1126 $params['contact_type'] = 'Individual';
1129 // get array of subtypes - CRM-18708
1130 if (in_array($csType, CRM_Contact_BAO_ContactType
::basicTypes(TRUE), TRUE)) {
1131 $csType = self
::getSubtypes($params['contact_type']);
1134 if (is_array($csType)) {
1135 // fetch custom fields for every subtype and add it to $customFields array
1138 foreach ($csType as $cType) {
1139 $customFields +
= CRM_Core_BAO_CustomField
::getFields($params['contact_type'], FALSE, FALSE, $cType);
1143 $customFields = CRM_Core_BAO_CustomField
::getFields($params['contact_type'], FALSE, FALSE, $csType);
1146 $addressCustomFields = CRM_Core_BAO_CustomField
::getFields('Address');
1147 $customFields = $customFields +
$addressCustomFields;
1148 foreach ($params as $key => $value) {
1149 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1150 /* check if it's a valid custom field id */
1152 if (!array_key_exists($customFieldID, $customFields)) {
1153 $errors[] = ts('field ID');
1155 // validate null values for required custom fields of type boolean
1156 if (!empty($customFields[$customFieldID]['is_required']) && (empty($params['custom_' . $customFieldID]) && !is_numeric($params['custom_' . $customFieldID])) && $customFields[$customFieldID]['data_type'] == 'Boolean') {
1157 $errors[] = $customFields[$customFieldID]['label'] . '::' . $customFields[$customFieldID]['groupTitle'];
1160 //For address custom fields, we do get actual custom field value as an inner array of
1161 //values so need to modify
1162 if (array_key_exists($customFieldID, $addressCustomFields)) {
1163 $value = $value[0][$key];
1165 /* validate the data against the CF type */
1168 $dataType = $customFields[$customFieldID]['data_type'];
1169 $htmlType = $customFields[$customFieldID]['html_type'];
1170 $isSerialized = CRM_Core_BAO_CustomField
::isSerialized($customFields[$customFieldID]);
1171 if ($dataType == 'Date') {
1172 if (array_key_exists($customFieldID, $addressCustomFields) && CRM_Utils_Date
::convertToDefaultDate($params[$key][0], $dateType, $key)) {
1173 $value = $params[$key][0][$key];
1175 elseif (CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key)) {
1176 $value = $params[$key];
1179 $errors[] = $customFields[$customFieldID]['label'];
1182 elseif ($dataType == 'Boolean') {
1183 if (CRM_Utils_String
::strtoboolstr($value) === FALSE) {
1184 $errors[] = $customFields[$customFieldID]['label'] . '::' . $customFields[$customFieldID]['groupTitle'];
1187 // need not check for label filed import
1188 $selectHtmlTypes = [
1193 if ((!$isSerialized && !in_array($htmlType, $selectHtmlTypes)) ||
$dataType == 'Boolean' ||
$dataType == 'ContactReference') {
1194 $valid = CRM_Core_BAO_CustomValue
::typecheck($dataType, $value);
1196 $errors[] = $customFields[$customFieldID]['label'];
1200 // check for values for custom fields for checkboxes and multiselect
1201 if ($isSerialized && $dataType != 'ContactReference') {
1202 $value = trim($value);
1203 $value = str_replace('|', ',', $value);
1204 $mulValues = explode(',', $value);
1205 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1206 foreach ($mulValues as $v1) {
1207 if (strlen($v1) == 0) {
1212 foreach ($customOption as $v2) {
1213 if ((strtolower(trim($v2['label'])) == strtolower(trim($v1))) ||
(strtolower(trim($v2['value'])) == strtolower(trim($v1)))) {
1219 $errors[] = $customFields[$customFieldID]['label'];
1223 elseif ($htmlType == 'Select' ||
($htmlType == 'Radio' && $dataType != 'Boolean')) {
1224 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1226 foreach ($customOption as $v2) {
1227 if ((strtolower(trim($v2['label'])) == strtolower(trim($value))) ||
(strtolower(trim($v2['value'])) == strtolower(trim($value)))) {
1232 $errors[] = $customFields[$customFieldID]['label'];
1235 elseif ($isSerialized && $dataType === 'StateProvince') {
1236 $mulValues = explode(',', $value);
1237 foreach ($mulValues as $stateValue) {
1239 if (self
::in_value(trim($stateValue), CRM_Core_PseudoConstant
::stateProvinceAbbreviation()) || self
::in_value(trim($stateValue), CRM_Core_PseudoConstant
::stateProvince())) {
1243 $errors[] = $customFields[$customFieldID]['label'];
1248 elseif ($isSerialized && $dataType == 'Country') {
1249 $mulValues = explode(',', $value);
1250 foreach ($mulValues as $countryValue) {
1251 if ($countryValue) {
1252 CRM_Core_PseudoConstant
::populate($countryNames, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active');
1253 CRM_Core_PseudoConstant
::populate($countryIsoCodes, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
1254 $limitCodes = CRM_Core_BAO_Country
::countryLimit();
1262 if (in_array(trim($countryValue), $values)) {
1269 $errors[] = $customFields[$customFieldID]['label'];
1276 elseif (is_array($params[$key]) && isset($params[$key]["contact_type"])) {
1278 //supporting custom data of related contact subtypes
1280 if ($relationships) {
1281 if (array_key_exists($key, $relationships)) {
1284 elseif (CRM_Utils_Array
::key($key, $relationships)) {
1285 $relation = CRM_Utils_Array
::key($key, $relationships);
1288 if (!empty($relation)) {
1289 [$id, $first, $second] = CRM_Utils_System
::explode('_', $relation, 3);
1290 $direction = "contact_sub_type_$second";
1291 $relationshipType = new CRM_Contact_BAO_RelationshipType();
1292 $relationshipType->id
= $id;
1293 if ($relationshipType->find(TRUE)) {
1294 if (isset($relationshipType->$direction)) {
1295 $params[$key]['contact_sub_type'] = $relationshipType->$direction;
1300 self
::isErrorInCustomData($params[$key], $errorMessage, $csType, $relationships);
1304 $errorMessage .= ($errorMessage ?
'; ' : '') . implode('; ', $errors);
1309 * Check if value present in all genders or.
1310 * as a substring of any gender value, if yes than return corresponding gender.
1311 * eg value might be m/M, ma/MA, mal/MAL, male return 'Male'
1312 * but if value is 'maleabc' than return false
1314 * @param string $gender
1315 * Check this value across gender values.
1317 * retunr gender value / false
1321 public function checkGender($gender) {
1322 $gender = trim($gender, '.');
1327 $allGenders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
1328 foreach ($allGenders as $key => $value) {
1329 if (strlen($gender) > strlen($value)) {
1332 if ($gender == $value) {
1335 if (substr_compare($value, $gender, 0, strlen($gender), TRUE) === 0) {
1344 * Check if an error in Core( non-custom fields ) field
1346 * @param array $params
1347 * @param string $errorMessage
1348 * A string containing all the error-fields.
1350 public function isErrorInCoreData($params, &$errorMessage) {
1352 foreach ($params as $key => $value) {
1354 $session = CRM_Core_Session
::singleton();
1355 $dateType = $session->get("dateTypes");
1359 if (CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key)) {
1360 if (!CRM_Utils_Rule
::date($params[$key])) {
1361 $errors[] = ts('Birth Date');
1365 $errors[] = ts('Birth-Date');
1369 case 'deceased_date':
1370 if (CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key)) {
1371 if (!CRM_Utils_Rule
::date($params[$key])) {
1372 $errors[] = ts('Deceased Date');
1376 $errors[] = ts('Deceased Date');
1381 if (CRM_Utils_String
::strtoboolstr($value) === FALSE) {
1382 $errors[] = ts('Deceased');
1387 if (!self
::checkGender($value)) {
1388 $errors[] = ts('Gender');
1392 case 'preferred_communication_method':
1394 $preffComm = explode(',', $value);
1395 foreach ($preffComm as $v) {
1396 if (!self
::in_value(trim($v), CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method'))) {
1397 $errors[] = ts('Preferred Communication Method');
1402 case 'preferred_mail_format':
1403 if (!array_key_exists(strtolower($value), array_change_key_case(CRM_Core_SelectValues
::pmf(), CASE_LOWER
))) {
1404 $errors[] = ts('Preferred Mail Format');
1408 case 'individual_prefix':
1410 if (!self
::in_value($value, CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id'))) {
1411 $errors[] = ts('Individual Prefix');
1415 case 'individual_suffix':
1417 if (!self
::in_value($value, CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id'))) {
1418 $errors[] = ts('Individual Suffix');
1422 case 'state_province':
1423 if (!empty($value)) {
1424 foreach ($value as $stateValue) {
1425 if ($stateValue['state_province']) {
1426 if (self
::in_value($stateValue['state_province'], CRM_Core_PseudoConstant
::stateProvinceAbbreviation()) ||
1427 self
::in_value($stateValue['state_province'], CRM_Core_PseudoConstant
::stateProvince())
1432 $errors[] = ts('State/Province');
1440 if (!empty($value)) {
1441 foreach ($value as $stateValue) {
1442 if ($stateValue['country']) {
1443 CRM_Core_PseudoConstant
::populate($countryNames, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active');
1444 CRM_Core_PseudoConstant
::populate($countryIsoCodes, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
1445 $limitCodes = CRM_Core_BAO_Country
::countryLimit();
1446 //If no country is selected in
1447 //localization then take all countries
1448 if (empty($limitCodes)) {
1449 $limitCodes = $countryIsoCodes;
1452 if (self
::in_value($stateValue['country'], $limitCodes) || self
::in_value($stateValue['country'], CRM_Core_PseudoConstant
::country())) {
1455 if (self
::in_value($stateValue['country'], $countryIsoCodes) || self
::in_value($stateValue['country'], $countryNames)) {
1456 $errors[] = ts('Country input value is in table but not "available": "This Country is valid but is NOT in the list of Available Countries currently configured for your site. This can be viewed and modifed from Administer > Localization > Languages Currency Locations." ');
1459 $errors[] = ts('Country input value not in country table: "The Country value appears to be invalid. It does not match any value in CiviCRM table of countries."');
1467 if (!empty($value)) {
1468 foreach ($value as $county) {
1469 if ($county['county']) {
1470 $countyNames = CRM_Core_PseudoConstant
::county();
1471 if (!empty($county['county']) && !in_array($county['county'], $countyNames)) {
1472 $errors[] = ts('County input value not in county table: The County value appears to be invalid. It does not match any value in CiviCRM table of counties.');
1480 if (!empty($value)) {
1481 foreach ($value as $codeValue) {
1482 if (!empty($codeValue['geo_code_1'])) {
1483 if (CRM_Utils_Rule
::numeric($codeValue['geo_code_1'])) {
1486 $errors[] = ts('Geo code 1');
1493 if (!empty($value)) {
1494 foreach ($value as $codeValue) {
1495 if (!empty($codeValue['geo_code_2'])) {
1496 if (CRM_Utils_Rule
::numeric($codeValue['geo_code_2'])) {
1499 $errors[] = ts('Geo code 2');
1505 //check for any error in email/postal greeting, addressee,
1506 //custom email/postal greeting, custom addressee, CRM-4575
1508 case 'email_greeting':
1509 $emailGreetingFilter = [
1510 'contact_type' => $this->_contactType
,
1511 'greeting_type' => 'email_greeting',
1513 if (!self
::in_value($value, CRM_Core_PseudoConstant
::greeting($emailGreetingFilter))) {
1514 $errors[] = ts('Email Greeting must be one of the configured format options. Check Administer >> System Settings >> Option Groups >> Email Greetings for valid values');
1518 case 'postal_greeting':
1519 $postalGreetingFilter = [
1520 'contact_type' => $this->_contactType
,
1521 'greeting_type' => 'postal_greeting',
1523 if (!self
::in_value($value, CRM_Core_PseudoConstant
::greeting($postalGreetingFilter))) {
1524 $errors[] = ts('Postal Greeting must be one of the configured format options. Check Administer >> System Settings >> Option Groups >> Postal Greetings for valid values');
1529 $addresseeFilter = [
1530 'contact_type' => $this->_contactType
,
1531 'greeting_type' => 'addressee',
1533 if (!self
::in_value($value, CRM_Core_PseudoConstant
::greeting($addresseeFilter))) {
1534 $errors[] = ts('Addressee must be one of the configured format options. Check Administer >> System Settings >> Option Groups >> Addressee for valid values');
1538 case 'email_greeting_custom':
1539 if (array_key_exists('email_greeting', $params)) {
1540 $emailGreetingLabel = key(CRM_Core_OptionGroup
::values('email_greeting', TRUE, NULL, NULL, 'AND v.name = "Customized"'));
1541 if (CRM_Utils_Array
::value('email_greeting', $params) != $emailGreetingLabel) {
1542 $errors[] = ts('Email Greeting - Custom');
1547 case 'postal_greeting_custom':
1548 if (array_key_exists('postal_greeting', $params)) {
1549 $postalGreetingLabel = key(CRM_Core_OptionGroup
::values('postal_greeting', TRUE, NULL, NULL, 'AND v.name = "Customized"'));
1550 if (CRM_Utils_Array
::value('postal_greeting', $params) != $postalGreetingLabel) {
1551 $errors[] = ts('Postal Greeting - Custom');
1556 case 'addressee_custom':
1557 if (array_key_exists('addressee', $params)) {
1558 $addresseeLabel = key(CRM_Core_OptionGroup
::values('addressee', TRUE, NULL, NULL, 'AND v.name = "Customized"'));
1559 if (CRM_Utils_Array
::value('addressee', $params) != $addresseeLabel) {
1560 $errors[] = ts('Addressee - Custom');
1566 if (is_array($value)) {
1567 foreach ($value as $values) {
1568 if (!empty($values['url']) && !CRM_Utils_Rule
::url($values['url'])) {
1569 $errors[] = ts('Website');
1576 case 'do_not_email':
1577 case 'do_not_phone':
1580 case 'do_not_trade':
1581 if (CRM_Utils_Rule
::boolean($value) == FALSE) {
1582 $key = ucwords(str_replace("_", " ", $key));
1588 if (is_array($value)) {
1589 foreach ($value as $values) {
1590 if (!empty($values['email']) && !CRM_Utils_Rule
::email($values['email'])) {
1599 if (is_array($params[$key]) && isset($params[$key]["contact_type"])) {
1600 //check for any relationship data ,FIX ME
1601 self
::isErrorInCoreData($params[$key], $errorMessage);
1607 $errorMessage .= ($errorMessage ?
'; ' : '') . implode('; ', $errors);
1612 * Ckeck a value present or not in a array.
1615 * @param $valueArray
1619 public static function in_value($value, $valueArray) {
1620 foreach ($valueArray as $key => $v) {
1622 if (strtolower(trim($v, ".")) == strtolower(trim($value, "."))) {
1630 * Build error-message containing error-fields
1632 * Once upon a time there was a dev who hadn't heard of implode. That dev wrote this function.
1634 * @todo just say no!
1636 * @param string $errorName
1637 * A string containing error-field name.
1638 * @param string $errorMessage
1639 * A string containing all the error-fields, where the new errorName is concatenated.
1642 public static function addToErrorMsg($errorName, &$errorMessage) {
1643 if ($errorMessage) {
1644 $errorMessage .= "; $errorName";
1647 $errorMessage = $errorName;
1652 * Method for creating contact.
1654 * @param array $formatted
1655 * @param array $contactFields
1656 * @param int $onDuplicate
1657 * @param int $contactId
1658 * @param bool $requiredCheck
1659 * @param int $dedupeRuleGroupID
1661 * @return array|bool|\CRM_Contact_BAO_Contact|\CRM_Core_Error|null
1663 public function createContact(&$formatted, &$contactFields, $onDuplicate, $contactId = NULL, $requiredCheck = TRUE, $dedupeRuleGroupID = NULL) {
1667 if (is_null($contactId) && ($onDuplicate != CRM_Import_Parser
::DUPLICATE_NOCHECK
)) {
1668 $dupeCheck = (bool) ($onDuplicate);
1671 //get the prefix id etc if exists
1672 CRM_Contact_BAO_Contact
::resolveDefaults($formatted, TRUE);
1674 //@todo direct call to API function not supported.
1675 // setting required check to false, CRM-2839
1676 // plus we do our own required check in import
1678 $error = $this->deprecated_contact_check_params($formatted, $dupeCheck, $dedupeRuleGroupID);
1682 $this->deprecated_validate_formatted_contact($formatted);
1684 catch (CRM_Core_Exception
$e) {
1685 return ['error_message' => $e->getMessage(), 'is_error' => 1, 'code' => $e->getCode()];
1689 $this->formatParams($formatted, $onDuplicate, (int) $contactId);
1692 // Resetting and rebuilding cache could be expensive.
1693 CRM_Core_Config
::setPermitCacheFlushMode(FALSE);
1695 // If a user has logged in, or accessed via a checksum
1696 // Then deliberately 'blanking' a value in the profile should remove it from their record
1697 // @todo this should either be TRUE or FALSE in the context of import - once
1698 // we figure out which we can remove all the rest.
1699 // Also note the meaning of this parameter is less than it used to
1700 // be following block cleanup.
1701 $formatted['updateBlankLocInfo'] = TRUE;
1702 if ((CRM_Core_Session
::singleton()->get('authSrc') & (CRM_Core_Permission
::AUTH_SRC_CHECKSUM + CRM_Core_Permission
::AUTH_SRC_LOGIN
)) == 0) {
1703 $formatted['updateBlankLocInfo'] = FALSE;
1706 [$data, $contactDetails] = CRM_Contact_BAO_Contact
::formatProfileContactParams($formatted, $contactFields, $contactId, NULL, $formatted['contact_type']);
1708 // manage is_opt_out
1709 if (array_key_exists('is_opt_out', $contactFields) && array_key_exists('is_opt_out', $formatted)) {
1710 $wasOptOut = $contactDetails['is_opt_out'] ??
FALSE;
1711 $isOptOut = $formatted['is_opt_out'];
1712 $data['is_opt_out'] = $isOptOut;
1713 // on change, create new civicrm_subscription_history entry
1714 if (($wasOptOut != $isOptOut) && !empty($contactDetails['contact_id'])) {
1716 'contact_id' => $contactDetails['contact_id'],
1717 'status' => $isOptOut ?
'Removed' : 'Added',
1720 CRM_Contact_BAO_SubscriptionHistory
::create($shParams);
1724 $contact = civicrm_api3('Contact', 'create', $data);
1725 $cid = $contact['id'];
1727 CRM_Core_Config
::setPermitCacheFlushMode(TRUE);
1730 'contact_id' => $cid,
1734 $newContact = CRM_Contact_BAO_Contact
::retrieve($contact, $defaults);
1736 //get the id of the contact whose street address is not parsable, CRM-5886
1737 if ($this->_parseStreetAddress
&& is_object($newContact) && property_exists($newContact, 'address') && $newContact->address
) {
1738 foreach ($newContact->address
as $address) {
1739 if (!empty($address['street_address']) && (empty($address['street_number']) ||
empty($address['street_name']))) {
1740 $this->_unparsedStreetAddressContacts
[] = [
1741 'id' => $newContact->id
,
1742 'streetAddress' => $address['street_address'],
1751 * Format params for update and fill mode.
1753 * @param array $params
1754 * reference to an array containing all the.
1756 * @param int $onDuplicate
1760 public function formatParams(&$params, $onDuplicate, $cid) {
1761 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_SKIP
) {
1766 'contact_id' => $cid,
1770 $contactObj = CRM_Contact_BAO_Contact
::retrieve($contactParams, $defaults);
1772 $modeFill = ($onDuplicate == CRM_Import_Parser
::DUPLICATE_FILL
);
1774 $groupTree = CRM_Core_BAO_CustomGroup
::getTree($params['contact_type'], NULL, $cid, 0, NULL);
1775 CRM_Core_BAO_CustomGroup
::setDefaults($groupTree, $defaults, FALSE, FALSE);
1781 'website' => 'website',
1782 'address' => 'address',
1785 $contact = get_object_vars($contactObj);
1787 foreach ($params as $key => $value) {
1788 if ($key == 'id' ||
$key == 'contact_type') {
1792 if (array_key_exists($key, $locationFields)) {
1795 if (in_array($key, [
1800 // CRM-4575, need to null custom
1801 if ($params["{$key}_id"] != 4) {
1802 $params["{$key}_custom"] = 'null';
1804 unset($params[$key]);
1807 if ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1808 $custom_params = ['id' => $contact['id'], 'return' => $key];
1809 $getValue = civicrm_api3('Contact', 'getvalue', $custom_params);
1810 if (empty($getValue)) {
1815 $getValue = CRM_Utils_Array
::retrieveValueRecursive($contact, $key);
1817 if ($key == 'contact_source') {
1818 $params['source'] = $params[$key];
1819 unset($params[$key]);
1822 if ($modeFill && isset($getValue)) {
1823 unset($params[$key]);
1824 if ($customFieldId) {
1825 // Extra values must be unset to ensure the values are not
1827 unset($params['custom'][$customFieldId]);
1833 foreach ($locationFields as $locKeys) {
1834 if (isset($params[$locKeys]) && is_array($params[$locKeys])) {
1835 foreach ($params[$locKeys] as $key => $value) {
1837 $getValue = CRM_Utils_Array
::retrieveValueRecursive($contact, $locKeys);
1839 if (isset($getValue)) {
1840 foreach ($getValue as $cnt => $values) {
1841 if ($locKeys == 'website') {
1842 if (($getValue[$cnt]['website_type_id'] == $params[$locKeys][$key]['website_type_id'])) {
1843 unset($params[$locKeys][$key]);
1847 if ((!empty($getValue[$cnt]['location_type_id']) && !empty($params[$locKeys][$key]['location_type_id'])) && $getValue[$cnt]['location_type_id'] == $params[$locKeys][$key]['location_type_id']) {
1848 unset($params[$locKeys][$key]);
1855 if (count($params[$locKeys]) == 0) {
1856 unset($params[$locKeys]);
1863 * Convert any given date string to default date array.
1865 * @param array $params
1866 * Has given date-format.
1867 * @param array $formatted
1868 * Store formatted date in this array.
1869 * @param int $dateType
1871 * @param string $dateParam
1874 public static function formatCustomDate(&$params, &$formatted, $dateType, $dateParam) {
1876 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $dateParam);
1877 $formatted[$dateParam] = CRM_Utils_Date
::processDate($params[$dateParam]);
1881 * Generate status and error message for unparsed street address records.
1883 * @param array $values
1884 * The array of values belonging to each row.
1885 * @param $returnCode
1889 private function processMessage(&$values, $returnCode) {
1890 if (empty($this->_unparsedStreetAddressContacts
)) {
1891 $this->setImportStatus((int) ($values[count($values) - 1]), 'IMPORTED', '');
1894 $errorMessage = ts("Record imported successfully but unable to parse the street address: ");
1895 foreach ($this->_unparsedStreetAddressContacts
as $contactInfo => $contactValue) {
1896 $contactUrl = CRM_Utils_System
::url('civicrm/contact/add', 'reset=1&action=update&cid=' . $contactValue['id'], TRUE, NULL, FALSE);
1897 $errorMessage .= "\n Contact ID:" . $contactValue['id'] . " <a href=\"$contactUrl\"> " . $contactValue['streetAddress'] . "</a>";
1899 array_unshift($values, $errorMessage);
1900 $returnCode = CRM_Import_Parser
::UNPARSED_ADDRESS_WARNING
;
1901 $this->setImportStatus((int) ($values[count($values) - 1]), 'ERROR', $errorMessage);
1907 * get subtypes given the contact type
1909 * @param string $contactType
1910 * @return array $subTypes
1912 public static function getSubtypes($contactType) {
1914 $types = CRM_Contact_BAO_ContactType
::subTypeInfo($contactType);
1916 if (count($types) > 0) {
1917 foreach ($types as $type) {
1918 $subTypes[] = $type['name'];
1925 * Get the possible contact matches.
1927 * 1) the chosen dedupe rule falling back to
1928 * 2) a check for the external ID.
1930 * @see https://issues.civicrm.org/jira/browse/CRM-17275
1932 * @param array $params
1935 * IDs of possible matches.
1937 * @throws \CRM_Core_Exception
1938 * @throws \CiviCRM_API3_Exception
1940 protected function getPossibleContactMatches($params) {
1943 if (!empty($params['external_identifier'])) {
1944 // Check for any match on external id, deleted or otherwise.
1945 $extIDContact = civicrm_api3('Contact', 'get', [
1946 'external_identifier' => $params['external_identifier'],
1948 'return' => ['id', 'contact_is_deleted'],
1950 if (isset($extIDContact['id'])) {
1951 $extIDMatch = $extIDContact['id'];
1953 if ($extIDContact['values'][$extIDMatch]['contact_is_deleted'] == 1) {
1954 // If the contact is deleted, update external identifier to be blank
1955 // to avoid key error from MySQL.
1956 $params = ['id' => $extIDMatch, 'external_identifier' => ''];
1957 civicrm_api3('Contact', 'create', $params);
1959 // And now it is no longer a match.
1964 $checkParams = ['check_permissions' => FALSE, 'match' => $params];
1965 $checkParams['match']['contact_type'] = $this->_contactType
;
1967 $possibleMatches = civicrm_api3('Contact', 'duplicatecheck', $checkParams);
1969 return array_keys($possibleMatches['values']);
1971 if ($possibleMatches['count']) {
1972 if (array_key_exists($extIDMatch, $possibleMatches['values'])) {
1973 return [$extIDMatch];
1975 throw new CRM_Core_Exception(ts(
1976 'Matching this contact based on the de-dupe rule would cause an external ID conflict'));
1978 return [$extIDMatch];
1982 * Format the form mapping parameters ready for the parser.
1987 * @return array $parserParameters
1989 public static function getParameterForParser($count) {
1991 for ($i = 0; $i < $count; $i++
) {
1992 $baseArray[$i] = NULL;
1994 $parserParameters['mapperLocType'] = $baseArray;
1995 $parserParameters['mapperPhoneType'] = $baseArray;
1996 $parserParameters['mapperImProvider'] = $baseArray;
1997 $parserParameters['mapperWebsiteType'] = $baseArray;
1998 $parserParameters['mapperRelated'] = $baseArray;
1999 $parserParameters['relatedContactType'] = $baseArray;
2000 $parserParameters['relatedContactDetails'] = $baseArray;
2001 $parserParameters['relatedContactLocType'] = $baseArray;
2002 $parserParameters['relatedContactPhoneType'] = $baseArray;
2003 $parserParameters['relatedContactImProvider'] = $baseArray;
2004 $parserParameters['relatedContactWebsiteType'] = $baseArray;
2006 return $parserParameters;
2011 * Set field metadata.
2013 protected function setFieldMetadata() {
2014 $this->setImportableFieldsMetadata($this->getContactImportMetadata());
2015 // Probably no longer needed but here for now.
2016 $this->_relationships
= $this->getRelationships();
2020 * @param array $newContact
2021 * @param $statusFieldName
2022 * @param array $values
2023 * @param int $onDuplicate
2024 * @param array $formatted
2025 * @param array $contactFields
2029 * @throws \CRM_Core_Exception
2030 * @throws \CiviCRM_API3_Exception
2031 * @throws \Civi\API\Exception\UnauthorizedException
2033 protected function handleDuplicateError(array $newContact, $statusFieldName, array $values, int $onDuplicate, array $formatted, array $contactFields): int {
2035 // need to fix at some stage and decide if the error will return an
2036 // array or string, crude hack for now
2037 if (is_array($newContact['error_message']['params'][0])) {
2038 $cids = $newContact['error_message']['params'][0];
2041 $cids = explode(',', $newContact['error_message']['params'][0]);
2044 foreach ($cids as $cid) {
2045 $urls[] = CRM_Utils_System
::url('civicrm/contact/view', 'reset=1&cid=' . $cid, TRUE);
2048 $url_string = implode("\n", $urls);
2050 // If we duplicate more than one record, skip no matter what
2051 if (count($cids) > 1) {
2052 $errorMessage = ts('Record duplicates multiple contacts');
2053 //combine error msg to avoid mismatch between error file columns.
2054 $errorMessage .= "\n" . $url_string;
2055 array_unshift($values, $errorMessage);
2056 $this->setImportStatus((int) $values[count($values) - 1], 'ERROR', $errorMessage);
2057 return CRM_Import_Parser
::ERROR
;
2060 // Params only had one id, so shift it out
2061 $contactId = array_shift($cids);
2064 $vals = ['contact_id' => $contactId];
2066 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_REPLACE
) {
2067 civicrm_api('contact', 'delete', $vals);
2068 $cid = CRM_Contact_BAO_Contact
::createProfileContact($formatted, $contactFields, $contactId, NULL, NULL, $formatted['contact_type']);
2070 if (in_array((int) $onDuplicate, [CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::DUPLICATE_FILL
], TRUE)) {
2071 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactId);
2073 // else skip does nothing and just returns an error code.
2076 'contact_id' => $cid,
2079 $newContact = CRM_Contact_BAO_Contact
::retrieve($contact, $defaults);
2082 if (civicrm_error($newContact)) {
2083 if (empty($newContact['error_message']['params'])) {
2084 // different kind of error other than DUPLICATE
2085 $errorMessage = $newContact['error_message'];
2086 array_unshift($values, $errorMessage);
2087 $this->setImportStatus((int) $values[count($values) - 1], 'ERROR', $errorMessage);
2088 return CRM_Import_Parser
::ERROR
;
2091 $contactID = $newContact['error_message']['params'][0];
2092 if (is_array($contactID)) {
2093 $contactID = array_pop($contactID);
2095 if (!in_array($contactID, $this->_newContacts
)) {
2096 $this->_newContacts
[] = $contactID;
2099 //CRM-262 No Duplicate Checking
2100 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_SKIP
) {
2101 array_unshift($values, $url_string);
2102 $this->setImportStatus((int) $values[count($values) - 1], 'DUPLICATE', 'Skipping duplicate record');
2103 return CRM_Import_Parser
::DUPLICATE
;
2106 $this->setImportStatus((int) $values[count($values) - 1], 'Imported', '');
2107 //return warning if street address is not parsed, CRM-5886
2108 return $this->processMessage($values, CRM_Import_Parser
::VALID
);
2112 * Validate a formatted contact parameter list.
2114 * @param array $params
2115 * Structured parameter list (as in crm_format_params).
2117 * @throw CRM_Core_Error
2119 public function deprecated_validate_formatted_contact(&$params): void
{
2120 // Validate custom data fields
2121 if (array_key_exists('custom', $params) && is_array($params['custom'])) {
2122 foreach ($params['custom'] as $key => $custom) {
2123 if (is_array($custom)) {
2124 foreach ($custom as $fieldId => $value) {
2125 $valid = CRM_Core_BAO_CustomValue
::typecheck(CRM_Utils_Array
::value('type', $value),
2126 CRM_Utils_Array
::value('value', $value)
2128 if (!$valid && $value['is_required']) {
2129 throw new CRM_Core_Exception('Invalid value for custom field \'' .
2130 $custom['name'] . '\''
2133 if (CRM_Utils_Array
::value('type', $custom) == 'Date') {
2134 $params['custom'][$key][$fieldId]['value'] = str_replace('-', '', $params['custom'][$key][$fieldId]['value']);
2143 * @param array $params
2144 * @param bool $dupeCheck
2145 * @param null|int $dedupeRuleGroupID
2147 * @throws \CRM_Core_Exception
2149 public function deprecated_contact_check_params(
2152 $dedupeRuleGroupID = NULL) {
2154 if (isset($params['id']) && is_numeric($params['id'])) {
2155 // @todo - ensure this is tested & remove - expectation is api call further
2156 // down validates it.
2157 if ($csType = CRM_Utils_Array
::value('contact_sub_type', $params)) {
2158 if (!(CRM_Contact_BAO_ContactType
::isExtendsContactType($csType, $params['contact_type']))) {
2159 throw new CRM_Core_Exception("Invalid or Mismatched Contact Subtype: " . implode(', ', (array) $csType));
2165 // @todo switch to using api version
2166 // $dupes = civicrm_api3('Contact', 'duplicatecheck', (array('match' => $params, 'dedupe_rule_id' => $dedupeRuleGroupID)));
2167 // $ids = $dupes['count'] ? implode(',', array_keys($dupes['values'])) : NULL;
2168 $ids = CRM_Contact_BAO_Contact
::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised', [], CRM_Utils_Array
::value('check_permissions', $params), $dedupeRuleGroupID);
2170 $error = CRM_Core_Error
::createError("Found matching contacts: " . implode(',', $ids),
2171 CRM_Core_Error
::DUPLICATE_CONTACT
,
2174 return civicrm_api3_create_error($error->pop());
2178 // check for organisations with same name
2179 if (!empty($params['current_employer'])) {
2180 $organizationParams = ['organization_name' => $params['current_employer']];
2181 $dupeIds = CRM_Contact_BAO_Contact
::getDuplicateContacts($organizationParams, 'Organization', 'Supervised', [], FALSE);
2183 // check for mismatch employer name and id
2184 if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds)
2186 throw new CRM_Core_Exception('Employer name and Employer id Mismatch');
2189 // show error if multiple organisation with same name exist
2190 if (empty($params['employer_id']) && (count($dupeIds) > 1)
2192 return civicrm_api3_create_error('Found more than one Organisation with same Name.');
2200 * @param array $mapper
2202 * @param int $statusID
2205 * @throws \API_Exception|\CRM_Core_Exception
2207 public function run(
2209 $mode = self
::MODE_PREVIEW
,
2213 // TODO: Make the timeout actually work
2214 $this->_onDuplicate
= $onDuplicate = $this->getSubmittedValue('onDuplicate');
2215 $this->_dedupeRuleGroupID
= $this->getSubmittedValue('dedupe_rule_id');
2216 // Since $this->_contactType is still being called directly do a get call
2217 // here to make sure it is instantiated.
2218 $this->getContactType();
2219 $this->getContactSubType();
2223 $this->_rowCount
= 0;
2224 $this->_totalCount
= 0;
2226 $this->_primaryKeyName
= '_id';
2227 $this->_statusFieldName
= '_status';
2230 $this->progressImport($statusID);
2231 $startTimestamp = $currTimestamp = $prevTimestamp = time();
2233 $dataSource = $this->getDataSourceObject();
2234 $totalRowCount = $dataSource->getRowCount(['new']);
2235 if ($mode == self
::MODE_IMPORT
) {
2236 $dataSource->setStatuses(['new']);
2238 if ($this->_maxLinesToProcess
> 0) {
2239 // Note this would only be the case in MapForm mode, where it is set to 100
2240 // rows. In fact mapField really only needs 2 rows - the reason for
2241 // 100 seems to be that the other import classes are processing a
2242 // csv file, and there was a concern that some rows might have more
2243 // columns than others - hence checking 100 rows perhaps seemed like
2244 // a good precaution presumably when determining the activeFieldsCount
2245 // which is the number of columns a row might have.
2246 // However, the mapField class may no longer use activeFieldsCount for contact
2247 // to be continued....
2248 $dataSource->setLimit($this->_maxLinesToProcess
);
2251 while ($row = $dataSource->getRow()) {
2252 $values = array_values($row);
2255 $this->_totalCount++
;
2257 if ($mode == self
::MODE_MAPFIELD
) {
2258 $returnCode = CRM_Import_Parser
::VALID
;
2260 elseif ($mode == self
::MODE_PREVIEW
) {
2261 $returnCode = $this->preview($values);
2263 elseif ($mode == self
::MODE_SUMMARY
) {
2264 $returnCode = $this->summary($values);
2266 elseif ($mode == self
::MODE_IMPORT
) {
2268 $returnCode = $this->import($onDuplicate, $values);
2270 catch (CiviCRM_API3_Exception
$e) {
2271 // When we catch errors here we are not adding to the errors array - mostly
2272 // because that will become obsolete once https://github.com/civicrm/civicrm-core/pull/23292
2273 // is merged and this will replace it as the main way to handle errors (ie. update the table
2275 $this->setImportStatus((int) $values[count($values) - 1], 'ERROR', $e->getMessage());
2277 if ($statusID && (($this->_rowCount %
50) == 0)) {
2278 $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
2282 $returnCode = self
::ERROR
;
2285 if ($returnCode & self
::NO_MATCH
) {
2286 $this->setImportStatus((int) $values[count($values) - 1], 'invalid_no_match', array_shift($values));
2289 if ($returnCode & self
::UNPARSED_ADDRESS_WARNING
) {
2290 $this->setImportStatus((int) $values[count($values) - 1], 'warning_unparsed_address', array_shift($values));
2296 * Given a list of the importable field keys that the user has selected.
2297 * set the active fields array to this list
2299 * @param array $fieldKeys
2300 * Mapped array of values.
2302 public function setActiveFields($fieldKeys) {
2303 foreach ($fieldKeys as $key) {
2304 if (empty($this->_fields
[$key])) {
2305 $this->_activeFields
[] = new CRM_Contact_Import_Field('', ts('- do not import -'));
2308 $this->_activeFields
[] = clone($this->_fields
[$key]);
2314 * Format the field values for input to the api.
2316 * @param array $values
2317 * The row from the datasource.
2320 * Parameters mapped as described in getMappedRow
2322 * @throws \API_Exception
2323 * @todo - clean this up a bit & merge back into `getMappedRow`
2326 private function getParams(array $values): array {
2329 foreach ($this->getFieldMappings() as $i => $mappedField) {
2330 // The key is in the format 5_a_b where 5 is the relationship_type_id and a_b is the direction.
2331 $relatedContactKey = $mappedField['relationship_type_id'] ?
($mappedField['relationship_type_id'] . '_' . $mappedField['relationship_direction']) : NULL;
2332 $fieldName = $mappedField['name'];
2333 $importedValue = $values[$i];
2334 if ($fieldName === 'do_not_import' ||
$importedValue === NULL) {
2338 $locationFields = ['location_type_id', 'phone_type_id', 'provider_id', 'website_type_id'];
2339 $value = array_filter(array_intersect_key($mappedField, array_fill_keys($locationFields, 1)));
2340 if (!empty($value)) {
2341 $value[$fieldName] = $importedValue;
2343 if (!$relatedContactKey) {
2344 if (!empty($value)) {
2345 if (!isset($params[$fieldName])) {
2346 $params[$fieldName] = [];
2348 $params[$fieldName][] = $value;
2351 if (!isset($params[$fieldName])) {
2352 $params[$fieldName] = $importedValue;
2357 if (!isset($params[$relatedContactKey])) {
2358 $params[$relatedContactKey] = ['contact_type' => $this->getRelatedContactType($mappedField['relationship_type_id'], $mappedField['relationship_direction'])];
2361 if (!empty($value)) {
2362 $params[$relatedContactKey][$fieldName][] = $importedValue ?
[] : $value;
2365 $params[$relatedContactKey][$fieldName] = $importedValue;
2374 * @param string $name
2377 * @param string $headerPattern
2378 * @param string $dataPattern
2379 * @param bool $hasLocationType
2381 public function addField(
2382 $name, $title, $type = CRM_Utils_Type
::T_INT
,
2383 $headerPattern = '//', $dataPattern = '//',
2384 $hasLocationType = FALSE
2386 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
2388 $this->_fields
['doNotImport'] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
2393 * Store parser values.
2395 * @param CRM_Core_Session $store
2399 public function set($store, $mode = self
::MODE_SUMMARY
) {
2403 * Export data to a CSV file.
2405 * @param string $fileName
2406 * @param array $header
2407 * @param array $data
2409 public static function exportCSV($fileName, $header, $data) {
2411 if (file_exists($fileName) && !is_writable($fileName)) {
2412 CRM_Core_Error
::movedSiteError($fileName);
2414 //hack to remove '_status', '_statusMsg' and '_id' from error file
2416 $dbRecordStatus = ['IMPORTED', 'ERROR', 'DUPLICATE', 'INVALID', 'NEW'];
2417 foreach ($data as $rowCount => $rowValues) {
2419 foreach ($rowValues as $key => $val) {
2420 if (in_array($val, $dbRecordStatus) && $count == (count($rowValues) - 3)) {
2423 $errorValues[$rowCount][$key] = $val;
2427 $data = $errorValues;
2430 $fd = fopen($fileName, 'w');
2432 foreach ($header as $key => $value) {
2433 $header[$key] = "\"$value\"";
2435 $config = CRM_Core_Config
::singleton();
2436 $output[] = implode($config->fieldSeparator
, $header);
2438 foreach ($data as $datum) {
2439 foreach ($datum as $key => $value) {
2440 $datum[$key] = "\"$value\"";
2442 $output[] = implode($config->fieldSeparator
, $datum);
2444 fwrite($fd, implode("\n", $output));
2449 * Update the status of the import row to reflect the processing outcome.
2452 * @param string $status
2453 * @param string $message
2454 * @param int|null $entityID
2455 * Optional created entity ID
2456 * @param array $relatedEntityIDs
2457 * Optional array e.g ['related_contact' => 4]
2459 * @throws \API_Exception
2460 * @throws \CRM_Core_Exception
2462 public function setImportStatus(int $id, string $status, string $message, ?
int $entityID = NULL, array $relatedEntityIDs = []): void
{
2463 $this->getDataSourceObject()->updateStatus($id, $status, $message, $entityID, $relatedEntityIDs);
2467 * Format contact parameters.
2469 * @todo this function needs re-writing & re-merging into the main function.
2473 * @param array $values
2474 * @param array $params
2478 protected function formatContactParameters(&$values, &$params) {
2479 // Crawl through the possible classes:
2492 // first add core contact values since for other Civi modules they are not added
2493 $contactFields = CRM_Contact_DAO_Contact
::fields();
2494 _civicrm_api3_store_values($contactFields, $values, $params);
2496 if (isset($values['contact_type'])) {
2497 // we're an individual/household/org property
2499 $fields[$values['contact_type']] = CRM_Contact_DAO_Contact
::fields();
2501 _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params);
2505 // Cache the various object fields
2506 // @todo - remove this after confirming this is just a compilation of other-wise-cached fields.
2507 static $fields = [];
2509 if (isset($values['individual_prefix'])) {
2510 if (!empty($params['prefix_id'])) {
2511 $prefixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
2512 $params['prefix'] = $prefixes[$params['prefix_id']];
2515 $params['prefix'] = $values['individual_prefix'];
2520 if (isset($values['individual_suffix'])) {
2521 if (!empty($params['suffix_id'])) {
2522 $suffixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
2523 $params['suffix'] = $suffixes[$params['suffix_id']];
2526 $params['suffix'] = $values['individual_suffix'];
2532 if (isset($values['email_greeting'])) {
2533 if (!empty($params['email_greeting_id'])) {
2534 $emailGreetingFilter = [
2535 'contact_type' => $params['contact_type'] ??
NULL,
2536 'greeting_type' => 'email_greeting',
2538 $emailGreetings = CRM_Core_PseudoConstant
::greeting($emailGreetingFilter);
2539 $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']];
2542 $params['email_greeting'] = $values['email_greeting'];
2548 if (isset($values['postal_greeting'])) {
2549 if (!empty($params['postal_greeting_id'])) {
2550 $postalGreetingFilter = [
2551 'contact_type' => $params['contact_type'] ??
NULL,
2552 'greeting_type' => 'postal_greeting',
2554 $postalGreetings = CRM_Core_PseudoConstant
::greeting($postalGreetingFilter);
2555 $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']];
2558 $params['postal_greeting'] = $values['postal_greeting'];
2563 if (isset($values['addressee'])) {
2564 $params['addressee'] = $values['addressee'];
2568 if (isset($values['gender'])) {
2569 if (!empty($params['gender_id'])) {
2570 $genders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
2571 $params['gender'] = $genders[$params['gender_id']];
2574 $params['gender'] = $values['gender'];
2579 if (!empty($values['preferred_communication_method'])) {
2581 $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER
);
2583 $preffComm = explode(',', $values['preferred_communication_method']);
2584 foreach ($preffComm as $v) {
2585 $v = strtolower(trim($v));
2586 if (array_key_exists($v, $pcm)) {
2587 $comm[$pcm[$v]] = 1;
2591 $params['preferred_communication_method'] = $comm;
2595 // format the website params.
2596 if (!empty($values['url'])) {
2597 static $websiteFields;
2598 if (!is_array($websiteFields)) {
2599 $websiteFields = CRM_Core_DAO_Website
::fields();
2601 if (!array_key_exists('website', $params) ||
2602 !is_array($params['website'])
2604 $params['website'] = [];
2607 $websiteCount = count($params['website']);
2608 _civicrm_api3_store_values($websiteFields, $values,
2609 $params['website'][++
$websiteCount]
2615 if (isset($values['note'])) {
2617 if (!isset($params['note'])) {
2618 $params['note'] = [];
2620 $noteBlock = count($params['note']) +
1;
2622 $params['note'][$noteBlock] = [];
2623 if (!isset($fields['Note'])) {
2624 $fields['Note'] = CRM_Core_DAO_Note
::fields();
2627 // get the current logged in civicrm user
2628 $session = CRM_Core_Session
::singleton();
2629 $userID = $session->get('userID');
2632 $values['contact_id'] = $userID;
2635 _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]);
2640 // Check for custom field values
2641 $customFields = CRM_Core_BAO_CustomField
::getFields(CRM_Utils_Array
::value('contact_type', $values),
2642 FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE
2645 foreach ($values as $key => $value) {
2646 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
2647 // check if it's a valid custom field id
2649 if (!array_key_exists($customFieldID, $customFields)) {
2650 return civicrm_api3_create_error('Invalid custom field ID');
2653 $params[$key] = $value;
2661 * Format location block ready for importing.
2663 * There is some test coverage for this in CRM_Contact_Import_Parser_ContactTest
2664 * e.g. testImportPrimaryAddress.
2666 * @param array $values
2667 * @param array $params
2671 protected function formatLocationBlock(&$values, &$params) {
2676 'openid' => 'OpenID',
2677 'phone_ext' => 'Phone',
2679 foreach ($blockTypes as $blockFieldName => $block) {
2680 if (!array_key_exists($blockFieldName, $values)) {
2683 $blockIndex = $values['location_type_id'] . (!empty($values['phone_type_id']) ?
'_' . $values['phone_type_id'] : '');
2685 // block present in value array.
2686 if (!array_key_exists($blockFieldName, $params) ||
!is_array($params[$blockFieldName])) {
2687 $params[$blockFieldName] = [];
2690 $fields[$block] = $this->getMetadataForEntity($block);
2692 // copy value to dao field name.
2693 if ($blockFieldName == 'im') {
2694 $values['name'] = $values[$blockFieldName];
2697 _civicrm_api3_store_values($fields[$block], $values,
2698 $params[$blockFieldName][$blockIndex]
2701 $this->fillPrimary($params[$blockFieldName][$blockIndex], $values, $block, CRM_Utils_Array
::value('id', $params));
2703 if (empty($params['id']) && (count($params[$blockFieldName]) == 1)) {
2704 $params[$blockFieldName][$blockIndex]['is_primary'] = TRUE;
2707 // we only process single block at a time.
2711 // handle address fields.
2712 if (!array_key_exists('address', $params) ||
!is_array($params['address'])) {
2713 $params['address'] = [];
2716 // Note: we doing multiple value formatting here for address custom fields, plus putting into right format.
2717 // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving
2718 // the address in CRM_Core_BAO_Address::create method
2719 if (!empty($values['location_type_id'])) {
2720 static $customFields = [];
2721 if (empty($customFields)) {
2722 $customFields = CRM_Core_BAO_CustomField
::getFields('Address');
2724 // make a copy of values, as we going to make changes
2725 $newValues = $values;
2726 foreach ($values as $key => $val) {
2727 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
2728 if ($customFieldID && array_key_exists($customFieldID, $customFields)) {
2730 $htmlType = $customFields[$customFieldID]['html_type'] ??
NULL;
2731 if (CRM_Core_BAO_CustomField
::isSerialized($customFields[$customFieldID]) && $val) {
2732 $mulValues = explode(',', $val);
2733 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
2734 $newValues[$key] = [];
2735 foreach ($mulValues as $v1) {
2736 foreach ($customOption as $v2) {
2737 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
2738 (strtolower($v2['value']) == strtolower(trim($v1)))
2740 if ($htmlType == 'CheckBox') {
2741 $newValues[$key][$v2['value']] = 1;
2744 $newValues[$key][] = $v2['value'];
2752 // consider new values
2753 $values = $newValues;
2756 $fields['Address'] = $this->getMetadataForEntity('Address');
2757 // @todo this is kinda replicated below....
2758 _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$values['location_type_id']]);
2764 'supplemental_address_1',
2765 'supplemental_address_2',
2766 'supplemental_address_3',
2767 'StateProvince.name',
2769 foreach (array_keys($customFields) as $customFieldID) {
2770 $addressFields[] = 'custom_' . $customFieldID;
2773 foreach ($addressFields as $field) {
2774 if (array_key_exists($field, $values)) {
2775 if (!array_key_exists('address', $params)) {
2776 $params['address'] = [];
2778 $params['address'][$values['location_type_id']][$field] = $values[$field];
2782 $this->fillPrimary($params['address'][$values['location_type_id']], $values, 'address', CRM_Utils_Array
::value('id', $params));
2787 * Get the field metadata for the relevant entity.
2789 * @param string $entity
2793 protected function getMetadataForEntity($entity) {
2794 if (!isset($this->fieldMetadata
[$entity])) {
2795 $className = "CRM_Core_DAO_$entity";
2796 $this->fieldMetadata
[$entity] = $className::fields();
2798 return $this->fieldMetadata
[$entity];
2802 * Fill in the primary location.
2804 * If the contact has a primary address we update it. Otherwise
2805 * we add an address of the default location type.
2807 * @param array $params
2808 * Address block parameters
2809 * @param array $values
2811 * @param string $entity
2812 * - address, email, phone
2813 * @param int|null $contactID
2815 * @throws \CiviCRM_API3_Exception
2817 protected function fillPrimary(&$params, $values, $entity, $contactID) {
2818 if ($values['location_type_id'] === 'Primary') {
2820 $primary = civicrm_api3($entity, 'get', [
2821 'return' => 'location_type_id',
2822 'contact_id' => $contactID,
2827 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
2828 $params['location_type_id'] = (int) (isset($primary) && $primary['count']) ?
$primary['values'][0]['location_type_id'] : $defaultLocationType->id
;
2829 $params['is_primary'] = 1;
2834 * Get the civicrm_mapping_field appropriate layout for the mapper input.
2836 * The input looks something like ['street_address', 1]
2837 * and would be mapped to ['name' => 'street_address', 'location_type_id' =>
2840 * @param array $fieldMapping
2841 * @param int $mappingID
2842 * @param int $columnNumber
2845 * @throws \API_Exception
2847 public function getMappingFieldFromMapperInput(array $fieldMapping, int $mappingID, int $columnNumber): array {
2848 $isRelationshipField = preg_match('/\d*_a_b|b_a$/', $fieldMapping[0]);
2849 $fieldName = $isRelationshipField ?
$fieldMapping[1] : $fieldMapping[0];
2850 $locationTypeID = NULL;
2851 $possibleLocationField = $isRelationshipField ?
2 : 1;
2852 if ($fieldName !== 'url' && is_numeric($fieldMapping[$possibleLocationField] ??
NULL)) {
2853 $locationTypeID = $fieldMapping[$possibleLocationField];
2856 'name' => $fieldName,
2857 'mapping_id' => $mappingID,
2858 'relationship_type_id' => $isRelationshipField ?
substr($fieldMapping[0], 0, -4) : NULL,
2859 'relationship_direction' => $isRelationshipField ?
substr($fieldMapping[0], -3) : NULL,
2860 'column_number' => $columnNumber,
2861 'contact_type' => $this->getContactType(),
2862 'website_type_id' => $fieldName !== 'url' ?
NULL : ($isRelationshipField ?
$fieldMapping[2] : $fieldMapping[1]),
2863 'phone_type_id' => $fieldName !== 'phone' ?
NULL : ($isRelationshipField ?
$fieldMapping[3] : $fieldMapping[2]),
2864 'im_provider_id' => $fieldName !== 'im' ?
NULL : ($isRelationshipField ?
$fieldMapping[3] : $fieldMapping[2]),
2865 'location_type_id' => $locationTypeID,
2870 * @param array $mappedField
2871 * Field detail as would be saved in field_mapping table
2872 * or as returned from getMappingFieldFromMapperInput
2875 * @throws \API_Exception
2877 public function getMappedFieldLabel(array $mappedField): string {
2878 $this->setFieldMetadata();
2880 if ($mappedField['relationship_type_id']) {
2881 $title[] = $this->getRelationshipLabel($mappedField['relationship_type_id'], $mappedField['relationship_direction']);
2883 $title[] = $this->getImportableFieldsMetadata()[$mappedField['name']]['title'];
2884 if ($mappedField['location_type_id']) {
2885 $title[] = CRM_Core_PseudoConstant
::getLabel('CRM_Core_BAO_Address', 'location_type_id', $mappedField['location_type_id']);
2887 if ($mappedField['website_type_id']) {
2888 $title[] = CRM_Core_PseudoConstant
::getLabel('CRM_Core_BAO_Website', 'website_type_id', $mappedField['website_type_id']);
2890 if ($mappedField['phone_type_id']) {
2891 $title[] = CRM_Core_PseudoConstant
::getLabel('CRM_Core_BAO_Phone', 'phone_type_id', $mappedField['phone_type_id']);
2893 if ($mappedField['im_provider_id']) {
2894 $title[] = CRM_Core_PseudoConstant
::getLabel('CRM_Core_BAO_IM', 'provider_id', $mappedField['provider_id']);
2896 return implode(' - ', $title);
2900 * Get the relevant label for the relationship.
2903 * @param string $direction
2906 * @throws \API_Exception
2908 protected function getRelationshipLabel(int $id, string $direction): string {
2909 if (empty($this->relationshipLabels
[$id . $direction])) {
2910 $this->relationshipLabels
[$id . $direction] =
2911 $fieldName = 'label_' . $direction;
2912 $this->relationshipLabels
[$id . $direction] = (string) RelationshipType
::get(FALSE)
2913 ->addWhere('id', '=', $id)
2914 ->addSelect($fieldName)->execute()->first()[$fieldName];
2916 return $this->relationshipLabels
[$id . $direction];
2920 * Transform the input parameters into the form handled by the input routine.
2922 * @param array $values
2923 * Input parameters as they come in from the datasource
2924 * eg. ['Bob', 'Smith', 'bob@example.org', '123-456']
2927 * Parameters mapped to CiviCRM fields based on the mapping
2928 * and specified contact type. eg.
2930 * 'contact_type' => 'Individual',
2931 * 'first_name' => 'Bob',
2932 * 'last_name' => 'Smith',
2933 * 'phone' => ['phone' => '123', 'location_type_id' => 1, 'phone_type_id' => 1],
2934 * '5_a_b' => ['contact_type' => 'Organization', 'url' => ['url' => 'https://example.org', 'website_type_id' => 1]]
2935 * 'im' => ['im' => 'my-handle', 'location_type_id' => 1, 'provider_id' => 1],
2937 * @throws \API_Exception
2939 public function getMappedRow(array $values): array {
2940 $params = $this->getParams($values);
2941 $params['contact_type'] = $this->getContactType();
2946 * Validate the import values.
2948 * The values array represents a row in the datasource.
2950 * @param array $values
2952 * @throws \API_Exception
2953 * @throws \CRM_Core_Exception
2955 public function validateValues(array $values): void
{
2956 $params = $this->getMappedRow($values);
2957 $this->validateRequiredContactFields($params['contact_type'], $params, $this->isUpdateExistingContacts());
2958 foreach ($params as $key => $value) {
2959 // If the key is a relationship key - eg. 5_a_b or 10_b_a
2960 // then the value is an array that describes an existing contact.
2961 // We need to check the fields are present to identify or create this
2963 if (preg_match('/^\d+_[a|b]_[a|b]$/', $key)) {
2964 $this->validateRequiredContactFields($value['contact_type'], $value, TRUE, '(' . $this->getRelatedContactLabel(substr($key, 0, -4), substr($key, -3)) . ')');
2968 //check for duplicate external Identifier
2969 $externalID = $params['external_identifier'] ??
NULL;
2971 /* If it's a dupe,external Identifier */
2973 if ($externalDupe = CRM_Utils_Array
::value($externalID, $this->_allExternalIdentifiers
)) {
2974 $errorMessage = ts('External ID conflicts with record %1', [1 => $externalDupe]);
2975 throw new CRM_Core_Exception($errorMessage);
2977 //otherwise, count it and move on
2978 $this->_allExternalIdentifiers
[$externalID] = $this->_lineCount
;
2981 //date-format part ends
2983 $errorMessage = NULL;
2986 //add custom fields for contact sub type
2988 if (!empty($this->_contactSubType
)) {
2989 $csType = $this->_contactSubType
;
2992 //checking error in custom data
2993 $this->isErrorInCustomData($params, $errorMessage, $csType, $this->_relationships
);
2995 //checking error in core data
2996 $this->isErrorInCoreData($params, $errorMessage);
2997 if ($errorMessage) {
2998 $tempMsg = "Invalid value for field(s) : $errorMessage";
2999 throw new CRM_Core_Exception($tempMsg);
3004 * Get the field mappings for the import.
3006 * This is the same format as saved in civicrm_mapping_field except
3007 * that location_type_id = 'Primary' rather than empty where relevant.
3008 * Also 'im_provider_id' is mapped to the 'real' field name 'provider_id'
3011 * @throws \API_Exception
3013 protected function getFieldMappings(): array {
3015 foreach ($this->getSubmittedValue('mapper') as $i => $mapperRow) {
3016 $mappedField = $this->getMappingFieldFromMapperInput($mapperRow, 0, $i);
3017 if (!$mappedField['location_type_id'] && !empty($this->importableFieldsMetadata
[$mappedField['name']]['hasLocationType'])) {
3018 $mappedField['location_type_id'] = 'Primary';
3020 // Just for clarity since 0 is a pseudo-value
3021 unset($mappedField['mapping_id']);
3022 // Annoyingly the civicrm_mapping_field name for this differs from civicrm_im.
3023 // Test cover in `CRM_Contact_Import_Parser_ContactTest::testMapFields`
3024 $mappedField['provider_id'] = $mappedField['im_provider_id'];
3025 unset($mappedField['im_provider_id']);
3026 $mappedFields[] = $mappedField;
3028 return $mappedFields;
3032 * Get the related contact type.
3034 * @param int|null $relationshipTypeID
3035 * @param int|string $relationshipDirection
3037 * @return null|string
3039 * @throws \API_Exception
3041 protected function getRelatedContactType($relationshipTypeID, $relationshipDirection): ?
string {
3042 if (!$relationshipTypeID) {
3045 $relationshipField = 'contact_type_' . substr($relationshipDirection, -1);
3046 return $this->getRelationshipType($relationshipTypeID, $relationshipDirection)[$relationshipField];
3050 * Get the related contact type.
3052 * @param int|null $relationshipTypeID
3053 * @param int|string $relationshipDirection
3055 * @return null|string
3057 * @throws \API_Exception
3059 protected function getRelatedContactLabel($relationshipTypeID, $relationshipDirection): ?
string {
3060 $relationshipField = 'label_' . $relationshipDirection;
3061 return $this->getRelationshipType($relationshipTypeID, $relationshipDirection)[$relationshipField];
3065 * Get the relationship type.
3067 * @param int $relationshipTypeID
3070 * @throws \API_Exception
3072 protected function getRelationshipType(int $relationshipTypeID): array {
3073 $cacheKey = 'relationship_type' . $relationshipTypeID;
3074 if (!isset(Civi
::$statics[__CLASS__
][$cacheKey])) {
3075 Civi
::$statics[__CLASS__
][$cacheKey] = RelationshipType
::get(FALSE)
3076 ->addWhere('id', '=', $relationshipTypeID)
3077 ->addSelect('*')->execute()->first();
3079 return Civi
::$statics[__CLASS__
][$cacheKey];