Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Import] Upgrade saved import names for contacts #23288

Merged
merged 3 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 37 additions & 119 deletions CRM/Contact/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

use Civi\Api4\MappingField;

/**
* This class gets the name of the file to upload.
*/
Expand Down Expand Up @@ -539,69 +541,8 @@ public function submit($params, $mapperKeys) {

//Updating Mapping Records
if (!empty($params['updateMapping'])) {

$mappingFields = new CRM_Core_DAO_MappingField();
$mappingFields->mapping_id = $params['mappingId'];
$mappingFields->find();

$mappingFieldsId = [];
while ($mappingFields->fetch()) {
if ($mappingFields->id) {
$mappingFieldsId[$mappingFields->column_number] = $mappingFields->id;
}
}

for ($i = 0; $i < $this->_columnCount; $i++) {
$updateMappingFields = new CRM_Core_DAO_MappingField();
$updateMappingFields->id = $mappingFieldsId[$i] ?? NULL;
$updateMappingFields->mapping_id = $params['mappingId'];
$updateMappingFields->column_number = $i;

$mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
$id = $mapperKeyParts[0] ?? NULL;
$first = $mapperKeyParts[1] ?? NULL;
$second = $mapperKeyParts[2] ?? NULL;
if (($first == 'a' && $second == 'b') || ($first == 'b' && $second == 'a')) {
$updateMappingFields->relationship_type_id = $id;
$updateMappingFields->relationship_direction = "{$first}_{$second}";
$updateMappingFields->name = ucwords(str_replace("_", " ", $mapperKeys[$i][1]));
// get phoneType id and provider id separately
// before updating mappingFields of phone and IM for related contact, CRM-3140
if (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'url') {
$updateMappingFields->website_type_id = $mapperKeys[$i][2] ?? NULL;
}
else {
if (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'phone') {
$updateMappingFields->phone_type_id = $mapperKeys[$i][3] ?? NULL;
}
elseif (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'im') {
$updateMappingFields->im_provider_id = $mapperKeys[$i][3] ?? NULL;
}
$updateMappingFields->location_type_id = isset($mapperKeys[$i][2]) && is_numeric($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : NULL;
}
}
else {
$updateMappingFields->name = $mapper[$i];
$updateMappingFields->relationship_type_id = 'NULL';
$updateMappingFields->relationship_type_direction = 'NULL';
// to store phoneType id and provider id separately
// before updating mappingFields for phone and IM, CRM-3140
if (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'url') {
$updateMappingFields->website_type_id = $mapperKeys[$i][1] ?? NULL;
}
else {
if (($mapperKeys[$i][0] ?? NULL) === 'phone' || ($mapperKeys[$i][0] ?? NULL) === 'phone_ext') {
$updateMappingFields->phone_type_id = $mapperKeys[$i][2] ?? NULL;
}
elseif (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'im') {
$updateMappingFields->im_provider_id = $mapperKeys[$i][2] ?? NULL;
}
$locationTypeID = $parserParameters['mapperLocType'][$i];
// location_type_id is NULL for non-location fields, and for Primary location.
$updateMappingFields->location_type_id = is_numeric($locationTypeID) ? $locationTypeID : 'null';
}
}
$updateMappingFields->save();
$this->saveMappingField($params['mappingId'], $i, TRUE);
}
}

Expand All @@ -615,11 +556,10 @@ public function submit($params, $mapperKeys) {

$saveMapping = civicrm_api3('Mapping', 'create', $mappingParams);

$mappingID = NULL;
foreach (array_keys($this->getColumnHeaders()) as $i) {
$mappingID = $this->saveMappingField($mapperKeys, $saveMapping, $this->getContactType(), $i, $mapper, $parserParameters);
$this->saveMappingField($saveMapping['id'], $i);
}
$this->set('savedMapping', $mappingID);
$this->set('savedMapping', $saveMapping['id']);
}

$parser = new CRM_Contact_Import_Parser_Contact($mapperKeysMain, $parserParameters['mapperLocType'], $parserParameters['mapperPhoneType'],
Expand All @@ -646,65 +586,43 @@ public function submit($params, $mapperKeys) {
}

/**
* @param $mapperKeys
* @param array $saveMapping
* @param string $cType
* @param int $i
* @param array $mapper
* @param array $parserParameters
* Save the mapping field.
*
* @param int $mappingID
* @param int $columnNumber
* @param bool $isUpdate
*
* @return int
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
protected function saveMappingField($mapperKeys, array $saveMapping, string $cType, int $i, array $mapper, array $parserParameters): int {
$saveMappingFields = new CRM_Core_DAO_MappingField();
$saveMappingFields->mapping_id = $saveMapping['id'];
$saveMappingFields->contact_type = $cType;
$saveMappingFields->column_number = $i;

$mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
$id = $mapperKeyParts[0] ?? NULL;
$first = $mapperKeyParts[1] ?? NULL;
$second = $mapperKeyParts[2] ?? NULL;
if (($first == 'a' && $second == 'b') || ($first == 'b' && $second == 'a')) {
$saveMappingFields->name = ucwords(str_replace("_", " ", $mapperKeys[$i][1]));
$saveMappingFields->relationship_type_id = $id;
$saveMappingFields->relationship_direction = "{$first}_{$second}";
// to get phoneType id and provider id separately
// before saving mappingFields of phone and IM for related contact, CRM-3140
if (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'url') {
$saveMappingFields->website_type_id = $mapperKeys[$i][2] ?? NULL;
}
else {
if (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'phone') {
$saveMappingFields->phone_type_id = $mapperKeys[$i][3] ?? NULL;
}
elseif (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'im') {
$saveMappingFields->im_provider_id = $mapperKeys[$i][3] ?? NULL;
}
$saveMappingFields->location_type_id = (isset($mapperKeys[$i][2]) && $mapperKeys[$i][2] !== 'Primary') ? $mapperKeys[$i][2] : NULL;
}
protected function saveMappingField(int $mappingID, int $columnNumber, bool $isUpdate = FALSE): void {
$fieldMapping = (array) $this->getSubmittedValue('mapper')[$columnNumber];
$mappedField = $this->getMappedField($fieldMapping, $mappingID, $columnNumber);
if ($isUpdate) {
MappingField::update(FALSE)
->setValues($mappedField)
->addWhere('column_number', '=', $columnNumber)
->addWhere('mapping_id', '=', $mappingID)
->execute();
}
else {
$saveMappingFields->name = $mapper[$i];
$locationTypeID = $parserParameters['mapperLocType'][$i];
// to get phoneType id and provider id separately
// before saving mappingFields of phone and IM, CRM-3140
if (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'url') {
$saveMappingFields->website_type_id = $mapperKeys[$i][1] ?? NULL;
}
else {
if (($mapperKeys[$i][0] ?? NULL) === 'phone' || ($mapperKeys[$i][0] ?? NULL) === 'phone_ext') {
$saveMappingFields->phone_type_id = $mapperKeys[$i][2] ?? NULL;
}
elseif (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'im') {
$saveMappingFields->im_provider_id = $mapperKeys[$i][2] ?? NULL;
}
$saveMappingFields->location_type_id = is_numeric($locationTypeID) ? $locationTypeID : NULL;
}
$saveMappingFields->relationship_type_id = NULL;
MappingField::create(FALSE)
->setValues($mappedField)->execute();
}
$saveMappingFields->save();
return $saveMappingFields->mapping_id;
}

/**
* Get the field mapped to the savable format.
*
* @param array $fieldMapping
* @param int $mappingID
* @param int $columnNumber
*
* @return array
* @throws \CRM_Core_Exception
*/
protected function getMappedField(array $fieldMapping, int $mappingID, int $columnNumber): array {
return (new CRM_Contact_Import_Parser_Contact())->setContactType($this->getContactType())->getMappingFieldFromMapperInput($fieldMapping, $mappingID, $columnNumber);
}

/**
Expand Down
55 changes: 55 additions & 0 deletions CRM/Import/ImportProcessor.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use Civi\Api4\Mapping;
use Civi\Api4\MappingField;

/**
* Class CRM_Import_ImportProcessor.
*
Expand Down Expand Up @@ -447,6 +450,29 @@ public function getImporterObject() {
* @throws \CiviCRM_API3_Exception
*/
protected function loadSavedMapping() {
$fields = civicrm_api3('MappingField', 'get', [
'mapping_id' => $this->getMappingID(),
'options' => ['limit' => 0],
])['values'];
foreach ($fields as $index => $field) {
$fieldSpec = $this->getMetadata()[$fields[$index]['name']];
$fields[$index]['label'] = $fieldSpec['title'];
if (empty($field['location_type_id']) && !empty($fieldSpec['hasLocationType'])) {
$fields[$index]['location_type_id'] = 'Primary';
}
}
$this->mappingFields = $this->rekeyBySortedColumnNumbers($fields);
}

/**
* Load the mapping from the database into the pre-5.50 format.
*
* This is preserved as a copy the upgrade script can use - since the
* upgrade allows the other to be 'fixed'.
*
* @throws \CiviCRM_API3_Exception
*/
protected function legacyLoadSavedMapping() {
$fields = civicrm_api3('MappingField', 'get', [
'mapping_id' => $this->getMappingID(),
'options' => ['limit' => 0],
Expand Down Expand Up @@ -589,4 +615,33 @@ public function getSavedQuickformDefaultsForColumn($column) {
return [(string) $this->getFieldName($column), $this->getLocationTypeID($column), $this->getPhoneOrIMTypeID($column)];
}

/**
* This exists for use in the FiveFifty Upgrade
*
* @throws \API_Exception|\CiviCRM_API3_Exception
*/
public static function convertSavedFields(): void {
$mappings = Mapping::get(FALSE)
->setSelect(['id', 'contact_type'])
->addWhere('mapping_type_id:name', '=', 'Import Contact')
->execute();

foreach ($mappings as $mapping) {
$processor = new CRM_Import_ImportProcessor();
$processor->setMappingID($mapping['id']);
$processor->setMetadata(CRM_Contact_BAO_Contact::importableFields('All'));
$processor->legacyLoadSavedMapping();;
foreach ($processor->getMappingFields() as $field) {
// The if is mostly precautionary against running this more than once
// - which is common in dev if not live...
if ($field['name']) {
MappingField::update(FALSE)
->setValues(['name' => $field['name']])
->addWhere('id', '=', $field['id'])
->execute();
}
}
}
}

}
10 changes: 10 additions & 0 deletions CRM/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ public function setImportableFieldsMetadata(array $importableFieldsMetadata): vo
*/
public $_contactType;

/**
* @param string $contactType
*
* @return CRM_Import_Parser
*/
public function setContactType(string $contactType): CRM_Import_Parser {
$this->_contactType = $contactType;
return $this;
}

/**
* Contact sub-type
*
Expand Down
24 changes: 24 additions & 0 deletions CRM/Upgrade/Incremental/php/FiveFifty.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ class CRM_Upgrade_Incremental_php_FiveFifty extends CRM_Upgrade_Incremental_Base
*/
public function upgrade_5_50_alpha1($rev): void {
$this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
$this->addTask(ts('Convert import mappings to use names'), 'convertMappingFieldLabelsToNames', $rev);

}

/**
* Convert saved mapping fields for contact imports to use name rather than
* label.
*
* Currently the 'name' column in civicrm_mapping_field holds names like
* 'First Name' or, more tragically 'Contact ID (match to contact)'.
*
* This updates them to hold the name - eg. 'first_name' in conjunction with
* a
* change in the contact import.
*
* (Getting the other entities done is a stretch goal).
*
* @return bool
* @throws \API_Exception
* @throws \CiviCRM_API3_Exception
*/
public static function convertMappingFieldLabelsToNames(): bool {
CRM_Import_ImportProcessor::convertSavedFields();
return TRUE;
}

}
Loading