Commit ae0483ab authored by Marc Egger's avatar Marc Egger
Browse files

Refs #10120 when form path is created, export all forms from DB

parent a0778931
Pipeline #3616 failed with stages
in 41 seconds
......@@ -107,7 +107,7 @@ class FormAsFile
public static function importForm(string $formName, Database $database): bool
{
// Get file stats both from file system and DB and exit if they are equal
$pathFileName = self::formPathFileName($formName);
$pathFileName = self::formPathFileName($formName, $database);
$fileReadException = new \UserFormException(json_encode([
ERROR_MESSAGE_TO_USER => "Form file not found or missing permission: " . baseName($pathFileName),
ERROR_MESSAGE_TO_DEVELOPER => "Form definition file not found or no permission to read file: '$pathFileName'"]),
......@@ -244,7 +244,7 @@ class FormAsFile
// write form as JSON to file
$form[FORM_FILE_FORM_ELEMENT] = $formElements;
$formJson = json_encode($form, JSON_PRETTY_PRINT);
$pathFileName = self::formPathFileName($formName);
$pathFileName = self::formPathFileName($formName, $database);
$success = file_put_contents($pathFileName, $formJson);
if ($success === false) {
throw new \UserFormException(json_encode([
......@@ -263,12 +263,15 @@ class FormAsFile
* Deletes the form file for the given form.
*
* @param $formName
* @param Database $database
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/
public static function deleteFormFile($formName): void
public static function deleteFormFile($formName, Database $database): void
{
self::enforceFormFileWritable($formName);
$pathFileName = self::formPathFileName($formName);
self::enforceFormFileWritable($formName, $database);
$pathFileName = self::formPathFileName($formName, $database);
if(file_exists($pathFileName)) {
unlink($pathFileName);
}
......@@ -294,12 +297,15 @@ class FormAsFile
* Throw exception if form file or form directory is not writeable.
*
* @param string $formName
* @param Database $database
* @return void
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/
public static function enforceFormFileWritable(string $formName): void
public static function enforceFormFileWritable(string $formName, Database $database): void
{
$pathFileName = self::formPathFileName($formName);
$pathFileName = self::formPathFileName($formName, $database);
if (file_exists($pathFileName)) {
if (!is_writable($pathFileName)) {
throw new \UserFormException(json_encode([
......@@ -391,11 +397,12 @@ class FormAsFile
public static function updateAllForms(Database $database): void
{
// Import all form files
$files = scandir(self::formPath());
$formPath = self::formPath($database);
$files = scandir($formPath);
if ($files === false) {
throw new \UserFormException(json_encode([
ERROR_MESSAGE_TO_USER => "Reading directory failed.",
ERROR_MESSAGE_TO_DEVELOPER => "Can't read directory: " . self::formPath()]),
ERROR_MESSAGE_TO_DEVELOPER => "Can't read directory: " . $formPath]),
ERROR_IO_READ_FILE);
}
$formNamesFile = [];
......@@ -410,7 +417,7 @@ class FormAsFile
// Delete all forms which are in DB but not in files
$NAME = F_NAME;
$FORM = TABLE_NAME_FORM;
$formNamesDB = array_column($database->sql("SELECT `$NAME` FROM `$FORM` ", ROW_REGULAR), $NAME);
$formNamesDB = array_column($database->sql("SELECT `$NAME` FROM `$FORM`", ROW_REGULAR), $NAME);
$formsToDelete = array_diff($formNamesDB, $formNamesFile);
foreach ($formsToDelete as $formToDelete) {
self::deleteFormDB($formToDelete, $database);
......@@ -509,13 +516,16 @@ class FormAsFile
/**
* Return correct pathFileName of form file relative to current working directory.
* Create path if it doesn't exist.
* Create path if it doesn't exist and export all forms from DB.
*
* @param string $formName
* @param Database $database
* @return string
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/
private static function formPathFileName(string $formName): string
private static function formPathFileName(string $formName, Database $database): string
{
// validate form name
if (!HelperFile::isValidFileName($formName)) {
......@@ -524,20 +534,25 @@ class FormAsFile
ERROR_MESSAGE_TO_DEVELOPER => "Form name '$formName' not valid. Name may only consist of alphanumeric characters and _ . -"]),
ERROR_FORM_INVALID_NAME);
}
return self::formPath() . '/' . $formName . ".json";
return self::formPath($database) . '/' . $formName . ".json";
}
/**
* Return the path of the form directory relative to CWD.
* Create path if it doesn't exist.
* Create path if it doesn't exist and export all forms from DB.
*
* @param Database $database
* @return string
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
*/
private static function formPath(): string
private static function formPath(Database $database): string
{
$formPath = HelperFile::correctRelativePathFileName(SYSTEM_FORM_FILE_PATH);
if (!is_dir($formPath)) {
// create path
$success = mkdir($formPath, 0777, true);
if ($success === false) {
throw new \UserFormException(json_encode([
......@@ -545,6 +560,14 @@ class FormAsFile
ERROR_MESSAGE_TO_DEVELOPER => "Can't create path: " . $formPath]),
ERROR_IO_WRITE_FILE);
}
// export all forms
$NAME = F_NAME;
$FORM = TABLE_NAME_FORM;
$formNamesDB = array_column($database->sql("SELECT `$NAME` FROM `$FORM`", ROW_REGULAR), $NAME);
foreach ($formNamesDB as $formNameDB) {
self::exportForm($formNameDB, $database);
}
}
return $formPath;
}
......
......@@ -431,7 +431,7 @@ class QuickFormQuery {
$formFileName = $formFileName === false ? $formFileNameDB : $formFileName;
if ($formFileNameDB !== null && $formFileName !== $formFileNameDB && $formModeNew === FORM_SAVE) {
$formFileNameDelete = $formFileNameDB;
FormAsFile::enforceFormFileWritable($formFileNameDelete); // file will be deleted after DB changes
FormAsFile::enforceFormFileWritable($formFileNameDelete, $this->dbArray[$this->dbIndexQfq]); // file will be deleted after DB changes
}
break;
case TABLE_NAME_FORM_ELEMENT: // cases covered: new formElement, existing formElement
......@@ -442,7 +442,7 @@ class QuickFormQuery {
$formFileName = $formFileNameDB;
}
if ($formFileName !== null && in_array($formModeNew, [FORM_SAVE, FORM_DRAG_AND_DROP, FORM_DELETE])) {
FormAsFile::enforceFormFileWritable($formFileName);
FormAsFile::enforceFormFileWritable($formFileName, $this->dbArray[$this->dbIndexQfq]);
}
// For 'new' record always create a new Browser TAB-uniq (for this current form, nowhere else used) SIP.
......@@ -654,7 +654,7 @@ class QuickFormQuery {
if (TABLE_NAME_FORM_ELEMENT === $this->formSpec[F_TABLE_NAME]) {
FormAsFile::exportForm($formFileName, $this->dbArray[$this->dbIndexQfq]);
} else {
FormAsFile::deleteFormFile($formFileName);
FormAsFile::deleteFormFile($formFileName, $this->dbArray[$this->dbIndexQfq]);
}
break;
}
......@@ -662,7 +662,7 @@ class QuickFormQuery {
// delete old form file if form name was changed
if (isset($formFileNameDelete)) {
FormAsFile::deleteFormFile($formFileNameDelete);
FormAsFile::deleteFormFile($formFileNameDelete, $this->dbArray[$this->dbIndexQfq]);
}
return $data;
......
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