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 +--------------------------------------------------------------------+
11 require_once 'CRM/Utils/DeprecatedUtils.php';
12 require_once 'api/v3/utils.php';
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * class to parse contact csv files
23 class CRM_Contact_Import_Parser_Contact
extends CRM_Contact_Import_Parser
{
25 use CRM_Contact_Import_MetadataTrait
;
27 protected $_mapperKeys = [];
28 protected $_mapperLocType = [];
29 protected $_mapperPhoneType;
30 protected $_mapperImProvider;
31 protected $_mapperWebsiteType;
32 protected $_mapperRelated;
33 protected $_mapperRelatedContactType;
34 protected $_mapperRelatedContactDetails;
35 protected $_mapperRelatedContactEmailType;
36 protected $_mapperRelatedContactImProvider;
37 protected $_mapperRelatedContactWebsiteType;
38 protected $_relationships;
40 protected $_emailIndex;
41 protected $_firstNameIndex;
42 protected $_lastNameIndex;
44 protected $_householdNameIndex;
45 protected $_organizationNameIndex;
47 protected $_allEmails;
49 protected $_phoneIndex;
52 * Is update only permitted on an id match.
54 * Note this historically was true for when id or external identifier was
55 * present. However, CRM-17275 determined that a dedupe-match could over-ride
56 * external identifier.
60 protected $_updateWithId;
63 protected $_externalIdentifierIndex;
64 protected $_allExternalIdentifiers;
65 protected $_parseStreetAddress;
68 * Array of successfully imported contact id's
72 protected $_newContacts;
79 protected $_lineCount;
82 * Array of successfully imported related contact id's
86 protected $_newRelatedContacts;
89 * Array of all the contacts whose street addresses are not parsed.
90 * of this import process
93 protected $_unparsedStreetAddressContacts;
98 * @param array $mapperKeys
99 * @param array $mapperLocType
100 * @param array $mapperPhoneType
101 * @param array $mapperImProvider
102 * @param array $mapperRelated
103 * @param array $mapperRelatedContactType
104 * @param array $mapperRelatedContactDetails
105 * @param array $mapperRelatedContactLocType
106 * @param array $mapperRelatedContactPhoneType
107 * @param array $mapperRelatedContactImProvider
108 * @param array $mapperWebsiteType
109 * @param array $mapperRelatedContactWebsiteType
111 public function __construct(
112 $mapperKeys, $mapperLocType = [], $mapperPhoneType = [], $mapperImProvider = [], $mapperRelated = [], $mapperRelatedContactType = [], $mapperRelatedContactDetails = [], $mapperRelatedContactLocType = [], $mapperRelatedContactPhoneType = [], $mapperRelatedContactImProvider = [],
113 $mapperWebsiteType = [], $mapperRelatedContactWebsiteType = []
115 parent
::__construct();
116 $this->_mapperKeys
= $mapperKeys;
117 $this->_mapperLocType
= &$mapperLocType;
118 $this->_mapperPhoneType
= &$mapperPhoneType;
119 $this->_mapperWebsiteType
= $mapperWebsiteType;
120 // get IM service provider type id for contact
121 $this->_mapperImProvider
= &$mapperImProvider;
122 $this->_mapperRelated
= &$mapperRelated;
123 $this->_mapperRelatedContactType
= &$mapperRelatedContactType;
124 $this->_mapperRelatedContactDetails
= &$mapperRelatedContactDetails;
125 $this->_mapperRelatedContactLocType
= &$mapperRelatedContactLocType;
126 $this->_mapperRelatedContactPhoneType
= &$mapperRelatedContactPhoneType;
127 $this->_mapperRelatedContactWebsiteType
= $mapperRelatedContactWebsiteType;
128 // get IM service provider type id for related contact
129 $this->_mapperRelatedContactImProvider
= &$mapperRelatedContactImProvider;
133 * The initializer code, called before processing.
135 public function init() {
136 $this->setFieldMetadata();
137 foreach ($this->getImportableFieldsMetadata() as $name => $field) {
138 $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));
141 $this->_newContacts
= [];
143 $this->setActiveFields($this->_mapperKeys
);
144 $this->setActiveFieldLocationTypes($this->_mapperLocType
);
145 $this->setActiveFieldPhoneTypes($this->_mapperPhoneType
);
146 $this->setActiveFieldWebsiteTypes($this->_mapperWebsiteType
);
147 //set active fields of IM provider of contact
148 $this->setActiveFieldImProviders($this->_mapperImProvider
);
151 $this->setActiveFieldRelated($this->_mapperRelated
);
152 $this->setActiveFieldRelatedContactType($this->_mapperRelatedContactType
);
153 $this->setActiveFieldRelatedContactDetails($this->_mapperRelatedContactDetails
);
154 $this->setActiveFieldRelatedContactLocType($this->_mapperRelatedContactLocType
);
155 $this->setActiveFieldRelatedContactPhoneType($this->_mapperRelatedContactPhoneType
);
156 $this->setActiveFieldRelatedContactWebsiteType($this->_mapperRelatedContactWebsiteType
);
157 //set active fields of IM provider of related contact
158 $this->setActiveFieldRelatedContactImProvider($this->_mapperRelatedContactImProvider
);
160 $this->_phoneIndex
= -1;
161 $this->_emailIndex
= -1;
162 $this->_firstNameIndex
= -1;
163 $this->_lastNameIndex
= -1;
164 $this->_householdNameIndex
= -1;
165 $this->_organizationNameIndex
= -1;
166 $this->_externalIdentifierIndex
= -1;
169 foreach ($this->_mapperKeys
as $key) {
170 if (substr($key, 0, 5) == 'email' && substr($key, 0, 14) != 'email_greeting') {
171 $this->_emailIndex
= $index;
172 $this->_allEmails
= [];
174 if (substr($key, 0, 5) == 'phone') {
175 $this->_phoneIndex
= $index;
177 if ($key == 'first_name') {
178 $this->_firstNameIndex
= $index;
180 if ($key == 'last_name') {
181 $this->_lastNameIndex
= $index;
183 if ($key == 'household_name') {
184 $this->_householdNameIndex
= $index;
186 if ($key == 'organization_name') {
187 $this->_organizationNameIndex
= $index;
190 if ($key == 'external_identifier') {
191 $this->_externalIdentifierIndex
= $index;
192 $this->_allExternalIdentifiers
= [];
197 $this->_updateWithId
= FALSE;
198 if (in_array('id', $this->_mapperKeys
) ||
($this->_externalIdentifierIndex
>= 0 && in_array($this->_onDuplicate
, [
199 CRM_Import_Parser
::DUPLICATE_UPDATE
,
200 CRM_Import_Parser
::DUPLICATE_FILL
,
202 $this->_updateWithId
= TRUE;
205 $this->_parseStreetAddress
= CRM_Utils_Array
::value('street_address_parsing', CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'address_options'), FALSE);
209 * Handle the values in mapField mode.
211 * @param array $values
212 * The array of values belonging to this line.
216 public function mapField(&$values) {
217 return CRM_Import_Parser
::VALID
;
221 * Handle the values in preview mode.
223 * @param array $values
224 * The array of values belonging to this line.
227 * the result of this processing
229 public function preview(&$values) {
230 return $this->summary($values);
234 * Handle the values in summary mode.
236 * @param array $values
237 * The array of values belonging to this line.
240 * the result of this processing
242 public function summary(&$values) {
243 $erroneousField = NULL;
244 $response = $this->setActiveFieldValues($values, $erroneousField);
246 $errorMessage = NULL;
247 $errorRequired = FALSE;
248 switch ($this->_contactType
) {
251 if ($this->_firstNameIndex
< 0 ||
empty($values[$this->_firstNameIndex
])) {
252 $errorRequired = TRUE;
253 $missingNames[] = ts('First Name');
255 if ($this->_lastNameIndex
< 0 ||
empty($values[$this->_lastNameIndex
])) {
256 $errorRequired = TRUE;
257 $missingNames[] = ts('Last Name');
259 if ($errorRequired) {
260 $and = ' ' . ts('and') . ' ';
261 $errorMessage = ts('Missing required fields:') . ' ' . implode($and, $missingNames);
266 if ($this->_householdNameIndex
< 0 ||
empty($values[$this->_householdNameIndex
])) {
267 $errorRequired = TRUE;
268 $errorMessage = ts('Missing required fields:') . ' ' . ts('Household Name');
273 if ($this->_organizationNameIndex
< 0 ||
empty($values[$this->_organizationNameIndex
])) {
274 $errorRequired = TRUE;
275 $errorMessage = ts('Missing required fields:') . ' ' . ts('Organization Name');
280 $statusFieldName = $this->_statusFieldName
;
282 if ($this->_emailIndex
>= 0) {
283 /* If we don't have the required fields, bail */
285 if ($this->_contactType
== 'Individual' && !$this->_updateWithId
) {
286 if ($errorRequired && empty($values[$this->_emailIndex
])) {
288 $errorMessage .= ' ' . ts('OR') . ' ' . ts('Email Address');
291 $errorMessage = ts('Missing required field:') . ' ' . ts('Email Address');
293 array_unshift($values, $errorMessage);
294 $importRecordParams = [
295 $statusFieldName => 'ERROR',
296 "${statusFieldName}Msg" => $errorMessage,
298 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
300 return CRM_Import_Parser
::ERROR
;
304 $email = $values[$this->_emailIndex
] ??
NULL;
306 /* If the email address isn't valid, bail */
308 if (!CRM_Utils_Rule
::email($email)) {
309 $errorMessage = ts('Invalid Email address');
310 array_unshift($values, $errorMessage);
311 $importRecordParams = [
312 $statusFieldName => 'ERROR',
313 "${statusFieldName}Msg" => $errorMessage,
315 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
317 return CRM_Import_Parser
::ERROR
;
320 /* otherwise, count it and move on */
321 $this->_allEmails
[$email] = $this->_lineCount
;
324 elseif ($errorRequired && !$this->_updateWithId
) {
326 $errorMessage .= ' ' . ts('OR') . ' ' . ts('Email Address');
329 $errorMessage = ts('Missing required field:') . ' ' . ts('Email Address');
331 array_unshift($values, $errorMessage);
332 $importRecordParams = [
333 $statusFieldName => 'ERROR',
334 "${statusFieldName}Msg" => $errorMessage,
336 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
338 return CRM_Import_Parser
::ERROR
;
341 //check for duplicate external Identifier
342 $externalID = $values[$this->_externalIdentifierIndex
] ??
NULL;
344 /* If it's a dupe,external Identifier */
346 if ($externalDupe = CRM_Utils_Array
::value($externalID, $this->_allExternalIdentifiers
)) {
347 $errorMessage = ts('External ID conflicts with record %1', [1 => $externalDupe]);
348 array_unshift($values, $errorMessage);
349 $importRecordParams = [
350 $statusFieldName => 'ERROR',
351 "${statusFieldName}Msg" => $errorMessage,
353 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
354 return CRM_Import_Parser
::ERROR
;
356 //otherwise, count it and move on
357 $this->_allExternalIdentifiers
[$externalID] = $this->_lineCount
;
360 //Checking error in custom data
361 $params = &$this->getActiveFieldParams();
362 $params['contact_type'] = $this->_contactType
;
363 //date-format part ends
365 $errorMessage = NULL;
368 //add custom fields for contact sub type
370 if (!empty($this->_contactSubType
)) {
371 $csType = $this->_contactSubType
;
374 //checking error in custom data
375 $this->isErrorInCustomData($params, $errorMessage, $csType, $this->_relationships
);
377 //checking error in core data
378 $this->isErrorInCoreData($params, $errorMessage);
380 $tempMsg = "Invalid value for field(s) : $errorMessage";
381 // put the error message in the import record in the DB
382 $importRecordParams = [
383 $statusFieldName => 'ERROR',
384 "${statusFieldName}Msg" => $tempMsg,
386 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
387 array_unshift($values, $tempMsg);
388 $errorMessage = NULL;
389 return CRM_Import_Parser
::ERROR
;
392 //if user correcting errors by walking back
393 //need to reset status ERROR msg to null
394 //now currently we are having valid data.
395 $importRecordParams = [
396 $statusFieldName => 'NEW',
398 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
400 return CRM_Import_Parser
::VALID
;
404 * Get Array of all the fields that could potentially be part
409 public function getAllFields() {
410 return $this->_fields
;
414 * Handle the values in import mode.
416 * @param int $onDuplicate
417 * The code for what action to take on duplicates.
418 * @param array $values
419 * The array of values belonging to this line.
421 * @param bool $doGeocodeAddress
424 * the result of this processing
426 * @throws \CiviCRM_API3_Exception
427 * @throws \CRM_Core_Exception
429 public function import($onDuplicate, &$values, $doGeocodeAddress = FALSE) {
430 $config = CRM_Core_Config
::singleton();
431 $this->_unparsedStreetAddressContacts
= [];
432 if (!$doGeocodeAddress) {
433 // CRM-5854, reset the geocode method to null to prevent geocoding
434 CRM_Utils_GeocodeProvider
::disableForSession();
437 // first make sure this is a valid line
438 //$this->_updateWithId = false;
439 $response = $this->summary($values);
440 $statusFieldName = $this->_statusFieldName
;
442 if ($response != CRM_Import_Parser
::VALID
) {
443 $importRecordParams = [
444 $statusFieldName => 'INVALID',
445 "${statusFieldName}Msg" => "Invalid (Error Code: $response)",
447 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
451 $params = &$this->getActiveFieldParams();
453 'contact_type' => $this->_contactType
,
456 $contactFields = CRM_Contact_DAO_Contact
::import();
458 //check if external identifier exists in database
459 if (!empty($params['external_identifier']) && (!empty($params['id']) ||
in_array($onDuplicate, [
460 CRM_Import_Parser
::DUPLICATE_SKIP
,
461 CRM_Import_Parser
::DUPLICATE_NOCHECK
,
464 $extIDResult = civicrm_api3('Contact', 'get', [
465 'external_identifier' => $params['external_identifier'],
467 'return' => ['id', 'contact_is_deleted'],
469 if (isset($extIDResult['id'])) {
470 // record with matching external identifier does exist.
471 $internalCid = $extIDResult['id'];
472 if ($internalCid != CRM_Utils_Array
::value('id', $params)) {
473 if ($extIDResult['values'][$internalCid]['contact_is_deleted'] == 1) {
474 // And it is deleted. What to do? If we skip it, they user
475 // will be under the impression that the record exists in
476 // the database, yet they won't be able to find it. If we
477 // don't skip it, the database will try to insert a new record
478 // with an external_identifier that is non-unique. So...
479 // we will update this contact to remove the external_identifier
480 // and let a new record be created.
481 $update_params = ['id' => $internalCid, 'external_identifier' => ''];
482 civicrm_api3('Contact', 'create', $update_params);
485 $errorMessage = ts('External ID already exists in Database.');
486 array_unshift($values, $errorMessage);
487 $importRecordParams = [
488 $statusFieldName => 'ERROR',
489 "${statusFieldName}Msg" => $errorMessage,
491 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
492 return CRM_Import_Parser
::DUPLICATE
;
498 if (!empty($this->_contactSubType
)) {
499 $params['contact_sub_type'] = $this->_contactSubType
;
502 if ($subType = CRM_Utils_Array
::value('contact_sub_type', $params)) {
503 if (CRM_Contact_BAO_ContactType
::isExtendsContactType($subType, $this->_contactType
, FALSE, 'label')) {
504 $subTypes = CRM_Contact_BAO_ContactType
::subTypePairs($this->_contactType
, FALSE, NULL);
505 $params['contact_sub_type'] = array_search($subType, $subTypes);
507 elseif (!CRM_Contact_BAO_ContactType
::isExtendsContactType($subType, $this->_contactType
)) {
508 $message = "Mismatched or Invalid Contact Subtype.";
509 array_unshift($values, $message);
510 return CRM_Import_Parser
::NO_MATCH
;
514 // Get contact id to format common data in update/fill mode,
515 // prioritising a dedupe rule check over an external_identifier check, but falling back on ext id.
516 if ($this->_updateWithId
&& empty($params['id'])) {
518 $possibleMatches = $this->getPossibleContactMatches($params);
520 catch (CRM_Core_Exception
$e) {
521 $errorMessage = $e->getMessage();
522 array_unshift($values, $errorMessage);
524 $importRecordParams = [
525 $statusFieldName => 'ERROR',
526 "${statusFieldName}Msg" => $errorMessage,
528 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
529 return CRM_Import_Parser
::ERROR
;
531 foreach ($possibleMatches as $possibleID) {
532 $params['id'] = $formatted['id'] = $possibleID;
535 //format common data, CRM-4062
536 $this->formatCommonData($params, $formatted, $contactFields);
538 $relationship = FALSE;
539 $createNewContact = TRUE;
540 // Support Match and Update Via Contact ID
541 if ($this->_updateWithId
&& isset($params['id'])) {
542 $createNewContact = FALSE;
543 // @todo - it feels like all the rows from here to the end of the IF
544 // could be removed in favour of a simple check for whether the contact_type & id match
545 // the call to the deprecated function seems to add no value other that to do an additional
546 // check for the contact_id & type.
547 $error = _civicrm_api3_deprecated_duplicate_formatted_contact($formatted);
548 if (CRM_Core_Error
::isAPIError($error, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
549 if (is_array($error['error_message']['params'][0])) {
550 $matchedIDs = $error['error_message']['params'][0];
553 $matchedIDs = explode(',', $error['error_message']['params'][0]);
555 if (count($matchedIDs) >= 1) {
557 foreach ($matchedIDs as $contactId) {
558 if ($params['id'] == $contactId) {
559 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_type');
560 if ($formatted['contact_type'] == $contactType) {
561 //validation of subtype for update mode
563 $contactSubType = NULL;
564 if (!empty($params['contact_sub_type'])) {
565 $contactSubType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_sub_type');
568 if (!empty($contactSubType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($params['id'], $contactSubType) && $contactSubType != CRM_Utils_Array
::value('contact_sub_type', $formatted))) {
570 $message = "Mismatched contact SubTypes :";
571 array_unshift($values, $message);
573 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
577 $this->_retCode
= CRM_Import_Parser
::VALID
;
581 $message = "Mismatched contact Types :";
582 array_unshift($values, $message);
584 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
589 $message = "Mismatched contact IDs OR Mismatched contact Types :";
590 array_unshift($values, $message);
591 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
597 if (!empty($params['id'])) {
598 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_type');
600 if ($formatted['contact_type'] == $contactType) {
601 //validation of subtype for update mode
603 $contactSubType = NULL;
604 if (!empty($params['contact_sub_type'])) {
605 $contactSubType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_sub_type');
608 if (!empty($contactSubType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($params['id'], $contactSubType) && $contactSubType != CRM_Utils_Array
::value('contact_sub_type', $formatted))) {
610 $message = "Mismatched contact SubTypes :";
611 array_unshift($values, $message);
612 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
615 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $params['id'], FALSE, $this->_dedupeRuleGroupID
);
616 $this->_retCode
= CRM_Import_Parser
::VALID
;
620 $message = "Mismatched contact Types :";
621 array_unshift($values, $message);
622 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
626 // we should avoid multiple errors for single record
627 // since we have already retCode and we trying to force again.
628 if ($this->_retCode
!= CRM_Import_Parser
::NO_MATCH
) {
629 $message = "No contact found for this contact ID:" . $params['id'];
630 array_unshift($values, $message);
631 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
637 //now we want to create new contact on update/fill also.
638 $createNewContact = TRUE;
642 if (isset($newContact) && is_a($newContact, 'CRM_Contact_BAO_Contact')) {
643 $relationship = TRUE;
645 elseif (is_a($error, 'CRM_Core_Error')) {
646 $newContact = $error;
647 $relationship = TRUE;
652 //now we create new contact in update/fill mode also.
654 if ($createNewContact ||
($this->_retCode
!= CRM_Import_Parser
::NO_MATCH
&& $this->_updateWithId
)) {
656 //CRM-4430, don't carry if not submitted.
657 foreach (['prefix_id', 'suffix_id', 'gender_id'] as $name) {
658 if (!empty($formatted[$name])) {
659 $options = CRM_Contact_BAO_Contact
::buildOptions($name, 'get');
660 if (!isset($options[$formatted[$name]])) {
661 $formatted[$name] = CRM_Utils_Array
::key((string) $formatted[$name], $options);
665 if ($this->_updateWithId
&& !empty($params['id'])) {
666 $contactID = $params['id'];
668 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactID, TRUE, $this->_dedupeRuleGroupID
);
671 if (isset($newContact) && is_object($newContact) && ($newContact instanceof CRM_Contact_BAO_Contact
)) {
672 $relationship = TRUE;
673 $newContact = clone($newContact);
674 $contactID = $newContact->id
;
675 $this->_newContacts
[] = $contactID;
677 //get return code if we create new contact in update mode, CRM-4148
678 if ($this->_updateWithId
) {
679 $this->_retCode
= CRM_Import_Parser
::VALID
;
682 elseif (isset($newContact) && CRM_Core_Error
::isAPIError($newContact, CRM_Core_Error
::DUPLICATE_CONTACT
)) {
683 // if duplicate, no need of further processing
684 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_SKIP
) {
685 $errorMessage = "Skipping duplicate record";
686 array_unshift($values, $errorMessage);
687 $importRecordParams = [
688 $statusFieldName => 'DUPLICATE',
689 "${statusFieldName}Msg" => $errorMessage,
691 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
692 return CRM_Import_Parser
::DUPLICATE
;
695 $relationship = TRUE;
696 // CRM-10433/CRM-20739 - IDs could be string or array; handle accordingly
697 if (!is_array($dupeContactIDs = $newContact['error_message']['params'][0])) {
698 $dupeContactIDs = explode(',', $dupeContactIDs);
700 $dupeCount = count($dupeContactIDs);
701 $contactID = array_pop($dupeContactIDs);
702 // check to see if we had more than one duplicate contact id.
703 // if we have more than one, the record will be rejected below
704 if ($dupeCount == 1) {
705 // there was only one dupe, we will continue normally...
706 if (!in_array($contactID, $this->_newContacts
)) {
707 $this->_newContacts
[] = $contactID;
714 $currentImportID = end($values);
717 'contactID' => $contactID,
718 'importID' => $currentImportID,
719 'importTempTable' => $this->_tableName
,
720 'fieldHeaders' => $this->_mapperKeys
,
721 'fields' => $this->_activeFields
,
724 CRM_Utils_Hook
::import('Contact', 'process', $this, $hookParams);
728 $primaryContactId = NULL;
729 if (CRM_Core_Error
::isAPIError($newContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
730 if ($dupeCount == 1 && CRM_Utils_Rule
::integer($contactID)) {
731 $primaryContactId = $contactID;
735 $primaryContactId = $newContact->id
;
738 if ((CRM_Core_Error
::isAPIError($newContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
) ||
is_a($newContact, 'CRM_Contact_BAO_Contact')) && $primaryContactId) {
740 //relationship contact insert
741 foreach ($params as $key => $field) {
742 list($id, $first, $second) = CRM_Utils_System
::explode('_', $key, 3);
743 if (!($first == 'a' && $second == 'b') && !($first == 'b' && $second == 'a')) {
747 $relationType = new CRM_Contact_DAO_RelationshipType();
748 $relationType->id
= $id;
749 $relationType->find(TRUE);
750 $direction = "contact_sub_type_$second";
753 'contact_type' => $params[$key]['contact_type'],
756 //set subtype for related contact CRM-5125
757 if (isset($relationType->$direction)) {
758 //validation of related contact subtype for update mode
759 if ($relCsType = CRM_Utils_Array
::value('contact_sub_type', $params[$key]) && $relCsType != $relationType->$direction) {
760 $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact.");
761 array_unshift($values, $errorMessage);
762 return CRM_Import_Parser
::NO_MATCH
;
765 $formatting['contact_sub_type'] = $relationType->$direction;
769 $contactFields = NULL;
770 $contactFields = CRM_Contact_DAO_Contact
::import();
772 //Relation on the basis of External Identifier.
773 if (empty($params[$key]['id']) && !empty($params[$key]['external_identifier'])) {
774 $params[$key]['id'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['external_identifier'], 'id', 'external_identifier');
776 // check for valid related contact id in update/fill mode, CRM-4424
777 if (in_array($onDuplicate, [
778 CRM_Import_Parser
::DUPLICATE_UPDATE
,
779 CRM_Import_Parser
::DUPLICATE_FILL
,
780 ]) && !empty($params[$key]['id'])) {
781 $relatedContactType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['id'], 'contact_type');
782 if (!$relatedContactType) {
783 $errorMessage = ts("No contact found for this related contact ID: %1", [1 => $params[$key]['id']]);
784 array_unshift($values, $errorMessage);
785 return CRM_Import_Parser
::NO_MATCH
;
788 //validation of related contact subtype for update mode
790 $relatedCsType = NULL;
791 if (!empty($formatting['contact_sub_type'])) {
792 $relatedCsType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['id'], 'contact_sub_type');
795 if (!empty($relatedCsType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($params[$key]['id'], $relatedCsType) &&
796 $relatedCsType != CRM_Utils_Array
::value('contact_sub_type', $formatting))
798 $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact.") . ' ' . ts("ID: %1", [1 => $params[$key]['id']]);
799 array_unshift($values, $errorMessage);
800 return CRM_Import_Parser
::NO_MATCH
;
803 // get related contact id to format data in update/fill mode,
804 //if external identifier is present, CRM-4423
805 $formatting['id'] = $params[$key]['id'];
810 //format common data, CRM-4062
811 $this->formatCommonData($field, $formatting, $contactFields);
813 //do we have enough fields to create related contact.
814 $allowToCreate = $this->checkRelatedContactFields($key, $formatting);
816 if (!$allowToCreate) {
817 $errorMessage = ts('Related contact required fields are missing.');
818 array_unshift($values, $errorMessage);
819 return CRM_Import_Parser
::NO_MATCH
;
823 if (!empty($params[$key]['id'])) {
825 'contact_id' => $params[$key]['id'],
828 $relatedNewContact = CRM_Contact_BAO_Contact
::retrieve($contact, $defaults);
831 $relatedNewContact = $this->createContact($formatting, $contactFields, $onDuplicate, NULL, FALSE);
834 if (is_object($relatedNewContact) ||
($relatedNewContact instanceof CRM_Contact_BAO_Contact
)) {
835 $relatedNewContact = clone($relatedNewContact);
839 // To update/fill contact, get the matching contact Ids if duplicate contact found
840 // otherwise get contact Id from object of related contact
841 if (is_array($relatedNewContact) && civicrm_error($relatedNewContact)) {
842 if (CRM_Core_Error
::isAPIError($relatedNewContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
843 $matchedIDs = $relatedNewContact['error_message']['params'][0];
844 if (!is_array($matchedIDs)) {
845 $matchedIDs = explode(',', $matchedIDs);
849 $errorMessage = $relatedNewContact['error_message'];
850 array_unshift($values, $errorMessage);
851 $importRecordParams = [
852 $statusFieldName => 'ERROR',
853 "${statusFieldName}Msg" => $errorMessage,
855 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
856 return CRM_Import_Parser
::ERROR
;
860 $matchedIDs[] = $relatedNewContact->id
;
862 // update/fill related contact after getting matching Contact Ids, CRM-4424
863 if (in_array($onDuplicate, [
864 CRM_Import_Parser
::DUPLICATE_UPDATE
,
865 CRM_Import_Parser
::DUPLICATE_FILL
,
867 //validation of related contact subtype for update mode
869 $relatedCsType = NULL;
870 if (!empty($formatting['contact_sub_type'])) {
871 $relatedCsType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $matchedIDs[0], 'contact_sub_type');
874 if (!empty($relatedCsType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($matchedIDs[0], $relatedCsType) && $relatedCsType != CRM_Utils_Array
::value('contact_sub_type', $formatting))) {
875 $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact.");
876 array_unshift($values, $errorMessage);
877 return CRM_Import_Parser
::NO_MATCH
;
880 $updatedContact = $this->createContact($formatting, $contactFields, $onDuplicate, $matchedIDs[0]);
883 static $relativeContact = [];
884 if (CRM_Core_Error
::isAPIError($relatedNewContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
885 if (count($matchedIDs) >= 1) {
886 $relContactId = $matchedIDs[0];
887 //add relative contact to count during update & fill mode.
888 //logic to make count distinct by contact id.
889 if ($this->_newRelatedContacts ||
!empty($relativeContact)) {
890 $reContact = array_keys($relativeContact, $relContactId);
892 if (empty($reContact)) {
893 $this->_newRelatedContacts
[] = $relativeContact[] = $relContactId;
897 $this->_newRelatedContacts
[] = $relativeContact[] = $relContactId;
902 $relContactId = $relatedNewContact->id
;
903 $this->_newRelatedContacts
[] = $relativeContact[] = $relContactId;
906 if (CRM_Core_Error
::isAPIError($relatedNewContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
) ||
($relatedNewContact instanceof CRM_Contact_BAO_Contact
)) {
907 //fix for CRM-1993.Checks for duplicate related contacts
908 if (count($matchedIDs) >= 1) {
909 //if more than one duplicate contact
910 //found, create relationship with first contact
911 // now create the relationship record
912 $relationParams = [];
914 'relationship_type_id' => $key,
919 'skipRecentView' => TRUE,
922 // we only handle related contact success, we ignore failures for now
923 // at some point wold be nice to have related counts as separate
925 'contact' => $primaryContactId,
928 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship
::legacyCreateMultiple($relationParams, $relationIds);
930 if ($valid ||
$duplicate) {
931 $relationIds['contactTarget'] = $relContactId;
932 $action = ($duplicate) ? CRM_Core_Action
::UPDATE
: CRM_Core_Action
::ADD
;
933 CRM_Contact_BAO_Relationship
::relatedMemberships($primaryContactId, $relationParams, $relationIds, $action);
936 //handle current employer, CRM-3532
938 $allRelationships = CRM_Core_PseudoConstant
::relationshipType('name');
939 $relationshipTypeId = str_replace([
946 $relationshipType = str_replace($relationshipTypeId . '_', '', $key);
947 $orgId = $individualId = NULL;
948 if ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employee of') {
949 $orgId = $relContactId;
950 $individualId = $primaryContactId;
952 elseif ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employer of') {
953 $orgId = $primaryContactId;
954 $individualId = $relContactId;
956 if ($orgId && $individualId) {
957 $currentEmpParams[$individualId] = $orgId;
958 CRM_Contact_BAO_Contact_Utils
::setCurrentEmployer($currentEmpParams);
966 if ($this->_updateWithId
) {
967 //return warning if street address is unparsed, CRM-5886
968 return $this->processMessage($values, $statusFieldName, $this->_retCode
);
971 if (is_array($newContact) && civicrm_error($newContact)) {
974 if (($code = CRM_Utils_Array
::value('code', $newContact['error_message'])) && ($code == CRM_Core_Error
::DUPLICATE_CONTACT
)) {
976 // need to fix at some stage and decide if the error will return an
977 // array or string, crude hack for now
978 if (is_array($newContact['error_message']['params'][0])) {
979 $cids = $newContact['error_message']['params'][0];
982 $cids = explode(',', $newContact['error_message']['params'][0]);
985 foreach ($cids as $cid) {
986 $urls[] = CRM_Utils_System
::url('civicrm/contact/view', 'reset=1&cid=' . $cid, TRUE);
989 $url_string = implode("\n", $urls);
991 // If we duplicate more than one record, skip no matter what
992 if (count($cids) > 1) {
993 $errorMessage = ts('Record duplicates multiple contacts');
994 $importRecordParams = [
995 $statusFieldName => 'ERROR',
996 "${statusFieldName}Msg" => $errorMessage,
999 //combine error msg to avoid mismatch between error file columns.
1000 $errorMessage .= "\n" . $url_string;
1001 array_unshift($values, $errorMessage);
1002 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
1003 return CRM_Import_Parser
::ERROR
;
1006 // Params only had one id, so shift it out
1007 $contactId = array_shift($cids);
1010 $vals = ['contact_id' => $contactId];
1012 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_REPLACE
) {
1013 civicrm_api('contact', 'delete', $vals);
1014 $cid = CRM_Contact_BAO_Contact
::createProfileContact($formatted, $contactFields, $contactId, NULL, NULL, $formatted['contact_type']);
1016 elseif ($onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
) {
1017 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactId);
1019 elseif ($onDuplicate == CRM_Import_Parser
::DUPLICATE_FILL
) {
1020 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactId);
1022 // else skip does nothing and just returns an error code.
1025 'contact_id' => $cid,
1028 $newContact = CRM_Contact_BAO_Contact
::retrieve($contact, $defaults);
1031 if (civicrm_error($newContact)) {
1032 if (empty($newContact['error_message']['params'])) {
1033 // different kind of error other than DUPLICATE
1034 $errorMessage = $newContact['error_message'];
1035 array_unshift($values, $errorMessage);
1036 $importRecordParams = [
1037 $statusFieldName => 'ERROR',
1038 "${statusFieldName}Msg" => $errorMessage,
1040 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
1041 return CRM_Import_Parser
::ERROR
;
1044 $contactID = $newContact['error_message']['params'][0];
1045 if (is_array($contactID)) {
1046 $contactID = array_pop($contactID);
1048 if (!in_array($contactID, $this->_newContacts
)) {
1049 $this->_newContacts
[] = $contactID;
1052 //CRM-262 No Duplicate Checking
1053 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_SKIP
) {
1054 array_unshift($values, $url_string);
1055 $importRecordParams = [
1056 $statusFieldName => 'DUPLICATE',
1057 "${statusFieldName}Msg" => "Skipping duplicate record",
1059 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
1060 return CRM_Import_Parser
::DUPLICATE
;
1063 $importRecordParams = [
1064 $statusFieldName => 'IMPORTED',
1066 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
1067 //return warning if street address is not parsed, CRM-5886
1068 return $this->processMessage($values, $statusFieldName, CRM_Import_Parser
::VALID
);
1071 // Not a dupe, so we had an error
1072 $errorMessage = $newContact['error_message'];
1073 array_unshift($values, $errorMessage);
1074 $importRecordParams = [
1075 $statusFieldName => 'ERROR',
1076 "${statusFieldName}Msg" => $errorMessage,
1078 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
1079 return CRM_Import_Parser
::ERROR
;
1083 return $this->processMessage($values, $statusFieldName, CRM_Import_Parser
::VALID
);
1087 * Get the array of successfully imported contact id's
1091 public function &getImportedContacts() {
1092 return $this->_newContacts
;
1096 * Get the array of successfully imported related contact id's
1100 public function &getRelatedImportedContacts() {
1101 return $this->_newRelatedContacts
;
1105 * The initializer code, called before the processing.
1107 public function fini() {
1111 * Check if an error in custom data.
1113 * @param array $params
1114 * @param string $errorMessage
1115 * A string containing all the error-fields.
1117 * @param null $csType
1118 * @param null $relationships
1120 public static function isErrorInCustomData($params, &$errorMessage, $csType = NULL, $relationships = NULL) {
1121 $dateType = CRM_Core_Session
::singleton()->get("dateTypes");
1123 if (!empty($params['contact_sub_type'])) {
1124 $csType = $params['contact_sub_type'] ??
NULL;
1127 if (empty($params['contact_type'])) {
1128 $params['contact_type'] = 'Individual';
1131 // get array of subtypes - CRM-18708
1132 if (in_array($csType, ['Individual', 'Organization', 'Household'])) {
1133 $csType = self
::getSubtypes($params['contact_type']);
1136 if (is_array($csType)) {
1137 // fetch custom fields for every subtype and add it to $customFields array
1140 foreach ($csType as $cType) {
1141 $customFields +
= CRM_Core_BAO_CustomField
::getFields($params['contact_type'], FALSE, FALSE, $cType);
1145 $customFields = CRM_Core_BAO_CustomField
::getFields($params['contact_type'], FALSE, FALSE, $csType);
1148 $addressCustomFields = CRM_Core_BAO_CustomField
::getFields('Address');
1149 $customFields = $customFields +
$addressCustomFields;
1150 foreach ($params as $key => $value) {
1151 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1152 /* check if it's a valid custom field id */
1154 if (!array_key_exists($customFieldID, $customFields)) {
1155 self
::addToErrorMsg(ts('field ID'), $errorMessage);
1157 // validate null values for required custom fields of type boolean
1158 if (!empty($customFields[$customFieldID]['is_required']) && (empty($params['custom_' . $customFieldID]) && !is_numeric($params['custom_' . $customFieldID])) && $customFields[$customFieldID]['data_type'] == 'Boolean') {
1159 self
::addToErrorMsg($customFields[$customFieldID]['label'] . '::' . $customFields[$customFieldID]['groupTitle'], $errorMessage);
1162 //For address custom fields, we do get actual custom field value as an inner array of
1163 //values so need to modify
1164 if (array_key_exists($customFieldID, $addressCustomFields)) {
1165 $value = $value[0][$key];
1167 /* validate the data against the CF type */
1170 if ($customFields[$customFieldID]['data_type'] == 'Date') {
1171 if (array_key_exists($customFieldID, $addressCustomFields) && CRM_Utils_Date
::convertToDefaultDate($params[$key][0], $dateType, $key)) {
1172 $value = $params[$key][0][$key];
1174 elseif (CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key)) {
1175 $value = $params[$key];
1178 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1181 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
1182 if (CRM_Utils_String
::strtoboolstr($value) === FALSE) {
1183 self
::addToErrorMsg($customFields[$customFieldID]['label'] . '::' . $customFields[$customFieldID]['groupTitle'], $errorMessage);
1186 // need not check for label filed import
1192 'Multi-Select State/Province',
1193 'Multi-Select Country',
1195 if (!in_array($customFields[$customFieldID]['html_type'], $htmlType) ||
$customFields[$customFieldID]['data_type'] == 'Boolean' ||
$customFields[$customFieldID]['data_type'] == 'ContactReference') {
1196 $valid = CRM_Core_BAO_CustomValue
::typecheck($customFields[$customFieldID]['data_type'], $value);
1198 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1202 // check for values for custom fields for checkboxes and multiselect
1203 if ($customFields[$customFieldID]['html_type'] == 'CheckBox' ||
$customFields[$customFieldID]['html_type'] == 'Multi-Select') {
1204 $value = trim($value);
1205 $value = str_replace('|', ',', $value);
1206 $mulValues = explode(',', $value);
1207 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1208 foreach ($mulValues as $v1) {
1209 if (strlen($v1) == 0) {
1214 foreach ($customOption as $v2) {
1215 if ((strtolower(trim($v2['label'])) == strtolower(trim($v1))) ||
(strtolower(trim($v2['value'])) == strtolower(trim($v1)))) {
1221 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1225 elseif ($customFields[$customFieldID]['html_type'] == 'Select' ||
($customFields[$customFieldID]['html_type'] == 'Radio' && $customFields[$customFieldID]['data_type'] != 'Boolean')) {
1226 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1228 foreach ($customOption as $v2) {
1229 if ((strtolower(trim($v2['label'])) == strtolower(trim($value))) ||
(strtolower(trim($v2['value'])) == strtolower(trim($value)))) {
1234 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1237 elseif ($customFields[$customFieldID]['html_type'] == 'Multi-Select State/Province') {
1238 $mulValues = explode(',', $value);
1239 foreach ($mulValues as $stateValue) {
1241 if (self
::in_value(trim($stateValue), CRM_Core_PseudoConstant
::stateProvinceAbbreviation()) || self
::in_value(trim($stateValue), CRM_Core_PseudoConstant
::stateProvince())) {
1245 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1250 elseif ($customFields[$customFieldID]['html_type'] == 'Multi-Select Country') {
1251 $mulValues = explode(',', $value);
1252 foreach ($mulValues as $countryValue) {
1253 if ($countryValue) {
1254 CRM_Core_PseudoConstant
::populate($countryNames, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active');
1255 CRM_Core_PseudoConstant
::populate($countryIsoCodes, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
1256 $limitCodes = CRM_Core_BAO_Country
::countryLimit();
1264 if (in_array(trim($countryValue), $values)) {
1271 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1278 elseif (is_array($params[$key]) && isset($params[$key]["contact_type"])) {
1280 //supporting custom data of related contact subtypes
1282 if ($relationships) {
1283 if (array_key_exists($key, $relationships)) {
1286 elseif (CRM_Utils_Array
::key($key, $relationships)) {
1287 $relation = CRM_Utils_Array
::key($key, $relationships);
1290 if (!empty($relation)) {
1291 list($id, $first, $second) = CRM_Utils_System
::explode('_', $relation, 3);
1292 $direction = "contact_sub_type_$second";
1293 $relationshipType = new CRM_Contact_BAO_RelationshipType();
1294 $relationshipType->id
= $id;
1295 if ($relationshipType->find(TRUE)) {
1296 if (isset($relationshipType->$direction)) {
1297 $params[$key]['contact_sub_type'] = $relationshipType->$direction;
1302 self
::isErrorInCustomData($params[$key], $errorMessage, $csType, $relationships);
1308 * Check if value present in all genders or.
1309 * as a substring of any gender value, if yes than return corresponding gender.
1310 * eg value might be m/M, ma/MA, mal/MAL, male return 'Male'
1311 * but if value is 'maleabc' than return false
1313 * @param string $gender
1314 * Check this value across gender values.
1316 * retunr gender value / false
1320 public function checkGender($gender) {
1321 $gender = trim($gender, '.');
1326 $allGenders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
1327 foreach ($allGenders as $key => $value) {
1328 if (strlen($gender) > strlen($value)) {
1331 if ($gender == $value) {
1334 if (substr_compare($value, $gender, 0, strlen($gender), TRUE) === 0) {
1343 * Check if an error in Core( non-custom fields ) field
1345 * @param array $params
1346 * @param string $errorMessage
1347 * A string containing all the error-fields.
1349 public function isErrorInCoreData($params, &$errorMessage) {
1350 foreach ($params as $key => $value) {
1352 $session = CRM_Core_Session
::singleton();
1353 $dateType = $session->get("dateTypes");
1357 if (CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key)) {
1358 if (!CRM_Utils_Rule
::date($params[$key])) {
1359 self
::addToErrorMsg(ts('Birth Date'), $errorMessage);
1363 self
::addToErrorMsg(ts('Birth-Date'), $errorMessage);
1367 case 'deceased_date':
1368 if (CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key)) {
1369 if (!CRM_Utils_Rule
::date($params[$key])) {
1370 self
::addToErrorMsg(ts('Deceased Date'), $errorMessage);
1374 self
::addToErrorMsg(ts('Deceased Date'), $errorMessage);
1379 if (CRM_Utils_String
::strtoboolstr($value) === FALSE) {
1380 self
::addToErrorMsg(ts('Deceased'), $errorMessage);
1385 if (!self
::checkGender($value)) {
1386 self
::addToErrorMsg(ts('Gender'), $errorMessage);
1390 case 'preferred_communication_method':
1392 $preffComm = explode(',', $value);
1393 foreach ($preffComm as $v) {
1394 if (!self
::in_value(trim($v), CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method'))) {
1395 self
::addToErrorMsg(ts('Preferred Communication Method'), $errorMessage);
1400 case 'preferred_mail_format':
1401 if (!array_key_exists(strtolower($value), array_change_key_case(CRM_Core_SelectValues
::pmf(), CASE_LOWER
))) {
1402 self
::addToErrorMsg(ts('Preferred Mail Format'), $errorMessage);
1406 case 'individual_prefix':
1408 if (!self
::in_value($value, CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id'))) {
1409 self
::addToErrorMsg(ts('Individual Prefix'), $errorMessage);
1413 case 'individual_suffix':
1415 if (!self
::in_value($value, CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id'))) {
1416 self
::addToErrorMsg(ts('Individual Suffix'), $errorMessage);
1420 case 'state_province':
1421 if (!empty($value)) {
1422 foreach ($value as $stateValue) {
1423 if ($stateValue['state_province']) {
1424 if (self
::in_value($stateValue['state_province'], CRM_Core_PseudoConstant
::stateProvinceAbbreviation()) ||
1425 self
::in_value($stateValue['state_province'], CRM_Core_PseudoConstant
::stateProvince())
1430 self
::addToErrorMsg(ts('State/Province'), $errorMessage);
1438 if (!empty($value)) {
1439 foreach ($value as $stateValue) {
1440 if ($stateValue['country']) {
1441 CRM_Core_PseudoConstant
::populate($countryNames, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active');
1442 CRM_Core_PseudoConstant
::populate($countryIsoCodes, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
1443 $limitCodes = CRM_Core_BAO_Country
::countryLimit();
1444 //If no country is selected in
1445 //localization then take all countries
1446 if (empty($limitCodes)) {
1447 $limitCodes = $countryIsoCodes;
1450 if (self
::in_value($stateValue['country'], $limitCodes) || self
::in_value($stateValue['country'], CRM_Core_PseudoConstant
::country())) {
1454 if (self
::in_value($stateValue['country'], $countryIsoCodes) || self
::in_value($stateValue['country'], $countryNames)) {
1455 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);
1458 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);
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 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);
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'])) {
1487 self
::addToErrorMsg(ts('Geo code 1'), $errorMessage);
1495 if (!empty($value)) {
1496 foreach ($value as $codeValue) {
1497 if (!empty($codeValue['geo_code_2'])) {
1498 if (CRM_Utils_Rule
::numeric($codeValue['geo_code_2'])) {
1502 self
::addToErrorMsg(ts('Geo code 2'), $errorMessage);
1509 //check for any error in email/postal greeting, addressee,
1510 //custom email/postal greeting, custom addressee, CRM-4575
1512 case 'email_greeting':
1513 $emailGreetingFilter = [
1514 'contact_type' => $this->_contactType
,
1515 'greeting_type' => 'email_greeting',
1517 if (!self
::in_value($value, CRM_Core_PseudoConstant
::greeting($emailGreetingFilter))) {
1518 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);
1522 case 'postal_greeting':
1523 $postalGreetingFilter = [
1524 'contact_type' => $this->_contactType
,
1525 'greeting_type' => 'postal_greeting',
1527 if (!self
::in_value($value, CRM_Core_PseudoConstant
::greeting($postalGreetingFilter))) {
1528 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);
1533 $addresseeFilter = [
1534 'contact_type' => $this->_contactType
,
1535 'greeting_type' => 'addressee',
1537 if (!self
::in_value($value, CRM_Core_PseudoConstant
::greeting($addresseeFilter))) {
1538 self
::addToErrorMsg(ts('Addressee must be one of the configured format options. Check Administer >> System Settings >> Option Groups >> Addressee for valid values'), $errorMessage);
1542 case 'email_greeting_custom':
1543 if (array_key_exists('email_greeting', $params)) {
1544 $emailGreetingLabel = key(CRM_Core_OptionGroup
::values('email_greeting', TRUE, NULL, NULL, 'AND v.name = "Customized"'));
1545 if (CRM_Utils_Array
::value('email_greeting', $params) != $emailGreetingLabel) {
1546 self
::addToErrorMsg(ts('Email Greeting - Custom'), $errorMessage);
1551 case 'postal_greeting_custom':
1552 if (array_key_exists('postal_greeting', $params)) {
1553 $postalGreetingLabel = key(CRM_Core_OptionGroup
::values('postal_greeting', TRUE, NULL, NULL, 'AND v.name = "Customized"'));
1554 if (CRM_Utils_Array
::value('postal_greeting', $params) != $postalGreetingLabel) {
1555 self
::addToErrorMsg(ts('Postal Greeting - Custom'), $errorMessage);
1560 case 'addressee_custom':
1561 if (array_key_exists('addressee', $params)) {
1562 $addresseeLabel = key(CRM_Core_OptionGroup
::values('addressee', TRUE, NULL, NULL, 'AND v.name = "Customized"'));
1563 if (CRM_Utils_Array
::value('addressee', $params) != $addresseeLabel) {
1564 self
::addToErrorMsg(ts('Addressee - Custom'), $errorMessage);
1570 if (is_array($value)) {
1571 foreach ($value as $values) {
1572 if (!empty($values['url']) && !CRM_Utils_Rule
::url($values['url'])) {
1573 self
::addToErrorMsg(ts('Website'), $errorMessage);
1580 case 'do_not_email':
1581 case 'do_not_phone':
1584 case 'do_not_trade':
1585 if (CRM_Utils_Rule
::boolean($value) == FALSE) {
1586 $key = ucwords(str_replace("_", " ", $key));
1587 self
::addToErrorMsg($key, $errorMessage);
1592 if (is_array($value)) {
1593 foreach ($value as $values) {
1594 if (!empty($values['email']) && !CRM_Utils_Rule
::email($values['email'])) {
1595 self
::addToErrorMsg($key, $errorMessage);
1603 if (is_array($params[$key]) && isset($params[$key]["contact_type"])) {
1604 //check for any relationship data ,FIX ME
1605 self
::isErrorInCoreData($params[$key], $errorMessage);
1613 * Ckeck a value present or not in a array.
1616 * @param $valueArray
1620 public static function in_value($value, $valueArray) {
1621 foreach ($valueArray as $key => $v) {
1623 if (strtolower(trim($v, ".")) == strtolower(trim($value, "."))) {
1631 * Build error-message containing error-fields
1633 * Once upon a time there was a dev who hadn't heard of implode. That dev wrote this function.
1635 * @todo just say no!
1637 * @param string $errorName
1638 * A string containing error-field name.
1639 * @param string $errorMessage
1640 * A string containing all the error-fields, where the new errorName is concatenated.
1643 public static function addToErrorMsg($errorName, &$errorMessage) {
1644 if ($errorMessage) {
1645 $errorMessage .= "; $errorName";
1648 $errorMessage = $errorName;
1653 * Method for creating contact.
1655 * @param array $formatted
1656 * @param array $contactFields
1657 * @param int $onDuplicate
1658 * @param int $contactId
1659 * @param bool $requiredCheck
1660 * @param int $dedupeRuleGroupID
1662 * @return array|bool|\CRM_Contact_BAO_Contact|\CRM_Core_Error|null
1664 public function createContact(&$formatted, &$contactFields, $onDuplicate, $contactId = NULL, $requiredCheck = TRUE, $dedupeRuleGroupID = NULL) {
1668 if (is_null($contactId) && ($onDuplicate != CRM_Import_Parser
::DUPLICATE_NOCHECK
)) {
1669 $dupeCheck = (bool) ($onDuplicate);
1672 //get the prefix id etc if exists
1673 CRM_Contact_BAO_Contact
::resolveDefaults($formatted, TRUE);
1675 //@todo direct call to API function not supported.
1676 // setting required check to false, CRM-2839
1677 // plus we do our own required check in import
1678 $error = _civicrm_api3_deprecated_contact_check_params($formatted, $dupeCheck, $dedupeRuleGroupID);
1680 if ((is_null($error)) && (civicrm_error(_civicrm_api3_deprecated_validate_formatted_contact($formatted)))) {
1681 $error = _civicrm_api3_deprecated_validate_formatted_contact($formatted);
1684 $newContact = $error;
1686 if (is_null($error)) {
1688 $this->formatParams($formatted, $onDuplicate, (int) $contactId);
1691 // Resetting and rebuilding cache could be expensive.
1692 CRM_Core_Config
::setPermitCacheFlushMode(FALSE);
1693 $cid = CRM_Contact_BAO_Contact
::createProfileContact($formatted, $contactFields, $contactId, NULL, NULL, $formatted['contact_type']);
1694 CRM_Core_Config
::setPermitCacheFlushMode(TRUE);
1697 'contact_id' => $cid,
1701 $newContact = CRM_Contact_BAO_Contact
::retrieve($contact, $defaults);
1704 //get the id of the contact whose street address is not parsable, CRM-5886
1705 if ($this->_parseStreetAddress
&& is_object($newContact) && property_exists($newContact, 'address') && $newContact->address
) {
1706 foreach ($newContact->address
as $address) {
1707 if (!empty($address['street_address']) && (empty($address['street_number']) ||
empty($address['street_name']))) {
1708 $this->_unparsedStreetAddressContacts
[] = [
1709 'id' => $newContact->id
,
1710 'streetAddress' => $address['street_address'],
1719 * Format params for update and fill mode.
1721 * @param array $params
1722 * reference to an array containing all the.
1724 * @param int $onDuplicate
1728 public function formatParams(&$params, $onDuplicate, $cid) {
1729 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_SKIP
) {
1734 'contact_id' => $cid,
1738 $contactObj = CRM_Contact_BAO_Contact
::retrieve($contactParams, $defaults);
1740 $modeUpdate = $modeFill = FALSE;
1742 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
) {
1746 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_FILL
) {
1750 $groupTree = CRM_Core_BAO_CustomGroup
::getTree($params['contact_type'], NULL, $cid, 0, NULL);
1751 CRM_Core_BAO_CustomGroup
::setDefaults($groupTree, $defaults, FALSE, FALSE);
1757 'website' => 'website',
1758 'address' => 'address',
1761 $contact = get_object_vars($contactObj);
1763 foreach ($params as $key => $value) {
1764 if ($key == 'id' ||
$key == 'contact_type') {
1768 if (array_key_exists($key, $locationFields)) {
1771 elseif (in_array($key, [
1776 // CRM-4575, need to null custom
1777 if ($params["{$key}_id"] != 4) {
1778 $params["{$key}_custom"] = 'null';
1780 unset($params[$key]);
1783 if ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1784 $custom_params = ['id' => $contact['id'], 'return' => $key];
1785 $getValue = civicrm_api3('Contact', 'getvalue', $custom_params);
1786 if (empty($getValue)) {
1791 $getValue = CRM_Utils_Array
::retrieveValueRecursive($contact, $key);
1793 if ($key == 'contact_source') {
1794 $params['source'] = $params[$key];
1795 unset($params[$key]);
1798 if ($modeFill && isset($getValue)) {
1799 unset($params[$key]);
1800 if ($customFieldId) {
1801 // Extra values must be unset to ensure the values are not
1803 unset($params['custom'][$customFieldId]);
1809 foreach ($locationFields as $locKeys) {
1810 if (isset($params[$locKeys]) && is_array($params[$locKeys])) {
1811 foreach ($params[$locKeys] as $key => $value) {
1813 $getValue = CRM_Utils_Array
::retrieveValueRecursive($contact, $locKeys);
1815 if (isset($getValue)) {
1816 foreach ($getValue as $cnt => $values) {
1817 if ($locKeys == 'website') {
1818 if (($getValue[$cnt]['website_type_id'] == $params[$locKeys][$key]['website_type_id'])) {
1819 unset($params[$locKeys][$key]);
1823 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']) {
1824 unset($params[$locKeys][$key]);
1831 if (count($params[$locKeys]) == 0) {
1832 unset($params[$locKeys]);
1839 * Convert any given date string to default date array.
1841 * @param array $params
1842 * Has given date-format.
1843 * @param array $formatted
1844 * Store formatted date in this array.
1845 * @param int $dateType
1847 * @param string $dateParam
1850 public static function formatCustomDate(&$params, &$formatted, $dateType, $dateParam) {
1852 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $dateParam);
1853 $formatted[$dateParam] = CRM_Utils_Date
::processDate($params[$dateParam]);
1857 * Generate status and error message for unparsed street address records.
1859 * @param array $values
1860 * The array of values belonging to each row.
1861 * @param array $statusFieldName
1862 * Store formatted date in this array.
1863 * @param $returnCode
1867 public function processMessage(&$values, $statusFieldName, $returnCode) {
1868 if (empty($this->_unparsedStreetAddressContacts
)) {
1869 $importRecordParams = [
1870 $statusFieldName => 'IMPORTED',
1874 $errorMessage = ts("Record imported successfully but unable to parse the street address: ");
1875 foreach ($this->_unparsedStreetAddressContacts
as $contactInfo => $contactValue) {
1876 $contactUrl = CRM_Utils_System
::url('civicrm/contact/add', 'reset=1&action=update&cid=' . $contactValue['id'], TRUE, NULL, FALSE);
1877 $errorMessage .= "\n Contact ID:" . $contactValue['id'] . " <a href=\"$contactUrl\"> " . $contactValue['streetAddress'] . "</a>";
1879 array_unshift($values, $errorMessage);
1880 $importRecordParams = [
1881 $statusFieldName => 'ERROR',
1882 "${statusFieldName}Msg" => $errorMessage,
1884 $returnCode = CRM_Import_Parser
::UNPARSED_ADDRESS_WARNING
;
1886 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
1892 * @param array $params
1896 public function checkRelatedContactFields($relKey, $params) {
1897 //avoid blank contact creation.
1898 $allowToCreate = FALSE;
1900 //build the mapper field array.
1901 static $relatedContactFields = [];
1902 if (!isset($relatedContactFields[$relKey])) {
1903 foreach ($this->_mapperRelated
as $key => $name) {
1908 if (!empty($relatedContactFields[$name]) && !is_array($relatedContactFields[$name])) {
1909 $relatedContactFields[$name] = [];
1911 $fldName = $this->_mapperRelatedContactDetails
[$key] ??
NULL;
1912 if ($fldName == 'url') {
1913 $fldName = 'website';
1916 $relatedContactFields[$name][] = $fldName;
1921 //validate for passed data.
1922 if (is_array($relatedContactFields[$relKey])) {
1923 foreach ($relatedContactFields[$relKey] as $fld) {
1924 if (!empty($params[$fld])) {
1925 $allowToCreate = TRUE;
1931 return $allowToCreate;
1935 * get subtypes given the contact type
1937 * @param string $contactType
1938 * @return array $subTypes
1940 public static function getSubtypes($contactType) {
1942 $types = CRM_Contact_BAO_ContactType
::subTypeInfo($contactType);
1944 if (count($types) > 0) {
1945 foreach ($types as $type) {
1946 $subTypes[] = $type['name'];
1953 * Get the possible contact matches.
1955 * 1) the chosen dedupe rule falling back to
1956 * 2) a check for the external ID.
1960 * @param array $params
1963 * IDs of possible matches.
1965 * @throws \CRM_Core_Exception
1966 * @throws \CiviCRM_API3_Exception
1968 protected function getPossibleContactMatches($params) {
1971 if (!empty($params['external_identifier'])) {
1972 // Check for any match on external id, deleted or otherwise.
1973 $extIDContact = civicrm_api3('Contact', 'get', [
1974 'external_identifier' => $params['external_identifier'],
1976 'return' => ['id', 'contact_is_deleted'],
1978 if (isset($extIDContact['id'])) {
1979 $extIDMatch = $extIDContact['id'];
1981 if ($extIDContact['values'][$extIDMatch]['contact_is_deleted'] == 1) {
1982 // If the contact is deleted, update external identifier to be blank
1983 // to avoid key error from MySQL.
1984 $params = ['id' => $extIDMatch, 'external_identifier' => ''];
1985 civicrm_api3('Contact', 'create', $params);
1987 // And now it is no longer a match.
1992 $checkParams = ['check_permissions' => FALSE, 'match' => $params];
1993 $checkParams['match']['contact_type'] = $this->_contactType
;
1995 $possibleMatches = civicrm_api3('Contact', 'duplicatecheck', $checkParams);
1997 return array_keys($possibleMatches['values']);
1999 if ($possibleMatches['count']) {
2000 if (in_array($extIDMatch, array_keys($possibleMatches['values']))) {
2001 return [$extIDMatch];
2004 throw new CRM_Core_Exception(ts(
2005 'Matching this contact based on the de-dupe rule would cause an external ID conflict'));
2008 return [$extIDMatch];
2012 * Format the form mapping parameters ready for the parser.
2017 * @return array $parserParameters
2019 public static function getParameterForParser($count) {
2021 for ($i = 0; $i < $count; $i++
) {
2022 $baseArray[$i] = NULL;
2024 $parserParameters['mapperLocType'] = $baseArray;
2025 $parserParameters['mapperPhoneType'] = $baseArray;
2026 $parserParameters['mapperImProvider'] = $baseArray;
2027 $parserParameters['mapperWebsiteType'] = $baseArray;
2028 $parserParameters['mapperRelated'] = $baseArray;
2029 $parserParameters['relatedContactType'] = $baseArray;
2030 $parserParameters['relatedContactDetails'] = $baseArray;
2031 $parserParameters['relatedContactLocType'] = $baseArray;
2032 $parserParameters['relatedContactPhoneType'] = $baseArray;
2033 $parserParameters['relatedContactImProvider'] = $baseArray;
2034 $parserParameters['relatedContactWebsiteType'] = $baseArray;
2036 return $parserParameters;
2041 * Set field metadata.
2043 protected function setFieldMetadata() {
2044 $this->setImportableFieldsMetadata($this->getContactImportMetadata());
2045 // Probably no longer needed but here for now.
2046 $this->_relationships
= $this->getRelationships();