- $form->set('importTableName', $importJob->getTableName());
- $this->dataSourceMetadata = [
- 'table_name' => $importJob->getTableName(),
- ];
+ // 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(
+ 'SHOW FIELDS FROM ' . $tableName . "
+ WHERE Field NOT LIKE '\_%'");
+
+ $columnNames = [];
+ while ($columnsResult->fetch()) {
+ if (strpos($columnsResult->Field, ' ') !== FALSE) {
+ // Remove spaces as the Database object does this
+ // $keys = str_replace(array(".", " "), "_", array_keys($array));
+ // https://lab.civicrm.org/dev/core/-/issues/1337
+ $usableColumnName = str_replace(' ', '_', $columnsResult->Field);
+ CRM_Core_DAO::executeQuery('ALTER TABLE ' . $tableName . ' CHANGE `' . $columnsResult->Field . '` ' . $usableColumnName . ' ' . $columnsResult->Type);
+ $columnNames[] = $usableColumnName;
+ }
+ else {
+ $columnNames[] = $columnsResult->Field;
+ }
+ }
+
+ $this->addTrackingFieldsToTable($tableName);
+ $this->updateUserJobMetadata('DataSource', [
+ 'table_name' => $tableName,
+ 'column_headers' => $columnNames,
+ 'number_of_columns' => count($columnNames),
+ ]);