Commit d5a03b6a authored by Elias Villiger's avatar Elias Villiger
Browse files

Feature #4922 - Working POC for Excel Import

parent e69c016b
Pipeline #731 passed with stage
in 1 minute and 55 seconds
...@@ -261,6 +261,7 @@ const ERROR_DOWNLOAD_FILE_NOT_READABLE = 1705; ...@@ -261,6 +261,7 @@ const ERROR_DOWNLOAD_FILE_NOT_READABLE = 1705;
// Excel // Excel
const ERROR_EXCEL_POSITION_ARGUMENT_EMPTY = 1800; const ERROR_EXCEL_POSITION_ARGUMENT_EMPTY = 1800;
const ERROR_EXCEL_INVALID_COORDINATES = 1801; const ERROR_EXCEL_INVALID_COORDINATES = 1801;
const ERROR_NO_IMPORT_TABLE = 1802;
// KeyValueParser // KeyValueParser
const ERROR_KVP_VALUE_HAS_NO_KEY = 1900; const ERROR_KVP_VALUE_HAS_NO_KEY = 1900;
...@@ -950,6 +951,7 @@ const FE_FILE_SPLIT = 'fileSplit'; ...@@ -950,6 +951,7 @@ const FE_FILE_SPLIT = 'fileSplit';
const FE_FILE_SPLIT_SVG = 'svg'; const FE_FILE_SPLIT_SVG = 'svg';
const FE_FILE_SPLIT_TABLE_NAME = 'tableNameSplit'; const FE_FILE_SPLIT_TABLE_NAME = 'tableNameSplit';
const FE_FILE_DOWNLOAD_BUTTON = 'downloadButton'; const FE_FILE_DOWNLOAD_BUTTON = 'downloadButton';
const FE_DATA_IMPORT = 'dataImport';
const FE_IMAGE_SOURCE = 'imageSource'; // Image source for a fabric element const FE_IMAGE_SOURCE = 'imageSource'; // Image source for a fabric element
const FE_SQL_VALIDATE = 'sqlValidate'; // Action: Query to validate form load const FE_SQL_VALIDATE = 'sqlValidate'; // Action: Query to validate form load
......
...@@ -536,7 +536,11 @@ class Save { ...@@ -536,7 +536,11 @@ class Save {
* *
* @return false|string New pathFilename or false on error * @return false|string New pathFilename or false on error
* @throws CodeException * @throws CodeException
* @throws DbException
* @throws UserFormException * @throws UserFormException
* @throws UserReportException
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
* @internal param $recordId * @internal param $recordId
*/ */
private function doUpload($formElement, $sipUpload, Sip $sip, &$modeUpload) { private function doUpload($formElement, $sipUpload, Sip $sip, &$modeUpload) {
...@@ -549,35 +553,91 @@ class Save { ...@@ -549,35 +553,91 @@ class Save {
return false; return false;
} }
// Take care the necessary target directories exist. if (isset($formElement[FE_DATA_IMPORT])) { // Import
$cwd = getcwd(); // TODO EV
$sitePath = $this->store->getVar(SYSTEM_SITE_PATH, STORE_SYSTEM); $dataImports = explode('|', $formElement[FE_DATA_IMPORT]);
if ($cwd === false || $sitePath === false || !chdir($sitePath)) { foreach ($dataImports as $dataImport) {
throw new UserFormException("getcwd() failed or SITE_PATH undefined or chdir('$sitePath') failed.", ERROR_IO_CHDIR); $arr = explode(',', $dataImport);
} $tableName = $arr[0];
$tabNum = 1;
$cellStart = 'A1';
if (!empty($arr[1])) {
$tabNum = $arr[1];
}
if (!empty($arr[2])) {
$cellStart = $arr[2];
}
if ($tableName == '') {
throw new UserFormException("Please specify a table name to import the data into.", ERROR_NO_IMPORT_TABLE);
}
$tmpFile = Support::extendFilename($statusUpload[FILES_TMP_NAME], UPLOAD_CACHED);
// Read tmp file with Spreadsheet reader
// Read the specified region
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($tmpFile);
$worksheetData = $spreadsheet->getActiveSheet()->toArray(''); // TODO specified tab
// formatting options can be passed to toArray
// Delete existing old file. //TODO: Only specified region
if (isset($statusUpload[FILES_FLAG_DELETE]) && $statusUpload[FILES_FLAG_DELETE] == '1') { $columnBegin = 'A';
$arr = $sip->getVarsFromSip($sipUpload); $columnEnd = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(count($worksheetData[0]));
$oldFile = $arr[EXISTING_PATH_FILE_NAME]; $columnEnd++; // the first non-existing column (important for use in for loop below)
if (file_exists($oldFile)) {
if (!unlink($oldFile)) { $columnDefinitionArr = [];
throw new UserFormException('Unlink file failed: ' . $oldFile, ERROR_IO_UNLINK); $columnListArr = [];
for ($column = $columnBegin; $column != $columnEnd; $column++) {
$columnDefinitionArr[] = "`$column` TEXT NOT NULL DEFAULT ''";
$columnListArr[] = "$column";
}
$createTableSql = "CREATE TABLE IF NOT EXISTS `$tableName` (" .
"`id` INT(11) NOT NULL AUTO_INCREMENT," .
implode(', ', $columnDefinitionArr) . ',' .
"`modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP," .
"`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP," .
"PRIMARY KEY (`id`) )" .
"ENGINE = InnoDB DEFAULT CHARSET = utf8 AUTO_INCREMENT = 0;";
$this->db->sql($createTableSql);
// Import the data
foreach ($worksheetData AS $rowIndex => $row) {
$columnList = implode(',', $columnListArr);
$paramPlaceholders = str_repeat('?,', count($worksheetData[0])-1) . '?';
$insertSql = "INSERT INTO `$tableName` ($columnList) VALUES ($paramPlaceholders)";
$this->db->sql($insertSql, ROW_REGULAR, $row);
} }
} }
$flagDelete = ($oldFile != ''); } else { // Upload
} // Take care the necessary target directories exist.
$cwd = getcwd();
$sitePath = $this->store->getVar(SYSTEM_SITE_PATH, STORE_SYSTEM);
if ($cwd === false || $sitePath === false || !chdir($sitePath)) {
throw new UserFormException("getcwd() failed or SITE_PATH undefined or chdir('$sitePath') failed.", ERROR_IO_CHDIR);
}
// Set $modeUpload // Delete existing old file.
if (isset($statusUpload[FILES_TMP_NAME]) && $statusUpload[FILES_TMP_NAME] != '') { if (isset($statusUpload[FILES_FLAG_DELETE]) && $statusUpload[FILES_FLAG_DELETE] == '1') {
$modeUpload = $flagDelete ? UPLOAD_MODE_DELETEOLD_NEW : UPLOAD_MODE_NEW; $arr = $sip->getVarsFromSip($sipUpload);
} else { $oldFile = $arr[EXISTING_PATH_FILE_NAME];
$modeUpload = $flagDelete ? UPLOAD_MODE_DELETEOLD : UPLOAD_MODE_UNCHANGED; if (file_exists($oldFile)) {
} if (!unlink($oldFile)) {
throw new UserFormException('Unlink file failed: ' . $oldFile, ERROR_IO_UNLINK);
}
}
$flagDelete = ($oldFile != '');
}
// Set $modeUpload
if (isset($statusUpload[FILES_TMP_NAME]) && $statusUpload[FILES_TMP_NAME] != '') {
$modeUpload = $flagDelete ? UPLOAD_MODE_DELETEOLD_NEW : UPLOAD_MODE_NEW;
} else {
$modeUpload = $flagDelete ? UPLOAD_MODE_DELETEOLD : UPLOAD_MODE_UNCHANGED;
}
$pathFileName = $this->copyUploadFile($formElement, $statusUpload); $pathFileName = $this->copyUploadFile($formElement, $statusUpload);
chdir($cwd); chdir($cwd);
}
// Delete current used uniq SIP // Delete current used uniq SIP
$this->store->setVar($sipUpload, array(), STORE_EXTRA); $this->store->setVar($sipUpload, array(), STORE_EXTRA);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment