From 8d88fae036b4cae4363f4b2ac4c00cdd9f61f71a Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 21 Apr 2022 16:12:07 +1200 Subject: [PATCH] Move adding status fields on import table to datasource --- CRM/Contact/Import/Form/DataSource.php | 44 +------------------------- CRM/Contact/Import/Form/Preview.php | 4 +-- CRM/Contact/Import/Parser/Contact.php | 2 +- CRM/Import/DataSource/CSV.php | 9 +++--- CRM/Import/DataSource/SQL.php | 5 ++- 5 files changed, 13 insertions(+), 51 deletions(-) diff --git a/CRM/Contact/Import/Form/DataSource.php b/CRM/Contact/Import/Form/DataSource.php index 80f460dea3..6ba0d22a39 100644 --- a/CRM/Contact/Import/Form/DataSource.php +++ b/CRM/Contact/Import/Form/DataSource.php @@ -212,15 +212,12 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Import_Forms { $this->instantiateDataSource(); - // We should have the data in the DB now, parse it - $importTableName = $this->get('importTableName'); - $this->_prepareImportTable($importTableName); $mapper = []; $parser = new CRM_Contact_Import_Parser_Contact($mapper); $parser->setMaxLinesToProcess(100); $parser->setUserJobID($this->getUserJobID()); - $parser->run($importTableName, + $parser->run(NULL, [], CRM_Import_Parser::MODE_MAPFIELD, $this->getSubmittedValue('contactType'), @@ -253,45 +250,6 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Import_Forms { $dataSource->postProcess($this->_params, $db, $this); } - /** - * Add a PK and status column to the import table so we can track our progress. - * Returns the name of the primary key and status columns - * - * @param $db - * @param string $importTableName - * - * @return array - */ - private function _prepareImportTable($importTableName) { - /* TODO: Add a check for an existing _status field; - * if it exists, create __status instead and return that - */ - - $statusFieldName = '_status'; - $primaryKeyName = '_id'; - - $this->set('primaryKeyName', $primaryKeyName); - $this->set('statusFieldName', $statusFieldName); - - /* Make sure the PK is always last! We rely on this later. - * Should probably stop doing that at some point, but it - * would require moving to associative arrays rather than - * relying on numerical order of the fields. This could in - * turn complicate matters for some DataSources, which - * would also not be good. Decisions, decisions... - */ - - $alterQuery = "ALTER TABLE $importTableName - ADD COLUMN $statusFieldName VARCHAR(32) - DEFAULT 'NEW' NOT NULL, - ADD COLUMN ${statusFieldName}Msg TEXT, - ADD COLUMN $primaryKeyName INT PRIMARY KEY NOT NULL - AUTO_INCREMENT"; - CRM_Core_DAO::executeQuery($alterQuery); - - return ['status' => $statusFieldName, 'pk' => $primaryKeyName]; - } - /** * General function for handling invalid configuration. * diff --git a/CRM/Contact/Import/Form/Preview.php b/CRM/Contact/Import/Form/Preview.php index 20ecbdcd0d..e3c5990744 100644 --- a/CRM/Contact/Import/Form/Preview.php +++ b/CRM/Contact/Import/Form/Preview.php @@ -213,8 +213,8 @@ class CRM_Contact_Import_Form_Preview extends CRM_Import_Form_Preview { 'mapFields' => $this->getAvailableFields(), 'contactType' => $this->get('contactType'), 'contactSubType' => $this->getSubmittedValue('contactSubType'), - 'primaryKeyName' => $this->get('primaryKeyName'), - 'statusFieldName' => $this->get('statusFieldName'), + 'primaryKeyName' => '_id', + 'statusFieldName' => '_status', 'statusID' => $this->get('statusID'), 'totalRowCount' => $this->get('totalRowCount'), 'userJobID' => $this->getUserJobID(), diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 459060f54b..6879a3b427 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -2609,7 +2609,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { // get the contents of the temp. import table $query = "SELECT * FROM $tableName"; if ($mode == self::MODE_IMPORT) { - $query .= " WHERE $statusFieldName = 'NEW'"; + $query .= " WHERE _status = 'NEW'"; } if ($this->_maxLinesToProcess > 0) { // Note this would only be the case in MapForm mode, where it is set to 100 diff --git a/CRM/Import/DataSource/CSV.php b/CRM/Import/DataSource/CSV.php index d8a64f8c0f..0c921e68ad 100644 --- a/CRM/Import/DataSource/CSV.php +++ b/CRM/Import/DataSource/CSV.php @@ -76,15 +76,16 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource { * * @throws \API_Exception * @throws \CRM_Core_Exception + * @throws \API_Exception */ public function postProcess(&$params, &$db, &$form) { - $file = $params['uploadFile']['name']; $firstRowIsColumnHeader = $params['skipColumnHeader'] ?? FALSE; $result = self::_CsvToTable( - $file, - $firstRowIsColumnHeader, - CRM_Utils_Array::value('fieldSeparator', $params, ',') + $this->getSubmittedValue('uploadFile')['name'], + $this->getSubmittedValue('skipColumnHeader'), + $this->getSubmittedValue('fieldSeparator') ?? ',' ); + $this->addTrackingFieldsToTable($result['import_table_name']); $form->set('originalColHeader', CRM_Utils_Array::value('column_headers', $result)); $form->set('importTableName', $result['import_table_name']); diff --git a/CRM/Import/DataSource/SQL.php b/CRM/Import/DataSource/SQL.php index f3e6d3b4c0..6ac730faf3 100644 --- a/CRM/Import/DataSource/SQL.php +++ b/CRM/Import/DataSource/SQL.php @@ -89,8 +89,9 @@ class CRM_Import_DataSource_SQL extends CRM_Import_DataSource { NULL, $params['sqlQuery'], TRUE ); + $tableName = $importJob->getTableName(); - $form->set('importTableName', $importJob->getTableName()); + $form->set('importTableName', $tableName); // Get the names of the fields to be imported. Any fields starting with an // underscore are considered to be internal to the import process) $columnsResult = CRM_Core_DAO::executeQuery( @@ -101,6 +102,8 @@ class CRM_Import_DataSource_SQL extends CRM_Import_DataSource { while ($columnsResult->fetch()) { $columnNames[] = $columnsResult->Field; } + + $this->addTrackingFieldsToTable($tableName); $this->updateUserJobMetadata('DataSource', [ 'table_name' => $importJob->getTableName(), 'column_headers' => $columnNames, -- 2.25.1