3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
36 //@todo calling api functions directly is not supported
37 require_once 'api/v3/utils.php';
38 require_once 'api/v3/Contact.php';
41 * class to parse contact csv files
43 class CRM_Import_Parser_Contact
extends CRM_Import_Parser
{
44 protected $_mapperKeys;
45 protected $_mapperLocType;
46 protected $_mapperPhoneType;
47 protected $_mapperImProvider;
48 protected $_mapperWebsiteType;
49 protected $_mapperRelated;
50 protected $_mapperRelatedContactType;
51 protected $_mapperRelatedContactDetails;
52 protected $_mapperRelatedContactEmailType;
53 protected $_mapperRelatedContactImProvider;
54 protected $_mapperRelatedContactWebsiteType;
55 protected $_relationships;
57 protected $_emailIndex;
58 protected $_firstNameIndex;
59 protected $_lastNameIndex;
61 protected $_householdNameIndex;
62 protected $_organizationNameIndex;
64 protected $_allEmails;
66 protected $_phoneIndex;
67 protected $_updateWithId;
70 protected $_externalIdentifierIndex;
71 protected $_allExternalIdentifiers;
72 protected $_parseStreetAddress;
75 * Array of successfully imported contact id's
79 protected $_newContacts;
86 protected $_lineCount;
89 * Array of successfully imported related contact id's
93 protected $_newRelatedContacts;
96 * array of all the contacts whose street addresses are not parsed
97 * of this import process
100 protected $_unparsedStreetAddressContacts;
105 function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL, $mapperImProvider = NULL, $mapperRelated = NULL, $mapperRelatedContactType = NULL, $mapperRelatedContactDetails = NULL, $mapperRelatedContactLocType = NULL, $mapperRelatedContactPhoneType = NULL, $mapperRelatedContactImProvider = NULL,
106 $mapperWebsiteType = NULL, $mapperRelatedContactWebsiteType = NULL
108 parent
::__construct();
109 $this->_mapperKeys
= &$mapperKeys;
110 $this->_mapperLocType
= &$mapperLocType;
111 $this->_mapperPhoneType
= &$mapperPhoneType;
112 $this->_mapperWebsiteType
= $mapperWebsiteType;
113 // get IM service provider type id for contact
114 $this->_mapperImProvider
= &$mapperImProvider;
115 $this->_mapperRelated
= &$mapperRelated;
116 $this->_mapperRelatedContactType
= &$mapperRelatedContactType;
117 $this->_mapperRelatedContactDetails
= &$mapperRelatedContactDetails;
118 $this->_mapperRelatedContactLocType
= &$mapperRelatedContactLocType;
119 $this->_mapperRelatedContactPhoneType
= &$mapperRelatedContactPhoneType;
120 $this->_mapperRelatedContactWebsiteType
= $mapperRelatedContactWebsiteType;
121 // get IM service provider type id for related contact
122 $this->_mapperRelatedContactImProvider
= &$mapperRelatedContactImProvider;
126 * the initializer code, called before the processing
132 $contactFields = CRM_Contact_BAO_Contact
::importableFields($this->_contactType
);
133 // exclude the address options disabled in the Address Settings
134 $fields = CRM_Core_BAO_Address
::validateAddressOptions($contactFields);
137 //supporting import for contact subtypes
139 if (!empty($this->_contactSubType
)) {
140 //custom fields for sub type
141 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($this->_contactSubType
);
143 if (!empty($subTypeFields)) {
144 foreach ($subTypeFields as $customSubTypeField => $details) {
145 $fields[$customSubTypeField] = $details;
150 //Relationship importables
151 $this->_relationships
= $relations =
152 CRM_Contact_BAO_Relationship
::getContactRelationshipType(
153 NULL, NULL, NULL, $this->_contactType
,
154 FALSE, 'label', TRUE, $this->_contactSubType
158 foreach ($relations as $key => $var) {
159 list($type) = explode('_', $key);
160 $relationshipType[$key]['title'] = $var;
161 $relationshipType[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
162 $relationshipType[$key]['import'] = TRUE;
163 $relationshipType[$key]['relationship_type_id'] = $type;
164 $relationshipType[$key]['related'] = TRUE;
167 if (!empty($relationshipType)) {
168 $fields = array_merge($fields, array(
170 'title' => ts('- related contact info -'),
172 ), $relationshipType);
175 foreach ($fields as $name => $field) {
176 $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));
179 $this->_newContacts
= array();
181 $this->setActiveFields($this->_mapperKeys
);
182 $this->setActiveFieldLocationTypes($this->_mapperLocType
);
183 $this->setActiveFieldPhoneTypes($this->_mapperPhoneType
);
184 $this->setActiveFieldWebsiteTypes($this->_mapperWebsiteType
);
185 //set active fields of IM provider of contact
186 $this->setActiveFieldImProviders($this->_mapperImProvider
);
189 $this->setActiveFieldRelated($this->_mapperRelated
);
190 $this->setActiveFieldRelatedContactType($this->_mapperRelatedContactType
);
191 $this->setActiveFieldRelatedContactDetails($this->_mapperRelatedContactDetails
);
192 $this->setActiveFieldRelatedContactLocType($this->_mapperRelatedContactLocType
);
193 $this->setActiveFieldRelatedContactPhoneType($this->_mapperRelatedContactPhoneType
);
194 $this->setActiveFieldRelatedContactWebsiteType($this->_mapperRelatedContactWebsiteType
);
195 //set active fields of IM provider of related contact
196 $this->setActiveFieldRelatedContactImProvider($this->_mapperRelatedContactImProvider
);
198 $this->_phoneIndex
= -1;
199 $this->_emailIndex
= -1;
200 $this->_firstNameIndex
= -1;
201 $this->_lastNameIndex
= -1;
202 $this->_householdNameIndex
= -1;
203 $this->_organizationNameIndex
= -1;
204 $this->_externalIdentifierIndex
= -1;
207 foreach ($this->_mapperKeys
as $key) {
208 if (substr($key, 0, 5) == 'email' && substr($key, 0, 14) != 'email_greeting') {
209 $this->_emailIndex
= $index;
210 $this->_allEmails
= array();
212 if (substr($key, 0, 5) == 'phone') {
213 $this->_phoneIndex
= $index;
215 if ($key == 'first_name') {
216 $this->_firstNameIndex
= $index;
218 if ($key == 'last_name') {
219 $this->_lastNameIndex
= $index;
221 if ($key == 'household_name') {
222 $this->_householdNameIndex
= $index;
224 if ($key == 'organization_name') {
225 $this->_organizationNameIndex
= $index;
228 if ($key == 'external_identifier') {
229 $this->_externalIdentifierIndex
= $index;
230 $this->_allExternalIdentifiers
= array();
235 $this->_updateWithId
= FALSE;
236 if (in_array('id', $this->_mapperKeys
) ||
($this->_externalIdentifierIndex
>= 0 && in_array($this->_onDuplicate
, array(
237 CRM_Import_Parser
::DUPLICATE_UPDATE
,
238 CRM_Import_Parser
::DUPLICATE_FILL
,
240 $this->_updateWithId
= TRUE;
243 $this->_parseStreetAddress
= CRM_Utils_Array
::value('street_address_parsing', CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'address_options'), FALSE);
247 * handle the values in mapField mode
249 * @param array $values the array of values belonging to this line
254 function mapField(&$values) {
255 return CRM_Import_Parser
::VALID
;
259 * handle the values in preview mode
261 * @param array $values the array of values belonging to this line
263 * @return boolean the result of this processing
266 function preview(&$values) {
267 return $this->summary($values);
271 * handle the values in summary mode
273 * @param array $values the array of values belonging to this line
275 * @return boolean the result of this processing
278 function summary(&$values) {
279 $response = $this->setActiveFieldValues($values);
281 $errorMessage = NULL;
282 $errorRequired = FALSE;
283 switch ($this->_contactType
) {
285 $missingNames = array();
286 if ($this->_firstNameIndex
< 0 ||
!CRM_Utils_Array
::value($this->_firstNameIndex
, $values)) {
287 $errorRequired = TRUE;
288 $missingNames[] = ts('First Name');
290 if ($this->_lastNameIndex
< 0 ||
!CRM_Utils_Array
::value($this->_lastNameIndex
, $values)) {
291 $errorRequired = TRUE;
292 $missingNames[] = ts('Last Name');
294 if ($errorRequired) {
295 $and = ' ' . ts('and') . ' ';
296 $errorMessage = ts('Missing required fields:') . ' ' . implode($and, $missingNames);
301 if ($this->_householdNameIndex
< 0 ||
!CRM_Utils_Array
::value($this->_householdNameIndex
, $values)) {
302 $errorRequired = TRUE;
303 $errorMessage = ts('Missing required fields:') . ' ' . ts('Household Name');
308 if ($this->_organizationNameIndex
< 0 ||
!CRM_Utils_Array
::value($this->_organizationNameIndex
, $values)) {
309 $errorRequired = TRUE;
310 $errorMessage = ts('Missing required fields:') . ' ' . ts('Organization Name');
315 $statusFieldName = $this->_statusFieldName
;
317 if ($this->_emailIndex
>= 0) {
318 /* If we don't have the required fields, bail */
320 if ($this->_contactType
== 'Individual' && !$this->_updateWithId
) {
321 if ($errorRequired && !CRM_Utils_Array
::value($this->_emailIndex
, $values)) {
323 $errorMessage .= ' ' . ts('OR') . ' ' . ts('Email Address');
326 $errorMessage = ts('Missing required field:') . ' ' . ts('Email Address');
328 array_unshift($values, $errorMessage);
329 $importRecordParams = array(
330 $statusFieldName => 'ERROR',
331 "${statusFieldName}Msg" => $errorMessage,
333 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
335 return CRM_Import_Parser
::ERROR
;
339 $email = CRM_Utils_Array
::value($this->_emailIndex
, $values);
341 /* If the email address isn't valid, bail */
343 if (!CRM_Utils_Rule
::email($email)) {
344 $errorMessage = ts('Invalid Email address');
345 array_unshift($values, $errorMessage);
346 $importRecordParams = array(
347 $statusFieldName => 'ERROR',
348 "${statusFieldName}Msg" => $errorMessage,
350 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
352 return CRM_Import_Parser
::ERROR
;
355 /* otherwise, count it and move on */
356 $this->_allEmails
[$email] = $this->_lineCount
;
359 elseif ($errorRequired && !$this->_updateWithId
) {
361 $errorMessage .= ' ' . ts('OR') . ' ' . ts('Email Address');
364 $errorMessage = ts('Missing required field:') . ' ' . ts('Email Address');
366 array_unshift($values, $errorMessage);
367 $importRecordParams = array(
368 $statusFieldName => 'ERROR',
369 "${statusFieldName}Msg" => $errorMessage,
371 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
373 return CRM_Import_Parser
::ERROR
;
376 //check for duplicate external Identifier
377 $externalID = CRM_Utils_Array
::value($this->_externalIdentifierIndex
, $values);
379 /* If it's a dupe,external Identifier */
381 if ($externalDupe = CRM_Utils_Array
::value($externalID, $this->_allExternalIdentifiers
)) {
382 $errorMessage = ts('External Identifier conflicts with record %1', array(1 => $externalDupe));
383 array_unshift($values, $errorMessage);
384 $importRecordParams = array(
385 $statusFieldName => 'ERROR',
386 "${statusFieldName}Msg" => $errorMessage,
388 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
389 return CRM_Import_Parser
::ERROR
;
391 //otherwise, count it and move on
392 $this->_allExternalIdentifiers
[$externalID] = $this->_lineCount
;
395 //Checking error in custom data
396 $params = &$this->getActiveFieldParams();
397 $params['contact_type'] = $this->_contactType
;
398 //date-format part ends
400 $errorMessage = NULL;
403 //add custom fields for contact sub type
405 if (!empty($this->_contactSubType
)) {
406 $csType = $this->_contactSubType
;
409 //checking error in custom data
410 $this->isErrorInCustomData($params, $errorMessage, $csType, $this->_relationships
);
412 //checking error in core data
413 $this->isErrorInCoreData($params, $errorMessage);
415 $tempMsg = "Invalid value for field(s) : $errorMessage";
416 // put the error message in the import record in the DB
417 $importRecordParams = array(
418 $statusFieldName => 'ERROR',
419 "${statusFieldName}Msg" => $tempMsg,
421 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
422 array_unshift($values, $tempMsg);
423 $errorMessage = NULL;
424 return CRM_Import_Parser
::ERROR
;
427 //if user correcting errors by walking back
428 //need to reset status ERROR msg to null
429 //now currently we are having valid data.
430 $importRecordParams = array(
431 $statusFieldName => 'NEW',
433 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
435 return CRM_Import_Parser
::VALID
;
439 * handle the values in import mode
441 * @param int $onDuplicate the code for what action to take on duplicates
442 * @param array $values the array of values belonging to this line
444 * @return boolean the result of this processing
447 function import($onDuplicate, &$values, $doGeocodeAddress = FALSE) {
448 $config = CRM_Core_Config
::singleton();
449 $this->_unparsedStreetAddressContacts
= array();
450 if (!$doGeocodeAddress) {
451 // CRM-5854, reset the geocode method to null to prevent geocoding
452 $config->geocodeMethod
= NULL;
455 // first make sure this is a valid line
456 //$this->_updateWithId = false;
457 $response = $this->summary($values);
458 $statusFieldName = $this->_statusFieldName
;
460 if ($response != CRM_Import_Parser
::VALID
) {
461 $importRecordParams = array(
462 $statusFieldName => 'INVALID',
463 "${statusFieldName}Msg" => "Invalid (Error Code: $response)",
465 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
469 $params = &$this->getActiveFieldParams();
471 'contact_type' => $this->_contactType
,
474 static $contactFields = NULL;
475 if ($contactFields == NULL) {
476 $contactFields = CRM_Contact_DAO_Contact
::import();
479 //check if external identifier exists in database
480 if (CRM_Utils_Array
::value('external_identifier', $params) && (CRM_Utils_Array
::value('id', $params) ||
in_array($onDuplicate, array(
481 CRM_Import_Parser
::DUPLICATE_SKIP
,
482 CRM_Import_Parser
::DUPLICATE_NOCHECK
,
485 if ($internalCid = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['external_identifier'], 'id', 'external_identifier')) {
486 if ($internalCid != CRM_Utils_Array
::value('id', $params)) {
487 $errorMessage = ts('External Identifier already exists in database.');
488 array_unshift($values, $errorMessage);
489 $importRecordParams = array(
490 $statusFieldName => 'ERROR',
491 "${statusFieldName}Msg" => $errorMessage,
493 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
494 return CRM_Import_Parser
::DUPLICATE
;
499 if (!empty($this->_contactSubType
)) {
500 $params['contact_sub_type'] = $this->_contactSubType
;
503 if ($subType = CRM_Utils_Array
::value('contact_sub_type', $params)) {
504 if (CRM_Contact_BAO_ContactType
::isExtendsContactType($subType, $this->_contactType
, FALSE, 'label')) {
505 $subTypes = CRM_Contact_BAO_ContactType
::subTypePairs($this->_contactType
, FALSE, NULL);
506 $params['contact_sub_type'] = array_search($subType, $subTypes);
508 elseif (!CRM_Contact_BAO_ContactType
::isExtendsContactType($subType, $this->_contactType
)) {
509 $message = "Mismatched or Invalid Contact SubType.";
510 array_unshift($values, $message);
511 return CRM_Import_Parser
::NO_MATCH
;
515 //get contact id to format common data in update/fill mode,
516 //if external identifier is present, CRM-4423
517 if ($this->_updateWithId
&& !CRM_Utils_Array
::value('id', $params) && CRM_Utils_Array
::value('external_identifier', $params)) {
518 if ($cid = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['external_identifier'], 'id', 'external_identifier')) {
519 $formatted['id'] = $cid;
523 //format common data, CRM-4062
524 $this->formatCommonData($params, $formatted, $contactFields);
526 $relationship = FALSE;
527 $createNewContact = TRUE;
528 // Support Match and Update Via Contact ID
529 if ($this->_updateWithId
) {
530 $createNewContact = FALSE;
531 if (!CRM_Utils_Array
::value('id', $params) && CRM_Utils_Array
::value('external_identifier', $params)) {
533 $params['id'] = $cid;
536 //update contact if dedupe found contact id, CRM-4148
537 $dedupeParams = $formatted;
539 //special case to check dedupe if external id present.
540 //if we send external id dedupe will stop.
541 unset($dedupeParams['external_identifier']);
542 require_once 'CRM/Utils/DeprecatedUtils.php';
543 $checkDedupe = _civicrm_api3_deprecated_duplicate_formatted_contact($dedupeParams);
544 if (CRM_Core_Error
::isAPIError($checkDedupe, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
545 $matchingContactIds = explode(',', $checkDedupe['error_message']['params'][0]);
546 if (count($matchingContactIds) == 1) {
547 $params['id'] = array_pop($matchingContactIds);
550 $message = "More than one matching contact found for given criteria.";
551 array_unshift($values, $message);
552 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
556 $createNewContact = TRUE;
561 $error = _civicrm_api3_deprecated_duplicate_formatted_contact($formatted);
562 if (CRM_Core_Error
::isAPIError($error, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
563 $matchedIDs = explode(',', $error['error_message']['params'][0]);
564 if (count($matchedIDs) >= 1) {
566 foreach ($matchedIDs as $contactId) {
567 if ($params['id'] == $contactId) {
568 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_type');
570 if ($formatted['contact_type'] == $contactType) {
571 //validation of subtype for update mode
573 $contactSubType = NULL;
574 if (CRM_Utils_Array
::value('contact_sub_type', $params)) {
575 $contactSubType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_sub_type');
578 if (!empty($contactSubType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($params['id'], $contactSubType) && $contactSubType != CRM_Utils_Array
::value('contact_sub_type', $formatted))) {
580 $message = "Mismatched contact SubTypes :";
581 array_unshift($values, $message);
583 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
586 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactId, FALSE, $this->_dedupeRuleGroupID
);
588 $this->_retCode
= CRM_Import_Parser
::VALID
;
592 $message = "Mismatched contact Types :";
593 array_unshift($values, $message);
595 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
600 $message = "Mismatched contact IDs OR Mismatched contact Types :";
601 array_unshift($values, $message);
602 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
608 if (CRM_Utils_Array
::value('id', $params)) {
609 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_type');
611 if ($formatted['contact_type'] == $contactType) {
612 //validation of subtype for update mode
614 $contactSubType = NULL;
615 if (CRM_Utils_Array
::value('contact_sub_type', $params)) {
616 $contactSubType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_sub_type');
619 if (!empty($contactSubType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($params['id'], $contactSubType) && $contactSubType != CRM_Utils_Array
::value('contact_sub_type', $formatted))) {
621 $message = "Mismatched contact SubTypes :";
622 array_unshift($values, $message);
623 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
626 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $params['id'], FALSE, $this->_dedupeRuleGroupID
);
627 $this->_retCode
= CRM_Import_Parser
::VALID
;
631 $message = "Mismatched contact Types :";
632 array_unshift($values, $message);
633 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
637 // we should avoid multiple errors for single record
638 // since we have already retCode and we trying to force again.
639 if ($this->_retCode
!= CRM_Import_Parser
::NO_MATCH
) {
640 $message = "No contact found for this contact ID:" . $params['id'];
641 array_unshift($values, $message);
642 $this->_retCode
= CRM_Import_Parser
::NO_MATCH
;
648 //now we want to create new contact on update/fill also.
649 $createNewContact = TRUE;
653 if (isset($newContact) && is_a($newContact, 'CRM_Contact_BAO_Contact')) {
654 $relationship = TRUE;
656 elseif (is_a($error, 'CRM_Core_Error')) {
657 $newContact = $error;
658 $relationship = TRUE;
663 //now we create new contact in update/fill mode also.
664 if ($createNewContact) {
666 //CRM-4430, don't carry if not submitted.
672 if (array_key_exists($name, $formatted)) {
673 if (in_array($name, array(
677 $formattedName = "individual_{$name}";
678 $formatted[$formattedName] = CRM_Core_OptionGroup
::getValue($formattedName, (string) $formatted[$name]);
681 $formatted[$name] = CRM_Core_OptionGroup
::getValue($name, (string) $formatted[$name]);
685 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, NULL, TRUE, $this->_dedupeRuleGroupID
);
689 if (isset($newContact) && is_object($newContact) && ($newContact instanceof CRM_Contact_BAO_Contact
)) {
690 $relationship = TRUE;
691 $newContact = clone($newContact);
692 $contactID = $newContact->id
;
693 $this->_newContacts
[] = $contactID;
695 //get return code if we create new contact in update mode, CRM-4148
696 if ($this->_updateWithId
) {
697 $this->_retCode
= CRM_Import_Parser
::VALID
;
700 elseif (isset($newContact) && CRM_Core_Error
::isAPIError($newContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
701 // if duplicate, no need of further processing
702 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_SKIP
) {
703 $errorMessage = "Skipping duplicate record";
704 array_unshift($values, $errorMessage);
705 $importRecordParams = array(
706 $statusFieldName => 'DUPLICATE',
707 "${statusFieldName}Msg" => $errorMessage,
709 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
710 return CRM_Import_Parser
::DUPLICATE
;
713 $relationship = TRUE;
714 # see CRM-10433 - might return comma separate list of all dupes
715 $dupeContactIDs = explode(',',$newContact['error_message']['params'][0]);
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 (CRM_Utils_Rule
::integer($newContact['error_message']['params'][0])) {
747 $primaryContactId = $newContact['error_message']['params'][0];
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;
784 $relationType->free();
786 $contactFields = NULL;
787 $contactFields = CRM_Contact_DAO_Contact
::import();
789 //Relation on the basis of External Identifier.
790 if (!CRM_Utils_Array
::value('id', $params[$key]) && !empty($params[$key]['external_identifier'])) {
791 $params[$key]['id'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['external_identifier'], 'id', 'external_identifier');
793 // check for valid related contact id in update/fill mode, CRM-4424
794 if (in_array($onDuplicate, array(
795 CRM_Import_Parser
::DUPLICATE_UPDATE
,
796 CRM_Import_Parser
::DUPLICATE_FILL
,
797 )) && CRM_Utils_Array
::value('id', $params[$key])) {
798 $relatedContactType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['id'], 'contact_type');
799 if (!$relatedContactType) {
800 $errorMessage = ts("No contact found for this related contact ID: %1", array(1 => $params[$key]['id']));
801 array_unshift($values, $errorMessage);
802 return CRM_Import_Parser
::NO_MATCH
;
805 //validation of related contact subtype for update mode
807 $relatedCsType = NULL;
808 if (CRM_Utils_Array
::value('contact_sub_type', $formatting)) {
809 $relatedCsType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['id'], 'contact_sub_type');
812 if (!empty($relatedCsType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($params[$key]['id'], $relatedCsType) &&
813 $relatedCsType != CRM_Utils_Array
::value('contact_sub_type', $formatting))) {
814 $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact ID: %1", array(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 (CRM_Utils_Array
::value('id', $params[$key])) {
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);
854 $matchedIDs = array();
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 = explode(',', $relatedNewContact['error_message']['params'][0]);
862 $errorMessage = $relatedNewContact['error_message'];
863 array_unshift($values, $errorMessage);
864 $importRecordParams = array(
865 $statusFieldName => 'ERROR',
866 "${statusFieldName}Msg" => $errorMessage,
868 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
869 return CRM_Import_Parser
::ERROR
;
873 $matchedIDs[] = $relatedNewContact->id
;
875 // update/fill related contact after getting matching Contact Ids, CRM-4424
876 if (in_array($onDuplicate, array(
877 CRM_Import_Parser
::DUPLICATE_UPDATE
,
878 CRM_Import_Parser
::DUPLICATE_FILL
,
880 //validation of related contact subtype for update mode
882 $relatedCsType = NULL;
883 if (CRM_Utils_Array
::value('contact_sub_type', $formatting)) {
884 $relatedCsType = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $matchedIDs[0], 'contact_sub_type');
887 if (!empty($relatedCsType) && (!CRM_Contact_BAO_ContactType
::isAllowEdit($matchedIDs[0], $relatedCsType) && $relatedCsType != CRM_Utils_Array
::value('contact_sub_type', $formatting))) {
888 $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact.");
889 array_unshift($values, $errorMessage);
890 return CRM_Import_Parser
::NO_MATCH
;
893 $updatedContact = $this->createContact($formatting, $contactFields, $onDuplicate, $matchedIDs[0]);
896 static $relativeContact = array();
897 if (CRM_Core_Error
::isAPIError($relatedNewContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
898 if (count($matchedIDs) >= 1) {
899 $relContactId = $matchedIDs[0];
900 //add relative contact to count during update & fill mode.
901 //logic to make count distinct by contact id.
902 if ($this->_newRelatedContacts ||
!empty($relativeContact)) {
903 $reContact = array_keys($relativeContact, $relContactId);
905 if (empty($reContact)) {
906 $this->_newRelatedContacts
[] = $relativeContact[] = $relContactId;
910 $this->_newRelatedContacts
[] = $relativeContact[] = $relContactId;
915 $relContactId = $relatedNewContact->id
;
916 $this->_newRelatedContacts
[] = $relativeContact[] = $relContactId;
919 if (CRM_Core_Error
::isAPIError($relatedNewContact, CRM_Core_ERROR
::DUPLICATE_CONTACT
) ||
($relatedNewContact instanceof CRM_Contact_BAO_Contact
)) {
920 //fix for CRM-1993.Checks for duplicate related contacts
921 if (count($matchedIDs) >= 1) {
922 //if more than one duplicate contact
923 //found, create relationship with first contact
924 // now create the relationship record
925 $relationParams = array();
926 $relationParams = array(
927 'relationship_type_id' => $key,
928 'contact_check' => array(
932 'skipRecentView' => TRUE,
935 // we only handle related contact success, we ignore failures for now
936 // at some point wold be nice to have related counts as separate
937 $relationIds = array(
938 'contact' => $primaryContactId,
941 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship
::create($relationParams, $relationIds);
943 if ($valid ||
$duplicate) {
944 $relationIds['contactTarget'] = $relContactId;
945 $action = ($duplicate) ? CRM_Core_Action
::UPDATE
: CRM_Core_Action
::ADD
;
946 CRM_Contact_BAO_Relationship
::relatedMemberships($primaryContactId, $relationParams, $relationIds, $action);
949 //handle current employer, CRM-3532
951 $allRelationships = CRM_Core_PseudoConstant
::relationshipType('name');
952 $relationshipTypeId = str_replace(array(
959 $relationshipType = str_replace($relationshipTypeId . '_', '', $key);
960 $orgId = $individualId = NULL;
961 if ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employee of') {
962 $orgId = $relContactId;
963 $individualId = $primaryContactId;
965 elseif ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employer of') {
966 $orgId = $primaryContactId;
967 $individualId = $relContactId;
969 if ($orgId && $individualId) {
970 $currentEmpParams[$individualId] = $orgId;
971 CRM_Contact_BAO_Contact_Utils
::setCurrentEmployer($currentEmpParams);
979 if ($this->_updateWithId
) {
980 //return warning if street address is unparsed, CRM-5886
981 return $this->processMessage($values, $statusFieldName, $this->_retCode
);
984 if (is_array($newContact) && civicrm_error($newContact)) {
987 if (($code = CRM_Utils_Array
::value('code', $newContact['error_message'])) && ($code == CRM_Core_Error
::DUPLICATE_CONTACT
)) {
989 // need to fix at some stage and decide if the error will return an
990 // array or string, crude hack for now
991 if (is_array($newContact['error_message']['params'][0])) {
992 $cids = $newContact['error_message']['params'][0];
995 $cids = explode(',', $newContact['error_message']['params'][0]);
998 foreach ($cids as $cid) {
999 $urls[] = CRM_Utils_System
::url('civicrm/contact/view', 'reset=1&cid=' . $cid, TRUE);
1002 $url_string = implode("\n", $urls);
1004 // If we duplicate more than one record, skip no matter what
1005 if (count($cids) > 1) {
1006 $errorMessage = ts('Record duplicates multiple contacts');
1007 $importRecordParams = array(
1008 $statusFieldName => 'ERROR',
1009 "${statusFieldName}Msg" => $errorMessage,
1012 //combine error msg to avoid mismatch between error file columns.
1013 $errorMessage .= "\n" . $url_string;
1014 array_unshift($values, $errorMessage);
1015 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
1016 return CRM_Import_Parser
::ERROR
;
1019 // Params only had one id, so shift it out
1020 $contactId = array_shift($cids);
1023 $vals = array('contact_id' => $contactId);
1025 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_REPLACE
) {
1026 civicrm_api('contact', 'delete', $vals);
1027 $cid = CRM_Contact_BAO_Contact
::createProfileContact($formatted, $contactFields, $contactId, NULL, NULL, $formatted['contact_type']);
1029 elseif ($onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
) {
1030 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactId);
1032 elseif ($onDuplicate == CRM_Import_Parser
::DUPLICATE_FILL
) {
1033 $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactId);
1035 // else skip does nothing and just returns an error code.
1040 'contact_id' => $cid,
1042 $defaults = array();
1043 $newContact = CRM_Contact_BAO_Contact
::retrieve($contact, $defaults);
1046 if (civicrm_error($newContact)) {
1047 $contactID = $newContact['error_message']['params'][0];
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 = array(
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 = array(
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 = array(
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
1092 function &getImportedContacts() {
1093 return $this->_newContacts
;
1097 * Get the array of successfully imported related contact id's
1102 function &getRelatedImportedContacts() {
1103 return $this->_newRelatedContacts
;
1107 * the initializer code, called before the processing
1115 * function to check if an error in custom data
1117 * @param String $errorMessage A string containing all the error-fields.
1121 static function isErrorInCustomData($params, &$errorMessage, $csType = NULL, $relationships = NULL) {
1122 $session = CRM_Core_Session
::singleton();
1123 $dateType = $session->get("dateTypes");
1125 if (CRM_Utils_Array
::value('contact_sub_type', $params)) {
1126 $csType = CRM_Utils_Array
::value('contact_sub_type', $params);
1129 if (!CRM_Utils_Array
::value('contact_type', $params)) {
1130 $params['contact_type'] = 'Individual';
1132 $customFields = CRM_Core_BAO_CustomField
::getFields($params['contact_type'], FALSE, FALSE, $csType);
1134 $addressCustomFields = CRM_Core_BAO_CustomField
::getFields('Address');
1135 $customFields = $customFields +
$addressCustomFields;
1136 foreach ($params as $key => $value) {
1137 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1138 /* check if it's a valid custom field id */
1140 if (!array_key_exists($customFieldID, $customFields)) {
1141 self
::addToErrorMsg(ts('field ID'), $errorMessage);
1143 //For address custom fields, we do get actual custom field value as an inner array of
1144 //values so need to modify
1145 if (array_key_exists($customFieldID, $addressCustomFields)) {
1146 $value = $value[0][$key];
1148 /* validate the data against the CF type */
1151 if ($customFields[$customFieldID]['data_type'] == 'Date') {
1152 if (array_key_exists($customFieldID, $addressCustomFields) && CRM_Utils_Date
::convertToDefaultDate($params[$key][0], $dateType, $key)) {
1153 $value = $params[$key][0][$key];
1155 else if (CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key)) {
1156 $value = $params[$key];
1159 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1162 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
1163 if (CRM_Utils_String
::strtoboolstr($value) === FALSE) {
1164 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1167 // need not check for label filed import
1174 'Multi-Select State/Province',
1175 'Multi-Select Country',
1177 if (!in_array($customFields[$customFieldID]['html_type'], $htmlType) ||
$customFields[$customFieldID]['data_type'] == 'Boolean' ||
$customFields[$customFieldID]['data_type'] == 'ContactReference') {
1178 $valid = CRM_Core_BAO_CustomValue
::typecheck($customFields[$customFieldID]['data_type'], $value);
1180 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1184 // check for values for custom fields for checkboxes and multiselect
1185 if ($customFields[$customFieldID]['html_type'] == 'CheckBox' ||
$customFields[$customFieldID]['html_type'] == 'AdvMulti-Select' ||
$customFields[$customFieldID]['html_type'] == 'Multi-Select') {
1186 $value = trim($value);
1187 $value = str_replace('|', ',', $value);
1188 $mulValues = explode(',', $value);
1189 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1190 foreach ($mulValues as $v1) {
1191 if (strlen($v1) == 0) {
1196 foreach ($customOption as $v2) {
1197 if ((strtolower(trim($v2['label'])) == strtolower(trim($v1))) ||
(strtolower(trim($v2['value'])) == strtolower(trim($v1)))) {
1203 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1207 elseif ($customFields[$customFieldID]['html_type'] == 'Select' ||
($customFields[$customFieldID]['html_type'] == 'Radio' && $customFields[$customFieldID]['data_type'] != 'Boolean')) {
1208 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1210 foreach ($customOption as $v2) {
1211 if ((strtolower(trim($v2['label'])) == strtolower(trim($value))) ||
(strtolower(trim($v2['value'])) == strtolower(trim($value)))) {
1216 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1219 elseif ($customFields[$customFieldID]['html_type'] == 'Multi-Select State/Province') {
1220 $mulValues = explode(',', $value);
1221 foreach ($mulValues as $stateValue) {
1223 if (self
::in_value(trim($stateValue), CRM_Core_PseudoConstant
::stateProvinceAbbreviation()) || self
::in_value(trim($stateValue), CRM_Core_PseudoConstant
::stateProvince())) {
1227 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1232 elseif ($customFields[$customFieldID]['html_type'] == 'Multi-Select Country') {
1233 $mulValues = explode(',', $value);
1234 foreach ($mulValues as $countryValue) {
1235 if ($countryValue) {
1236 CRM_Core_PseudoConstant
::populate($countryNames, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active');
1237 CRM_Core_PseudoConstant
::populate($countryIsoCodes, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
1238 $config = CRM_Core_Config
::singleton();
1239 $limitCodes = $config->countryLimit();
1247 if (in_array(trim($countryValue), $values)) {
1254 self
::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
1261 elseif (is_array($params[$key]) && isset($params[$key]["contact_type"])) {
1263 //supporting custom data of related contact subtypes
1265 if ($relationships) {
1266 if (array_key_exists($key, $relationships)) {
1269 elseif (CRM_Utils_Array
::key($key, $relationships)) {
1270 $relation = CRM_Utils_Array
::key($key, $relationships);
1273 if (!empty($relation)) {
1274 list($id, $first, $second) = CRM_Utils_System
::explode('_', $relation, 3);
1275 $direction = "contact_sub_type_$second";
1276 $relationshipType = new CRM_Contact_BAO_RelationshipType();
1277 $relationshipType->id
= $id;
1278 if ($relationshipType->find(TRUE)) {
1279 if (isset($relationshipType->$direction)) {
1280 $params[$key]['contact_sub_type'] = $relationshipType->$direction;
1283 $relationshipType->free();
1286 self
::isErrorInCustomData($params[$key], $errorMessage, $csType, $relationships);
1292 * Check if value present in all genders or
1293 * as a substring of any gender value, if yes than return corresponding gender.
1294 * eg value might be m/M, ma/MA, mal/MAL, male return 'Male'
1295 * but if value is 'maleabc' than return false
1297 * @param string $gender check this value across gender values.
1299 * retunr gender value / false
1302 public function checkGender($gender) {
1303 $gender = trim($gender, '.');
1308 $allGenders = CRM_Core_PseudoConstant
::gender();
1309 foreach ($allGenders as $key => $value) {
1310 if (strlen($gender) > strlen($value)) {
1313 if ($gender == $value) {
1316 if (substr_compare($value, $gender, 0, strlen($gender), TRUE) === 0) {
1325 * function to check if an error in Core( non-custom fields ) field
1327 * @param String $errorMessage A string containing all the error-fields.
1331 function isErrorInCoreData($params, &$errorMessage) {
1332 foreach ($params as $key => $value) {
1334 $session = CRM_Core_Session
::singleton();
1335 $dateType = $session->get("dateTypes");
1339 if (CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key)) {
1340 if (!CRM_Utils_Rule
::date($params[$key])) {
1341 self
::addToErrorMsg(ts('Birth Date'), $errorMessage);
1345 self
::addToErrorMsg(ts('Birth-Date'), $errorMessage);
1349 case 'deceased_date':
1350 if (CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key)) {
1351 if (!CRM_Utils_Rule
::date($params[$key])) {
1352 self
::addToErrorMsg(ts('Deceased Date'), $errorMessage);
1356 self
::addToErrorMsg(ts('Deceased Date'), $errorMessage);
1361 if (CRM_Utils_String
::strtoboolstr($value) === FALSE) {
1362 self
::addToErrorMsg(ts('Is Deceased'), $errorMessage);
1367 if (!self
::checkGender($value)) {
1368 self
::addToErrorMsg(ts('Gender'), $errorMessage);
1372 case 'preferred_communication_method':
1373 $preffComm = array();
1374 $preffComm = explode(',', $value);
1375 foreach ($preffComm as $v) {
1376 if (!self
::in_value(trim($v), CRM_Core_PseudoConstant
::pcm())) {
1377 self
::addToErrorMsg(ts('Preferred Communication Method'), $errorMessage);
1382 case 'preferred_mail_format':
1383 if (!array_key_exists(strtolower($value), array_change_key_case(CRM_Core_SelectValues
::pmf(), CASE_LOWER
))) {
1384 self
::addToErrorMsg(ts('Preferred Mail Format'), $errorMessage);
1388 case 'individual_prefix':
1389 if (!self
::in_value($value, CRM_Core_PseudoConstant
::individualPrefix())) {
1390 self
::addToErrorMsg(ts('Individual Prefix'), $errorMessage);
1394 case 'individual_suffix':
1395 if (!self
::in_value($value, CRM_Core_PseudoConstant
::individualSuffix())) {
1396 self
::addToErrorMsg(ts('Individual Suffix'), $errorMessage);
1400 case 'state_province':
1401 if (!empty($value)) {
1402 foreach ($value as $stateValue) {
1403 if ($stateValue['state_province']) {
1404 if (self
::in_value($stateValue['state_province'], CRM_Core_PseudoConstant
::stateProvinceAbbreviation()) ||
1405 self
::in_value($stateValue['state_province'], CRM_Core_PseudoConstant
::stateProvince())) {
1409 self
::addToErrorMsg(ts('State / Province'), $errorMessage);
1417 if (!empty($value)) {
1418 foreach ($value as $stateValue) {
1419 if ($stateValue['country']) {
1420 CRM_Core_PseudoConstant
::populate($countryNames, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active');
1421 CRM_Core_PseudoConstant
::populate($countryIsoCodes, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
1422 $config = CRM_Core_Config
::singleton();
1423 $limitCodes = $config->countryLimit();
1424 //If no country is selected in
1425 //localization then take all countries
1426 if (empty($limitCodes)) {
1427 $limitCodes = $countryIsoCodes;
1430 if (self
::in_value($stateValue['country'], $limitCodes) || self
::in_value($stateValue['country'], CRM_Core_PseudoConstant
::country())) {
1434 if (self
::in_value($stateValue['country'], $countryIsoCodes) || self
::in_value($stateValue['country'], $countryNames)) {
1435 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 Global Settings >> Localization." '), $errorMessage);
1438 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);
1447 if (!empty($value)) {
1448 foreach ($value as $county) {
1449 if ($county['county']) {
1450 $countyNames = CRM_Core_PseudoConstant
::county();
1451 if (!empty($county['county']) && !in_array($county['county'], $countyNames)) {
1452 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);
1460 if (!empty($value)) {
1461 foreach ($value as $codeValue) {
1462 if (CRM_Utils_Array
::value('geo_code_1', $codeValue)) {
1463 if (CRM_Utils_Rule
::numeric($codeValue['geo_code_1'])) {
1467 self
::addToErrorMsg(ts('Geo code 1'), $errorMessage);
1475 if (!empty($value)) {
1476 foreach ($value as $codeValue) {
1477 if (CRM_Utils_Array
::value('geo_code_2', $codeValue)) {
1478 if (CRM_Utils_Rule
::numeric($codeValue['geo_code_2'])) {
1482 self
::addToErrorMsg(ts('Geo code 2'), $errorMessage);
1488 //check for any error in email/postal greeting, addressee,
1489 //custom email/postal greeting, custom addressee, CRM-4575
1491 case 'email_greeting':
1492 $emailGreetingFilter = array(
1493 'contact_type' => $this->_contactType
,
1494 'greeting_type' => 'email_greeting',
1496 if (!self
::in_value($value, CRM_Core_PseudoConstant
::greeting($emailGreetingFilter))) {
1497 self
::addToErrorMsg(ts('Email Greeting must be one of the configured format options. Check Administer >> Option Lists >> Email Greetings for valid values'), $errorMessage);
1501 case 'postal_greeting':
1502 $postalGreetingFilter = array(
1503 'contact_type' => $this->_contactType
,
1504 'greeting_type' => 'postal_greeting',
1506 if (!self
::in_value($value, CRM_Core_PseudoConstant
::greeting($postalGreetingFilter))) {
1507 self
::addToErrorMsg(ts('Postal Greeting must be one of the configured format options. Check Administer >> Option Lists >> Postal Greetings for valid values'), $errorMessage);
1512 $addresseeFilter = array(
1513 'contact_type' => $this->_contactType
,
1514 'greeting_type' => 'addressee',
1516 if (!self
::in_value($value, CRM_Core_PseudoConstant
::greeting($addresseeFilter))) {
1517 self
::addToErrorMsg(ts('Addressee must be one of the configured format options. Check Administer >> Option Lists >> Addressee for valid values'), $errorMessage);
1521 case 'email_greeting_custom':
1522 if (array_key_exists('email_greeting', $params)) {
1523 $emailGreetingLabel = key(CRM_Core_OptionGroup
::values('email_greeting', TRUE, NULL, NULL, 'AND v.name = "Customized"'));
1524 if (CRM_Utils_Array
::value('email_greeting', $params) != $emailGreetingLabel) {
1525 self
::addToErrorMsg(ts('Email Greeting - Custom'), $errorMessage);
1530 case 'postal_greeting_custom':
1531 if (array_key_exists('postal_greeting', $params)) {
1532 $postalGreetingLabel = key(CRM_Core_OptionGroup
::values('postal_greeting', TRUE, NULL, NULL, 'AND v.name = "Customized"'));
1533 if (CRM_Utils_Array
::value('postal_greeting', $params) != $postalGreetingLabel) {
1534 self
::addToErrorMsg(ts('Postal Greeting - Custom'), $errorMessage);
1539 case 'addressee_custom':
1540 if (array_key_exists('addressee', $params)) {
1541 $addresseeLabel = key(CRM_Core_OptionGroup
::values('addressee', TRUE, NULL, NULL, 'AND v.name = "Customized"'));
1542 if (CRM_Utils_Array
::value('addressee', $params) != $addresseeLabel) {
1543 self
::addToErrorMsg(ts('Addressee - Custom'), $errorMessage);
1549 if (is_array($value)) {
1550 foreach ($value as $values) {
1551 if (CRM_Utils_Array
::value('url', $values) && !CRM_Utils_Rule
::url($values['url'])) {
1552 self
::addToErrorMsg(ts('Website'), $errorMessage);
1559 case 'do_not_email':
1560 case 'do_not_phone':
1563 case 'do_not_trade':
1564 if (CRM_Utils_Rule
::boolean($value) == FALSE) {
1565 $key = ucwords(str_replace("_", " ", $key));
1566 self
::addToErrorMsg($key, $errorMessage);
1571 if (is_array($value)) {
1572 foreach ($value as $values) {
1573 if (CRM_Utils_Array
::value('email', $values) && !CRM_Utils_Rule
::email($values['email'])) {
1574 self
::addToErrorMsg($key, $errorMessage);
1582 if (is_array($params[$key]) && isset($params[$key]["contact_type"])) {
1583 //check for any relationship data ,FIX ME
1584 self
::isErrorInCoreData($params[$key], $errorMessage);
1592 * function to ckeck a value present or not in a array
1594 * @return ture if value present in array or retun false
1598 function in_value($value, $valueArray) {
1599 foreach ($valueArray as $key => $v) {
1601 if (strtolower(trim($v, ".")) == strtolower(trim($value, "."))) {
1609 * function to build error-message containing error-fields
1611 * @param String $errorName A string containing error-field name.
1612 * @param String $errorMessage A string containing all the error-fields, where the new errorName is concatenated.
1617 static function addToErrorMsg($errorName, &$errorMessage) {
1618 if ($errorMessage) {
1619 $errorMessage .= "; $errorName";
1622 $errorMessage = $errorName;
1627 * method for creating contact
1631 function createContact(&$formatted, &$contactFields, $onDuplicate, $contactId = NULL, $requiredCheck = TRUE, $dedupeRuleGroupID = NULL) {
1636 if (is_null($contactId) && ($onDuplicate != CRM_Import_Parser
::DUPLICATE_NOCHECK
)) {
1637 $dupeCheck = (bool)($onDuplicate);
1640 //get the prefix id etc if exists
1641 CRM_Contact_BAO_Contact
::resolveDefaults($formatted, TRUE);
1643 require_once 'CRM/Utils/DeprecatedUtils.php';
1644 //@todo direct call to API function not supported.
1645 // setting required check to false, CRM-2839
1646 // plus we do our own required check in import
1647 $error = _civicrm_api3_deprecated_contact_check_params($formatted, $dupeCheck, TRUE, FALSE, $dedupeRuleGroupID);
1649 if ((is_null($error)) && (civicrm_error(_civicrm_api3_deprecated_validate_formatted_contact($formatted)))) {
1650 $error = _civicrm_api3_deprecated_validate_formatted_contact($formatted);
1653 $newContact = $error;
1655 if (is_null($error)) {
1657 $this->formatParams($formatted, $onDuplicate, (int) $contactId);
1660 // pass doNotResetCache flag since resetting and rebuilding cache could be expensive.
1661 $config = CRM_Core_Config
::singleton();
1662 $config->doNotResetCache
= 1;
1663 $cid = CRM_Contact_BAO_Contact
::createProfileContact($formatted, $contactFields, $contactId, NULL, NULL, $formatted['contact_type']);
1664 $config->doNotResetCache
= 0;
1667 'contact_id' => $cid,
1670 $defaults = array();
1671 $newContact = CRM_Contact_BAO_Contact
::retrieve($contact, $defaults);
1674 //get the id of the contact whose street address is not parsable, CRM-5886
1675 if ($this->_parseStreetAddress
&& $newContact->address
) {
1676 foreach ($newContact->address
as $address) {
1677 if ($address['street_address'] && (!CRM_Utils_Array
::value('street_number', $address) ||
!CRM_Utils_Array
::value('street_name', $address))) {
1678 $this->_unparsedStreetAddressContacts
[] = array(
1679 'id' => $newContact->id
,
1680 'streetAddress' => $address['street_address'],
1689 * format params for update and fill mode
1691 * @param $params array referance to an array containg all the
1693 * @param $onDuplicate int
1694 * @param $cid int contact id
1696 function formatParams(&$params, $onDuplicate, $cid) {
1697 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_SKIP
) {
1701 $contactParams = array(
1702 'contact_id' => $cid,
1705 $defaults = array();
1706 $contactObj = CRM_Contact_BAO_Contact
::retrieve($contactParams, $defaults);
1708 $modeUpdate = $modeFill = FALSE;
1710 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
) {
1714 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_FILL
) {
1718 $groupTree = CRM_Core_BAO_CustomGroup
::getTree($params['contact_type'], CRM_Core_DAO
::$_nullObject, $cid, 0, NULL);
1719 CRM_Core_BAO_CustomGroup
::setDefaults($groupTree, $defaults, FALSE, FALSE);
1721 $locationFields = array(
1725 'website' => 'website',
1726 'address' => 'address',
1729 $contact = get_object_vars($contactObj);
1731 foreach ($params as $key => $value) {
1732 if ($key == 'id' ||
$key == 'contact_type') {
1736 if (array_key_exists($key, $locationFields)) {
1739 elseif (in_array($key, array(
1744 // CRM-4575, need to null custom
1745 if ($params["{$key}_id"] != 4) {
1746 $params["{$key}_custom"] = 'null';
1748 unset($params[$key]);
1750 elseif ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1754 $getValue = CRM_Utils_Array
::retrieveValueRecursive($contact, $key);
1756 if ($key == 'contact_source') {
1757 $params['source'] = $params[$key];
1758 unset($params[$key]);
1761 if ($modeFill && isset($getValue)) {
1762 unset($params[$key]);
1767 foreach ($locationFields as $locKeys) {
1768 if (is_array(CRM_Utils_Array
::value($locKeys, $params))) {
1769 foreach ($params[$locKeys] as $key => $value) {
1771 $getValue = CRM_Utils_Array
::retrieveValueRecursive($contact, $locKeys);
1773 if (isset($getValue)) {
1774 foreach ($getValue as $cnt => $values) {
1775 if ($locKeys == 'website') {
1776 if (($getValue[$cnt]['website_type_id'] == $params[$locKeys][$key]['website_type_id'])) {
1777 unset($params[$locKeys][$key]);
1781 if ($getValue[$cnt]['location_type_id'] == $params[$locKeys][$key]['location_type_id']) {
1782 unset($params[$locKeys][$key]);
1789 if (count($params[$locKeys]) == 0) {
1790 unset($params[$locKeys]);
1797 * convert any given date string to default date array.
1799 * @param array $params has given date-format
1800 * @param array $formatted store formatted date in this array
1801 * @param int $dateType type of date
1802 * @param string $dateParam index of params
1805 function formatCustomDate(&$params, &$formatted, $dateType, $dateParam) {
1807 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $dateParam);
1808 $formatted[$dateParam] = CRM_Utils_Date
::processDate($params[$dateParam]);
1812 * format common params data to proper format to store.
1814 * @param array $params contain record values.
1815 * @param array $formatted array of formatted data.
1816 * @param array $contactFields contact DAO fields.
1819 function formatCommonData($params, &$formatted, &$contactFields) {
1821 CRM_Utils_Array
::value('contact_type', $formatted),
1825 //add custom fields for contact sub type
1826 if (!empty($this->_contactSubType
)) {
1827 $csType = $this->_contactSubType
;
1830 if ($relCsType = CRM_Utils_Array
::value('contact_sub_type', $formatted)) {
1831 $csType = $relCsType;
1834 $customFields = CRM_Core_BAO_CustomField
::getFields($formatted['contact_type'], FALSE, FALSE, $csType);
1836 $addressCustomFields = CRM_Core_BAO_CustomField
::getFields('Address');
1837 $customFields = $customFields +
$addressCustomFields;
1839 //if a Custom Email Greeting, Custom Postal Greeting or Custom Addressee is mapped, and no "Greeting / Addressee Type ID" is provided, then automatically set the type = Customized, CRM-4575
1841 'email_greeting_custom' => 'email_greeting',
1842 'postal_greeting_custom' => 'postal_greeting',
1843 'addressee_custom' => 'addressee',
1845 foreach ($elements as $k => $v) {
1846 if (array_key_exists($k, $params) && !(array_key_exists($v, $params))) {
1847 $label = key(CRM_Core_OptionGroup
::values($v, TRUE, NULL, NULL, 'AND v.name = "Customized"'));
1848 $params[$v] = $label;
1853 $session = CRM_Core_Session
::singleton();
1854 $dateType = $session->get("dateTypes");
1855 foreach ($params as $key => $val) {
1856 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
1857 if ($customFieldID &&
1858 !array_key_exists($customFieldID, $addressCustomFields)) {
1859 //we should not update Date to null, CRM-4062
1860 if ($val && ($customFields[$customFieldID]['data_type'] == 'Date')) {
1861 self
::formatCustomDate($params, $formatted, $dateType, $key);
1863 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
1864 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
1868 if ($key == 'birth_date' && $val) {
1869 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key);
1871 elseif ($key == 'deceased_date' && $val) {
1872 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key);
1874 elseif ($key == 'is_deceased' && $val) {
1875 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
1877 elseif ($key == 'gender') {
1879 $params[$key] = $this->checkGender($val);
1883 //now format custom data.
1884 foreach ($params as $key => $field) {
1885 if (!isset($field)){
1886 // if ($field == NULL || $field === '') {
1887 unset($params[$key]);
1891 if (is_array($field)) {
1892 $isAddressCustomField = FALSE;
1893 foreach ($field as $value) {
1895 if (is_array($value)) {
1896 foreach ($value as $name => $testForEmpty) {
1897 if ($addressCustomFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
1898 $isAddressCustomField = TRUE;
1901 // check if $value does not contain IM provider or phoneType
1902 if (($name !== 'phone_type_id' ||
$name !== 'provider_id') && ($testForEmpty === '' ||
$testForEmpty == NULL)) {
1913 require_once 'CRM/Utils/DeprecatedUtils.php';
1914 _civicrm_api3_deprecated_add_formatted_param($value, $formatted);
1917 if (!$isAddressCustomField) {
1922 $formatValues = array(
1926 if (($key !== 'preferred_communication_method') && (array_key_exists($key, $contactFields))) {
1927 // due to merging of individual table and
1928 // contact table, we need to avoid
1929 // preferred_communication_method forcefully
1930 $formatValues['contact_type'] = $formatted['contact_type'];
1933 if ($key == 'id' && isset($field)) {
1934 $formatted[$key] = $field;
1936 require_once 'CRM/Utils/DeprecatedUtils.php';
1937 _civicrm_api3_deprecated_add_formatted_param($formatValues, $formatted);
1939 //Handling Custom Data
1940 // note: Address custom fields will be handled separately inside _civicrm_api3_deprecated_add_formatted_param
1941 if (($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) &&
1942 array_key_exists($customFieldID, $customFields) &&
1943 !array_key_exists($customFieldID, $addressCustomFields)) {
1945 $extends = CRM_Utils_Array
::value('extends', $customFields[$customFieldID]);
1946 $htmlType = CRM_Utils_Array
::value( 'html_type', $customFields[$customFieldID] );
1947 switch ( $htmlType ) {
1949 case 'AdvMulti-Select':
1950 case 'Multi-Select':
1952 if ( CRM_Utils_Array
::value( $key, $formatted ) && CRM_Utils_Array
::value( $key, $params ) ) {
1953 $mulValues = explode( ',', $formatted[$key] );
1954 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption( $customFieldID, true );
1955 $formatted[$key] = array( );
1956 $params[$key] = array( );
1957 foreach ( $mulValues as $v1 ) {
1958 foreach ( $customOption as $v2 ) {
1959 if ( ( strtolower( $v2['label'] ) == strtolower( trim( $v1 ) ) ) ||
1960 ( strtolower( $v2['value'] ) == strtolower( trim( $v1 ) ) ) ) {
1961 if ( $htmlType == 'CheckBox' ) {
1962 $params[$key][$v2['value']] = $formatted[$key][$v2['value']] = 1;
1964 $params[$key][] = $formatted[$key][] = $v2['value'];
1975 if (($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) && array_key_exists($customFieldID, $customFields) &&
1976 !array_key_exists($customFieldID, $addressCustomFields)) {
1977 // @todo calling api functions directly is not supported
1978 _civicrm_api3_custom_format_params($params, $formatted, $extends);
1981 // to check if not update mode and unset the fields with empty value.
1982 if (!$this->_updateWithId
&& array_key_exists('custom', $formatted)) {
1983 foreach ($formatted['custom'] as $customKey => $customvalue) {
1984 $emptyValue = CRM_Utils_Array
::value('value', $customvalue[ - 1]);
1985 if (!isset($emptyValue)) {
1986 unset($formatted['custom'][$customKey]);
1991 // parse street address, CRM-5450
1992 if ($this->_parseStreetAddress
) {
1993 if (array_key_exists('address', $formatted) && is_array($formatted['address'])) {
1994 foreach ($formatted['address'] as $instance => & $address) {
1995 $streetAddress = CRM_Utils_Array
::value('street_address', $address);
1996 if (empty($streetAddress)) {
1999 // parse address field.
2000 $parsedFields = CRM_Core_BAO_Address
::parseStreetAddress($streetAddress);
2002 //street address consider to be parsed properly,
2003 //If we get street_name and street_number.
2004 if (!CRM_Utils_Array
::value('street_name', $parsedFields) ||
!CRM_Utils_Array
::value('street_number', $parsedFields)) {
2005 $parsedFields = array_fill_keys(array_keys($parsedFields), '');
2008 // merge parse address w/ main address block.
2009 $address = array_merge($address, $parsedFields);
2016 * Function to generate status and error message for unparsed street address records.
2018 * @param array $values the array of values belonging to each row
2019 * @param array $statusFieldName store formatted date in this array
2023 function processMessage(&$values, $statusFieldName, $returnCode) {
2024 if (empty($this->_unparsedStreetAddressContacts
)) {
2025 $importRecordParams = array(
2026 $statusFieldName => 'IMPORTED',
2030 $errorMessage = ts("Record imported successfully but unable to parse the street address: ");
2031 foreach ($this->_unparsedStreetAddressContacts
as $contactInfo => $contactValue) {
2032 $contactUrl = CRM_Utils_System
::url('civicrm/contact/add', 'reset=1&action=update&cid=' . $contactValue['id'], TRUE, NULL, FALSE);
2033 $errorMessage .= "\n Contact ID:" . $contactValue['id'] . " <a href=\"$contactUrl\"> " . $contactValue['streetAddress'] . "</a>";
2035 array_unshift($values, $errorMessage);
2036 $importRecordParams = array(
2037 $statusFieldName => 'ERROR',
2038 "${statusFieldName}Msg" => $errorMessage,
2040 $returnCode = CRM_Import_Parser
::UNPARSED_ADDRESS_WARNING
;
2042 $this->updateImportRecord($values[count($values) - 1], $importRecordParams);
2046 function checkRelatedContactFields($relKey, $params) {
2047 //avoid blank contact creation.
2048 $allowToCreate = FALSE;
2050 //build the mapper field array.
2051 static $relatedContactFields = array();
2052 if (!isset($relatedContactFields[$relKey])) {
2053 foreach ($this->_mapperRelated
as $key => $name) {
2058 if ( CRM_Utils_Array
::value($name, $relatedContactFields) && !is_array($relatedContactFields[$name])) {
2059 $relatedContactFields[$name] = array();
2061 $fldName = CRM_Utils_Array
::value($key, $this->_mapperRelatedContactDetails
);
2062 if ($fldName == 'url') {
2063 $fldName = 'website';
2066 $relatedContactFields[$name][] = $fldName;
2071 //validate for passed data.
2072 if (is_array($relatedContactFields[$relKey])) {
2073 foreach ($relatedContactFields[$relKey] as $fld) {
2074 if (CRM_Utils_Array
::value($fld, $params)) {
2075 $allowToCreate = TRUE;
2081 return $allowToCreate;