- public function postProcess(&$params, &$db, &$form) {
- $importJob = new CRM_Contact_Import_ImportJob(
- CRM_Utils_Array::value('import_table_name', $params),
- $params['sqlQuery'], TRUE
- );
+ public function initialize(): void {
+ $table = CRM_Utils_SQL_TempTable::build()->setDurable();
+ $tableName = $table->getName();
+ $table->createWithQuery($this->getSubmittedValue('sqlQuery'));
+
+ // 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;
+ }
+ }