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
;
14 require_once 'CRM/Utils/DeprecatedUtils.php';
15 require_once 'api/v3/utils.php';
20 * @copyright CiviCRM LLC https://civicrm.org/licensing
24 * class to parse contact csv files
26 class CRM_Contact_Import_Parser_Contact
extends CRM_Contact_Import_Parser
{
28 use CRM_Contact_Import_MetadataTrait
;
30 protected $_mapperKeys = [];
31 protected $_mapperLocType = [];
32 protected $_mapperPhoneType;
33 protected $_mapperImProvider;
34 protected $_mapperWebsiteType;
35 protected $_mapperRelated;
36 protected $_mapperRelatedContactType;
37 protected $_mapperRelatedContactDetails;
38 protected $_mapperRelatedContactEmailType;
39 protected $_mapperRelatedContactImProvider;
40 protected $_mapperRelatedContactWebsiteType;
41 protected $_relationships;
43 protected $_emailIndex;
44 protected $_firstNameIndex;
45 protected $_lastNameIndex;
47 protected $_householdNameIndex;
48 protected $_organizationNameIndex;
50 protected $_allEmails;
52 protected $_phoneIndex;
55 * Is update only permitted on an id match.
57 * Note this historically was true for when id or external identifier was
58 * present. However, CRM-17275 determined that a dedupe-match could over-ride
59 * external identifier.
63 protected $_updateWithId;
66 protected $_externalIdentifierIndex;
67 protected $_allExternalIdentifiers;
68 protected $_parseStreetAddress;
71 * Array of successfully imported contact id's
75 protected $_newContacts;
82 protected $_lineCount;
85 * Array of successfully imported related contact id's
89 protected $_newRelatedContacts;
92 * Array of all the contacts whose street addresses are not parsed.
93 * of this import process
96 protected $_unparsedStreetAddressContacts;
101 * @param array $mapperKeys
102 * @param array $mapperLocType
103 * @param array $mapperPhoneType
104 * @param array $mapperImProvider
105 * @param array $mapperRelated
106 * @param array $mapperRelatedContactType
107 * @param array $mapperRelatedContactDetails
108 * @param array $mapperRelatedContactLocType
109 * @param array $mapperRelatedContactPhoneType
110 * @param array $mapperRelatedContactImProvider
111 * @param array $mapperWebsiteType
112 * @param array $mapperRelatedContactWebsiteType
114 public function __construct(
115 $mapperKeys, $mapperLocType = [], $mapperPhoneType = [], $mapperImProvider = [], $mapperRelated = [], $mapperRelatedContactType = [], $mapperRelatedContactDetails = [], $mapperRelatedContactLocType = [], $mapperRelatedContactPhoneType = [], $mapperRelatedContactImProvider = [],
116 $mapperWebsiteType = [], $mapperRelatedContactWebsiteType = []
118 parent
::__construct();
119 $this->_mapperKeys
= $mapperKeys;
120 $this->_mapperLocType
= &$mapperLocType;
121 $this->_mapperPhoneType
= &$mapperPhoneType;
122 $this->_mapperWebsiteType
= $mapperWebsiteType;
123 // get IM service provider type id for contact
124 $this->_mapperImProvider
= &$mapperImProvider;
125 $this->_mapperRelated
= &$mapperRelated;
126 $this->_mapperRelatedContactType
= &$mapperRelatedContactType;
127 $this->_mapperRelatedContactDetails
= &$mapperRelatedContactDetails;
128 $this->_mapperRelatedContactLocType
= &$mapperRelatedContactLocType;
129 $this->_mapperRelatedContactPhoneType
= &$mapperRelatedContactPhoneType;
130 $this->_mapperRelatedContactWebsiteType
= $mapperRelatedContactWebsiteType;
131 // get IM service provider type id for related contact
132 $this->_mapperRelatedContactImProvider
= &$mapperRelatedContactImProvider;
136 * The initializer code, called before processing.
138 public function init() {
139 $this->setFieldMetadata();
140 foreach ($this->getImportableFieldsMetadata() as $name => $field) {
141 $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));
144 $this->_newContacts
= [];
146 $this->setActiveFields($this->_mapperKeys
);
147 $this->setActiveFieldLocationTypes($this->_mapperLocType
);
148 $this->setActiveFieldPhoneTypes($this->_mapperPhoneType
);
149 $this->setActiveFieldWebsiteTypes($this->_mapperWebsiteType
);
150 //set active fields of IM provider of contact
151 $this->setActiveFieldImProviders($this->_mapperImProvider
);
154 $this->setActiveFieldRelated($this->_mapperRelated
);
155 $this->setActiveFieldRelatedContactType($this->_mapperRelatedContactType
);
156 $this->setActiveFieldRelatedContactDetails($this->_mapperRelatedContactDetails
);
157 $this->setActiveFieldRelatedContactLocType($this->_mapperRelatedContactLocType
);
158 $this->setActiveFieldRelatedContactPhoneType($this->_mapperRelatedContactPhoneType
);
159 $this->setActiveFieldRelatedContactWebsiteType($this->_mapperRelatedContactWebsiteType
);
160 //set active fields of IM provider of related contact
161 $this->setActiveFieldRelatedContactImProvider($this->_mapperRelatedContactImProvider
);
163 $this->_phoneIndex
= -1;
164 $this->_emailIndex
= -1;
165 $this->_firstNameIndex
= -1;
166 $this->_lastNameIndex
= -1;
167 $this->_householdNameIndex
= -1;
168 $this->_organizationNameIndex
= -1;
169 $this->_externalIdentifierIndex
= -1;
172 foreach ($this->_mapperKeys
as $key) {
173 if (substr($key, 0, 5) == 'email' && substr($key, 0, 14) != 'email_greeting') {
174 $this->_emailIndex
= $index;
175 $this->_allEmails
= [];
177 if (substr($key, 0, 5) == 'phone') {
178 $this->_phoneIndex
= $index;
180 if ($key == 'first_name') {
181 $this->_firstNameIndex
= $index;
183 if ($key == 'last_name') {
184 $this->_lastNameIndex
= $index;
186 if ($key == 'household_name') {
187 $this->_householdNameIndex
= $index;
189 if ($key == 'organization_name') {
190 $this->_organizationNameIndex
= $index;
193 if ($key == 'external_identifier') {
194 $this->_externalIdentifierIndex
= $index;
195 $this->_allExternalIdentifiers
= [];
200 $this->_updateWithId
= FALSE;
201 if (in_array('id', $this->_mapperKeys
) ||
($this->_externalIdentifierIndex
>= 0 && in_array($this->_onDuplicate
, [
202 CRM_Import_Parser
::DUPLICATE_UPDATE
,
203 CRM_Import_Parser
::DUPLICATE_FILL
,
205 $this->_updateWithId
= TRUE;
208 $this->_parseStreetAddress
= CRM_Utils_Array
::value('street_address_parsing', CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'address_options'), FALSE);
212 * Handle the values in mapField mode.
214 * @param array $values
215 * The array of values belonging to this line.
219 public function mapField(&$values) {
220 return CRM_Import_Parser
::VALID
;
224 * Handle the values in preview mode.
226 * @param array $values
227 * The array of values belonging to this line.
230 * the result of this processing
232 public function preview(&$values) {
233 return $this->summary($values);
237 * Handle the values in summary mode.
239 * @param array $values
240 * The array of values belonging to this line.
243 * the result of this processing
245 public function summary(&$values): int {
246 $erroneousField = NULL;
247 $this->setActiveFieldValues($values, $erroneousField);
248 $rowNumber = (int) ($values[count($values) - 1]);
249 $errorMessage = NULL;
250 $errorRequired = FALSE;
251 switch ($this->_contactType
) {
254 if ($this->_firstNameIndex
< 0 ||
empty($values[$this->_firstNameIndex
])) {
255 $errorRequired = TRUE;
256 $missingNames[] = ts('First Name');
258 if ($this->_lastNameIndex
< 0 ||
empty($values[$this->_lastNameIndex
])) {
259 $errorRequired = TRUE;
260 $missingNames[] = ts('Last Name');
262 if ($errorRequired) {
263 $and = ' ' . ts('and') . ' ';
264 $errorMessage = ts('Missing required fields:') . ' ' . implode($and, $missingNames);
269 if ($this->_householdNameIndex
< 0 ||
empty($values[$this->_householdNameIndex
])) {
270 $errorRequired = TRUE;
271 $errorMessage = ts('Missing required fields:') . ' ' . ts('Household Name');
276 if ($this->_organizationNameIndex
< 0 ||
empty($values[$this->_organizationNameIndex
])) {
277 $errorRequired = TRUE;
278 $errorMessage = ts('Missing required fields:') . ' ' . ts('Organization Name');
283 if ($this->_emailIndex
>= 0) {
284 /* If we don't have the required fields, bail */
286 if ($this->_contactType
=== 'Individual' && !$this->_updateWithId
) {
287 if ($errorRequired && empty($values[$this->_emailIndex
])) {
289 $errorMessage .= ' ' . ts('OR') . ' ' . ts('Email Address');
292 $errorMessage = ts('Missing required field:') . ' ' . ts('Email Address');
294 array_unshift($values, $errorMessage);
295 $this->setImportStatus($rowNumber, 'ERROR', $errorMessage);
297 return CRM_Import_Parser
::ERROR
;
301 $email = $values[$this->_emailIndex
] ??
NULL;
303 /* If the email address isn't valid, bail */
305 if (!CRM_Utils_Rule
::email($email)) {
306 $errorMessage = ts('Invalid Email address');
307 array_unshift($values, $errorMessage);
308 $this->setImportStatus($rowNumber, 'ERROR', $errorMessage);
310 return CRM_Import_Parser
::ERROR
;
313 /* otherwise, count it and move on */
314 $this->_allEmails
[$email] = $this->_lineCount
;
317 elseif ($errorRequired && !$this->_updateWithId
) {
319 $errorMessage .= ' ' . ts('OR') . ' ' . ts('Email Address');
322 $errorMessage = ts('Missing required field:') . ' ' . ts('Email Address');
324 array_unshift($values, $errorMessage);
325 $this->setImportStatus($rowNumber, 'ERROR', $errorMessage);
327 return CRM_Import_Parser
::ERROR
;
330 //check for duplicate external Identifier
331 $externalID = $values[$this->_externalIdentifierIndex
] ??
NULL;
333 /* If it's a dupe,external Identifier */
335 if ($externalDupe = CRM_Utils_Array
::value($externalID, $this->_allExternalIdentifiers
)) {
336 $errorMessage = ts('External ID conflicts with record %1', [1 => $externalDupe]);
337 array_unshift($values, $errorMessage);
338 $this->setImportStatus($rowNumber, 'ERROR', $errorMessage);
339 return CRM_Import_Parser
::ERROR
;
341 //otherwise, count it and move on
342 $this->_allExternalIdentifiers
[$externalID] = $this->_lineCount
;
345 //Checking error in custom data
346 $params = &$this->getActiveFieldParams();
347 $params['contact_type'] = $this->_contactType
;
348 //date-format part ends
350 $errorMessage = NULL;
353 //add custom fields for contact sub type
355 if (!empty($this->_contactSubType
)) {
356 $csType = $this->_contactSubType
;
359 //checking error in custom data
360 $this->isErrorInCustomData($params, $errorMessage, $csType, $this->_relationships
);
362 //checking error in core data
363 $this->isErrorInCoreData($params, $errorMessage);
365 $tempMsg = "Invalid value for field(s) : $errorMessage";
366 $this->setImportStatus($rowNumber, 'ERROR', $tempMsg);
367 array_unshift($values, $tempMsg);
368 $errorMessage = NULL;
369 return CRM_Import_Parser
::ERROR
;
371 $this->setImportStatus($rowNumber, 'NEW', '');
373 return CRM_Import_Parser
::VALID
;
377 * Get Array of all the fields that could potentially be part
382 public function getAllFields() {
383 return $this->_fields
;
387 * Handle the values in import mode.
389 * @param int $onDuplicate
390 * The code for what action to take on duplicates.
391 * @param array $values
392 * The array of values belonging to this line.
394 * @param bool $doGeocodeAddress
397 * the result of this processing
399 * @throws \CiviCRM_API3_Exception
400 * @throws \CRM_Core_Exception
401 * @throws \API_Exception
403 public function import($onDuplicate, &$values, $doGeocodeAddress = FALSE) {
404 $this->_unparsedStreetAddressContacts
= [];
405 if (!$doGeocodeAddress) {
406 // CRM-5854, reset the geocode method to null to prevent geocoding
407 CRM_Utils_GeocodeProvider
::disableForSession();
410 // first make sure this is a valid line
411 //$this->_updateWithId = false;
412 $response = $this->summary($values);
413 $statusFieldName = $this->_statusFieldName
;
415 if ($response != CRM_Import_Parser
::VALID
) {
416 $importRecordParams = [
417 $statusFieldName => 'INVALID',
418 "${statusFieldName}Msg" => "Invalid (Error Code: $response)",
420 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
424 $params = &$this->getActiveFieldParams();
426 'contact_type' => $this->_contactType
,
429 $contactFields = CRM_Contact_DAO_Contact
::import();
431 //check if external identifier exists in database
432 if (!empty($params['external_identifier']) && (!empty($params['id']) ||
in_array($onDuplicate, [
433 CRM_Import_Parser
::DUPLICATE_SKIP
,
434 CRM_Import_Parser
::DUPLICATE_NOCHECK
,
437 $extIDResult = civicrm_api3('Contact', 'get', [
438 'external_identifier' => $params['external_identifier'],
440 'return' => ['id', 'contact_is_deleted'],
442 if (isset($extIDResult['id'])) {
443 // record with matching external identifier does exist.
444 $internalCid = $extIDResult['id'];
445 if ($internalCid != CRM_Utils_Array
::value('id', $params)) {
446 if ($extIDResult['values'][$internalCid]['contact_is_deleted'] == 1) {
447 // And it is deleted. What to do? If we skip it, they user
448 // will be under the impression that the record exists in
449 // the database, yet they won't be able to find it. If we
450 // don't skip it, the database will try to insert a new record
451 // with an external_identifier that is non-unique. So...
452 // we will update this contact to remove the external_identifier
453 // and let a new record be created.
454 $update_params = ['id' => $internalCid, 'external_identifier' => ''];
455 civicrm_api3('Contact', 'create', $update_params);
458 $errorMessage = ts('External ID already exists in Database.');
459 array_unshift($values, $errorMessage);
460 $importRecordParams = [
461 $statusFieldName => 'ERROR',
462 "${statusFieldName}Msg" => $errorMessage,
464 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
465 return CRM_Import_Parser
::DUPLICATE
;
471 if (!empty($this->_contactSubType
)) {
472 $params['contact_sub_type'] = $this->_contactSubType
;
475 if ($subType = CRM_Utils_Array
::value('contact_sub_type', $params)) {
476 if (CRM_Contact_BAO_ContactType
::isExtendsContactType($subType, $this->_contactType
, FALSE, 'label')) {
477 $subTypes = CRM_Contact_BAO_ContactType
::subTypePairs($this->_contactType
, FALSE, NULL);
478 $params['contact_sub_type'] = array_search($subType, $subTypes);
480 elseif (!CRM_Contact_BAO_ContactType
::isExtendsContactType($subType, $this->_contactType
)) {
481 $message = "Mismatched or Invalid Contact Subtype.";
482 array_unshift($values, $message);
483 return CRM_Import_Parser
::NO_MATCH
;
487 // Get contact id to format common data in update/fill mode,
488 // prioritising a dedupe rule check over an external_identifier check, but falling back on ext id.
489 if ($this->_updateWithId
&& empty($params['id'])) {
491 $possibleMatches = $this->getPossibleContactMatches($params);
493 catch (CRM_Core_Exception
$e) {
494 $errorMessage = $e->getMessage();
495 array_unshift($values, $errorMessage);
497 $importRecordParams = [
498 $statusFieldName => 'ERROR',
499 "${statusFieldName}Msg" => $errorMessage,
501 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
502 return CRM_Import_Parser
::ERROR
;
504 foreach ($possibleMatches as $possibleID) {
505 $params['id'] = $formatted['id'] = $possibleID;
508 //format common data, CRM-4062
509 $this->formatCommonData($params, $formatted, $contactFields);
511 $relationship = FALSE;
512 $createNewContact = TRUE;
513 // Support Match and Update Via Contact ID
514 if ($this->_updateWithId
&& isset($params['id'])) {
515 $createNewContact = FALSE;
516 // @todo - it feels like all the rows from here to the end of the IF
517 // could be removed in favour of a simple check for whether the contact_type & id match
518 $matchedIDs = $this->getIdsOfMatchingContacts($formatted);
519 if (!empty($matchedIDs)) {
520 if (count($matchedIDs) >= 1) {
522 foreach ($matchedIDs as $contactId) {
523 if ($params['id'] == $contactId) {
524 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_type');
525 if ($formatted['contact_type'] == $contactType) {
526 //validation of subtype for update mode
528 $contactSubType = NULL;
529 if (!empty($params['contact_sub_type'])) {
530 $contactSubType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_sub_type');
533 if (!empty($contactSubType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($params['id'], $contactSubType) && $contactSubType != CRM_Utils_Array
::value('contact_sub_type', $formatted))) {
535 $message = "Mismatched contact SubTypes :";
536 array_unshift($values, $message);
538 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
542 $this->_retCode
= CRM_Import_Parser
::VALID
;
546 $message = "Mismatched contact Types :";
547 array_unshift($values, $message);
549 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
554 $message = "Mismatched contact IDs OR Mismatched contact Types :";
555 array_unshift($values, $message);
556 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
562 if (!empty($params['id'])) {
563 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_type');
565 if ($formatted['contact_type'] == $contactType) {
566 //validation of subtype for update mode
568 $contactSubType = NULL;
569 if (!empty($params['contact_sub_type'])) {
570 $contactSubType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_sub_type');
573 if (!empty($contactSubType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($params['id'], $contactSubType) && $contactSubType != CRM_Utils_Array
::value('contact_sub_type', $formatted))) {
575 $message = "Mismatched contact SubTypes :";
576 array_unshift($values, $message);
577 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
580 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $params['id'], FALSE, $this->_dedupeRuleGroupID
);
581 $this->_retCode
= CRM_Import_Parser
::VALID
;
585 $message = "Mismatched contact Types :";
586 array_unshift($values, $message);
587 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
591 // we should avoid multiple errors for single record
592 // since we have already retCode and we trying to force again.
593 if ($this->_retCode
!= CRM_Import_Parser
::NO_MATCH
) {
594 $message = "No contact found for this contact ID:" . $params['id'];
595 array_unshift($values, $message);
596 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
602 //now we want to create new contact on update/fill also.
603 $createNewContact = TRUE;
607 if (isset($newContact) && is_a($newContact, 'CRM_Contact_BAO_Contact')) {
608 $relationship = TRUE;
613 //now we create new contact in update/fill mode also.
615 if ($createNewContact ||
($this->_retCode
!= CRM_Import_Parser
::NO_MATCH
&& $this->_updateWithId
)) {
616 // @todo - there are multiple places where formatting is done that need consolidation.
617 // This handles where the label has been passed in and it has gotten this far.
618 // probably a bunch of hard-coded stuff could be removed to rely on this.
619 $fields = Contact
::getFields(FALSE)
620 ->addWhere('options', '=', TRUE)
621 ->setLoadOptions(TRUE)
622 ->execute()->indexBy('name');
623 foreach ($fields as $fieldName => $fieldSpec) {
624 if (isset($formatted[$fieldName]) && is_array($formatted[$fieldName])) {
625 // If we have an array at this stage, it's probably a multi-select
626 // field that has already been parsed properly into the value that
627 // should be inserted into the database.
630 if (!empty($formatted[$fieldName])
631 && empty($fieldSpec['options'][$formatted[$fieldName]])) {
632 $formatted[$fieldName] = array_search($formatted[$fieldName], $fieldSpec['options'], TRUE) ??
$formatted[$fieldName];
635 //CRM-4430, don't carry if not submitted.
636 if ($this->_updateWithId
&& !empty($params['id'])) {
637 $contactID = $params['id'];
639 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactID, TRUE, $this->_dedupeRuleGroupID
);
642 if (isset($newContact) && is_object($newContact) && ($newContact instanceof CRM_Contact_BAO_Contact
)) {
643 $relationship = TRUE;
644 $newContact = clone($newContact);
645 $contactID = $newContact->id
;
646 $this->_newContacts
[] = $contactID;
648 //get return code if we create new contact in update mode, CRM-4148
649 if ($this->_updateWithId
) {
650 $this->_retCode
= CRM_Import_Parser
::VALID
;
653 elseif (isset($newContact) && CRM_Core_Error
::isAPIError($newContact, CRM_Core_Error
::DUPLICATE_CONTACT
)) {
654 // if duplicate, no need of further processing
655 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_SKIP
) {
656 $errorMessage = "Skipping duplicate record";
657 array_unshift($values, $errorMessage);
658 $importRecordParams = [
659 $statusFieldName => 'DUPLICATE',
660 "${statusFieldName}Msg" => $errorMessage,
662 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
663 return CRM_Import_Parser
::DUPLICATE
;
666 $relationship = TRUE;
667 // CRM-10433/CRM-20739 - IDs could be string or array; handle accordingly
668 if (!is_array($dupeContactIDs = $newContact['error_message']['params'][0])) {
669 $dupeContactIDs = explode(',', $dupeContactIDs);
671 $dupeCount = count($dupeContactIDs);
672 $contactID = array_pop($dupeContactIDs);
673 // check to see if we had more than one duplicate contact id.
674 // if we have more than one, the record will be rejected below
675 if ($dupeCount == 1) {
676 // there was only one dupe, we will continue normally...
677 if (!in_array($contactID, $this->_newContacts
)) {
678 $this->_newContacts
[] = $contactID;
685 $currentImportID = end($values);
688 'contactID' => $contactID,
689 'importID' => $currentImportID,
690 'importTempTable' => $this->_tableName
,
691 'fieldHeaders' => $this->_mapperKeys
,
692 'fields' => $this->_activeFields
,
695 CRM_Utils_Hook
::import('Contact', 'process', $this, $hookParams);
699 $primaryContactId = NULL;
700 if (CRM_Core_Error
::isAPIError($newContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
701 if ($dupeCount == 1 && CRM_Utils_Rule
::integer($contactID)) {
702 $primaryContactId = $contactID;
706 $primaryContactId = $newContact->id
;
709 if ((CRM_Core_Error
::isAPIError($newContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
) ||
is_a($newContact, 'CRM_Contact_BAO_Contact')) && $primaryContactId) {
711 //relationship contact insert
712 foreach ($params as $key => $field) {
713 [$id, $first, $second] = CRM_Utils_System
::explode('_', $key, 3);
714 if (!($first == 'a' && $second == 'b') && !($first == 'b' && $second == 'a')) {
718 $relationType = new CRM_Contact_DAO_RelationshipType();
719 $relationType->id
= $id;
720 $relationType->find(TRUE);
721 $direction = "contact_sub_type_$second";
724 'contact_type' => $params[$key]['contact_type'],
727 //set subtype for related contact CRM-5125
728 if (isset($relationType->$direction)) {
729 //validation of related contact subtype for update mode
730 if ($relCsType = CRM_Utils_Array
::value('contact_sub_type', $params[$key]) && $relCsType != $relationType->$direction) {
731 $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact.");
732 array_unshift($values, $errorMessage);
733 return CRM_Import_Parser
::NO_MATCH
;
736 $formatting['contact_sub_type'] = $relationType->$direction;
740 $contactFields = NULL;
741 $contactFields = CRM_Contact_DAO_Contact
::import();
743 //Relation on the basis of External Identifier.
744 if (empty($params[$key]['id']) && !empty($params[$key]['external_identifier'])) {
745 $params[$key]['id'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['external_identifier'], 'id', 'external_identifier');
747 // check for valid related contact id in update/fill mode, CRM-4424
748 if (in_array($onDuplicate, [
749 CRM_Import_Parser
::DUPLICATE_UPDATE
,
750 CRM_Import_Parser
::DUPLICATE_FILL
,
751 ]) && !empty($params[$key]['id'])) {
752 $relatedContactType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['id'], 'contact_type');
753 if (!$relatedContactType) {
754 $errorMessage = ts("No contact found for this related contact ID: %1", [1 => $params[$key]['id']]);
755 array_unshift($values, $errorMessage);
756 return CRM_Import_Parser
::NO_MATCH
;
759 //validation of related contact subtype for update mode
761 $relatedCsType = NULL;
762 if (!empty($formatting['contact_sub_type'])) {
763 $relatedCsType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['id'], 'contact_sub_type');
766 if (!empty($relatedCsType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($params[$key]['id'], $relatedCsType) &&
767 $relatedCsType != CRM_Utils_Array
::value('contact_sub_type', $formatting))
769 $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact.") . ' ' . ts("ID: %1", [1 => $params[$key]['id']]);
770 array_unshift($values, $errorMessage);
771 return CRM_Import_Parser
::NO_MATCH
;
773 // get related contact id to format data in update/fill mode,
774 //if external identifier is present, CRM-4423
775 $formatting['id'] = $params[$key]['id'];
778 //format common data, CRM-4062
779 $this->formatCommonData($field, $formatting, $contactFields);
781 //do we have enough fields to create related contact.
782 $allowToCreate = $this->checkRelatedContactFields($key, $formatting);
784 if (!$allowToCreate) {
785 $errorMessage = ts('Related contact required fields are missing.');
786 array_unshift($values, $errorMessage);
787 return CRM_Import_Parser
::NO_MATCH
;
791 if (!empty($params[$key]['id'])) {
793 'contact_id' => $params[$key]['id'],
796 $relatedNewContact = CRM_Contact_BAO_Contact
::retrieve($contact, $defaults);
799 $relatedNewContact = $this->createContact($formatting, $contactFields, $onDuplicate, NULL, FALSE);
802 if (is_object($relatedNewContact) ||
($relatedNewContact instanceof CRM_Contact_BAO_Contact
)) {
803 $relatedNewContact = clone($relatedNewContact);
807 // To update/fill contact, get the matching contact Ids if duplicate contact found
808 // otherwise get contact Id from object of related contact
809 if (is_array($relatedNewContact) && civicrm_error($relatedNewContact)) {
810 if (CRM_Core_Error
::isAPIError($relatedNewContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
811 $matchedIDs = $relatedNewContact['error_message']['params'][0];
812 if (!is_array($matchedIDs)) {
813 $matchedIDs = explode(',', $matchedIDs);
817 $errorMessage = $relatedNewContact['error_message'];
818 array_unshift($values, $errorMessage);
819 $importRecordParams = [
820 $statusFieldName => 'ERROR',
821 "${statusFieldName}Msg" => $errorMessage,
823 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
824 return CRM_Import_Parser
::ERROR
;
828 $matchedIDs[] = $relatedNewContact->id
;
830 // update/fill related contact after getting matching Contact Ids, CRM-4424
831 if (in_array($onDuplicate, [
832 CRM_Import_Parser
::DUPLICATE_UPDATE
,
833 CRM_Import_Parser
::DUPLICATE_FILL
,
835 //validation of related contact subtype for update mode
837 $relatedCsType = NULL;
838 if (!empty($formatting['contact_sub_type'])) {
839 $relatedCsType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $matchedIDs[0], 'contact_sub_type');
842 if (!empty($relatedCsType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($matchedIDs[0], $relatedCsType) && $relatedCsType != CRM_Utils_Array
::value('contact_sub_type', $formatting))) {
843 $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact.");
844 array_unshift($values, $errorMessage);
845 return CRM_Import_Parser
::NO_MATCH
;
848 $updatedContact = $this->createContact($formatting, $contactFields, $onDuplicate, $matchedIDs[0]);
851 static $relativeContact = [];
852 if (CRM_Core_Error
::isAPIError($relatedNewContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
853 if (count($matchedIDs) >= 1) {
854 $relContactId = $matchedIDs[0];
855 //add relative contact to count during update & fill mode.
856 //logic to make count distinct by contact id.
857 if ($this->_newRelatedContacts ||
!empty($relativeContact)) {
858 $reContact = array_keys($relativeContact, $relContactId);
860 if (empty($reContact)) {
861 $this->_newRelatedContacts
[] = $relativeContact[] = $relContactId;
865 $this->_newRelatedContacts
[] = $relativeContact[] = $relContactId;
870 $relContactId = $relatedNewContact->id
;
871 $this->_newRelatedContacts
[] = $relativeContact[] = $relContactId;
874 if (CRM_Core_Error
::isAPIError($relatedNewContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
) ||
($relatedNewContact instanceof CRM_Contact_BAO_Contact
)) {
875 //fix for CRM-1993.Checks for duplicate related contacts
876 if (count($matchedIDs) >= 1) {
877 //if more than one duplicate contact
878 //found, create relationship with first contact
879 // now create the relationship record
881 'relationship_type_id' => $key,
886 'skipRecentView' => TRUE,
889 // we only handle related contact success, we ignore failures for now
890 // at some point wold be nice to have related counts as separate
892 'contact' => $primaryContactId,
895 [$valid, $duplicate] = self
::legacyCreateMultiple($relationParams, $relationIds);
897 if ($valid ||
$duplicate) {
898 $relationIds['contactTarget'] = $relContactId;
899 $action = ($duplicate) ? CRM_Core_Action
::UPDATE
: CRM_Core_Action
::ADD
;
900 CRM_Contact_BAO_Relationship
::relatedMemberships($primaryContactId, $relationParams, $relationIds, $action);
903 //handle current employer, CRM-3532
905 $allRelationships = CRM_Core_PseudoConstant
::relationshipType('name');
906 $relationshipTypeId = str_replace([
913 $relationshipType = str_replace($relationshipTypeId . '_', '', $key);
914 $orgId = $individualId = NULL;
915 if ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employee of') {
916 $orgId = $relContactId;
917 $individualId = $primaryContactId;
919 elseif ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employer of') {
920 $orgId = $primaryContactId;
921 $individualId = $relContactId;
923 if ($orgId && $individualId) {
924 $currentEmpParams[$individualId] = $orgId;
925 CRM_Contact_BAO_Contact_Utils
::setCurrentEmployer($currentEmpParams);
933 if ($this->_updateWithId
) {
934 //return warning if street address is unparsed, CRM-5886
935 return $this->processMessage($values, $statusFieldName, $this->_retCode
);
938 if (is_array($newContact) && civicrm_error($newContact)) {
941 if (($code = CRM_Utils_Array
::value('code', $newContact['error_message'])) && ($code == CRM_Core_Error
::DUPLICATE_CONTACT
)) {
942 return $this->handleDuplicateError($newContact, $statusFieldName, $values, $onDuplicate, $formatted, $contactFields);
944 // Not a dupe, so we had an error
945 $errorMessage = $newContact['error_message'];
946 array_unshift($values, $errorMessage);
947 $importRecordParams = [
948 $statusFieldName => 'ERROR',
949 "${statusFieldName}Msg" => $errorMessage,
951 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
952 return CRM_Import_Parser
::ERROR
;
956 return $this->processMessage($values, $statusFieldName, CRM_Import_Parser
::VALID
);
960 * Only called from import now... plus one place outside of core & tests.
962 * @todo - deprecate more aggressively - will involve copying to the import
963 * class, adding a deprecation notice here & removing from tests.
965 * Takes an associative array and creates a relationship object.
967 * @deprecated For single creates use the api instead (it's tested).
968 * For multiple a new variant of this function needs to be written and migrated to as this is a bit
971 * @param array $params
972 * (reference ) an assoc array of name/value pairs.
974 * The array that holds all the db ids.
975 * per http://wiki.civicrm.org/confluence/display/CRM/Database+layer
976 * "we are moving away from the $ids param "
979 * @throws \CRM_Core_Exception
981 private static function legacyCreateMultiple($params, $ids = []) {
982 // clarify that the only key ever pass in the ids array is 'contact'
983 // There is legacy handling for other keys but a universe search on
984 // calls to this function (not supported to be called from outside core)
985 // only returns 2 calls - one in CRM_Contact_Import_Parser_Contact
986 // and the other in jma grant applications (CRM_Grant_Form_Grant_Confirm)
987 // both only pass in contact as a key here.
988 $contactID = $ids['contact'];
990 // There is only ever one value passed in from the 2 places above that call
991 // this - by clarifying here like this we can cleanup within this
992 // function without having to do more universe searches.
993 $relatedContactID = key($params['contact_check']);
995 // check if the relationship is valid between contacts.
996 // step 1: check if the relationship is valid if not valid skip and keep the count
997 // step 2: check the if two contacts already have a relationship if yes skip and keep the count
998 // step 3: if valid relationship then add the relation and keep the count
1001 [$contactFields['relationship_type_id'], $firstLetter, $secondLetter] = explode('_', $params['relationship_type_id']);
1002 $contactFields['contact_id_' . $firstLetter] = $contactID;
1003 $contactFields['contact_id_' . $secondLetter] = $relatedContactID;
1004 if (!CRM_Contact_BAO_Relationship
::checkRelationshipType($contactFields['contact_id_a'], $contactFields['contact_id_b'],
1005 $contactFields['relationship_type_id'])) {
1010 CRM_Contact_BAO_Relationship
::checkDuplicateRelationship(
1020 $singleInstanceParams = array_merge($params, $contactFields);
1021 CRM_Contact_BAO_Relationship
::add($singleInstanceParams);
1026 * Format common params data to proper format to store.
1028 * @param array $params
1029 * Contain record values.
1030 * @param array $formatted
1031 * Array of formatted data.
1032 * @param array $contactFields
1033 * Contact DAO fields.
1035 private function formatCommonData($params, &$formatted, $contactFields) {
1037 CRM_Utils_Array
::value('contact_type', $formatted),
1041 //add custom fields for contact sub type
1042 if (!empty($this->_contactSubType
)) {
1043 $csType = $this->_contactSubType
;
1046 if ($relCsType = CRM_Utils_Array
::value('contact_sub_type', $formatted)) {
1047 $csType = $relCsType;
1050 $customFields = CRM_Core_BAO_CustomField
::getFields($formatted['contact_type'], FALSE, FALSE, $csType);
1052 $addressCustomFields = CRM_Core_BAO_CustomField
::getFields('Address');
1053 $customFields = $customFields +
$addressCustomFields;
1055 //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
1057 'email_greeting_custom' => 'email_greeting',
1058 'postal_greeting_custom' => 'postal_greeting',
1059 'addressee_custom' => 'addressee',
1061 foreach ($elements as $k => $v) {
1062 if (array_key_exists($k, $params) && !(array_key_exists($v, $params))) {
1063 $label = key(CRM_Core_OptionGroup
::values($v, TRUE, NULL, NULL, 'AND v.name = "Customized"'));
1064 $params[$v] = $label;
1069 $session = CRM_Core_Session
::singleton();
1070 $dateType = $session->get("dateTypes");
1071 foreach ($params as $key => $val) {
1072 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
1073 if ($customFieldID &&
1074 !array_key_exists($customFieldID, $addressCustomFields)
1076 //we should not update Date to null, CRM-4062
1077 if ($val && ($customFields[$customFieldID]['data_type'] == 'Date')) {
1079 CRM_Contact_Import_Parser_Contact
::formatCustomDate($params, $formatted, $dateType, $key);
1081 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
1082 if (empty($val) && !is_numeric($val) && $this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_FILL
) {
1083 //retain earlier value when Import mode is `Fill`
1084 unset($params[$key]);
1087 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
1092 if ($key == 'birth_date' && $val) {
1093 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key);
1095 elseif ($key == 'deceased_date' && $val) {
1096 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key);
1097 $params['is_deceased'] = 1;
1099 elseif ($key == 'is_deceased' && $val) {
1100 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
1104 //now format custom data.
1105 foreach ($params as $key => $field) {
1106 if (is_array($field)) {
1107 $isAddressCustomField = FALSE;
1108 foreach ($field as $value) {
1110 if (is_array($value)) {
1111 foreach ($value as $name => $testForEmpty) {
1112 if ($addressCustomFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
1113 $isAddressCustomField = TRUE;
1116 // check if $value does not contain IM provider or phoneType
1117 if (($name !== 'phone_type_id' ||
$name !== 'provider_id') && ($testForEmpty === '' ||
$testForEmpty == NULL)) {
1128 if (!empty($value['location_type_id'])) {
1129 $this->formatLocationBlock($value, $formatted);
1132 // @todo - this is still reachable - e.g. import with related contact info like firstname,lastname,spouse-first-name,spouse-last-name,spouse-home-phone
1133 CRM_Core_Error
::deprecatedFunctionWarning('this is not expected to be reachable now');
1134 $this->formatContactParameters($value, $formatted);
1138 if (!$isAddressCustomField) {
1147 if (($key !== 'preferred_communication_method') && (array_key_exists($key, $contactFields))) {
1148 // due to merging of individual table and
1149 // contact table, we need to avoid
1150 // preferred_communication_method forcefully
1151 $formatValues['contact_type'] = $formatted['contact_type'];
1154 if ($key == 'id' && isset($field)) {
1155 $formatted[$key] = $field;
1157 $this->formatContactParameters($formatValues, $formatted);
1159 //Handling Custom Data
1160 // note: Address custom fields will be handled separately inside formatContactParameters
1161 if (($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) &&
1162 array_key_exists($customFieldID, $customFields) &&
1163 !array_key_exists($customFieldID, $addressCustomFields)
1166 $extends = $customFields[$customFieldID]['extends'] ??
NULL;
1167 $htmlType = $customFields[$customFieldID]['html_type'] ??
NULL;
1168 $dataType = $customFields[$customFieldID]['data_type'] ??
NULL;
1169 $serialized = CRM_Core_BAO_CustomField
::isSerialized($customFields[$customFieldID]);
1171 if (!$serialized && in_array($htmlType, ['Select', 'Radio', 'Autocomplete-Select']) && in_array($dataType, ['String', 'Int'])) {
1172 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1173 foreach ($customOption as $customValue) {
1174 $val = $customValue['value'] ??
NULL;
1175 $label = strtolower($customValue['label'] ??
'');
1176 $value = strtolower(trim($formatted[$key]));
1177 if (($value == $label) ||
($value == strtolower($val))) {
1178 $params[$key] = $formatted[$key] = $val;
1182 elseif ($serialized && !empty($formatted[$key]) && !empty($params[$key])) {
1183 $mulValues = explode(',', $formatted[$key]);
1184 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1185 $formatted[$key] = [];
1187 foreach ($mulValues as $v1) {
1188 foreach ($customOption as $v2) {
1189 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
1190 (strtolower($v2['value']) == strtolower(trim($v1)))
1192 if ($htmlType == 'CheckBox') {
1193 $params[$key][$v2['value']] = $formatted[$key][$v2['value']] = 1;
1196 $params[$key][] = $formatted[$key][] = $v2['value'];
1205 if (!empty($key) && ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) && array_key_exists($customFieldID, $customFields) &&
1206 !array_key_exists($customFieldID, $addressCustomFields)
1208 // @todo calling api functions directly is not supported
1209 _civicrm_api3_custom_format_params($params, $formatted, $extends);
1212 // to check if not update mode and unset the fields with empty value.
1213 if (!$this->_updateWithId
&& array_key_exists('custom', $formatted)) {
1214 foreach ($formatted['custom'] as $customKey => $customvalue) {
1215 if (empty($formatted['custom'][$customKey][-1]['is_required'])) {
1216 $formatted['custom'][$customKey][-1]['is_required'] = $customFields[$customKey]['is_required'];
1218 $emptyValue = $customvalue[-1]['value'] ??
NULL;
1219 if (!isset($emptyValue)) {
1220 unset($formatted['custom'][$customKey]);
1225 // parse street address, CRM-5450
1226 if ($this->_parseStreetAddress
) {
1227 if (array_key_exists('address', $formatted) && is_array($formatted['address'])) {
1228 foreach ($formatted['address'] as $instance => & $address) {
1229 $streetAddress = $address['street_address'] ??
NULL;
1230 if (empty($streetAddress)) {
1233 // parse address field.
1234 $parsedFields = CRM_Core_BAO_Address
::parseStreetAddress($streetAddress);
1236 //street address consider to be parsed properly,
1237 //If we get street_name and street_number.
1238 if (empty($parsedFields['street_name']) ||
empty($parsedFields['street_number'])) {
1239 $parsedFields = array_fill_keys(array_keys($parsedFields), '');
1242 // merge parse address w/ main address block.
1243 $address = array_merge($address, $parsedFields);
1250 * Get the array of successfully imported contact id's
1254 public function getImportedContacts() {
1255 return $this->_newContacts
;
1259 * Get the array of successfully imported related contact id's
1263 public function &getRelatedImportedContacts() {
1264 return $this->_newRelatedContacts
;
1268 * The initializer code, called before the processing.
1270 public function fini() {
1274 * Check if an error in custom data.
1276 * @param array $params
1277 * @param string $errorMessage
1278 * A string containing all the error-fields.
1280 * @param null $csType
1281 * @param null $relationships
1283 public static function isErrorInCustomData($params, &$errorMessage, $csType = NULL, $relationships = NULL) {
1284 $dateType = CRM_Core_Session
::singleton()->get("dateTypes");
1286 if (!empty($params['contact_sub_type'])) {
1287 $csType = $params['contact_sub_type'] ??
NULL;
1290 if (empty($params['contact_type'])) {
1291 $params['contact_type'] = 'Individual';
1294 // get array of subtypes - CRM-18708
1295 if (in_array($csType, CRM_Contact_BAO_ContactType
::basicTypes(TRUE), TRUE)) {
1296 $csType = self
::getSubtypes($params['contact_type']);
1299 if (is_array($csType)) {
1300 // fetch custom fields for every subtype and add it to $customFields array
1303 foreach ($csType as $cType) {
1304 $customFields +
= CRM_Core_BAO_CustomField
::getFields($params['contact_type'], FALSE, FALSE, $cType);
1308 $customFields = CRM_Core_BAO_CustomField
::getFields($params['contact_type'], FALSE, FALSE, $csType);
1311 $addressCustomFields = CRM_Core_BAO_CustomField
::getFields('Address');
1312 $customFields = $customFields +
$addressCustomFields;
1313 foreach ($params as $key => $value) {
1314 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1315 /* check if it's a valid custom field id */
1317 if (!array_key_exists($customFieldID, $customFields)) {
1318 self
::addToErrorMsg(ts('field ID'), $errorMessage);
1320 // validate null values for required custom fields of type boolean
1321 if (!empty($customFields[$customFieldID]['is_required']) && (empty($params['custom_' . $customFieldID]) && !is_numeric($params['custom_' . $customFieldID])) && $customFields[$customFieldID]['data_type'] == 'Boolean') {
1322 self
::addToErrorMsg($customFields[$customFieldID]['label'] . '::' . $customFields[$customFieldID]['groupTitle'], $errorMessage);
1325 //For address custom fields, we do get actual custom field value as an inner array of
1326 //values so need to modify
1327 if (array_key_exists($customFieldID, $addressCustomFields)) {
1328 $value = $value[0][$key];
1330 /* validate the data against the CF type */
1333 $dataType = $customFields[$customFieldID]['data_type'];
1334 $htmlType = $customFields[$customFieldID]['html_type'];
1335 $isSerialized = CRM_Core_BAO_CustomField
::isSerialized($customFields[$customFieldID]);
1336 if ($dataType == 'Date') {
1337 if (array_key_exists($customFieldID, $addressCustomFields) && CRM_Utils_Date
::convertToDefaultDate($params[$key][0], $dateType, $key)) {
1338 $value = $params[$key][0][$key];
1340 elseif (CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key)) {
1341 $value = $params[$key];
1344 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1347 elseif ($dataType == 'Boolean') {
1348 if (CRM_Utils_String
::strtoboolstr($value) === FALSE) {
1349 self
::addToErrorMsg($customFields[$customFieldID]['label'] . '::' . $customFields[$customFieldID]['groupTitle'], $errorMessage);
1352 // need not check for label filed import
1353 $selectHtmlTypes = [
1358 if ((!$isSerialized && !in_array($htmlType, $selectHtmlTypes)) ||
$dataType == 'Boolean' ||
$dataType == 'ContactReference') {
1359 $valid = CRM_Core_BAO_CustomValue
::typecheck($dataType, $value);
1361 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1365 // check for values for custom fields for checkboxes and multiselect
1366 if ($isSerialized && $dataType != 'ContactReference') {
1367 $value = trim($value);
1368 $value = str_replace('|', ',', $value);
1369 $mulValues = explode(',', $value);
1370 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1371 foreach ($mulValues as $v1) {
1372 if (strlen($v1) == 0) {
1377 foreach ($customOption as $v2) {
1378 if ((strtolower(trim($v2['label'])) == strtolower(trim($v1))) ||
(strtolower(trim($v2['value'])) == strtolower(trim($v1)))) {
1384 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1388 elseif ($htmlType == 'Select' ||
($htmlType == 'Radio' && $dataType != 'Boolean')) {
1389 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1391 foreach ($customOption as $v2) {
1392 if ((strtolower(trim($v2['label'])) == strtolower(trim($value))) ||
(strtolower(trim($v2['value'])) == strtolower(trim($value)))) {
1397 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1400 elseif ($isSerialized && $dataType === 'StateProvince') {
1401 $mulValues = explode(',', $value);
1402 foreach ($mulValues as $stateValue) {
1404 if (self
::in_value(trim($stateValue), CRM_Core_PseudoConstant
::stateProvinceAbbreviation()) || self
::in_value(trim($stateValue), CRM_Core_PseudoConstant
::stateProvince())) {
1408 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1413 elseif ($isSerialized && $dataType == 'Country') {
1414 $mulValues = explode(',', $value);
1415 foreach ($mulValues as $countryValue) {
1416 if ($countryValue) {
1417 CRM_Core_PseudoConstant
::populate($countryNames, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active');
1418 CRM_Core_PseudoConstant
::populate($countryIsoCodes, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
1419 $limitCodes = CRM_Core_BAO_Country
::countryLimit();
1427 if (in_array(trim($countryValue), $values)) {
1434 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1441 elseif (is_array($params[$key]) && isset($params[$key]["contact_type"])) {
1443 //supporting custom data of related contact subtypes
1445 if ($relationships) {
1446 if (array_key_exists($key, $relationships)) {
1449 elseif (CRM_Utils_Array
::key($key, $relationships)) {
1450 $relation = CRM_Utils_Array
::key($key, $relationships);
1453 if (!empty($relation)) {
1454 [$id, $first, $second] = CRM_Utils_System
::explode('_', $relation, 3);
1455 $direction = "contact_sub_type_$second";
1456 $relationshipType = new CRM_Contact_BAO_RelationshipType();
1457 $relationshipType->id
= $id;
1458 if ($relationshipType->find(TRUE)) {
1459 if (isset($relationshipType->$direction)) {
1460 $params[$key]['contact_sub_type'] = $relationshipType->$direction;
1465 self
::isErrorInCustomData($params[$key], $errorMessage, $csType, $relationships);
1471 * Check if value present in all genders or.
1472 * as a substring of any gender value, if yes than return corresponding gender.
1473 * eg value might be m/M, ma/MA, mal/MAL, male return 'Male'
1474 * but if value is 'maleabc' than return false
1476 * @param string $gender
1477 * Check this value across gender values.
1479 * retunr gender value / false
1483 public function checkGender($gender) {
1484 $gender = trim($gender, '.');
1489 $allGenders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
1490 foreach ($allGenders as $key => $value) {
1491 if (strlen($gender) > strlen($value)) {
1494 if ($gender == $value) {
1497 if (substr_compare($value, $gender, 0, strlen($gender), TRUE) === 0) {
1506 * Check if an error in Core( non-custom fields ) field
1508 * @param array $params
1509 * @param string $errorMessage
1510 * A string containing all the error-fields.
1512 public function isErrorInCoreData($params, &$errorMessage) {
1513 foreach ($params as $key => $value) {
1515 $session = CRM_Core_Session
::singleton();
1516 $dateType = $session->get("dateTypes");
1520 if (CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key)) {
1521 if (!CRM_Utils_Rule
::date($params[$key])) {
1522 self
::addToErrorMsg(ts('Birth Date'), $errorMessage);
1526 self
::addToErrorMsg(ts('Birth-Date'), $errorMessage);
1530 case 'deceased_date':
1531 if (CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key)) {
1532 if (!CRM_Utils_Rule
::date($params[$key])) {
1533 self
::addToErrorMsg(ts('Deceased Date'), $errorMessage);
1537 self
::addToErrorMsg(ts('Deceased Date'), $errorMessage);
1542 if (CRM_Utils_String
::strtoboolstr($value) === FALSE) {
1543 self
::addToErrorMsg(ts('Deceased'), $errorMessage);
1548 if (!self
::checkGender($value)) {
1549 self
::addToErrorMsg(ts('Gender'), $errorMessage);
1553 case 'preferred_communication_method':
1555 $preffComm = explode(',', $value);
1556 foreach ($preffComm as $v) {
1557 if (!self
::in_value(trim($v), CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method'))) {
1558 self
::addToErrorMsg(ts('Preferred Communication Method'), $errorMessage);
1563 case 'preferred_mail_format':
1564 if (!array_key_exists(strtolower($value), array_change_key_case(CRM_Core_SelectValues
::pmf(), CASE_LOWER
))) {
1565 self
::addToErrorMsg(ts('Preferred Mail Format'), $errorMessage);
1569 case 'individual_prefix':
1571 if (!self
::in_value($value, CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id'))) {
1572 self
::addToErrorMsg(ts('Individual Prefix'), $errorMessage);
1576 case 'individual_suffix':
1578 if (!self
::in_value($value, CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id'))) {
1579 self
::addToErrorMsg(ts('Individual Suffix'), $errorMessage);
1583 case 'state_province':
1584 if (!empty($value)) {
1585 foreach ($value as $stateValue) {
1586 if ($stateValue['state_province']) {
1587 if (self
::in_value($stateValue['state_province'], CRM_Core_PseudoConstant
::stateProvinceAbbreviation()) ||
1588 self
::in_value($stateValue['state_province'], CRM_Core_PseudoConstant
::stateProvince())
1593 self
::addToErrorMsg(ts('State/Province'), $errorMessage);
1601 if (!empty($value)) {
1602 foreach ($value as $stateValue) {
1603 if ($stateValue['country']) {
1604 CRM_Core_PseudoConstant
::populate($countryNames, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active');
1605 CRM_Core_PseudoConstant
::populate($countryIsoCodes, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
1606 $limitCodes = CRM_Core_BAO_Country
::countryLimit();
1607 //If no country is selected in
1608 //localization then take all countries
1609 if (empty($limitCodes)) {
1610 $limitCodes = $countryIsoCodes;
1613 if (self
::in_value($stateValue['country'], $limitCodes) || self
::in_value($stateValue['country'], CRM_Core_PseudoConstant
::country())) {
1616 if (self
::in_value($stateValue['country'], $countryIsoCodes) || self
::in_value($stateValue['country'], $countryNames)) {
1617 self
::addToErrorMsg(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." '), $errorMessage);
1620 self
::addToErrorMsg(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."'), $errorMessage);
1628 if (!empty($value)) {
1629 foreach ($value as $county) {
1630 if ($county['county']) {
1631 $countyNames = CRM_Core_PseudoConstant
::county();
1632 if (!empty($county['county']) && !in_array($county['county'], $countyNames)) {
1633 self
::addToErrorMsg(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.'), $errorMessage);
1641 if (!empty($value)) {
1642 foreach ($value as $codeValue) {
1643 if (!empty($codeValue['geo_code_1'])) {
1644 if (CRM_Utils_Rule
::numeric($codeValue['geo_code_1'])) {
1647 self
::addToErrorMsg(ts('Geo code 1'), $errorMessage);
1654 if (!empty($value)) {
1655 foreach ($value as $codeValue) {
1656 if (!empty($codeValue['geo_code_2'])) {
1657 if (CRM_Utils_Rule
::numeric($codeValue['geo_code_2'])) {
1660 self
::addToErrorMsg(ts('Geo code 2'), $errorMessage);
1666 //check for any error in email/postal greeting, addressee,
1667 //custom email/postal greeting, custom addressee, CRM-4575
1669 case 'email_greeting':
1670 $emailGreetingFilter = [
1671 'contact_type' => $this->_contactType
,
1672 'greeting_type' => 'email_greeting',
1674 if (!self
::in_value($value, CRM_Core_PseudoConstant
::greeting($emailGreetingFilter))) {
1675 self
::addToErrorMsg(ts('Email Greeting must be one of the configured format options. Check Administer >> System Settings >> Option Groups >> Email Greetings for valid values'), $errorMessage);
1679 case 'postal_greeting':
1680 $postalGreetingFilter = [
1681 'contact_type' => $this->_contactType
,
1682 'greeting_type' => 'postal_greeting',
1684 if (!self
::in_value($value, CRM_Core_PseudoConstant
::greeting($postalGreetingFilter))) {
1685 self
::addToErrorMsg(ts('Postal Greeting must be one of the configured format options. Check Administer >> System Settings >> Option Groups >> Postal Greetings for valid values'), $errorMessage);
1690 $addresseeFilter = [
1691 'contact_type' => $this->_contactType
,
1692 'greeting_type' => 'addressee',
1694 if (!self
::in_value($value, CRM_Core_PseudoConstant
::greeting($addresseeFilter))) {
1695 self
::addToErrorMsg(ts('Addressee must be one of the configured format options. Check Administer >> System Settings >> Option Groups >> Addressee for valid values'), $errorMessage);
1699 case 'email_greeting_custom':
1700 if (array_key_exists('email_greeting', $params)) {
1701 $emailGreetingLabel = key(CRM_Core_OptionGroup
::values('email_greeting', TRUE, NULL, NULL, 'AND v.name = "Customized"'));
1702 if (CRM_Utils_Array
::value('email_greeting', $params) != $emailGreetingLabel) {
1703 self
::addToErrorMsg(ts('Email Greeting - Custom'), $errorMessage);
1708 case 'postal_greeting_custom':
1709 if (array_key_exists('postal_greeting', $params)) {
1710 $postalGreetingLabel = key(CRM_Core_OptionGroup
::values('postal_greeting', TRUE, NULL, NULL, 'AND v.name = "Customized"'));
1711 if (CRM_Utils_Array
::value('postal_greeting', $params) != $postalGreetingLabel) {
1712 self
::addToErrorMsg(ts('Postal Greeting - Custom'), $errorMessage);
1717 case 'addressee_custom':
1718 if (array_key_exists('addressee', $params)) {
1719 $addresseeLabel = key(CRM_Core_OptionGroup
::values('addressee', TRUE, NULL, NULL, 'AND v.name = "Customized"'));
1720 if (CRM_Utils_Array
::value('addressee', $params) != $addresseeLabel) {
1721 self
::addToErrorMsg(ts('Addressee - Custom'), $errorMessage);
1727 if (is_array($value)) {
1728 foreach ($value as $values) {
1729 if (!empty($values['url']) && !CRM_Utils_Rule
::url($values['url'])) {
1730 self
::addToErrorMsg(ts('Website'), $errorMessage);
1737 case 'do_not_email':
1738 case 'do_not_phone':
1741 case 'do_not_trade':
1742 if (CRM_Utils_Rule
::boolean($value) == FALSE) {
1743 $key = ucwords(str_replace("_", " ", $key));
1744 self
::addToErrorMsg($key, $errorMessage);
1749 if (is_array($value)) {
1750 foreach ($value as $values) {
1751 if (!empty($values['email']) && !CRM_Utils_Rule
::email($values['email'])) {
1752 self
::addToErrorMsg($key, $errorMessage);
1760 if (is_array($params[$key]) && isset($params[$key]["contact_type"])) {
1761 //check for any relationship data ,FIX ME
1762 self
::isErrorInCoreData($params[$key], $errorMessage);
1770 * Ckeck a value present or not in a array.
1773 * @param $valueArray
1777 public static function in_value($value, $valueArray) {
1778 foreach ($valueArray as $key => $v) {
1780 if (strtolower(trim($v, ".")) == strtolower(trim($value, "."))) {
1788 * Build error-message containing error-fields
1790 * Once upon a time there was a dev who hadn't heard of implode. That dev wrote this function.
1792 * @todo just say no!
1794 * @param string $errorName
1795 * A string containing error-field name.
1796 * @param string $errorMessage
1797 * A string containing all the error-fields, where the new errorName is concatenated.
1800 public static function addToErrorMsg($errorName, &$errorMessage) {
1801 if ($errorMessage) {
1802 $errorMessage .= "; $errorName";
1805 $errorMessage = $errorName;
1810 * Method for creating contact.
1812 * @param array $formatted
1813 * @param array $contactFields
1814 * @param int $onDuplicate
1815 * @param int $contactId
1816 * @param bool $requiredCheck
1817 * @param int $dedupeRuleGroupID
1819 * @return array|bool|\CRM_Contact_BAO_Contact|\CRM_Core_Error|null
1821 public function createContact(&$formatted, &$contactFields, $onDuplicate, $contactId = NULL, $requiredCheck = TRUE, $dedupeRuleGroupID = NULL) {
1825 if (is_null($contactId) && ($onDuplicate != CRM_Import_Parser
::DUPLICATE_NOCHECK
)) {
1826 $dupeCheck = (bool) ($onDuplicate);
1829 //get the prefix id etc if exists
1830 CRM_Contact_BAO_Contact
::resolveDefaults($formatted, TRUE);
1832 //@todo direct call to API function not supported.
1833 // setting required check to false, CRM-2839
1834 // plus we do our own required check in import
1836 $error = $this->deprecated_contact_check_params($formatted, $dupeCheck, $dedupeRuleGroupID);
1840 $this->deprecated_validate_formatted_contact($formatted);
1842 catch (CRM_Core_Exception
$e) {
1843 return ['error_message' => $e->getMessage(), 'is_error' => 1, 'code' => $e->getCode()];
1847 $this->formatParams($formatted, $onDuplicate, (int) $contactId);
1850 // Resetting and rebuilding cache could be expensive.
1851 CRM_Core_Config
::setPermitCacheFlushMode(FALSE);
1853 // If a user has logged in, or accessed via a checksum
1854 // Then deliberately 'blanking' a value in the profile should remove it from their record
1855 // @todo this should either be TRUE or FALSE in the context of import - once
1856 // we figure out which we can remove all the rest.
1857 // Also note the meaning of this parameter is less than it used to
1858 // be following block cleanup.
1859 $formatted['updateBlankLocInfo'] = TRUE;
1860 if ((CRM_Core_Session
::singleton()->get('authSrc') & (CRM_Core_Permission
::AUTH_SRC_CHECKSUM + CRM_Core_Permission
::AUTH_SRC_LOGIN
)) == 0) {
1861 $formatted['updateBlankLocInfo'] = FALSE;
1864 [$data, $contactDetails] = CRM_Contact_BAO_Contact
::formatProfileContactParams($formatted, $contactFields, $contactId, NULL, $formatted['contact_type']);
1866 // manage is_opt_out
1867 if (array_key_exists('is_opt_out', $contactFields) && array_key_exists('is_opt_out', $formatted)) {
1868 $wasOptOut = $contactDetails['is_opt_out'] ??
FALSE;
1869 $isOptOut = $formatted['is_opt_out'];
1870 $data['is_opt_out'] = $isOptOut;
1871 // on change, create new civicrm_subscription_history entry
1872 if (($wasOptOut != $isOptOut) && !empty($contactDetails['contact_id'])) {
1874 'contact_id' => $contactDetails['contact_id'],
1875 'status' => $isOptOut ?
'Removed' : 'Added',
1878 CRM_Contact_BAO_SubscriptionHistory
::create($shParams);
1882 $contact = civicrm_api3('Contact', 'create', $data);
1883 $cid = $contact['id'];
1885 CRM_Core_Config
::setPermitCacheFlushMode(TRUE);
1888 'contact_id' => $cid,
1892 $newContact = CRM_Contact_BAO_Contact
::retrieve($contact, $defaults);
1894 //get the id of the contact whose street address is not parsable, CRM-5886
1895 if ($this->_parseStreetAddress
&& is_object($newContact) && property_exists($newContact, 'address') && $newContact->address
) {
1896 foreach ($newContact->address
as $address) {
1897 if (!empty($address['street_address']) && (empty($address['street_number']) ||
empty($address['street_name']))) {
1898 $this->_unparsedStreetAddressContacts
[] = [
1899 'id' => $newContact->id
,
1900 'streetAddress' => $address['street_address'],
1909 * Format params for update and fill mode.
1911 * @param array $params
1912 * reference to an array containing all the.
1914 * @param int $onDuplicate
1918 public function formatParams(&$params, $onDuplicate, $cid) {
1919 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_SKIP
) {
1924 'contact_id' => $cid,
1928 $contactObj = CRM_Contact_BAO_Contact
::retrieve($contactParams, $defaults);
1930 $modeFill = ($onDuplicate == CRM_Import_Parser
::DUPLICATE_FILL
);
1932 $groupTree = CRM_Core_BAO_CustomGroup
::getTree($params['contact_type'], NULL, $cid, 0, NULL);
1933 CRM_Core_BAO_CustomGroup
::setDefaults($groupTree, $defaults, FALSE, FALSE);
1939 'website' => 'website',
1940 'address' => 'address',
1943 $contact = get_object_vars($contactObj);
1945 foreach ($params as $key => $value) {
1946 if ($key == 'id' ||
$key == 'contact_type') {
1950 if (array_key_exists($key, $locationFields)) {
1953 if (in_array($key, [
1958 // CRM-4575, need to null custom
1959 if ($params["{$key}_id"] != 4) {
1960 $params["{$key}_custom"] = 'null';
1962 unset($params[$key]);
1965 if ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1966 $custom_params = ['id' => $contact['id'], 'return' => $key];
1967 $getValue = civicrm_api3('Contact', 'getvalue', $custom_params);
1968 if (empty($getValue)) {
1973 $getValue = CRM_Utils_Array
::retrieveValueRecursive($contact, $key);
1975 if ($key == 'contact_source') {
1976 $params['source'] = $params[$key];
1977 unset($params[$key]);
1980 if ($modeFill && isset($getValue)) {
1981 unset($params[$key]);
1982 if ($customFieldId) {
1983 // Extra values must be unset to ensure the values are not
1985 unset($params['custom'][$customFieldId]);
1991 foreach ($locationFields as $locKeys) {
1992 if (isset($params[$locKeys]) && is_array($params[$locKeys])) {
1993 foreach ($params[$locKeys] as $key => $value) {
1995 $getValue = CRM_Utils_Array
::retrieveValueRecursive($contact, $locKeys);
1997 if (isset($getValue)) {
1998 foreach ($getValue as $cnt => $values) {
1999 if ($locKeys == 'website') {
2000 if (($getValue[$cnt]['website_type_id'] == $params[$locKeys][$key]['website_type_id'])) {
2001 unset($params[$locKeys][$key]);
2005 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']) {
2006 unset($params[$locKeys][$key]);
2013 if (count($params[$locKeys]) == 0) {
2014 unset($params[$locKeys]);
2021 * Convert any given date string to default date array.
2023 * @param array $params
2024 * Has given date-format.
2025 * @param array $formatted
2026 * Store formatted date in this array.
2027 * @param int $dateType
2029 * @param string $dateParam
2032 public static function formatCustomDate(&$params, &$formatted, $dateType, $dateParam) {
2034 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $dateParam);
2035 $formatted[$dateParam] = CRM_Utils_Date
::processDate($params[$dateParam]);
2039 * Generate status and error message for unparsed street address records.
2041 * @param array $values
2042 * The array of values belonging to each row.
2043 * @param array $statusFieldName
2044 * Store formatted date in this array.
2045 * @param $returnCode
2049 public function processMessage(&$values, $statusFieldName, $returnCode) {
2050 if (empty($this->_unparsedStreetAddressContacts
)) {
2051 $importRecordParams = [
2052 $statusFieldName => 'IMPORTED',
2056 $errorMessage = ts("Record imported successfully but unable to parse the street address: ");
2057 foreach ($this->_unparsedStreetAddressContacts
as $contactInfo => $contactValue) {
2058 $contactUrl = CRM_Utils_System
::url('civicrm/contact/add', 'reset=1&action=update&cid=' . $contactValue['id'], TRUE, NULL, FALSE);
2059 $errorMessage .= "\n Contact ID:" . $contactValue['id'] . " <a href=\"$contactUrl\"> " . $contactValue['streetAddress'] . "</a>";
2061 array_unshift($values, $errorMessage);
2062 $importRecordParams = [
2063 $statusFieldName => 'ERROR',
2064 "${statusFieldName}Msg" => $errorMessage,
2066 $returnCode = CRM_Import_Parser
::UNPARSED_ADDRESS_WARNING
;
2068 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
2074 * @param array $params
2078 public function checkRelatedContactFields($relKey, $params) {
2079 //avoid blank contact creation.
2080 $allowToCreate = FALSE;
2082 //build the mapper field array.
2083 static $relatedContactFields = [];
2084 if (!isset($relatedContactFields[$relKey])) {
2085 foreach ($this->_mapperRelated
as $key => $name) {
2090 if (!empty($relatedContactFields[$name]) && !is_array($relatedContactFields[$name])) {
2091 $relatedContactFields[$name] = [];
2093 $fldName = $this->_mapperRelatedContactDetails
[$key] ??
NULL;
2094 if ($fldName == 'url') {
2095 $fldName = 'website';
2098 $relatedContactFields[$name][] = $fldName;
2103 //validate for passed data.
2104 if (is_array($relatedContactFields[$relKey])) {
2105 foreach ($relatedContactFields[$relKey] as $fld) {
2106 if (!empty($params[$fld])) {
2107 $allowToCreate = TRUE;
2113 return $allowToCreate;
2117 * get subtypes given the contact type
2119 * @param string $contactType
2120 * @return array $subTypes
2122 public static function getSubtypes($contactType) {
2124 $types = CRM_Contact_BAO_ContactType
::subTypeInfo($contactType);
2126 if (count($types) > 0) {
2127 foreach ($types as $type) {
2128 $subTypes[] = $type['name'];
2135 * Get the possible contact matches.
2137 * 1) the chosen dedupe rule falling back to
2138 * 2) a check for the external ID.
2140 * @see https://issues.civicrm.org/jira/browse/CRM-17275
2142 * @param array $params
2145 * IDs of possible matches.
2147 * @throws \CRM_Core_Exception
2148 * @throws \CiviCRM_API3_Exception
2150 protected function getPossibleContactMatches($params) {
2153 if (!empty($params['external_identifier'])) {
2154 // Check for any match on external id, deleted or otherwise.
2155 $extIDContact = civicrm_api3('Contact', 'get', [
2156 'external_identifier' => $params['external_identifier'],
2158 'return' => ['id', 'contact_is_deleted'],
2160 if (isset($extIDContact['id'])) {
2161 $extIDMatch = $extIDContact['id'];
2163 if ($extIDContact['values'][$extIDMatch]['contact_is_deleted'] == 1) {
2164 // If the contact is deleted, update external identifier to be blank
2165 // to avoid key error from MySQL.
2166 $params = ['id' => $extIDMatch, 'external_identifier' => ''];
2167 civicrm_api3('Contact', 'create', $params);
2169 // And now it is no longer a match.
2174 $checkParams = ['check_permissions' => FALSE, 'match' => $params];
2175 $checkParams['match']['contact_type'] = $this->_contactType
;
2177 $possibleMatches = civicrm_api3('Contact', 'duplicatecheck', $checkParams);
2179 return array_keys($possibleMatches['values']);
2181 if ($possibleMatches['count']) {
2182 if (array_key_exists($extIDMatch, $possibleMatches['values'])) {
2183 return [$extIDMatch];
2185 throw new CRM_Core_Exception(ts(
2186 'Matching this contact based on the de-dupe rule would cause an external ID conflict'));
2188 return [$extIDMatch];
2192 * Format the form mapping parameters ready for the parser.
2197 * @return array $parserParameters
2199 public static function getParameterForParser($count) {
2201 for ($i = 0; $i < $count; $i++
) {
2202 $baseArray[$i] = NULL;
2204 $parserParameters['mapperLocType'] = $baseArray;
2205 $parserParameters['mapperPhoneType'] = $baseArray;
2206 $parserParameters['mapperImProvider'] = $baseArray;
2207 $parserParameters['mapperWebsiteType'] = $baseArray;
2208 $parserParameters['mapperRelated'] = $baseArray;
2209 $parserParameters['relatedContactType'] = $baseArray;
2210 $parserParameters['relatedContactDetails'] = $baseArray;
2211 $parserParameters['relatedContactLocType'] = $baseArray;
2212 $parserParameters['relatedContactPhoneType'] = $baseArray;
2213 $parserParameters['relatedContactImProvider'] = $baseArray;
2214 $parserParameters['relatedContactWebsiteType'] = $baseArray;
2216 return $parserParameters;
2221 * Set field metadata.
2223 protected function setFieldMetadata() {
2224 $this->setImportableFieldsMetadata($this->getContactImportMetadata());
2225 // Probably no longer needed but here for now.
2226 $this->_relationships
= $this->getRelationships();
2230 * @param array $newContact
2231 * @param $statusFieldName
2232 * @param array $values
2233 * @param int $onDuplicate
2234 * @param array $formatted
2235 * @param array $contactFields
2239 * @throws \CRM_Core_Exception
2240 * @throws \CiviCRM_API3_Exception
2241 * @throws \Civi\API\Exception\UnauthorizedException
2243 protected function handleDuplicateError(array $newContact, $statusFieldName, array $values, int $onDuplicate, array $formatted, array $contactFields): int {
2245 // need to fix at some stage and decide if the error will return an
2246 // array or string, crude hack for now
2247 if (is_array($newContact['error_message']['params'][0])) {
2248 $cids = $newContact['error_message']['params'][0];
2251 $cids = explode(',', $newContact['error_message']['params'][0]);
2254 foreach ($cids as $cid) {
2255 $urls[] = CRM_Utils_System
::url('civicrm/contact/view', 'reset=1&cid=' . $cid, TRUE);
2258 $url_string = implode("\n", $urls);
2260 // If we duplicate more than one record, skip no matter what
2261 if (count($cids) > 1) {
2262 $errorMessage = ts('Record duplicates multiple contacts');
2263 $importRecordParams = [
2264 $statusFieldName => 'ERROR',
2265 "${statusFieldName}Msg" => $errorMessage,
2268 //combine error msg to avoid mismatch between error file columns.
2269 $errorMessage .= "\n" . $url_string;
2270 array_unshift($values, $errorMessage);
2271 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
2272 return CRM_Import_Parser
::ERROR
;
2275 // Params only had one id, so shift it out
2276 $contactId = array_shift($cids);
2279 $vals = ['contact_id' => $contactId];
2281 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_REPLACE
) {
2282 civicrm_api('contact', 'delete', $vals);
2283 $cid = CRM_Contact_BAO_Contact
::createProfileContact($formatted, $contactFields, $contactId, NULL, NULL, $formatted['contact_type']);
2285 if (in_array((int) $onDuplicate, [CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::DUPLICATE_FILL
], TRUE)) {
2286 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactId);
2288 // else skip does nothing and just returns an error code.
2291 'contact_id' => $cid,
2294 $newContact = CRM_Contact_BAO_Contact
::retrieve($contact, $defaults);
2297 if (civicrm_error($newContact)) {
2298 if (empty($newContact['error_message']['params'])) {
2299 // different kind of error other than DUPLICATE
2300 $errorMessage = $newContact['error_message'];
2301 array_unshift($values, $errorMessage);
2302 $importRecordParams = [
2303 $statusFieldName => 'ERROR',
2304 "${statusFieldName}Msg" => $errorMessage,
2306 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
2307 return CRM_Import_Parser
::ERROR
;
2310 $contactID = $newContact['error_message']['params'][0];
2311 if (is_array($contactID)) {
2312 $contactID = array_pop($contactID);
2314 if (!in_array($contactID, $this->_newContacts
)) {
2315 $this->_newContacts
[] = $contactID;
2318 //CRM-262 No Duplicate Checking
2319 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_SKIP
) {
2320 array_unshift($values, $url_string);
2321 $importRecordParams = [
2322 $statusFieldName => 'DUPLICATE',
2323 "${statusFieldName}Msg" => "Skipping duplicate record",
2325 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
2326 return CRM_Import_Parser
::DUPLICATE
;
2329 $importRecordParams = [
2330 $statusFieldName => 'IMPORTED',
2332 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
2333 //return warning if street address is not parsed, CRM-5886
2334 return $this->processMessage($values, $statusFieldName, CRM_Import_Parser
::VALID
);
2338 * Validate a formatted contact parameter list.
2340 * @param array $params
2341 * Structured parameter list (as in crm_format_params).
2343 * @throw CRM_Core_Error
2345 public function deprecated_validate_formatted_contact(&$params): void
{
2346 // Look for offending email addresses
2348 if (array_key_exists('email', $params)) {
2349 foreach ($params['email'] as $count => $values) {
2350 if (!is_array($values)) {
2353 if ($email = CRM_Utils_Array
::value('email', $values)) {
2354 // validate each email
2355 if (!CRM_Utils_Rule
::email($email)) {
2356 throw new CRM_Core_Exception('No valid email address');
2359 // check for loc type id.
2360 if (empty($values['location_type_id'])) {
2361 throw new CRM_Core_Exception('Location Type Id missing.');
2367 // Validate custom data fields
2368 if (array_key_exists('custom', $params) && is_array($params['custom'])) {
2369 foreach ($params['custom'] as $key => $custom) {
2370 if (is_array($custom)) {
2371 foreach ($custom as $fieldId => $value) {
2372 $valid = CRM_Core_BAO_CustomValue
::typecheck(CRM_Utils_Array
::value('type', $value),
2373 CRM_Utils_Array
::value('value', $value)
2375 if (!$valid && $value['is_required']) {
2376 throw new CRM_Core_Exception('Invalid value for custom field \'' .
2377 $custom['name'] . '\''
2380 if (CRM_Utils_Array
::value('type', $custom) == 'Date') {
2381 $params['custom'][$key][$fieldId]['value'] = str_replace('-', '', $params['custom'][$key][$fieldId]['value']);
2390 * @param array $params
2391 * @param bool $dupeCheck
2392 * @param null|int $dedupeRuleGroupID
2394 * @throws \CRM_Core_Exception
2396 public function deprecated_contact_check_params(
2399 $dedupeRuleGroupID = NULL) {
2401 $requiredCheck = TRUE;
2403 if (isset($params['id']) && is_numeric($params['id'])) {
2404 $requiredCheck = FALSE;
2406 if ($requiredCheck) {
2409 ['first_name', 'last_name'],
2416 'organization_name',
2420 // contact_type has a limited number of valid values
2421 if (empty($params['contact_type'])) {
2422 throw new CRM_Core_Exception("No Contact Type");
2424 $fields = $required[$params['contact_type']] ??
NULL;
2425 if ($fields == NULL) {
2426 throw new CRM_Core_Exception("Invalid Contact Type: {$params['contact_type']}");
2429 if ($csType = CRM_Utils_Array
::value('contact_sub_type', $params)) {
2430 if (!(CRM_Contact_BAO_ContactType
::isExtendsContactType($csType, $params['contact_type']))) {
2431 throw new CRM_Core_Exception("Invalid or Mismatched Contact Subtype: " . implode(', ', (array) $csType));
2435 if (empty($params['contact_id']) && !empty($params['id'])) {
2438 foreach ($fields as $field) {
2439 if (is_array($field)) {
2441 foreach ($field as $element) {
2442 if (empty($params[$element])) {
2450 if (!empty($params[$field])) {
2460 throw new CRM_Core_Exception("Required fields not found for {$params['contact_type']} : $error");
2466 // @todo switch to using api version
2467 // $dupes = civicrm_api3('Contact', 'duplicatecheck', (array('match' => $params, 'dedupe_rule_id' => $dedupeRuleGroupID)));
2468 // $ids = $dupes['count'] ? implode(',', array_keys($dupes['values'])) : NULL;
2469 $ids = CRM_Contact_BAO_Contact
::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised', [], CRM_Utils_Array
::value('check_permissions', $params), $dedupeRuleGroupID);
2471 $error = CRM_Core_Error
::createError("Found matching contacts: " . implode(',', $ids),
2472 CRM_Core_Error
::DUPLICATE_CONTACT
,
2475 return civicrm_api3_create_error($error->pop());
2479 // check for organisations with same name
2480 if (!empty($params['current_employer'])) {
2481 $organizationParams = ['organization_name' => $params['current_employer']];
2482 $dupeIds = CRM_Contact_BAO_Contact
::getDuplicateContacts($organizationParams, 'Organization', 'Supervised', [], FALSE);
2484 // check for mismatch employer name and id
2485 if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds)
2487 throw new CRM_Core_Exception('Employer name and Employer id Mismatch');
2490 // show error if multiple organisation with same name exist
2491 if (empty($params['employer_id']) && (count($dupeIds) > 1)
2493 return civicrm_api3_create_error('Found more than one Organisation with same Name.');