From 233e464692d87f9c66564df403d22b0ab06016b6 Mon Sep 17 00:00:00 2001 From: Uwe Steinmann Date: Fri, 24 Sep 2021 10:15:06 +0200 Subject: [PATCH] addDocumentFiles() clears internal list, make removeDocumentFile() more robust --- SeedDMS_Core/Core/inc.ClassDocument.php | 28 ++++++++++++++++--------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/SeedDMS_Core/Core/inc.ClassDocument.php b/SeedDMS_Core/Core/inc.ClassDocument.php index 2a89f7185..ba9f9bec8 100644 --- a/SeedDMS_Core/Core/inc.ClassDocument.php +++ b/SeedDMS_Core/Core/inc.ClassDocument.php @@ -2358,29 +2358,37 @@ class SeedDMS_Core_Document extends SeedDMS_Core_Object { /* {{{ */ } $db->commitTransaction(); + unset ($this->_documentFiles); return $file; } /* }}} */ function removeDocumentFile($ID) { /* {{{ */ $db = $this->_dms->getDB(); - if (!is_numeric($ID)) return false; + if (!is_numeric($ID) || $ID < 1) + return false; $file = $this->getDocumentFile($ID); if (is_bool($file) && !$file) return false; - if (file_exists( $this->_dms->contentDir . $file->getPath() )){ - if (!SeedDMS_Core_File::removeFile( $this->_dms->contentDir . $file->getPath() )) - return false; + $db->startTransaction(); + /* First delete the database record, because that can be undone + * if deletion of the file fails. + */ + $queryStr = "DELETE FROM `tblDocumentFiles` WHERE `document` = " . $this->getID() . " AND `id` = " . (int) $ID; + if (!$db->getResult($queryStr)) { + $db->rollbackTransaction(); + return false; } - $name=$file->getName(); - $comment=$file->getcomment(); - - $queryStr = "DELETE FROM `tblDocumentFiles` WHERE `document` = " . $this->getID() . " AND `id` = " . (int) $ID; - if (!$db->getResult($queryStr)) - return false; + if (file_exists( $this->_dms->contentDir . $file->getPath() )){ + if (!SeedDMS_Core_File::removeFile( $this->_dms->contentDir . $file->getPath() )) { + $db->rollbackTransaction(); + return false; + } + } + $db->commitTransaction(); unset ($this->_documentFiles); return true;