addDocumentFiles() clears internal list, make removeDocumentFile() more robust

This commit is contained in:
Uwe Steinmann 2021-09-24 10:15:06 +02:00
parent 08887fcbd9
commit 233e464692

View File

@ -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;