The datasource stores the table name on the job - we don't need to pass it around.
Also - stop cleaning up the temp table at the end - we want it to
output results but will add a cleanup routine later
*/
public function preProcess() {
$this->_mapperFields = $this->getAvailableFields();
- $this->_importTableName = $this->get('importTableName');
$this->_contactSubType = $this->getSubmittedValue('contactSubType');
//format custom field names, CRM-2676
$contactType = $this->getContactType();
);
$parser->setUserJobID($this->getUserJobID());
- $parser->run($this->_importTableName,
+ $parser->run(NULL,
$mapper,
CRM_Import_Parser::MODE_PREVIEW,
NULL,
'userJobID' => $this->getUserJobID(),
);
- $tableName = $this->get('importTableName');
- $importJob = new CRM_Contact_Import_ImportJob($tableName);
+ $importJob = new CRM_Contact_Import_ImportJob();
$importJob->setJobParams($importJobParams);
// If ACL applies to the current user, update cache before running the import.
protected $_userJobID;
- /**
- * @param string|null $tableName
- * @param string|null $createSql
- * @param bool $createTable
- *
- * @throws \CRM_Core_Exception
- */
- public function __construct($tableName = NULL, $createSql = NULL, $createTable = FALSE) {
- $dao = new CRM_Core_DAO();
- $db = $dao->getDatabaseConnection();
-
- if ($createTable) {
- if (!$createSql) {
- throw new CRM_Core_Exception(ts('Either an existing table name or an SQL query to build one are required'));
- }
- if ($tableName) {
- // Drop previous table if passed in and create new one.
- $db->query("DROP TABLE IF EXISTS $tableName");
- }
- $table = CRM_Utils_SQL_TempTable::build()->setDurable();
- $tableName = $table->getName();
- $table->createWithQuery($createSql);
- }
-
- if (!$tableName) {
- throw new CRM_Core_Exception(ts('Import Table is required.'));
- }
-
- $this->_tableName = $tableName;
- }
-
/**
* @return null|string
*/
* Has the job completed.
*
* @return bool
- * @throws Exception
*/
- public function isComplete() {
- if (!$this->_statusFieldName) {
- throw new CRM_Core_Exception("Could not get name of the import status field");
- }
- $query = "SELECT * FROM $this->_tableName
- WHERE $this->_statusFieldName = 'NEW' LIMIT 1";
- $result = CRM_Core_DAO::executeQuery($query);
- if ($result->fetch()) {
- return FALSE;
- }
- return TRUE;
+ public function isComplete(): bool {
+ return $this->_parser->isComplete();
}
/**
$parserParameters['relatedContactWebsiteType']
);
$this->_parser->setUserJobID($this->_userJobID);
- $this->_parser->run($this->_tableName, $mapperFields,
+ $this->_parser->run(NULL, $mapperFields,
CRM_Import_Parser::MODE_IMPORT,
$this->_contactType,
$this->_primaryKeyName,
$this->_conflicts = [];
$this->_unparsedAddresses = [];
- // Transitional support for deprecating table_name (and other fields)
- // form input - the goal is to load them from userJob - but eventually
- // we will just load the datasource object and this code will not know the
- // table name.
- if (!$tableName && $this->userJobID) {
- $tableName = $this->getUserJob()['metadata']['DataSource']['table_name'];
- }
-
- $this->_tableName = $tableName;
+ $this->_tableName = $tableName = $this->getUserJob()['metadata']['DataSource']['table_name'];
$this->_primaryKeyName = '_id';
$this->_statusFieldName = '_status';
return $params;
}
+ /**
+ * Is the job complete.
+ *
+ * This function transitionally accesses the table from the userJob
+ * directly - but the function should be moved to the dataSource class.
+ *
+ * @throws \API_Exception
+ */
+ public function isComplete() {
+ $tableName = $this->getUserJob()['metadata']['DataSource']['table_name'];
+ return (bool) CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM $tableName WHERE _status = 'NEW' LIMIT 1");
+ }
+
}
* @throws \API_Exception
*/
public function postProcess(&$params, &$db, &$form) {
- $firstRowIsColumnHeader = $params['skipColumnHeader'] ?? FALSE;
$result = self::_CsvToTable(
$this->getSubmittedValue('uploadFile')['name'],
$this->getSubmittedValue('skipColumnHeader'),
);
$this->addTrackingFieldsToTable($result['import_table_name']);
- $form->set('originalColHeader', CRM_Utils_Array::value('column_headers', $result));
- $form->set('importTableName', $result['import_table_name']);
$this->updateUserJobMetadata('DataSource', [
'table_name' => $result['import_table_name'],
- 'column_headers' => $firstRowIsColumnHeader ? $result['column_headers'] : [],
+ 'column_headers' => $this->getSubmittedValue('skipColumnHeader') ? $result['column_headers'] : [],
'number_of_columns' => $result['number_of_columns'],
]);
}
* @throws \Civi\API\Exception\UnauthorizedException
*/
public function postProcess(&$params, &$db, &$form) {
- $importJob = new CRM_Contact_Import_ImportJob(
- NULL,
- $params['sqlQuery'], TRUE
- );
- $tableName = $importJob->getTableName();
+ $table = CRM_Utils_SQL_TempTable::build()->setDurable();
+ $tableName = $table->getName();
+ $table->createWithQuery($this->getSubmittedValue('sqlQuery'));
- $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(
- 'SHOW FIELDS FROM ' . $importJob->getTableName() . "
+ 'SHOW FIELDS FROM ' . $tableName . "
WHERE Field NOT LIKE '\_%'");
$columnNames = [];
$this->addTrackingFieldsToTable($tableName);
$this->updateUserJobMetadata('DataSource', [
- 'table_name' => $importJob->getTableName(),
+ 'table_name' => $tableName,
'column_headers' => $columnNames,
'number_of_columns' => count($columnNames),
]);
* @param array $fileData
*
* @dataProvider getCsvFiles
+ *
+ * @throws \API_Exception
+ * @throws \CRM_Core_Exception
*/
public function testToCsv(array $fileData): void {
$form = $this->submitDatasourceForm($fileData['filename']);
- $tableName = $form->get('importTableName');
+ $csvObject = new CRM_Import_DataSource_Csv($form->getUserJobID());
+ $rows = $csvObject->getRows(0, 0, [], FALSE);
foreach (['first_name', 'last_name', 'email'] as $field) {
- $json = json_encode(CRM_Core_DAO::singleValueQuery("SELECT $field FROM $tableName"));
+ $json = json_encode($rows[0][$field]);
$this->assertEquals($fileData["{$field}_json"], $json, "{$fileData['filename']} failed on $field");
}
- CRM_Core_DAO::executeQuery("DROP TABLE $tableName");
+ $csvObject->purge();
}
/**
*/
public function testBlankLineAtEnd(): void {
$form = $this->submitDatasourceForm('blankLineAtEnd.csv');
- $tableName = $form->get('importTableName');
- $json = json_encode(CRM_Core_DAO::singleValueQuery("SELECT email FROM $tableName"));
+ $csvObject = new CRM_Import_DataSource_Csv($form->getUserJobID());
+
+ $json = json_encode($csvObject->getRow()['email']);
$this->assertEquals('"yogi@yellowstone.park"', $json);
- CRM_Core_DAO::executeQuery("DROP TABLE $tableName");
+ $csvObject->purge();
}
/**