*
* @return array
* (reference ) associative array of name/value pairs
+ * @throws \API_Exception
*/
public function &getActiveFieldParams() {
$params = [];
-
- for ($i = 0; $i < $this->_activeFieldCount; $i++) {
- $fieldName = $this->_activeFields[$i]->_name;
+ $mapper = $this->getSubmittedValue('mapper');
+ foreach ($this->getFieldMappings() as $i => $mappedField) {
+ // The key is in the format 5_a_b where 5 is the relationship_type_id and a_b is the direction.
+ $relatedContactKey = $mappedField['relationship_type_id'] ? ($mappedField['relationship_type_id'] . '_' . $mappedField['relationship_direction']) : NULL;
+ $fieldName = $relatedContactKey ? NULL : $mappedField['name'];
if ($fieldName === 'do_not_import') {
continue;
}
- $relatedContactFieldName = $this->_activeFields[$i]->_relatedContactDetails;
+ $relatedContactFieldName = $relatedContactKey ? $mappedField['name'] : NULL;
+ // RelatedContactType is not part of the mapping but rather calculated from the relationship.
$relatedContactType = $this->_activeFields[$i]->_relatedContactType;
- $relatedContactLocationTypeID = $this->_activeFields[$i]->_relatedContactLocType;
- $relatedContactWebsiteTypeID = $this->_activeFields[$i]->_relatedContactWebsiteType ?? NULL;
- $relatedContactIMProviderID = $this->_activeFields[$i]->_relatedContactImProvider ?? NULL;
- $relatedContactPhoneTypeID = $this->_activeFields[$i]->_relatedContactPhoneType ?? NULL;
- // The key is in the format 5_a_b where 5 is the relationship_type_id and a_b is the direction.
- $relatedContactKey = $this->_activeFields[$i]->_related;
+ $relatedContactLocationTypeID = $relatedContactKey ? $mappedField['location_type_id'] : NULL;
+ $relatedContactWebsiteTypeID = $relatedContactKey ? $mappedField['website_type_id'] : NULL;
+ $relatedContactIMProviderID = $relatedContactKey ? $mappedField['im_provider_id'] : NULL;
+ $relatedContactPhoneTypeID = $relatedContactKey ? $mappedField['phone_type_id'] : NULL;
- $locationTypeID = $this->_activeFields[$i]->_hasLocationType;
- $phoneTypeID = $this->_activeFields[$i]->_phoneType;
- $imProviderID = $this->_activeFields[$i]->_imProvider ?? NULL;
- $websiteTypeID = $this->_activeFields[$i]->_websiteType ?? NULL;
+ $locationTypeID = $relatedContactKey ? NULL : $mappedField['location_type_id'];
+ $phoneTypeID = $relatedContactKey ? NULL : $mappedField['phone_type_id'];
+ $imProviderID = $relatedContactKey ? NULL : $mappedField['im_provider_id'];
+ $websiteTypeID = $relatedContactKey ? NULL : $mappedField['website_type_id'];
$importedValue = $this->_activeFields[$i]->_value;
}
}
+ /**
+ * Get the field mappings for the import.
+ *
+ * This is the same format as saved in civicrm_mapping_field except
+ * that location_type_id = 'Primary' rather than empty where relevant.
+ *
+ * @return array
+ * @throws \API_Exception
+ */
+ protected function getFieldMappings(): array {
+ $mappedFields = [];
+ foreach ($this->getSubmittedValue('mapper') as $i => $mapperRow) {
+ $mappedField = $this->getMappingFieldFromMapperInput($mapperRow, 0, $i);
+ if (!$mappedField['location_type_id'] && !empty($this->importableFieldsMetadata[$mappedField['name']]['hasLocationType'])) {
+ $mappedField['location_type_id'] = 'Primary';
+ }
+ // Just for clarity since 0 is a pseudovalue
+ unset($mappedField['mapping_id']);
+ $mappedFields[] = $mappedField;
+ }
+ return $mappedFields;
+ }
+
}
/**
* Tear down after test.
- *
- * @throws \CRM_Core_Exception
*/
public function tearDown(): void {
$this->quickCleanup(['civicrm_address', 'civicrm_phone', 'civicrm_email', 'civicrm_user_job'], TRUE);
/**
* Test that import parser will add contact with employee of relationship.
+ *
+ * @throws \API_Exception
+ * @throws \CRM_Core_Exception
+ * @throws \CiviCRM_API3_Exception
+ * @throws \Civi\API\Exception\UnauthorizedException
*/
public function testImportParserWithEmployeeOfRelationship(): void {
$this->organizationCreate([
$fields = array_keys($contactImportValues);
$values = array_values($contactImportValues);
- $parser = new CRM_Contact_Import_Parser_Contact($fields, []);
- $parser->setUserJobID($this->getUserJobID());
- $parser->init();
- $this->mapRelationshipFields($fields, $parser->getAllFields());
+ $userJobID = $this->getUserJobID([
+ 'mapper' => [['first_name'], ['last_name'], ['5_a_b', 'organization_name']],
+ ]);
$parser = new CRM_Contact_Import_Parser_Contact($fields, [], [], [], [
NULL,
NULL,
- $fields[2],
+ '5_a_b',
], [
NULL,
NULL,
NULL,
'organization_name',
], [], [], [], [], []);
- $parser->setUserJobID($this->getUserJobID());
+ $parser->setUserJobID($userJobID);
$parser->_onDuplicate = CRM_Import_Parser::DUPLICATE_UPDATE;
$parser->init();
$this->assertEquals(CRM_Import_Parser::VALID, $parser->import(CRM_Import_Parser::DUPLICATE_UPDATE, $values), 'Return code from parser import was not as expected');
- $this->callAPISuccess("Contact", "get", [
+ $this->callAPISuccess('Contact', 'get', [
'first_name' => 'Alok',
'last_name' => 'Patel',
'organization_name' => 'Agileware',
/**
* Test that the import parser adds the address to the right location.
*
- * @throws \Exception
+ * @throws \API_Exception
+ * @throws \CRM_Core_Exception
+ * @throws \CiviCRM_API3_Exception
*/
- public function testImportBillingAddress() {
+ public function testImportBillingAddress(): void {
[$contactValues] = $this->setUpBaseContact();
$contactValues['nick_name'] = 'Old Bill';
$contactValues['external_identifier'] = 'android';
['name' => 'prefix_id', 'column_number' => 3],
['name' => 'suffix_id', 'column_number' => 4],
];
+ $mapperInput = [['first_name'], ['last_name'], ['email', CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Email', 'location_type_id', 'Home')], ['prefix_id'], ['suffix_id']];
$processor = new CRM_Import_ImportProcessor();
$processor->setMappingFields($mapping);
- $processor->setContactType('Individual');
- $processor->setUserJobID($this->getUserJobID());
+ $processor->setUserJobID($this->getUserJobID([
+ 'mapper' => $mapperInput,
+ ]));
$importer = $processor->getImporterObject();
$contactValues = [
*/
public function testImportTwoPhonesDifferentTypes(): void {
$processor = new CRM_Import_ImportProcessor();
- $processor->setUserJobID($this->getUserJobID());
+ $processor->setUserJobID($this->getUserJobID([
+ 'mapper' => [['first_name'], ['last_name'], ['email'], ['phone', 1, 2], ['phone', 1, 1]],
+ ]));
$processor->setMappingFields(
[
['name' => 'first_name'],
$contactValues['city'] = 'Big City';
$contactID = $this->callAPISuccessGetValue('Contact', ['external_identifier' => 'android', 'return' => 'id']);
$originalAddress = $this->callAPISuccess('Address', 'create', ['location_type_id' => 2, 'street_address' => 'small house', 'contact_id' => $contactID]);
- $originalPhone = $this->callAPISuccess('phone', 'create', ['location_type_id' => 2, 'phone' => '1234', 'contact_id' => $contactID]);
- $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, [0 => NULL, 1 => NULL, 2 => 'Primary', 3 => NULL, 4 => NULL, 5 => 'Primary', 6 => 'Primary', 7 => 'Primary']);
+ $originalPhone = $this->callAPISuccess('phone', 'create', ['location_type_id' => 2, 'phone' => '1234', 'contact_id' => $contactID, 'phone_type_id' => 1]);
+ $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, []);
$phone = $this->callAPISuccessGetSingle('Phone', ['phone' => '98765']);
$this->assertEquals(2, $phone['location_type_id']);
$this->assertEquals($originalPhone['id'], $phone['id']);
*/
public function testImportFieldsWithVariousOptions(): void {
$processor = new CRM_Import_ImportProcessor();
- $processor->setUserJobID($this->getUserJobID());
+ $processor->setUserJobID($this->getUserJobID([
+ 'mapper' => [['first_name'], ['last_name'], ['preferred_communication_method'], ['gender_id'], ['preferred_language']],
+ ]));
$processor->setMappingFields(
[
['name' => 'first_name'],
* @param int|null $ruleGroupId
* To test against a specific dedupe rule group, pass its ID as this argument.
*
+ * @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
$fields = array_keys($originalValues);
}
$values = array_values($originalValues);
+ $mapper = [];
+ foreach ($fields as $index => $field) {
+ $mapper[] = [$field, $mapperLocType[$index] ?? NULL, $field === 'phone' ? 1 : NULL];
+ }
$parser = new CRM_Contact_Import_Parser_Contact($fields, $mapperLocType);
- $parser->setUserJobID($this->getUserJobID());
+ $parser->setUserJobID($this->getUserJobID([
+ 'mapper' => $mapper,
+ ]));
$parser->_dedupeRuleGroupID = $ruleGroupId;
$parser->_onDuplicate = $onDuplicateAction;
$parser->init();