Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
typo3
qfq
Commits
d5a03b6a
Commit
d5a03b6a
authored
Aug 18, 2018
by
Elias Villiger
Browse files
Feature #4922 - Working POC for Excel Import
parent
e69c016b
Pipeline
#731
passed with stage
in 1 minute and 55 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
extension/qfq/qfq/Constants.php
View file @
d5a03b6a
...
...
@@ -261,6 +261,7 @@ const ERROR_DOWNLOAD_FILE_NOT_READABLE = 1705;
// Excel
const
ERROR_EXCEL_POSITION_ARGUMENT_EMPTY
=
1800
;
const
ERROR_EXCEL_INVALID_COORDINATES
=
1801
;
const
ERROR_NO_IMPORT_TABLE
=
1802
;
// KeyValueParser
const
ERROR_KVP_VALUE_HAS_NO_KEY
=
1900
;
...
...
@@ -950,6 +951,7 @@ const FE_FILE_SPLIT = 'fileSplit';
const
FE_FILE_SPLIT_SVG
=
'svg'
;
const
FE_FILE_SPLIT_TABLE_NAME
=
'tableNameSplit'
;
const
FE_FILE_DOWNLOAD_BUTTON
=
'downloadButton'
;
const
FE_DATA_IMPORT
=
'dataImport'
;
const
FE_IMAGE_SOURCE
=
'imageSource'
;
// Image source for a fabric element
const
FE_SQL_VALIDATE
=
'sqlValidate'
;
// Action: Query to validate form load
...
...
extension/qfq/qfq/Save.php
View file @
d5a03b6a
...
...
@@ -536,7 +536,11 @@ class Save {
*
* @return false|string New pathFilename or false on error
* @throws CodeException
* @throws DbException
* @throws UserFormException
* @throws UserReportException
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
* @internal param $recordId
*/
private
function
doUpload
(
$formElement
,
$sipUpload
,
Sip
$sip
,
&
$modeUpload
)
{
...
...
@@ -549,35 +553,91 @@ class Save {
return
false
;
}
// 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
);
}
if
(
isset
(
$formElement
[
FE_DATA_IMPORT
]))
{
// Import
// TODO EV
$dataImports
=
explode
(
'|'
,
$formElement
[
FE_DATA_IMPORT
]);
foreach
(
$dataImports
as
$dataImport
)
{
$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.
if
(
isset
(
$statusUpload
[
FILES_FLAG_DELETE
])
&&
$statusUpload
[
FILES_FLAG_DELETE
]
==
'1'
)
{
$arr
=
$sip
->
getVarsFromSip
(
$sipUpload
);
$oldFile
=
$arr
[
EXISTING_PATH_FILE_NAME
];
if
(
file_exists
(
$oldFile
))
{
if
(
!
unlink
(
$oldFile
))
{
throw
new
UserFormException
(
'Unlink file failed: '
.
$oldFile
,
ERROR_IO_UNLINK
);
//TODO: Only specified region
$columnBegin
=
'A'
;
$columnEnd
=
\
PhpOffice\PhpSpreadsheet\Cell\Coordinate
::
stringFromColumnIndex
(
count
(
$worksheetData
[
0
]));
$columnEnd
++
;
// the first non-existing column (important for use in for loop below)
$columnDefinitionArr
=
[];
$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
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
;
}
// Delete existing old file.
if
(
isset
(
$statusUpload
[
FILES_FLAG_DELETE
])
&&
$statusUpload
[
FILES_FLAG_DELETE
]
==
'1'
)
{
$arr
=
$sip
->
getVarsFromSip
(
$sipUpload
);
$oldFile
=
$arr
[
EXISTING_PATH_FILE_NAME
];
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
$this
->
store
->
setVar
(
$sipUpload
,
array
(),
STORE_EXTRA
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment