From 1163561bc6514dc43e418ce81b65da1857eb0d1d Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 21 Apr 2022 15:48:56 +1200 Subject: [PATCH] Cleanup cleanup on old tables for form re-submission --- CRM/Contact/Import/Form/DataSource.php | 6 +--- CRM/Import/DataSource/CSV.php | 2 +- CRM/Import/DataSource/SQL.php | 2 +- CRM/Import/Forms.php | 29 +++++++++++++++++++ .../Contact/Import/Form/DataSourceTest.php | 1 - 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/CRM/Contact/Import/Form/DataSource.php b/CRM/Contact/Import/Form/DataSource.php index 3934871bffe8..80f460dea3c3 100644 --- a/CRM/Contact/Import/Form/DataSource.php +++ b/CRM/Contact/Import/Form/DataSource.php @@ -176,6 +176,7 @@ public function postProcess() { $this->createUserJob(); } else { + $this->flushDataSource(); $this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues()); } // Setup the params array @@ -209,11 +210,6 @@ public function postProcess() { CRM_Core_Session::singleton()->set('dateTypes', $storeParams['dateFormats']); - //hack to prevent multiple tables. - $this->_params['import_table_name'] = $this->get('importTableName'); - if (!$this->_params['import_table_name']) { - $this->_params['import_table_name'] = 'civicrm_import_job_' . md5(uniqid(rand(), TRUE)); - } $this->instantiateDataSource(); // We should have the data in the DB now, parse it diff --git a/CRM/Import/DataSource/CSV.php b/CRM/Import/DataSource/CSV.php index f749631844f6..f4a3f756b97b 100644 --- a/CRM/Import/DataSource/CSV.php +++ b/CRM/Import/DataSource/CSV.php @@ -83,7 +83,7 @@ public function postProcess(&$params, &$db, &$form) { $result = self::_CsvToTable( $file, $firstRowIsColumnHeader, - CRM_Utils_Array::value('import_table_name', $params), + NULL, CRM_Utils_Array::value('fieldSeparator', $params, ',') ); diff --git a/CRM/Import/DataSource/SQL.php b/CRM/Import/DataSource/SQL.php index b3a9cde66408..f3e6d3b4c03d 100644 --- a/CRM/Import/DataSource/SQL.php +++ b/CRM/Import/DataSource/SQL.php @@ -86,7 +86,7 @@ public static function formRule($fields, $files, $form) { */ public function postProcess(&$params, &$db, &$form) { $importJob = new CRM_Contact_Import_ImportJob( - CRM_Utils_Array::value('import_table_name', $params), + NULL, $params['sqlQuery'], TRUE ); diff --git a/CRM/Import/Forms.php b/CRM/Import/Forms.php index eadc65047732..05fa85b04075 100644 --- a/CRM/Import/Forms.php +++ b/CRM/Import/Forms.php @@ -240,6 +240,35 @@ protected function buildDataSourceFields(): void { } } + /** + * Flush datasource on re-submission of the form. + * + * If the form has been re-submitted the datasource might have changed. + * We tell the dataSource class to remove any tables (and potentially files) + * created last form submission. + * + * If the DataSource in use is unchanged (ie still CSV or still SQL) + * we also pass in the new variables. In theory it could decide that they + * have not actually changed and it doesn't need to do any cleanup. + * + * In practice the datasource classes blast away as they always have for now + * - however, the sql class, for example, might realise the fields it cares + * about are unchanged and not flush the table. + * + * @throws \API_Exception + * @throws \CRM_Core_Exception + */ + protected function flushDataSource(): void { + // If the form has been resubmitted the datasource might have changed. + // We give the datasource a chance to clean up any tables it might have + // created. If we are still using the same type of datasource (e.g still + // an sql query + $oldDataSource = $this->getUserJobSubmittedValues()['dataSource']; + $oldDataSourceObject = new $oldDataSource($this->getUserJobID()); + $newParams = $this->getSubmittedValue('dataSource') === $oldDataSource ? $this->getSubmittedValues() : []; + $oldDataSourceObject->purge($newParams); + } + /** * Get the relevant datasource object. * diff --git a/tests/phpunit/CRM/Contact/Import/Form/DataSourceTest.php b/tests/phpunit/CRM/Contact/Import/Form/DataSourceTest.php index d8f3f1fddfce..10de94bd3a31 100644 --- a/tests/phpunit/CRM/Contact/Import/Form/DataSourceTest.php +++ b/tests/phpunit/CRM/Contact/Import/Form/DataSourceTest.php @@ -52,7 +52,6 @@ public function testBuildForm(): void { * * @throws \API_Exception * @throws \CRM_Core_Exception - * @throws \Civi\API\Exception\UnauthorizedException */ public function testDataSources(): void { $this->createLoggedInUser();