diff --git a/extension/Classes/Core/Form/FormAsFile.php b/extension/Classes/Core/Form/FormAsFile.php index f3015a02b6f27b62971b286061bfc397680ac7f1..958af1bf11f8932f4d250eba8835d475f80096bc 100644 --- a/extension/Classes/Core/Form/FormAsFile.php +++ b/extension/Classes/Core/Form/FormAsFile.php @@ -143,25 +143,25 @@ class FormAsFile } /** - * Create copy of given form file in form/backup. If given file does not exist, do nothing. + * Create copy of given form file in form/_backup. If given file does not exist, do nothing. * New file name: <formName>.YYYMMDDhhmmss.file.json * - * @param string $pathFileName + * @param string $cwdToFormFile * @throws \UserFormException */ - private static function backupFormFile(string $pathFileName) + private static function backupFormFile(string $cwdToFormFile) { - if (file_exists($pathFileName)) + if (file_exists($cwdToFormFile)) { - if (!is_readable($pathFileName)) { - Thrower::userFormException('Error while trying to backup form file.', "Form file is not readable: $pathFileName"); + if (!is_readable($cwdToFormFile)) { + Thrower::userFormException('Error while trying to backup form file.', "Form file is not readable: $cwdToFormFile"); } // copy file - $cwdToBackupFile = self::newBackupPathFileName(basename($pathFileName, '.json'), 'file'); - $success = copy($pathFileName, $cwdToBackupFile); + $cwdToBackupFile = self::newBackupPathFileName(basename($cwdToFormFile, '.json'), 'file'); + $success = copy($cwdToFormFile, $cwdToBackupFile); if ($success === false) { - Thrower::userFormException('Error while trying to backup form file.', "Can't copy file $pathFileName to $cwdToBackupFile"); + Thrower::userFormException('Error while trying to backup form file.', "Can't copy file $cwdToFormFile to $cwdToBackupFile"); } } } diff --git a/extension/Classes/Core/Helper/Path.php b/extension/Classes/Core/Helper/Path.php index 979923030953dee373a504e6bd0a66c1980b46b1..39ef5693b025f797f8ca564c75f32329cdacd5d8 100644 --- a/extension/Classes/Core/Helper/Path.php +++ b/extension/Classes/Core/Helper/Path.php @@ -52,6 +52,7 @@ class Path const PROJECT_TO_FORM = 'form'; const FORM_TO_FORM_BACKUP = '_backup'; const PROJECT_DIR_TO_REPORT = 'report'; + const REPORT_FILE_TO_BACKUP = '_backup'; // The path from a directory containing a report file to the directory containing backups of that report file // Config const APP_TO_TYPO3_CONF = 'typo3conf'; diff --git a/extension/Classes/Core/Report/ReportAsFile.php b/extension/Classes/Core/Report/ReportAsFile.php index 130121d0f9a2f9641dace3b9b4e1a957444fd8d8..4b9c44f178f1d0fc0b22c6fd5ff71de5bcf7b03f 100644 --- a/extension/Classes/Core/Report/ReportAsFile.php +++ b/extension/Classes/Core/Report/ReportAsFile.php @@ -3,6 +3,7 @@ namespace IMATHUZH\Qfq\Core\Report; use IMATHUZH\Qfq\Core\Database\Database; +use IMATHUZH\Qfq\Core\Exception\Thrower; use IMATHUZH\Qfq\Core\Helper\HelperFile; use IMATHUZH\Qfq\Core\Helper\Path; use IMATHUZH\Qfq\Core\Helper\Sanitize; @@ -158,8 +159,8 @@ class ReportAsFile $ttContent = $databaseT3->sql($sql, ROW_EXPECT_1, [$uid], "Typo3 content element not found. Uid: $uid"); - $reportPathFileNameFull = self::parseFileKeyword($ttContent['bodytext']); - if ($reportPathFileNameFull === null) { + $cwdToReportFile = self::parseFileKeyword($ttContent['bodytext']); + if ($cwdToReportFile === null) { throw new \UserReportException(json_encode([ ERROR_MESSAGE_TO_USER => "No report file defined.", ERROR_MESSAGE_TO_DEVELOPER => "The keyword '" . TOKEN_REPORT_FILE . "' is not present in the typo3 content element with id $uid"]), @@ -167,7 +168,59 @@ class ReportAsFile } // Update report file - HelperFile::file_put_contents($reportPathFileNameFull, $newContent); + self::backupReportFile($cwdToReportFile); + HelperFile::file_put_contents($cwdToReportFile, $newContent); + } + + /** + * Create copy of given report file in _backup directory. If given file does not exist, do nothing. + * New file name: <reportFileName>.YYYMMDDhhmmss.json + * + * @param string $cwdToReportFile + * @throws \UserFormException + */ + private static function backupReportFile(string $cwdToReportFile) + { + if (file_exists($cwdToReportFile)) + { + if (!is_readable($cwdToReportFile)) { + Thrower::userFormException('Error while trying to backup report file.', "Report file is not readable: $cwdToReportFile"); + } + + // copy file + $cwdToBackupFile = self::newBackupPathFileName($cwdToReportFile); + $success = copy($cwdToReportFile, $cwdToBackupFile); + if ($success === false) { + Thrower::userFormException('Error while trying to backup report file.', "Can't copy file $cwdToReportFile to $cwdToBackupFile"); + } + } + } + + /** + * Return the path to a (non-existing) report backup file with name: + * <reportFileName>.YYYMMDDhhmmss.<tag>.qfqr + * + * @param string $cwdToReportFile + * @return string + * @throws \UserFormException + */ + private static function newBackupPathFileName(string $cwdToReportFile): string + { + // create backup path if not exists + $cwdToBackup = Path::join(dirname($cwdToReportFile), Path::REPORT_FILE_TO_BACKUP); + if (!is_dir($cwdToBackup)) { + $success = mkdir($cwdToBackup, 0777, true); + if ($success === false) { + Thrower::userFormException('Error while trying to backup report file.', "Can't create backup path: $cwdToBackup"); + } + } + + // throw exception if backup file exists + $cwdToBackupFile = Path::join($cwdToBackup, basename($cwdToReportFile, REPORT_FILE_EXTENSION) . '.' . date('YmdHis') . ".json"); + if (file_exists($cwdToBackupFile)) { + Thrower::userFormException('Error while trying to backup report file.', "Backup file already exists: $cwdToBackupFile"); + } + return $cwdToBackupFile; } /**