Commit 495b656e authored by Marc Egger's avatar Marc Egger
Browse files

Refs #12145 create form with json form editor. Fix unicode error. Fix form...

Refs #12145 create form with json form editor. Fix unicode error. Fix form file sync (even though disabled). Throw errors if json encode/decode fails. Put form backups into folder named by form id.
parent 1f143c3f
Pipeline #5096 passed with stages
in 5 minutes and 20 seconds
......@@ -392,7 +392,10 @@ class FormAction {
// If given: save json form
if ($fe[FE_SAVE_FORM_JSON] !== null && $fe[FE_SAVE_FORM_JSON] !== '' && $fe[FE_SAVE_FORM_JSON_NAME] !== null && $fe[FE_SAVE_FORM_JSON_NAME] !== '') {
FormAsFile::jsonToDatabase($this->evaluate->parse($fe[FE_SAVE_FORM_JSON_NAME]), $this->evaluate->parse($fe[FE_SAVE_FORM_JSON]), $this->db);
$jsonContent = $this->evaluate->parse($fe[FE_SAVE_FORM_JSON]);
if ($jsonContent !== '') {
FormAsFile::jsonToDatabase($this->evaluate->parse($fe[FE_SAVE_FORM_JSON_NAME]), $jsonContent, $this->db);
}
}
return $rcStatus;
......
......@@ -107,6 +107,9 @@ class FormAsFile {
public static function jsonToDatabase(string $formName, string $formRawJson, Database $database, string $absoluteFormFilePath = null, string $fileStatsNew = null)
{
$formFromJson = json_decode($formRawJson, true);
if (json_last_error() !== JSON_ERROR_NONE) {
Thrower::userFormException('Error during form import.', 'Json encode error: ' . json_last_error_msg());
}
// form elements exist?
if (!isset($formFromJson[F_FILE_FORM_ELEMENT])) {
......@@ -186,17 +189,23 @@ class FormAsFile {
$formIdOld = $formFromDb[F_ID] ?? null;
if ($formIdOld !== null) {
self::deleteFormDBWithId($formIdOld, $database, "New version of form $formName was imported successfully. New form id: $formIdNew");
// Replace temporary name of new form and set id to old id
list($sql, $parameterArray) = SqlQuery::updateRecord(TABLE_NAME_FORM, [F_NAME => $formName, F_ID => $formIdOld], $formIdNew);
$database->sql($sql, ROW_REGULAR, $parameterArray);
// update formId on all new FormElements
$_FormElement = TABLE_NAME_FORM_ELEMENT;
$_formId = FE_FORM_ID;
$sql = "UPDATE `$_FormElement` SET `$_formId`=? WHERE `$_formId`=?";
$database->sql($sql, ROW_REGULAR, [$formIdOld, $formIdNew]);
} else {
// Replace temporary name of new form
list($sql, $parameterArray) = SqlQuery::updateRecord(TABLE_NAME_FORM, [F_NAME => $formName], $formIdNew);
$database->sql($sql, ROW_REGULAR, $parameterArray);
}
// Replace temporary name of new form and set id to old id
list($sql, $parameterArray) = SqlQuery::updateRecord(TABLE_NAME_FORM, [F_NAME => $formName, F_ID => $formIdOld], $formIdNew);
$database->sql($sql, ROW_REGULAR, $parameterArray);
// update formId on all new FormElements
$_FormElement = TABLE_NAME_FORM_ELEMENT;
$_formId = FE_FORM_ID;
$sql = "UPDATE `$_FormElement` SET `$_formId`=? WHERE `$_formId`=?";
$database->sql($sql, ROW_REGULAR, [$formIdOld, $formIdNew]);
}
/**
......@@ -588,7 +597,7 @@ class FormAsFile {
*/
private static function backupFormDb(int $formId, Database $database) {
list($formName, $formId, $formJson) = self::formToJson('', $database, $formId);
$absoluteBackupFilePath = self::newBackupPathFileName($formName, 'db');
$absoluteBackupFilePath = self::newBackupPathFileName($formName, 'db', $formId);
HelperFile::file_put_contents($absoluteBackupFilePath, $formJson);
}
......@@ -812,22 +821,27 @@ class FormAsFile {
// add form elements and create json
$form[F_FILE_FORM_ELEMENT] = $formElements;
$formJson = json_encode($form, JSON_PRETTY_PRINT);
$formJson = json_encode($form, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
if (json_last_error() !== JSON_ERROR_NONE) {
Thrower::userFormException('Error during form export.', 'Json encode error: ' . json_last_error_msg());
}
return array($formName, $formId, $formJson, $adjustedContainerNames > 0, $form);
}
/**
* Return the path to a (non-existing) form backup file with name:
* <formName>.YYYMMDDhhmmss.<tag>.json
* If the form id is given then the backup file is placed into a folder named by the id.
*
* @param string $formName
* @param string $tag
* @param int|null $formId
* @return string
* @throws \UserFormException
*/
private static function newBackupPathFileName(string $formName, string $tag): string {
private static function newBackupPathFileName(string $formName, string $tag, int $formId = null): string {
// create backup path if not exists
$absoluteBackupPath = Path::absoluteProject(Path::projectToForm(), Path::FORM_TO_FORM_BACKUP);
$absoluteBackupPath = Path::absoluteProject(Path::projectToForm(), Path::FORM_TO_FORM_BACKUP, $formId ?? '');
if (!is_dir($absoluteBackupPath)) {
$success = mkdir($absoluteBackupPath, 0777, true);
if ($success === false) {
......
......@@ -15,6 +15,7 @@ form={{form:SE}}
# Table header.
sql = SELECT '<th data-sorter="false" class="filter-false">'
, CONCAT('p:{{pageAlias:T}}&form=form&') as _pagen
, CONCAT('p:{{pageAlias:T}}&form=formJson&|t:json') as _pagen
, '</th><th>Name'
, '</th><th>Title'
, '</th><th>Table'
......
Supports Markdown
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