//need to differentiate non location elements.
// @todo merge this with duplicate code on MapField class.
if ($selOne && (is_numeric($selOne) || $selOne === 'Primary')) {
- if ($fldName == 'url') {
+ if ($fldName === 'url') {
$header[] = $websiteTypes[$selOne];
$parserParameters['mapperWebsiteType'][$key] = $selOne;
}
$header[] = $locationTypes[$selOne];
$parserParameters['mapperLocType'][$key] = $selOne;
if ($selTwo && is_numeric($selTwo)) {
- if ($fldName == 'phone') {
+ if ($fldName === 'phone') {
$header[] = $phoneTypes[$selTwo];
$parserParameters['mapperPhoneType'][$key] = $selTwo;
}
- elseif ($fldName == 'im') {
+ elseif ($fldName === 'im') {
$header[] = $imProviders[$selTwo];
$parserParameters['mapperImProvider'][$key] = $selTwo;
}
* @param $newTagDesc
*
* @return array|bool
+ * @throws \CRM_Core_Exception
*/
private function _tagImportedContactsWithNewTag(
$contactIds,
* @param array $params
* @param string $db
* @param \CRM_Core_Form $form
+ *
+ * @throws \CRM_Core_Exception
*/
public function postProcess(&$params, &$db, &$form) {
$file = $params['uploadFile']['name'];
* File name to load.
* @param bool $headers
* Whether the first row contains headers.
- * @param string $table
+ * @param string $tableName
* Name of table from which data imported.
* @param string $fieldSeparator
* Character that separates the various columns in the file.
*
- * @return string
+ * @return array
* name of the created table
+ * @throws \CRM_Core_Exception
*/
private static function _CsvToTable(
&$db,
$file,
$headers = FALSE,
- $table = NULL,
+ $tableName = NULL,
$fieldSeparator = ','
) {
$result = [];
}
}
- // FIXME: we should regen this table's name if it exists rather than drop it
- if (!$table) {
- $table = 'civicrm_import_job_' . md5(uniqid(rand(), TRUE));
+ if ($tableName) {
+ // Drop previous table if passed in and create new one.
+ $db->query("DROP TABLE IF EXISTS $tableName");
}
-
- $db->query("DROP TABLE IF EXISTS $table");
+ $table = CRM_Utils_SQL_TempTable::build()->setDurable();
+ $tableName = $table->getName();
+ // Do we still need this?
+ $db->query("DROP TABLE IF EXISTS $tableName");
+ $table->createWithColumns(implode(' text, ', $columns) . ' text');
$numColumns = count($columns);
- $create = "CREATE TABLE $table (" . implode(' text, ', $columns) . " text) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
- $db->query($create);
// the proper approach, but some MySQL installs do not have this enabled
// $load = "LOAD DATA LOCAL INFILE '$file' INTO TABLE $table FIELDS TERMINATED BY '$fieldSeparator' OPTIONALLY ENCLOSED BY '\"'";
$count++;
if ($count >= self::NUM_ROWS_TO_INSERT && !empty($sql)) {
- $sql = "INSERT IGNORE INTO $table VALUES $sql";
+ $sql = "INSERT IGNORE INTO $tableName VALUES $sql";
$db->query($sql);
$sql = NULL;
}
if (!empty($sql)) {
- $sql = "INSERT IGNORE INTO $table VALUES $sql";
+ $sql = "INSERT IGNORE INTO $tableName VALUES $sql";
$db->query($sql);
}
fclose($fd);
//get the import tmp table name.
- $result['import_table_name'] = $table;
+ $result['import_table_name'] = $tableName;
return $result;
}
$this->assertEquals([1 => 'Well dressed ducks'], CRM_Core_Smarty::singleton()->get_template_vars('savedMapping'));
}
+ /**
+ * Check for (lack of) sql errors on sql import post process.
+ */
+ public function testSQLSource() {
+ $this->callAPISuccess('Mapping', 'create', ['name' => 'Well dressed ducks', 'mapping_type_id' => 'Import Contact']);
+ /** @var CRM_Import_DataSource_SQL $form */
+ $form = $this->getFormObject('CRM_Import_DataSource_SQL');
+ $coreForm = $this->getFormObject('CRM_Core_Form');
+ $db = NULL;
+ $params = ['sqlQuery' => 'SELECT 1 as id'];
+ $form->postProcess($params, $db, $coreForm);
+ }
+
}