3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
27 require_once 'CRM/Utils/DeprecatedUtils.php';
28 require_once 'api/v3/utils.php';
33 * @copyright CiviCRM LLC (c) 2004-2019
37 * class to parse contact csv files
39 class CRM_Contact_Import_Parser_Contact
extends CRM_Contact_Import_Parser
{
41 use CRM_Contact_Import_MetadataTrait
;
43 protected $_mapperKeys = [];
44 protected $_mapperLocType = [];
45 protected $_mapperPhoneType;
46 protected $_mapperImProvider;
47 protected $_mapperWebsiteType;
48 protected $_mapperRelated;
49 protected $_mapperRelatedContactType;
50 protected $_mapperRelatedContactDetails;
51 protected $_mapperRelatedContactEmailType;
52 protected $_mapperRelatedContactImProvider;
53 protected $_mapperRelatedContactWebsiteType;
54 protected $_relationships;
56 protected $_emailIndex;
57 protected $_firstNameIndex;
58 protected $_lastNameIndex;
60 protected $_householdNameIndex;
61 protected $_organizationNameIndex;
63 protected $_allEmails;
65 protected $_phoneIndex;
68 * Is update only permitted on an id match.
70 * Note this historically was true for when id or external identifier was
71 * present. However, CRM-17275 determined that a dedupe-match could over-ride
72 * external identifier.
76 protected $_updateWithId;
79 protected $_externalIdentifierIndex;
80 protected $_allExternalIdentifiers;
81 protected $_parseStreetAddress;
84 * Array of successfully imported contact id's
88 protected $_newContacts;
95 protected $_lineCount;
98 * Array of successfully imported related contact id's
102 protected $_newRelatedContacts;
105 * Array of all the contacts whose street addresses are not parsed.
106 * of this import process
109 protected $_unparsedStreetAddressContacts;
114 * @param array $mapperKeys
115 * @param array $mapperLocType
116 * @param array $mapperPhoneType
117 * @param array $mapperImProvider
118 * @param array $mapperRelated
119 * @param array $mapperRelatedContactType
120 * @param array $mapperRelatedContactDetails
121 * @param array $mapperRelatedContactLocType
122 * @param array $mapperRelatedContactPhoneType
123 * @param array $mapperRelatedContactImProvider
124 * @param array $mapperWebsiteType
125 * @param array $mapperRelatedContactWebsiteType
127 public function __construct(
128 $mapperKeys, $mapperLocType = [], $mapperPhoneType = [], $mapperImProvider = [], $mapperRelated = [], $mapperRelatedContactType = [], $mapperRelatedContactDetails = [], $mapperRelatedContactLocType = [], $mapperRelatedContactPhoneType = [], $mapperRelatedContactImProvider = [],
129 $mapperWebsiteType = [], $mapperRelatedContactWebsiteType = []
131 parent
::__construct();
132 $this->_mapperKeys
= $mapperKeys;
133 $this->_mapperLocType
= &$mapperLocType;
134 $this->_mapperPhoneType
= &$mapperPhoneType;
135 $this->_mapperWebsiteType
= $mapperWebsiteType;
136 // get IM service provider type id for contact
137 $this->_mapperImProvider
= &$mapperImProvider;
138 $this->_mapperRelated
= &$mapperRelated;
139 $this->_mapperRelatedContactType
= &$mapperRelatedContactType;
140 $this->_mapperRelatedContactDetails
= &$mapperRelatedContactDetails;
141 $this->_mapperRelatedContactLocType
= &$mapperRelatedContactLocType;
142 $this->_mapperRelatedContactPhoneType
= &$mapperRelatedContactPhoneType;
143 $this->_mapperRelatedContactWebsiteType
= $mapperRelatedContactWebsiteType;
144 // get IM service provider type id for related contact
145 $this->_mapperRelatedContactImProvider
= &$mapperRelatedContactImProvider;
149 * The initializer code, called before processing.
151 public function init() {
152 $this->setFieldMetadata();
153 foreach ($this->getImportableFieldsMetadata() as $name => $field) {
154 $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));
157 $this->_newContacts
= [];
159 $this->setActiveFields($this->_mapperKeys
);
160 $this->setActiveFieldLocationTypes($this->_mapperLocType
);
161 $this->setActiveFieldPhoneTypes($this->_mapperPhoneType
);
162 $this->setActiveFieldWebsiteTypes($this->_mapperWebsiteType
);
163 //set active fields of IM provider of contact
164 $this->setActiveFieldImProviders($this->_mapperImProvider
);
167 $this->setActiveFieldRelated($this->_mapperRelated
);
168 $this->setActiveFieldRelatedContactType($this->_mapperRelatedContactType
);
169 $this->setActiveFieldRelatedContactDetails($this->_mapperRelatedContactDetails
);
170 $this->setActiveFieldRelatedContactLocType($this->_mapperRelatedContactLocType
);
171 $this->setActiveFieldRelatedContactPhoneType($this->_mapperRelatedContactPhoneType
);
172 $this->setActiveFieldRelatedContactWebsiteType($this->_mapperRelatedContactWebsiteType
);
173 //set active fields of IM provider of related contact
174 $this->setActiveFieldRelatedContactImProvider($this->_mapperRelatedContactImProvider
);
176 $this->_phoneIndex
= -1;
177 $this->_emailIndex
= -1;
178 $this->_firstNameIndex
= -1;
179 $this->_lastNameIndex
= -1;
180 $this->_householdNameIndex
= -1;
181 $this->_organizationNameIndex
= -1;
182 $this->_externalIdentifierIndex
= -1;
185 foreach ($this->_mapperKeys
as $key) {
186 if (substr($key, 0, 5) == 'email' && substr($key, 0, 14) != 'email_greeting') {
187 $this->_emailIndex
= $index;
188 $this->_allEmails
= [];
190 if (substr($key, 0, 5) == 'phone') {
191 $this->_phoneIndex
= $index;
193 if ($key == 'first_name') {
194 $this->_firstNameIndex
= $index;
196 if ($key == 'last_name') {
197 $this->_lastNameIndex
= $index;
199 if ($key == 'household_name') {
200 $this->_householdNameIndex
= $index;
202 if ($key == 'organization_name') {
203 $this->_organizationNameIndex
= $index;
206 if ($key == 'external_identifier') {
207 $this->_externalIdentifierIndex
= $index;
208 $this->_allExternalIdentifiers
= [];
213 $this->_updateWithId
= FALSE;
214 if (in_array('id', $this->_mapperKeys
) ||
($this->_externalIdentifierIndex
>= 0 && in_array($this->_onDuplicate
, [
215 CRM_Import_Parser
::DUPLICATE_UPDATE
,
216 CRM_Import_Parser
::DUPLICATE_FILL
,
218 $this->_updateWithId
= TRUE;
221 $this->_parseStreetAddress
= CRM_Utils_Array
::value('street_address_parsing', CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'address_options'), FALSE);
225 * Handle the values in mapField mode.
227 * @param array $values
228 * The array of values belonging to this line.
232 public function mapField(&$values) {
233 return CRM_Import_Parser
::VALID
;
237 * Handle the values in preview mode.
239 * @param array $values
240 * The array of values belonging to this line.
243 * the result of this processing
245 public function preview(&$values) {
246 return $this->summary($values);
250 * Handle the values in summary mode.
252 * @param array $values
253 * The array of values belonging to this line.
256 * the result of this processing
258 public function summary(&$values) {
259 $erroneousField = NULL;
260 $response = $this->setActiveFieldValues($values, $erroneousField);
262 $errorMessage = NULL;
263 $errorRequired = FALSE;
264 switch ($this->_contactType
) {
267 if ($this->_firstNameIndex
< 0 ||
empty($values[$this->_firstNameIndex
])) {
268 $errorRequired = TRUE;
269 $missingNames[] = ts('First Name');
271 if ($this->_lastNameIndex
< 0 ||
empty($values[$this->_lastNameIndex
])) {
272 $errorRequired = TRUE;
273 $missingNames[] = ts('Last Name');
275 if ($errorRequired) {
276 $and = ' ' . ts('and') . ' ';
277 $errorMessage = ts('Missing required fields:') . ' ' . implode($and, $missingNames);
282 if ($this->_householdNameIndex
< 0 ||
empty($values[$this->_householdNameIndex
])) {
283 $errorRequired = TRUE;
284 $errorMessage = ts('Missing required fields:') . ' ' . ts('Household Name');
289 if ($this->_organizationNameIndex
< 0 ||
empty($values[$this->_organizationNameIndex
])) {
290 $errorRequired = TRUE;
291 $errorMessage = ts('Missing required fields:') . ' ' . ts('Organization Name');
296 $statusFieldName = $this->_statusFieldName
;
298 if ($this->_emailIndex
>= 0) {
299 /* If we don't have the required fields, bail */
301 if ($this->_contactType
== 'Individual' && !$this->_updateWithId
) {
302 if ($errorRequired && empty($values[$this->_emailIndex
])) {
304 $errorMessage .= ' ' . ts('OR') . ' ' . ts('Email Address');
307 $errorMessage = ts('Missing required field:') . ' ' . ts('Email Address');
309 array_unshift($values, $errorMessage);
310 $importRecordParams = [
311 $statusFieldName => 'ERROR',
312 "${statusFieldName}Msg" => $errorMessage,
314 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
316 return CRM_Import_Parser
::ERROR
;
320 $email = CRM_Utils_Array
::value($this->_emailIndex
, $values);
322 /* If the email address isn't valid, bail */
324 if (!CRM_Utils_Rule
::email($email)) {
325 $errorMessage = ts('Invalid Email address');
326 array_unshift($values, $errorMessage);
327 $importRecordParams = [
328 $statusFieldName => 'ERROR',
329 "${statusFieldName}Msg" => $errorMessage,
331 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
333 return CRM_Import_Parser
::ERROR
;
336 /* otherwise, count it and move on */
337 $this->_allEmails
[$email] = $this->_lineCount
;
340 elseif ($errorRequired && !$this->_updateWithId
) {
342 $errorMessage .= ' ' . ts('OR') . ' ' . ts('Email Address');
345 $errorMessage = ts('Missing required field:') . ' ' . ts('Email Address');
347 array_unshift($values, $errorMessage);
348 $importRecordParams = [
349 $statusFieldName => 'ERROR',
350 "${statusFieldName}Msg" => $errorMessage,
352 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
354 return CRM_Import_Parser
::ERROR
;
357 //check for duplicate external Identifier
358 $externalID = CRM_Utils_Array
::value($this->_externalIdentifierIndex
, $values);
360 /* If it's a dupe,external Identifier */
362 if ($externalDupe = CRM_Utils_Array
::value($externalID, $this->_allExternalIdentifiers
)) {
363 $errorMessage = ts('External ID conflicts with record %1', [1 => $externalDupe]);
364 array_unshift($values, $errorMessage);
365 $importRecordParams = [
366 $statusFieldName => 'ERROR',
367 "${statusFieldName}Msg" => $errorMessage,
369 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
370 return CRM_Import_Parser
::ERROR
;
372 //otherwise, count it and move on
373 $this->_allExternalIdentifiers
[$externalID] = $this->_lineCount
;
376 //Checking error in custom data
377 $params = &$this->getActiveFieldParams();
378 $params['contact_type'] = $this->_contactType
;
379 //date-format part ends
381 $errorMessage = NULL;
384 //add custom fields for contact sub type
386 if (!empty($this->_contactSubType
)) {
387 $csType = $this->_contactSubType
;
390 //checking error in custom data
391 $this->isErrorInCustomData($params, $errorMessage, $csType, $this->_relationships
);
393 //checking error in core data
394 $this->isErrorInCoreData($params, $errorMessage);
396 $tempMsg = "Invalid value for field(s) : $errorMessage";
397 // put the error message in the import record in the DB
398 $importRecordParams = [
399 $statusFieldName => 'ERROR',
400 "${statusFieldName}Msg" => $tempMsg,
402 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
403 array_unshift($values, $tempMsg);
404 $errorMessage = NULL;
405 return CRM_Import_Parser
::ERROR
;
408 //if user correcting errors by walking back
409 //need to reset status ERROR msg to null
410 //now currently we are having valid data.
411 $importRecordParams = [
412 $statusFieldName => 'NEW',
414 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
416 return CRM_Import_Parser
::VALID
;
420 * Get Array of all the fields that could potentially be part
425 public function getAllFields() {
426 return $this->_fields
;
430 * Handle the values in import mode.
432 * @param int $onDuplicate
433 * The code for what action to take on duplicates.
434 * @param array $values
435 * The array of values belonging to this line.
437 * @param bool $doGeocodeAddress
440 * the result of this processing
442 * @throws \CiviCRM_API3_Exception
443 * @throws \CRM_Core_Exception
445 public function import($onDuplicate, &$values, $doGeocodeAddress = FALSE) {
446 $config = CRM_Core_Config
::singleton();
447 $this->_unparsedStreetAddressContacts
= [];
448 if (!$doGeocodeAddress) {
449 // CRM-5854, reset the geocode method to null to prevent geocoding
450 CRM_Utils_GeocodeProvider
::disableForSession();
453 // first make sure this is a valid line
454 //$this->_updateWithId = false;
455 $response = $this->summary($values);
456 $statusFieldName = $this->_statusFieldName
;
458 if ($response != CRM_Import_Parser
::VALID
) {
459 $importRecordParams = [
460 $statusFieldName => 'INVALID',
461 "${statusFieldName}Msg" => "Invalid (Error Code: $response)",
463 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
467 $params = &$this->getActiveFieldParams();
469 'contact_type' => $this->_contactType
,
472 $contactFields = CRM_Contact_DAO_Contact
::import();
474 //check if external identifier exists in database
475 if (!empty($params['external_identifier']) && (!empty($params['id']) ||
in_array($onDuplicate, [
476 CRM_Import_Parser
::DUPLICATE_SKIP
,
477 CRM_Import_Parser
::DUPLICATE_NOCHECK
,
480 $extIDResult = civicrm_api3('Contact', 'get', [
481 'external_identifier' => $params['external_identifier'],
483 'return' => ['id', 'contact_is_deleted'],
485 if (isset($extIDResult['id'])) {
486 // record with matching external identifier does exist.
487 $internalCid = $extIDResult['id'];
488 if ($internalCid != CRM_Utils_Array
::value('id', $params)) {
489 if ($extIDResult['values'][$internalCid]['contact_is_deleted'] == 1) {
490 // And it is deleted. What to do? If we skip it, they user
491 // will be under the impression that the record exists in
492 // the database, yet they won't be able to find it. If we
493 // don't skip it, the database will try to insert a new record
494 // with an external_identifier that is non-unique. So...
495 // we will update this contact to remove the external_identifier
496 // and let a new record be created.
497 $update_params = ['id' => $internalCid, 'external_identifier' => ''];
498 civicrm_api3('Contact', 'create', $update_params);
501 $errorMessage = ts('External ID already exists in Database.');
502 array_unshift($values, $errorMessage);
503 $importRecordParams = [
504 $statusFieldName => 'ERROR',
505 "${statusFieldName}Msg" => $errorMessage,
507 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
508 return CRM_Import_Parser
::DUPLICATE
;
514 if (!empty($this->_contactSubType
)) {
515 $params['contact_sub_type'] = $this->_contactSubType
;
518 if ($subType = CRM_Utils_Array
::value('contact_sub_type', $params)) {
519 if (CRM_Contact_BAO_ContactType
::isExtendsContactType($subType, $this->_contactType
, FALSE, 'label')) {
520 $subTypes = CRM_Contact_BAO_ContactType
::subTypePairs($this->_contactType
, FALSE, NULL);
521 $params['contact_sub_type'] = array_search($subType, $subTypes);
523 elseif (!CRM_Contact_BAO_ContactType
::isExtendsContactType($subType, $this->_contactType
)) {
524 $message = "Mismatched or Invalid Contact Subtype.";
525 array_unshift($values, $message);
526 return CRM_Import_Parser
::NO_MATCH
;
530 // Get contact id to format common data in update/fill mode,
531 // prioritising a dedupe rule check over an external_identifier check, but falling back on ext id.
532 if ($this->_updateWithId
&& empty($params['id'])) {
534 $possibleMatches = $this->getPossibleContactMatches($params);
536 catch (CRM_Core_Exception
$e) {
537 $errorMessage = $e->getMessage();
538 array_unshift($values, $errorMessage);
540 $importRecordParams = [
541 $statusFieldName => 'ERROR',
542 "${statusFieldName}Msg" => $errorMessage,
544 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
545 return CRM_Import_Parser
::ERROR
;
547 foreach ($possibleMatches as $possibleID) {
548 $params['id'] = $formatted['id'] = $possibleID;
551 //format common data, CRM-4062
552 $this->formatCommonData($params, $formatted, $contactFields);
554 $relationship = FALSE;
555 $createNewContact = TRUE;
556 // Support Match and Update Via Contact ID
557 if ($this->_updateWithId
&& isset($params['id'])) {
558 $createNewContact = FALSE;
559 // @todo - it feels like all the rows from here to the end of the IF
560 // could be removed in favour of a simple check for whether the contact_type & id match
561 // the call to the deprecated function seems to add no value other that to do an additional
562 // check for the contact_id & type.
563 $error = _civicrm_api3_deprecated_duplicate_formatted_contact($formatted);
564 if (CRM_Core_Error
::isAPIError($error, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
565 if (is_array($error['error_message']['params'][0])) {
566 $matchedIDs = $error['error_message']['params'][0];
569 $matchedIDs = explode(',', $error['error_message']['params'][0]);
571 if (count($matchedIDs) >= 1) {
573 foreach ($matchedIDs as $contactId) {
574 if ($params['id'] == $contactId) {
575 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_type');
576 if ($formatted['contact_type'] == $contactType) {
577 //validation of subtype for update mode
579 $contactSubType = NULL;
580 if (!empty($params['contact_sub_type'])) {
581 $contactSubType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_sub_type');
584 if (!empty($contactSubType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($params['id'], $contactSubType) && $contactSubType != CRM_Utils_Array
::value('contact_sub_type', $formatted))) {
586 $message = "Mismatched contact SubTypes :";
587 array_unshift($values, $message);
589 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
593 $this->_retCode
= CRM_Import_Parser
::VALID
;
597 $message = "Mismatched contact Types :";
598 array_unshift($values, $message);
600 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
605 $message = "Mismatched contact IDs OR Mismatched contact Types :";
606 array_unshift($values, $message);
607 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
613 if (!empty($params['id'])) {
614 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_type');
616 if ($formatted['contact_type'] == $contactType) {
617 //validation of subtype for update mode
619 $contactSubType = NULL;
620 if (!empty($params['contact_sub_type'])) {
621 $contactSubType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_sub_type');
624 if (!empty($contactSubType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($params['id'], $contactSubType) && $contactSubType != CRM_Utils_Array
::value('contact_sub_type', $formatted))) {
626 $message = "Mismatched contact SubTypes :";
627 array_unshift($values, $message);
628 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
631 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $params['id'], FALSE, $this->_dedupeRuleGroupID
);
632 $this->_retCode
= CRM_Import_Parser
::VALID
;
636 $message = "Mismatched contact Types :";
637 array_unshift($values, $message);
638 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
642 // we should avoid multiple errors for single record
643 // since we have already retCode and we trying to force again.
644 if ($this->_retCode
!= CRM_Import_Parser
::NO_MATCH
) {
645 $message = "No contact found for this contact ID:" . $params['id'];
646 array_unshift($values, $message);
647 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
653 //now we want to create new contact on update/fill also.
654 $createNewContact = TRUE;
658 if (isset($newContact) && is_a($newContact, 'CRM_Contact_BAO_Contact')) {
659 $relationship = TRUE;
661 elseif (is_a($error, 'CRM_Core_Error')) {
662 $newContact = $error;
663 $relationship = TRUE;
668 //now we create new contact in update/fill mode also.
670 if ($createNewContact ||
($this->_retCode
!= CRM_Import_Parser
::NO_MATCH
&& $this->_updateWithId
)) {
672 //CRM-4430, don't carry if not submitted.
673 foreach (['prefix_id', 'suffix_id', 'gender_id'] as $name) {
674 if (!empty($formatted[$name])) {
675 $options = CRM_Contact_BAO_Contact
::buildOptions($name, 'get');
676 if (!isset($options[$formatted[$name]])) {
677 $formatted[$name] = CRM_Utils_Array
::key((string) $formatted[$name], $options);
681 if ($this->_updateWithId
&& !empty($params['id'])) {
682 $contactID = $params['id'];
684 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactID, TRUE, $this->_dedupeRuleGroupID
);
687 if (isset($newContact) && is_object($newContact) && ($newContact instanceof CRM_Contact_BAO_Contact
)) {
688 $relationship = TRUE;
689 $newContact = clone($newContact);
690 $contactID = $newContact->id
;
691 $this->_newContacts
[] = $contactID;
693 //get return code if we create new contact in update mode, CRM-4148
694 if ($this->_updateWithId
) {
695 $this->_retCode
= CRM_Import_Parser
::VALID
;
698 elseif (isset($newContact) && CRM_Core_Error
::isAPIError($newContact, CRM_Core_Error
::DUPLICATE_CONTACT
)) {
699 // if duplicate, no need of further processing
700 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_SKIP
) {
701 $errorMessage = "Skipping duplicate record";
702 array_unshift($values, $errorMessage);
703 $importRecordParams = [
704 $statusFieldName => 'DUPLICATE',
705 "${statusFieldName}Msg" => $errorMessage,
707 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
708 return CRM_Import_Parser
::DUPLICATE
;
711 $relationship = TRUE;
712 // CRM-10433/CRM-20739 - IDs could be string or array; handle accordingly
713 if (!is_array($dupeContactIDs = $newContact['error_message']['params'][0])) {
714 $dupeContactIDs = explode(',', $dupeContactIDs);
716 $dupeCount = count($dupeContactIDs);
717 $contactID = array_pop($dupeContactIDs);
718 // check to see if we had more than one duplicate contact id.
719 // if we have more than one, the record will be rejected below
720 if ($dupeCount == 1) {
721 // there was only one dupe, we will continue normally...
722 if (!in_array($contactID, $this->_newContacts
)) {
723 $this->_newContacts
[] = $contactID;
730 $currentImportID = end($values);
733 'contactID' => $contactID,
734 'importID' => $currentImportID,
735 'importTempTable' => $this->_tableName
,
736 'fieldHeaders' => $this->_mapperKeys
,
737 'fields' => $this->_activeFields
,
740 CRM_Utils_Hook
::import('Contact', 'process', $this, $hookParams);
744 $primaryContactId = NULL;
745 if (CRM_Core_Error
::isAPIError($newContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
746 if ($dupeCount == 1 && CRM_Utils_Rule
::integer($contactID)) {
747 $primaryContactId = $contactID;
751 $primaryContactId = $newContact->id
;
754 if ((CRM_Core_Error
::isAPIError($newContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
) ||
is_a($newContact, 'CRM_Contact_BAO_Contact')) && $primaryContactId) {
756 //relationship contact insert
757 foreach ($params as $key => $field) {
758 list($id, $first, $second) = CRM_Utils_System
::explode('_', $key, 3);
759 if (!($first == 'a' && $second == 'b') && !($first == 'b' && $second == 'a')) {
763 $relationType = new CRM_Contact_DAO_RelationshipType();
764 $relationType->id
= $id;
765 $relationType->find(TRUE);
766 $direction = "contact_sub_type_$second";
769 'contact_type' => $params[$key]['contact_type'],
772 //set subtype for related contact CRM-5125
773 if (isset($relationType->$direction)) {
774 //validation of related contact subtype for update mode
775 if ($relCsType = CRM_Utils_Array
::value('contact_sub_type', $params[$key]) && $relCsType != $relationType->$direction) {
776 $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact.");
777 array_unshift($values, $errorMessage);
778 return CRM_Import_Parser
::NO_MATCH
;
781 $formatting['contact_sub_type'] = $relationType->$direction;
785 $contactFields = NULL;
786 $contactFields = CRM_Contact_DAO_Contact
::import();
788 //Relation on the basis of External Identifier.
789 if (empty($params[$key]['id']) && !empty($params[$key]['external_identifier'])) {
790 $params[$key]['id'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['external_identifier'], 'id', 'external_identifier');
792 // check for valid related contact id in update/fill mode, CRM-4424
793 if (in_array($onDuplicate, [
794 CRM_Import_Parser
::DUPLICATE_UPDATE
,
795 CRM_Import_Parser
::DUPLICATE_FILL
,
796 ]) && !empty($params[$key]['id'])) {
797 $relatedContactType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['id'], 'contact_type');
798 if (!$relatedContactType) {
799 $errorMessage = ts("No contact found for this related contact ID: %1", [1 => $params[$key]['id']]);
800 array_unshift($values, $errorMessage);
801 return CRM_Import_Parser
::NO_MATCH
;
804 //validation of related contact subtype for update mode
806 $relatedCsType = NULL;
807 if (!empty($formatting['contact_sub_type'])) {
808 $relatedCsType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['id'], 'contact_sub_type');
811 if (!empty($relatedCsType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($params[$key]['id'], $relatedCsType) &&
812 $relatedCsType != CRM_Utils_Array
::value('contact_sub_type', $formatting))
814 $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact.") . ' ' . ts("ID: %1", [1 => $params[$key]['id']]);
815 array_unshift($values, $errorMessage);
816 return CRM_Import_Parser
::NO_MATCH
;
819 // get related contact id to format data in update/fill mode,
820 //if external identifier is present, CRM-4423
821 $formatting['id'] = $params[$key]['id'];
826 //format common data, CRM-4062
827 $this->formatCommonData($field, $formatting, $contactFields);
829 //do we have enough fields to create related contact.
830 $allowToCreate = $this->checkRelatedContactFields($key, $formatting);
832 if (!$allowToCreate) {
833 $errorMessage = ts('Related contact required fields are missing.');
834 array_unshift($values, $errorMessage);
835 return CRM_Import_Parser
::NO_MATCH
;
839 if (!empty($params[$key]['id'])) {
841 'contact_id' => $params[$key]['id'],
844 $relatedNewContact = CRM_Contact_BAO_Contact
::retrieve($contact, $defaults);
847 $relatedNewContact = $this->createContact($formatting, $contactFields, $onDuplicate, NULL, FALSE);
850 if (is_object($relatedNewContact) ||
($relatedNewContact instanceof CRM_Contact_BAO_Contact
)) {
851 $relatedNewContact = clone($relatedNewContact);
855 // To update/fill contact, get the matching contact Ids if duplicate contact found
856 // otherwise get contact Id from object of related contact
857 if (is_array($relatedNewContact) && civicrm_error($relatedNewContact)) {
858 if (CRM_Core_Error
::isAPIError($relatedNewContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
859 $matchedIDs = $relatedNewContact['error_message']['params'][0];
860 if (!is_array($matchedIDs)) {
861 $matchedIDs = explode(',', $matchedIDs);
865 $errorMessage = $relatedNewContact['error_message'];
866 array_unshift($values, $errorMessage);
867 $importRecordParams = [
868 $statusFieldName => 'ERROR',
869 "${statusFieldName}Msg" => $errorMessage,
871 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
872 return CRM_Import_Parser
::ERROR
;
876 $matchedIDs[] = $relatedNewContact->id
;
878 // update/fill related contact after getting matching Contact Ids, CRM-4424
879 if (in_array($onDuplicate, [
880 CRM_Import_Parser
::DUPLICATE_UPDATE
,
881 CRM_Import_Parser
::DUPLICATE_FILL
,
883 //validation of related contact subtype for update mode
885 $relatedCsType = NULL;
886 if (!empty($formatting['contact_sub_type'])) {
887 $relatedCsType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $matchedIDs[0], 'contact_sub_type');
890 if (!empty($relatedCsType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($matchedIDs[0], $relatedCsType) && $relatedCsType != CRM_Utils_Array
::value('contact_sub_type', $formatting))) {
891 $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact.");
892 array_unshift($values, $errorMessage);
893 return CRM_Import_Parser
::NO_MATCH
;
896 $updatedContact = $this->createContact($formatting, $contactFields, $onDuplicate, $matchedIDs[0]);
899 static $relativeContact = [];
900 if (CRM_Core_Error
::isAPIError($relatedNewContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
901 if (count($matchedIDs) >= 1) {
902 $relContactId = $matchedIDs[0];
903 //add relative contact to count during update & fill mode.
904 //logic to make count distinct by contact id.
905 if ($this->_newRelatedContacts ||
!empty($relativeContact)) {
906 $reContact = array_keys($relativeContact, $relContactId);
908 if (empty($reContact)) {
909 $this->_newRelatedContacts
[] = $relativeContact[] = $relContactId;
913 $this->_newRelatedContacts
[] = $relativeContact[] = $relContactId;
918 $relContactId = $relatedNewContact->id
;
919 $this->_newRelatedContacts
[] = $relativeContact[] = $relContactId;
922 if (CRM_Core_Error
::isAPIError($relatedNewContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
) ||
($relatedNewContact instanceof CRM_Contact_BAO_Contact
)) {
923 //fix for CRM-1993.Checks for duplicate related contacts
924 if (count($matchedIDs) >= 1) {
925 //if more than one duplicate contact
926 //found, create relationship with first contact
927 // now create the relationship record
928 $relationParams = [];
930 'relationship_type_id' => $key,
935 'skipRecentView' => TRUE,
938 // we only handle related contact success, we ignore failures for now
939 // at some point wold be nice to have related counts as separate
941 'contact' => $primaryContactId,
944 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship
::legacyCreateMultiple($relationParams, $relationIds);
946 if ($valid ||
$duplicate) {
947 $relationIds['contactTarget'] = $relContactId;
948 $action = ($duplicate) ? CRM_Core_Action
::UPDATE
: CRM_Core_Action
::ADD
;
949 CRM_Contact_BAO_Relationship
::relatedMemberships($primaryContactId, $relationParams, $relationIds, $action);
952 //handle current employer, CRM-3532
954 $allRelationships = CRM_Core_PseudoConstant
::relationshipType('name');
955 $relationshipTypeId = str_replace([
962 $relationshipType = str_replace($relationshipTypeId . '_', '', $key);
963 $orgId = $individualId = NULL;
964 if ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employee of') {
965 $orgId = $relContactId;
966 $individualId = $primaryContactId;
968 elseif ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employer of') {
969 $orgId = $primaryContactId;
970 $individualId = $relContactId;
972 if ($orgId && $individualId) {
973 $currentEmpParams[$individualId] = $orgId;
974 CRM_Contact_BAO_Contact_Utils
::setCurrentEmployer($currentEmpParams);
982 if ($this->_updateWithId
) {
983 //return warning if street address is unparsed, CRM-5886
984 return $this->processMessage($values, $statusFieldName, $this->_retCode
);
987 if (is_array($newContact) && civicrm_error($newContact)) {
990 if (($code = CRM_Utils_Array
::value('code', $newContact['error_message'])) && ($code == CRM_Core_Error
::DUPLICATE_CONTACT
)) {
992 // need to fix at some stage and decide if the error will return an
993 // array or string, crude hack for now
994 if (is_array($newContact['error_message']['params'][0])) {
995 $cids = $newContact['error_message']['params'][0];
998 $cids = explode(',', $newContact['error_message']['params'][0]);
1001 foreach ($cids as $cid) {
1002 $urls[] = CRM_Utils_System
::url('civicrm/contact/view', 'reset=1&cid=' . $cid, TRUE);
1005 $url_string = implode("\n", $urls);
1007 // If we duplicate more than one record, skip no matter what
1008 if (count($cids) > 1) {
1009 $errorMessage = ts('Record duplicates multiple contacts');
1010 $importRecordParams = [
1011 $statusFieldName => 'ERROR',
1012 "${statusFieldName}Msg" => $errorMessage,
1015 //combine error msg to avoid mismatch between error file columns.
1016 $errorMessage .= "\n" . $url_string;
1017 array_unshift($values, $errorMessage);
1018 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
1019 return CRM_Import_Parser
::ERROR
;
1022 // Params only had one id, so shift it out
1023 $contactId = array_shift($cids);
1026 $vals = ['contact_id' => $contactId];
1028 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_REPLACE
) {
1029 civicrm_api('contact', 'delete', $vals);
1030 $cid = CRM_Contact_BAO_Contact
::createProfileContact($formatted, $contactFields, $contactId, NULL, NULL, $formatted['contact_type']);
1032 elseif ($onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
) {
1033 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactId);
1035 elseif ($onDuplicate == CRM_Import_Parser
::DUPLICATE_FILL
) {
1036 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactId);
1038 // else skip does nothing and just returns an error code.
1041 'contact_id' => $cid,
1044 $newContact = CRM_Contact_BAO_Contact
::retrieve($contact, $defaults);
1047 if (civicrm_error($newContact)) {
1048 if (empty($newContact['error_message']['params'])) {
1049 // different kind of error other than DUPLICATE
1050 $errorMessage = $newContact['error_message'];
1051 array_unshift($values, $errorMessage);
1052 $importRecordParams = [
1053 $statusFieldName => 'ERROR',
1054 "${statusFieldName}Msg" => $errorMessage,
1056 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
1057 return CRM_Import_Parser
::ERROR
;
1060 $contactID = $newContact['error_message']['params'][0];
1061 if (is_array($contactID)) {
1062 $contactID = array_pop($contactID);
1064 if (!in_array($contactID, $this->_newContacts
)) {
1065 $this->_newContacts
[] = $contactID;
1068 //CRM-262 No Duplicate Checking
1069 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_SKIP
) {
1070 array_unshift($values, $url_string);
1071 $importRecordParams = [
1072 $statusFieldName => 'DUPLICATE',
1073 "${statusFieldName}Msg" => "Skipping duplicate record",
1075 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
1076 return CRM_Import_Parser
::DUPLICATE
;
1079 $importRecordParams = [
1080 $statusFieldName => 'IMPORTED',
1082 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
1083 //return warning if street address is not parsed, CRM-5886
1084 return $this->processMessage($values, $statusFieldName, CRM_Import_Parser
::VALID
);
1087 // Not a dupe, so we had an error
1088 $errorMessage = $newContact['error_message'];
1089 array_unshift($values, $errorMessage);
1090 $importRecordParams = [
1091 $statusFieldName => 'ERROR',
1092 "${statusFieldName}Msg" => $errorMessage,
1094 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
1095 return CRM_Import_Parser
::ERROR
;
1099 return $this->processMessage($values, $statusFieldName, CRM_Import_Parser
::VALID
);
1103 * Get the array of successfully imported contact id's
1107 public function &getImportedContacts() {
1108 return $this->_newContacts
;
1112 * Get the array of successfully imported related contact id's
1116 public function &getRelatedImportedContacts() {
1117 return $this->_newRelatedContacts
;
1121 * The initializer code, called before the processing.
1123 public function fini() {
1127 * Check if an error in custom data.
1129 * @param array $params
1130 * @param string $errorMessage
1131 * A string containing all the error-fields.
1133 * @param null $csType
1134 * @param null $relationships
1136 public static function isErrorInCustomData($params, &$errorMessage, $csType = NULL, $relationships = NULL) {
1137 $dateType = CRM_Core_Session
::singleton()->get("dateTypes");
1139 if (!empty($params['contact_sub_type'])) {
1140 $csType = CRM_Utils_Array
::value('contact_sub_type', $params);
1143 if (empty($params['contact_type'])) {
1144 $params['contact_type'] = 'Individual';
1147 // get array of subtypes - CRM-18708
1148 if (in_array($csType, ['Individual', 'Organization', 'Household'])) {
1149 $csType = self
::getSubtypes($params['contact_type']);
1152 if (is_array($csType)) {
1153 // fetch custom fields for every subtype and add it to $customFields array
1156 foreach ($csType as $cType) {
1157 $customFields +
= CRM_Core_BAO_CustomField
::getFields($params['contact_type'], FALSE, FALSE, $cType);
1161 $customFields = CRM_Core_BAO_CustomField
::getFields($params['contact_type'], FALSE, FALSE, $csType);
1164 $addressCustomFields = CRM_Core_BAO_CustomField
::getFields('Address');
1165 $customFields = $customFields +
$addressCustomFields;
1166 foreach ($params as $key => $value) {
1167 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1168 /* check if it's a valid custom field id */
1170 if (!array_key_exists($customFieldID, $customFields)) {
1171 self
::addToErrorMsg(ts('field ID'), $errorMessage);
1173 // validate null values for required custom fields of type boolean
1174 if (!empty($customFields[$customFieldID]['is_required']) && (empty($params['custom_' . $customFieldID]) && !is_numeric($params['custom_' . $customFieldID])) && $customFields[$customFieldID]['data_type'] == 'Boolean') {
1175 self
::addToErrorMsg($customFields[$customFieldID]['label'] . '::' . $customFields[$customFieldID]['groupTitle'], $errorMessage);
1178 //For address custom fields, we do get actual custom field value as an inner array of
1179 //values so need to modify
1180 if (array_key_exists($customFieldID, $addressCustomFields)) {
1181 $value = $value[0][$key];
1183 /* validate the data against the CF type */
1186 if ($customFields[$customFieldID]['data_type'] == 'Date') {
1187 if (array_key_exists($customFieldID, $addressCustomFields) && CRM_Utils_Date
::convertToDefaultDate($params[$key][0], $dateType, $key)) {
1188 $value = $params[$key][0][$key];
1190 elseif (CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key)) {
1191 $value = $params[$key];
1194 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1197 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
1198 if (CRM_Utils_String
::strtoboolstr($value) === FALSE) {
1199 self
::addToErrorMsg($customFields[$customFieldID]['label'] . '::' . $customFields[$customFieldID]['groupTitle'], $errorMessage);
1202 // need not check for label filed import
1208 'Multi-Select State/Province',
1209 'Multi-Select Country',
1211 if (!in_array($customFields[$customFieldID]['html_type'], $htmlType) ||
$customFields[$customFieldID]['data_type'] == 'Boolean' ||
$customFields[$customFieldID]['data_type'] == 'ContactReference') {
1212 $valid = CRM_Core_BAO_CustomValue
::typecheck($customFields[$customFieldID]['data_type'], $value);
1214 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1218 // check for values for custom fields for checkboxes and multiselect
1219 if ($customFields[$customFieldID]['html_type'] == 'CheckBox' ||
$customFields[$customFieldID]['html_type'] == 'Multi-Select') {
1220 $value = trim($value);
1221 $value = str_replace('|', ',', $value);
1222 $mulValues = explode(',', $value);
1223 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1224 foreach ($mulValues as $v1) {
1225 if (strlen($v1) == 0) {
1230 foreach ($customOption as $v2) {
1231 if ((strtolower(trim($v2['label'])) == strtolower(trim($v1))) ||
(strtolower(trim($v2['value'])) == strtolower(trim($v1)))) {
1237 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1241 elseif ($customFields[$customFieldID]['html_type'] == 'Select' ||
($customFields[$customFieldID]['html_type'] == 'Radio' && $customFields[$customFieldID]['data_type'] != 'Boolean')) {
1242 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1244 foreach ($customOption as $v2) {
1245 if ((strtolower(trim($v2['label'])) == strtolower(trim($value))) ||
(strtolower(trim($v2['value'])) == strtolower(trim($value)))) {
1250 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1253 elseif ($customFields[$customFieldID]['html_type'] == 'Multi-Select State/Province') {
1254 $mulValues = explode(',', $value);
1255 foreach ($mulValues as $stateValue) {
1257 if (self
::in_value(trim($stateValue), CRM_Core_PseudoConstant
::stateProvinceAbbreviation()) || self
::in_value(trim($stateValue), CRM_Core_PseudoConstant
::stateProvince())) {
1261 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1266 elseif ($customFields[$customFieldID]['html_type'] == 'Multi-Select Country') {
1267 $mulValues = explode(',', $value);
1268 foreach ($mulValues as $countryValue) {
1269 if ($countryValue) {
1270 CRM_Core_PseudoConstant
::populate($countryNames, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active');
1271 CRM_Core_PseudoConstant
::populate($countryIsoCodes, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
1272 $limitCodes = CRM_Core_BAO_Country
::countryLimit();
1280 if (in_array(trim($countryValue), $values)) {
1287 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1294 elseif (is_array($params[$key]) && isset($params[$key]["contact_type"])) {
1296 //supporting custom data of related contact subtypes
1298 if ($relationships) {
1299 if (array_key_exists($key, $relationships)) {
1302 elseif (CRM_Utils_Array
::key($key, $relationships)) {
1303 $relation = CRM_Utils_Array
::key($key, $relationships);
1306 if (!empty($relation)) {
1307 list($id, $first, $second) = CRM_Utils_System
::explode('_', $relation, 3);
1308 $direction = "contact_sub_type_$second";
1309 $relationshipType = new CRM_Contact_BAO_RelationshipType();
1310 $relationshipType->id
= $id;
1311 if ($relationshipType->find(TRUE)) {
1312 if (isset($relationshipType->$direction)) {
1313 $params[$key]['contact_sub_type'] = $relationshipType->$direction;
1318 self
::isErrorInCustomData($params[$key], $errorMessage, $csType, $relationships);
1324 * Check if value present in all genders or.
1325 * as a substring of any gender value, if yes than return corresponding gender.
1326 * eg value might be m/M, ma/MA, mal/MAL, male return 'Male'
1327 * but if value is 'maleabc' than return false
1329 * @param string $gender
1330 * Check this value across gender values.
1332 * retunr gender value / false
1336 public function checkGender($gender) {
1337 $gender = trim($gender, '.');
1342 $allGenders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
1343 foreach ($allGenders as $key => $value) {
1344 if (strlen($gender) > strlen($value)) {
1347 if ($gender == $value) {
1350 if (substr_compare($value, $gender, 0, strlen($gender), TRUE) === 0) {
1359 * Check if an error in Core( non-custom fields ) field
1361 * @param array $params
1362 * @param string $errorMessage
1363 * A string containing all the error-fields.
1365 public function isErrorInCoreData($params, &$errorMessage) {
1366 foreach ($params as $key => $value) {
1368 $session = CRM_Core_Session
::singleton();
1369 $dateType = $session->get("dateTypes");
1373 if (CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key)) {
1374 if (!CRM_Utils_Rule
::date($params[$key])) {
1375 self
::addToErrorMsg(ts('Birth Date'), $errorMessage);
1379 self
::addToErrorMsg(ts('Birth-Date'), $errorMessage);
1383 case 'deceased_date':
1384 if (CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key)) {
1385 if (!CRM_Utils_Rule
::date($params[$key])) {
1386 self
::addToErrorMsg(ts('Deceased Date'), $errorMessage);
1390 self
::addToErrorMsg(ts('Deceased Date'), $errorMessage);
1395 if (CRM_Utils_String
::strtoboolstr($value) === FALSE) {
1396 self
::addToErrorMsg(ts('Deceased'), $errorMessage);
1401 if (!self
::checkGender($value)) {
1402 self
::addToErrorMsg(ts('Gender'), $errorMessage);
1406 case 'preferred_communication_method':
1408 $preffComm = explode(',', $value);
1409 foreach ($preffComm as $v) {
1410 if (!self
::in_value(trim($v), CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method'))) {
1411 self
::addToErrorMsg(ts('Preferred Communication Method'), $errorMessage);
1416 case 'preferred_mail_format':
1417 if (!array_key_exists(strtolower($value), array_change_key_case(CRM_Core_SelectValues
::pmf(), CASE_LOWER
))) {
1418 self
::addToErrorMsg(ts('Preferred Mail Format'), $errorMessage);
1422 case 'individual_prefix':
1424 if (!self
::in_value($value, CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id'))) {
1425 self
::addToErrorMsg(ts('Individual Prefix'), $errorMessage);
1429 case 'individual_suffix':
1431 if (!self
::in_value($value, CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id'))) {
1432 self
::addToErrorMsg(ts('Individual Suffix'), $errorMessage);
1436 case 'state_province':
1437 if (!empty($value)) {
1438 foreach ($value as $stateValue) {
1439 if ($stateValue['state_province']) {
1440 if (self
::in_value($stateValue['state_province'], CRM_Core_PseudoConstant
::stateProvinceAbbreviation()) ||
1441 self
::in_value($stateValue['state_province'], CRM_Core_PseudoConstant
::stateProvince())
1446 self
::addToErrorMsg(ts('State/Province'), $errorMessage);
1454 if (!empty($value)) {
1455 foreach ($value as $stateValue) {
1456 if ($stateValue['country']) {
1457 CRM_Core_PseudoConstant
::populate($countryNames, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active');
1458 CRM_Core_PseudoConstant
::populate($countryIsoCodes, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
1459 $limitCodes = CRM_Core_BAO_Country
::countryLimit();
1460 //If no country is selected in
1461 //localization then take all countries
1462 if (empty($limitCodes)) {
1463 $limitCodes = $countryIsoCodes;
1466 if (self
::in_value($stateValue['country'], $limitCodes) || self
::in_value($stateValue['country'], CRM_Core_PseudoConstant
::country())) {
1470 if (self
::in_value($stateValue['country'], $countryIsoCodes) || self
::in_value($stateValue['country'], $countryNames)) {
1471 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);
1474 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);
1483 if (!empty($value)) {
1484 foreach ($value as $county) {
1485 if ($county['county']) {
1486 $countyNames = CRM_Core_PseudoConstant
::county();
1487 if (!empty($county['county']) && !in_array($county['county'], $countyNames)) {
1488 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);
1496 if (!empty($value)) {
1497 foreach ($value as $codeValue) {
1498 if (!empty($codeValue['geo_code_1'])) {
1499 if (CRM_Utils_Rule
::numeric($codeValue['geo_code_1'])) {
1503 self
::addToErrorMsg(ts('Geo code 1'), $errorMessage);
1511 if (!empty($value)) {
1512 foreach ($value as $codeValue) {
1513 if (!empty($codeValue['geo_code_2'])) {
1514 if (CRM_Utils_Rule
::numeric($codeValue['geo_code_2'])) {
1518 self
::addToErrorMsg(ts('Geo code 2'), $errorMessage);
1525 //check for any error in email/postal greeting, addressee,
1526 //custom email/postal greeting, custom addressee, CRM-4575
1528 case 'email_greeting':
1529 $emailGreetingFilter = [
1530 'contact_type' => $this->_contactType
,
1531 'greeting_type' => 'email_greeting',
1533 if (!self
::in_value($value, CRM_Core_PseudoConstant
::greeting($emailGreetingFilter))) {
1534 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);
1538 case 'postal_greeting':
1539 $postalGreetingFilter = [
1540 'contact_type' => $this->_contactType
,
1541 'greeting_type' => 'postal_greeting',
1543 if (!self
::in_value($value, CRM_Core_PseudoConstant
::greeting($postalGreetingFilter))) {
1544 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);
1549 $addresseeFilter = [
1550 'contact_type' => $this->_contactType
,
1551 'greeting_type' => 'addressee',
1553 if (!self
::in_value($value, CRM_Core_PseudoConstant
::greeting($addresseeFilter))) {
1554 self
::addToErrorMsg(ts('Addressee must be one of the configured format options. Check Administer >> System Settings >> Option Groups >> Addressee for valid values'), $errorMessage);
1558 case 'email_greeting_custom':
1559 if (array_key_exists('email_greeting', $params)) {
1560 $emailGreetingLabel = key(CRM_Core_OptionGroup
::values('email_greeting', TRUE, NULL, NULL, 'AND v.name = "Customized"'));
1561 if (CRM_Utils_Array
::value('email_greeting', $params) != $emailGreetingLabel) {
1562 self
::addToErrorMsg(ts('Email Greeting - Custom'), $errorMessage);
1567 case 'postal_greeting_custom':
1568 if (array_key_exists('postal_greeting', $params)) {
1569 $postalGreetingLabel = key(CRM_Core_OptionGroup
::values('postal_greeting', TRUE, NULL, NULL, 'AND v.name = "Customized"'));
1570 if (CRM_Utils_Array
::value('postal_greeting', $params) != $postalGreetingLabel) {
1571 self
::addToErrorMsg(ts('Postal Greeting - Custom'), $errorMessage);
1576 case 'addressee_custom':
1577 if (array_key_exists('addressee', $params)) {
1578 $addresseeLabel = key(CRM_Core_OptionGroup
::values('addressee', TRUE, NULL, NULL, 'AND v.name = "Customized"'));
1579 if (CRM_Utils_Array
::value('addressee', $params) != $addresseeLabel) {
1580 self
::addToErrorMsg(ts('Addressee - Custom'), $errorMessage);
1586 if (is_array($value)) {
1587 foreach ($value as $values) {
1588 if (!empty($values['url']) && !CRM_Utils_Rule
::url($values['url'])) {
1589 self
::addToErrorMsg(ts('Website'), $errorMessage);
1596 case 'do_not_email':
1597 case 'do_not_phone':
1600 case 'do_not_trade':
1601 if (CRM_Utils_Rule
::boolean($value) == FALSE) {
1602 $key = ucwords(str_replace("_", " ", $key));
1603 self
::addToErrorMsg($key, $errorMessage);
1608 if (is_array($value)) {
1609 foreach ($value as $values) {
1610 if (!empty($values['email']) && !CRM_Utils_Rule
::email($values['email'])) {
1611 self
::addToErrorMsg($key, $errorMessage);
1619 if (is_array($params[$key]) && isset($params[$key]["contact_type"])) {
1620 //check for any relationship data ,FIX ME
1621 self
::isErrorInCoreData($params[$key], $errorMessage);
1629 * Ckeck a value present or not in a array.
1632 * @param $valueArray
1636 public static function in_value($value, $valueArray) {
1637 foreach ($valueArray as $key => $v) {
1639 if (strtolower(trim($v, ".")) == strtolower(trim($value, "."))) {
1647 * Build error-message containing error-fields
1649 * Once upon a time there was a dev who hadn't heard of implode. That dev wrote this function.
1651 * @todo just say no!
1653 * @param string $errorName
1654 * A string containing error-field name.
1655 * @param string $errorMessage
1656 * A string containing all the error-fields, where the new errorName is concatenated.
1659 public static function addToErrorMsg($errorName, &$errorMessage) {
1660 if ($errorMessage) {
1661 $errorMessage .= "; $errorName";
1664 $errorMessage = $errorName;
1669 * Method for creating contact.
1671 * @param array $formatted
1672 * @param array $contactFields
1673 * @param int $onDuplicate
1674 * @param int $contactId
1675 * @param bool $requiredCheck
1676 * @param int $dedupeRuleGroupID
1678 * @return array|bool|\CRM_Contact_BAO_Contact|\CRM_Core_Error|null
1680 public function createContact(&$formatted, &$contactFields, $onDuplicate, $contactId = NULL, $requiredCheck = TRUE, $dedupeRuleGroupID = NULL) {
1684 if (is_null($contactId) && ($onDuplicate != CRM_Import_Parser
::DUPLICATE_NOCHECK
)) {
1685 $dupeCheck = (bool) ($onDuplicate);
1688 //get the prefix id etc if exists
1689 CRM_Contact_BAO_Contact
::resolveDefaults($formatted, TRUE);
1691 //@todo direct call to API function not supported.
1692 // setting required check to false, CRM-2839
1693 // plus we do our own required check in import
1694 $error = _civicrm_api3_deprecated_contact_check_params($formatted, $dupeCheck, $dedupeRuleGroupID);
1696 if ((is_null($error)) && (civicrm_error(_civicrm_api3_deprecated_validate_formatted_contact($formatted)))) {
1697 $error = _civicrm_api3_deprecated_validate_formatted_contact($formatted);
1700 $newContact = $error;
1702 if (is_null($error)) {
1704 $this->formatParams($formatted, $onDuplicate, (int) $contactId);
1707 // Resetting and rebuilding cache could be expensive.
1708 CRM_Core_Config
::setPermitCacheFlushMode(FALSE);
1709 $cid = CRM_Contact_BAO_Contact
::createProfileContact($formatted, $contactFields, $contactId, NULL, NULL, $formatted['contact_type']);
1710 CRM_Core_Config
::setPermitCacheFlushMode(TRUE);
1713 'contact_id' => $cid,
1717 $newContact = CRM_Contact_BAO_Contact
::retrieve($contact, $defaults);
1720 //get the id of the contact whose street address is not parsable, CRM-5886
1721 if ($this->_parseStreetAddress
&& is_object($newContact) && property_exists($newContact, 'address') && $newContact->address
) {
1722 foreach ($newContact->address
as $address) {
1723 if (!empty($address['street_address']) && (empty($address['street_number']) ||
empty($address['street_name']))) {
1724 $this->_unparsedStreetAddressContacts
[] = [
1725 'id' => $newContact->id
,
1726 'streetAddress' => $address['street_address'],
1735 * Format params for update and fill mode.
1737 * @param array $params
1738 * reference to an array containing all the.
1740 * @param int $onDuplicate
1744 public function formatParams(&$params, $onDuplicate, $cid) {
1745 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_SKIP
) {
1750 'contact_id' => $cid,
1754 $contactObj = CRM_Contact_BAO_Contact
::retrieve($contactParams, $defaults);
1756 $modeUpdate = $modeFill = FALSE;
1758 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
) {
1762 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_FILL
) {
1766 $groupTree = CRM_Core_BAO_CustomGroup
::getTree($params['contact_type'], NULL, $cid, 0, NULL);
1767 CRM_Core_BAO_CustomGroup
::setDefaults($groupTree, $defaults, FALSE, FALSE);
1773 'website' => 'website',
1774 'address' => 'address',
1777 $contact = get_object_vars($contactObj);
1779 foreach ($params as $key => $value) {
1780 if ($key == 'id' ||
$key == 'contact_type') {
1784 if (array_key_exists($key, $locationFields)) {
1787 elseif (in_array($key, [
1792 // CRM-4575, need to null custom
1793 if ($params["{$key}_id"] != 4) {
1794 $params["{$key}_custom"] = 'null';
1796 unset($params[$key]);
1799 if ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1800 $custom_params = ['id' => $contact['id'], 'return' => $key];
1801 $getValue = civicrm_api3('Contact', 'getvalue', $custom_params);
1802 if (empty($getValue)) {
1807 $getValue = CRM_Utils_Array
::retrieveValueRecursive($contact, $key);
1809 if ($key == 'contact_source') {
1810 $params['source'] = $params[$key];
1811 unset($params[$key]);
1814 if ($modeFill && isset($getValue)) {
1815 unset($params[$key]);
1816 if ($customFieldId) {
1817 // Extra values must be unset to ensure the values are not
1819 unset($params['custom'][$customFieldId]);
1825 foreach ($locationFields as $locKeys) {
1826 if (is_array(CRM_Utils_Array
::value($locKeys, $params))) {
1827 foreach ($params[$locKeys] as $key => $value) {
1829 $getValue = CRM_Utils_Array
::retrieveValueRecursive($contact, $locKeys);
1831 if (isset($getValue)) {
1832 foreach ($getValue as $cnt => $values) {
1833 if ($locKeys == 'website') {
1834 if (($getValue[$cnt]['website_type_id'] == $params[$locKeys][$key]['website_type_id'])) {
1835 unset($params[$locKeys][$key]);
1839 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']) {
1840 unset($params[$locKeys][$key]);
1847 if (count($params[$locKeys]) == 0) {
1848 unset($params[$locKeys]);
1855 * Convert any given date string to default date array.
1857 * @param array $params
1858 * Has given date-format.
1859 * @param array $formatted
1860 * Store formatted date in this array.
1861 * @param int $dateType
1863 * @param string $dateParam
1866 public static function formatCustomDate(&$params, &$formatted, $dateType, $dateParam) {
1868 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $dateParam);
1869 $formatted[$dateParam] = CRM_Utils_Date
::processDate($params[$dateParam]);
1873 * Generate status and error message for unparsed street address records.
1875 * @param array $values
1876 * The array of values belonging to each row.
1877 * @param array $statusFieldName
1878 * Store formatted date in this array.
1879 * @param $returnCode
1883 public function processMessage(&$values, $statusFieldName, $returnCode) {
1884 if (empty($this->_unparsedStreetAddressContacts
)) {
1885 $importRecordParams = [
1886 $statusFieldName => 'IMPORTED',
1890 $errorMessage = ts("Record imported successfully but unable to parse the street address: ");
1891 foreach ($this->_unparsedStreetAddressContacts
as $contactInfo => $contactValue) {
1892 $contactUrl = CRM_Utils_System
::url('civicrm/contact/add', 'reset=1&action=update&cid=' . $contactValue['id'], TRUE, NULL, FALSE);
1893 $errorMessage .= "\n Contact ID:" . $contactValue['id'] . " <a href=\"$contactUrl\"> " . $contactValue['streetAddress'] . "</a>";
1895 array_unshift($values, $errorMessage);
1896 $importRecordParams = [
1897 $statusFieldName => 'ERROR',
1898 "${statusFieldName}Msg" => $errorMessage,
1900 $returnCode = CRM_Import_Parser
::UNPARSED_ADDRESS_WARNING
;
1902 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
1908 * @param array $params
1912 public function checkRelatedContactFields($relKey, $params) {
1913 //avoid blank contact creation.
1914 $allowToCreate = FALSE;
1916 //build the mapper field array.
1917 static $relatedContactFields = [];
1918 if (!isset($relatedContactFields[$relKey])) {
1919 foreach ($this->_mapperRelated
as $key => $name) {
1924 if (!empty($relatedContactFields[$name]) && !is_array($relatedContactFields[$name])) {
1925 $relatedContactFields[$name] = [];
1927 $fldName = CRM_Utils_Array
::value($key, $this->_mapperRelatedContactDetails
);
1928 if ($fldName == 'url') {
1929 $fldName = 'website';
1932 $relatedContactFields[$name][] = $fldName;
1937 //validate for passed data.
1938 if (is_array($relatedContactFields[$relKey])) {
1939 foreach ($relatedContactFields[$relKey] as $fld) {
1940 if (!empty($params[$fld])) {
1941 $allowToCreate = TRUE;
1947 return $allowToCreate;
1951 * get subtypes given the contact type
1953 * @param string $contactType
1954 * @return array $subTypes
1956 public static function getSubtypes($contactType) {
1958 $types = CRM_Contact_BAO_ContactType
::subTypeInfo($contactType);
1960 if (count($types) > 0) {
1961 foreach ($types as $type) {
1962 $subTypes[] = $type['name'];
1969 * Get the possible contact matches.
1971 * 1) the chosen dedupe rule falling back to
1972 * 2) a check for the external ID.
1976 * @param array $params
1979 * IDs of possible matches.
1981 * @throws \CRM_Core_Exception
1982 * @throws \CiviCRM_API3_Exception
1984 protected function getPossibleContactMatches($params) {
1987 if (!empty($params['external_identifier'])) {
1988 // Check for any match on external id, deleted or otherwise.
1989 $extIDContact = civicrm_api3('Contact', 'get', [
1990 'external_identifier' => $params['external_identifier'],
1992 'return' => ['id', 'contact_is_deleted'],
1994 if (isset($extIDContact['id'])) {
1995 $extIDMatch = $extIDContact['id'];
1997 if ($extIDContact['values'][$extIDMatch]['contact_is_deleted'] == 1) {
1998 // If the contact is deleted, update external identifier to be blank
1999 // to avoid key error from MySQL.
2000 $params = ['id' => $extIDMatch, 'external_identifier' => ''];
2001 civicrm_api3('Contact', 'create', $params);
2003 // And now it is no longer a match.
2008 $checkParams = ['check_permissions' => FALSE, 'match' => $params];
2009 $checkParams['match']['contact_type'] = $this->_contactType
;
2011 $possibleMatches = civicrm_api3('Contact', 'duplicatecheck', $checkParams);
2013 return array_keys($possibleMatches['values']);
2015 if ($possibleMatches['count']) {
2016 if (in_array($extIDMatch, array_keys($possibleMatches['values']))) {
2017 return [$extIDMatch];
2020 throw new CRM_Core_Exception(ts(
2021 'Matching this contact based on the de-dupe rule would cause an external ID conflict'));
2024 return [$extIDMatch];
2028 * Format the form mapping parameters ready for the parser.
2033 * @return array $parserParameters
2035 public static function getParameterForParser($count) {
2037 for ($i = 0; $i < $count; $i++
) {
2038 $baseArray[$i] = NULL;
2040 $parserParameters['mapperLocType'] = $baseArray;
2041 $parserParameters['mapperPhoneType'] = $baseArray;
2042 $parserParameters['mapperImProvider'] = $baseArray;
2043 $parserParameters['mapperWebsiteType'] = $baseArray;
2044 $parserParameters['mapperRelated'] = $baseArray;
2045 $parserParameters['relatedContactType'] = $baseArray;
2046 $parserParameters['relatedContactDetails'] = $baseArray;
2047 $parserParameters['relatedContactLocType'] = $baseArray;
2048 $parserParameters['relatedContactPhoneType'] = $baseArray;
2049 $parserParameters['relatedContactImProvider'] = $baseArray;
2050 $parserParameters['relatedContactWebsiteType'] = $baseArray;
2052 return $parserParameters;
2057 * Set field metadata.
2059 protected function setFieldMetadata() {
2060 $this->setImportableFieldsMetadata($this->getContactImportMetadata());
2061 // Probably no longer needed but here for now.
2062 $this->_relationships
= $this->getRelationships();