Skip to content

Commit

Permalink
Merge pull request #23263 from eileenmcnaughton/import_wippier
Browse files Browse the repository at this point in the history
 [Import] Rename dedupe rule id field from dedupe to dedupe_rule_id field
  • Loading branch information
colemanw authored Apr 21, 2022
2 parents 3c01954 + dfa2f16 commit 5bab835
Show file tree
Hide file tree
Showing 12 changed files with 313 additions and 133 deletions.
32 changes: 15 additions & 17 deletions CRM/Contact/Import/Form/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function buildQuickForm() {
$this->addRadio('contactType', ts('Contact Type'), $contactTypeOptions, [], NULL, FALSE, $contactTypeAttributes);

$this->addElement('select', 'subType', ts('Subtype'));
$this->addElement('select', 'dedupe', ts('Dedupe Rule'));
$this->addElement('select', 'dedupe_rule_id', ts('Dedupe Rule'));

CRM_Core_Form_Date::buildAllowedDateFormats($this);

Expand Down Expand Up @@ -182,19 +182,18 @@ public function postProcess() {
$this->_params = $this->controller->exportValues($this->_name);

$storeParams = [
'onDuplicate' => $this->exportValue('onDuplicate'),
'dedupe' => $this->exportValue('dedupe'),
'contactType' => $this->exportValue('contactType'),
'contactSubType' => $this->exportValue('subType'),
'dateFormats' => $this->exportValue('dateFormats'),
'savedMapping' => $this->exportValue('savedMapping'),
'onDuplicate' => $this->getSubmittedValue('onDuplicate'),
'dedupe' => $this->getSubmittedValue('dedupe_rule_id'),
'contactType' => $this->getSubmittedValue('contactType'),
'contactSubType' => $this->getSubmittedValue('subType'),
'dateFormats' => $this->getSubmittedValue('dateFormats'),
'savedMapping' => $this->getSubmittedValue('savedMapping'),
];

foreach ($storeParams as $storeName => $value) {
$this->set($storeName, $value);
}
$this->set('disableUSPS', !empty($this->_params['disableUSPS']));

$this->set('disableUSPS', $this->getSubmittedValue('disableUSPS'));
$this->set('dataSource', $this->getSubmittedValue('dataSource'));
$this->set('skipColumnHeader', CRM_Utils_Array::value('skipColumnHeader', $this->_params));

Expand All @@ -209,23 +208,23 @@ public function postProcess() {

// We should have the data in the DB now, parse it
$importTableName = $this->get('importTableName');
$fieldNames = $this->_prepareImportTable($importTableName);
$this->_prepareImportTable($importTableName);
$mapper = [];

$parser = new CRM_Contact_Import_Parser_Contact($mapper);
$parser->setMaxLinesToProcess(100);
$parser->setUserJobID($this->getUserJobID());
$parser->run($importTableName,
$mapper,
[],
CRM_Import_Parser::MODE_MAPFIELD,
$storeParams['contactType'],
$fieldNames['pk'],
$fieldNames['status'],
$this->getSubmittedValue('contactType'),
'_id',
'_status',
CRM_Import_Parser::DUPLICATE_SKIP,
NULL, NULL, FALSE,
CRM_Contact_Import_Parser_Contact::DEFAULT_TIMEOUT,
$storeParams['contactSubType'],
$storeParams['dedupe']
$this->getSubmittedValue('contactSubType'),
$this->getSubmittedValue('dedupe_rule_id')
);

// add all the necessary variables to the form
Expand All @@ -246,7 +245,6 @@ private function instantiateDataSource(): void {
$dao = new CRM_Core_DAO();
$db = $dao->getDatabaseConnection();
$dataSource->postProcess($this->_params, $db, $this);
$this->updateUserJobMetadata('DataSource', $dataSource->getDataSourceMetadata());
}

/**
Expand Down
50 changes: 16 additions & 34 deletions CRM/Contact/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ public function defaultFromColumnName($columnName) {

/**
* Set variables up before form is built.
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
public function preProcess() {
$dataSource = $this->get('dataSource');
$skipColumnHeader = $this->get('skipColumnHeader');
$this->_mapperFields = $this->get('fields');
$this->_importTableName = $this->get('importTableName');
$this->_onDuplicate = $this->get('onDuplicate');
Expand Down Expand Up @@ -142,35 +144,16 @@ public function preProcess() {
$this->assign('highlightedFields', $highlightedFields);
$this->_formattedFieldNames[$contactType] = $this->_mapperFields = array_merge($this->_mapperFields, $formattedFieldNames);

$columnNames = [];
//get original col headers from csv if present.
if ($dataSource == 'CRM_Import_DataSource_CSV' && $skipColumnHeader) {
$columnNames = $this->get('originalColHeader');
}
else {
// get the field names from the temp. DB table
$columnsQuery = "SHOW FIELDS FROM $this->_importTableName
WHERE Field NOT LIKE '\_%'";
$columnsResult = CRM_Core_DAO::executeQuery($columnsQuery);
while ($columnsResult->fetch()) {
$columnNames[] = $columnsResult->Field;
}
}

$showColNames = TRUE;
if ($dataSource === 'CRM_Import_DataSource_CSV' && !$skipColumnHeader) {
$showColNames = FALSE;
}
$this->assign('showColNames', $showColNames);
$columnNames = $this->getColumnHeaders();
$this->assign('showColNames', !empty($columnNames));

$this->_columnCount = count($columnNames);
$this->_columnCount = $this->getNumberOfColumns();
$this->_columnNames = $columnNames;
$this->assign('columnNames', $columnNames);
$this->assign('columnNames', $this->getColumnHeaders());
//$this->_columnCount = $this->get( 'columnCount' );
$this->assign('columnCount', $this->_columnCount);
$this->_dataValues = $this->get('dataValues');
$this->_dataValues = array_values($this->getDataRows(2));
$this->assign('dataValues', $this->_dataValues);
$this->assign('rowDisplayCount', 2);
}

/**
Expand Down Expand Up @@ -243,7 +226,7 @@ public function buildQuickForm() {
foreach ($mapperKeys as $key) {
// check if there is a _a_b or _b_a in the key
if (strpos($key, '_a_b') || strpos($key, '_b_a')) {
list($id, $first, $second) = explode('_', $key);
[$id, $first, $second] = explode('_', $key);
}
else {
$id = $first = $second = NULL;
Expand Down Expand Up @@ -559,7 +542,7 @@ public function submit($params, $mapperKeys) {
}

//relationship contact mapper info.
list($id, $first, $second) = CRM_Utils_System::explode('_', $fldName, 3);
[$id, $first, $second] = CRM_Utils_System::explode('_', $fldName, 3);
if (($first === 'a' && $second === 'b') ||
($first === 'b' && $second === 'a')
) {
Expand Down Expand Up @@ -700,7 +683,7 @@ public function submit($params, $mapperKeys) {
}

$mappingID = NULL;
for ($i = 0; $i < $this->_columnCount; $i++) {
foreach (array_keys($this->getColumnHeaders()) as $i) {
$mappingID = $this->saveMappingField($mapperKeys, $saveMapping, $cType, $i, $mapper, $parserParameters);
}
$this->set('savedMapping', $mappingID);
Expand All @@ -712,20 +695,19 @@ public function submit($params, $mapperKeys) {
$parserParameters['relatedContactPhoneType'], $parserParameters['relatedContactImProvider'],
$parserParameters['mapperWebsiteType'], $parserParameters['relatedContactWebsiteType']
);
$parser->setUserJobID($this->getUserJobID());

$primaryKeyName = $this->get('primaryKeyName');
$statusFieldName = $this->get('statusFieldName');
$parser->run($this->_importTableName,
$mapper,
CRM_Import_Parser::MODE_PREVIEW,
$this->get('contactType'),
$primaryKeyName,
$statusFieldName,
'_id',
'_status',
$this->_onDuplicate,
NULL, NULL, FALSE,
CRM_Contact_Import_Parser_Contact::DEFAULT_TIMEOUT,
$this->get('contactSubType'),
$this->get('dedupe')
$this->getSubmittedValue('dedupe_rule_id')
);
return $parser;
}
Expand Down
6 changes: 2 additions & 4 deletions CRM/Contact/Import/Form/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class CRM_Contact_Import_Form_Preview extends CRM_Import_Form_Preview {
* Set variables up before form is built.
*/
public function preProcess() {
//get the data from the session
$dataValues = $this->get('dataValues');
$mapper = $this->get('mapper');
$invalidRowCount = $this->get('invalidRowCount');
$conflictRowCount = $this->get('conflictRowCount');
Expand Down Expand Up @@ -82,7 +80,6 @@ public function preProcess() {
'locations',
'phones',
'ims',
'dataValues',
'columnCount',
'totalRowCount',
'validRowCount',
Expand All @@ -103,6 +100,7 @@ public function preProcess() {
foreach ($properties as $property) {
$this->assign($property, $this->get($property));
}
$this->assign('dataValues', $this->getDataRows(2));

$this->setStatusUrl();

Expand Down Expand Up @@ -216,7 +214,7 @@ public function postProcess() {
'invalidRowCount' => $this->get('invalidRowCount'),
'conflictRowCount' => $this->get('conflictRowCount'),
'onDuplicate' => $this->get('onDuplicate'),
'dedupe' => $this->get('dedupe'),
'dedupe' => $this->getSubmittedValue('dedupe_rule_id'),
'newGroupName' => $this->controller->exportValue($this->_name, 'newGroupName'),
'newGroupDesc' => $this->controller->exportValue($this->_name, 'newGroupDesc'),
'newGroupType' => $this->controller->exportValue($this->_name, 'newGroupType'),
Expand Down
21 changes: 14 additions & 7 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,16 @@ public function __construct(
$this->_mapperRelatedContactWebsiteType = $mapperRelatedContactWebsiteType;
// get IM service provider type id for related contact
$this->_mapperRelatedContactImProvider = &$mapperRelatedContactImProvider;
$this->setFieldMetadata();
foreach ($this->getImportableFieldsMetadata() as $name => $field) {
$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));
}
}

/**
* The initializer code, called before processing.
*/
public function init() {
$this->setFieldMetadata();
foreach ($this->getImportableFieldsMetadata() as $name => $field) {
$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));
}

$this->_newContacts = [];

$this->setActiveFields($this->_mapperKeys);
Expand Down Expand Up @@ -2598,9 +2597,17 @@ public function run(
$this->_conflicts = [];
$this->_unparsedAddresses = [];

// Transitional support for deprecating table_name (and other fields)
// form input - the goal is to load them from userJob - but eventually
// we will just load the datasource object and this code will not know the
// table name.
if (!$tableName && $this->userJobID) {
$tableName = $this->getUserJob()['metadata']['DataSource']['table_name'];
}

$this->_tableName = $tableName;
$this->_primaryKeyName = $primaryKeyName;
$this->_statusFieldName = $statusFieldName;
$this->_primaryKeyName = '_id';
$this->_statusFieldName = '_status';

if ($mode == self::MODE_MAPFIELD) {
$this->_rows = [];
Expand Down
Loading

0 comments on commit 5bab835

Please sign in to comment.