From 9eec6f2ba73481ecfef73cde11bb26d7a57eee12 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 25 May 2022 11:23:14 +1200 Subject: [PATCH] Import flow - separate out 'validate' function from 'run' --- CRM/Contact/Import/Form/MapField.php | 22 +++------------- CRM/Contact/Import/Parser/Contact.php | 36 ++++++++++----------------- CRM/Import/Parser.php | 21 ++++++++++++++++ 3 files changed, 37 insertions(+), 42 deletions(-) diff --git a/CRM/Contact/Import/Form/MapField.php b/CRM/Contact/Import/Form/MapField.php index d7dea49ca7..39e4ca1dce 100644 --- a/CRM/Contact/Import/Form/MapField.php +++ b/CRM/Contact/Import/Form/MapField.php @@ -400,10 +400,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { return; } $this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues()); - $parser = $this->submit($params); - - // add all the necessary variables to the form - $parser->set($this); + $this->submit($params); } /** @@ -447,18 +444,10 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { * @param $params * @param $mapperKeys * - * @return \CRM_Contact_Import_Parser_Contact * @throws \CiviCRM_API3_Exception * @throws \CRM_Core_Exception */ public function submit($params) { - $mapperKeys = $this->getSubmittedValue('mapper'); - $mapperKeysMain = []; - - for ($i = 0; $i < $this->_columnCount; $i++) { - $mapperKeysMain[$i] = $mapperKeys[$i][0] ?? NULL; - } - $this->set('columnNames', $this->_columnNames); // store mapping Id to display it in the preview page @@ -487,14 +476,9 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { $this->set('savedMapping', $saveMapping['id']); } - $parser = new CRM_Contact_Import_Parser_Contact($mapperKeysMain); + $parser = new CRM_Contact_Import_Parser_Contact(); $parser->setUserJobID($this->getUserJobID()); - - $parser->run( - [], - CRM_Import_Parser::MODE_PREVIEW - ); - return $parser; + $parser->validate(); } /** diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 95fc3fef09..e9c403f75c 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -1634,9 +1634,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { } $dataSource = $this->getDataSourceObject(); $totalRowCount = $dataSource->getRowCount(['new']); - if ($mode == self::MODE_IMPORT) { - $dataSource->setStatuses(['new']); - } + $dataSource->setStatuses(['new']); while ($row = $dataSource->getRow()) { $values = array_values($row); @@ -1644,29 +1642,21 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { $this->_totalCount++; - if ($mode == self::MODE_PREVIEW) { - $returnCode = $this->preview($values); + try { + $returnCode = $this->import($onDuplicate, $values); } - elseif ($mode == self::MODE_SUMMARY) { - $returnCode = $this->summary($values); + catch (CiviCRM_API3_Exception $e) { + // When we catch errors here we are not adding to the errors array - mostly + // because that will become obsolete once https://github.com/civicrm/civicrm-core/pull/23292 + // is merged and this will replace it as the main way to handle errors (ie. update the table + // and move on). + $this->setImportStatus((int) $values[count($values) - 1], 'ERROR', $e->getMessage()); } - elseif ($mode == self::MODE_IMPORT) { - try { - $returnCode = $this->import($onDuplicate, $values); - } - catch (CiviCRM_API3_Exception $e) { - // When we catch errors here we are not adding to the errors array - mostly - // because that will become obsolete once https://github.com/civicrm/civicrm-core/pull/23292 - // is merged and this will replace it as the main way to handle errors (ie. update the table - // and move on). - $this->setImportStatus((int) $values[count($values) - 1], 'ERROR', $e->getMessage()); - } - if ($statusID && (($this->_rowCount % 50) == 0)) { - $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount); - } + if ($statusID && (($this->_rowCount % 50) == 0)) { + $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount); } - // @todo this should be done within import - it probably is! - if (isset($returnCode) && $returnCode === self::UNPARSED_ADDRESS_WARNING) { + + if ($returnCode & self::UNPARSED_ADDRESS_WARNING) { $this->setImportStatus((int) $values[count($values) - 1], 'warning_unparsed_address', array_shift($values)); } } diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index bf05a50135..0f36753f79 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -1428,6 +1428,27 @@ abstract class CRM_Import_Parser { return $metadata['entity']; } + /** + * Validate the import file, updating the import table with results. + * + * @throws \API_Exception + * @throws \CRM_Core_Exception + */ + public function validate(): void { + $dataSource = $this->getDataSourceObject(); + while ($row = $dataSource->getRow()) { + try { + $rowNumber = $row['_id']; + $values = array_values($row); + $this->validateValues($values); + $this->setImportStatus($rowNumber, 'NEW', ''); + } + catch (CRM_Core_Exception $e) { + $this->setImportStatus($rowNumber, 'ERROR', $e->getMessage()); + } + } + } + /** * Search the value for the string 'invalid_import_value'. * -- 2.25.1