diff --git a/lib/documentmanagement/documentutil.inc.php b/lib/documentmanagement/documentutil.inc.php index 363fb41..ec7d70a 100644 --- a/lib/documentmanagement/documentutil.inc.php +++ b/lib/documentmanagement/documentutil.inc.php @@ -709,6 +709,43 @@ class KTDocumentUtil { return $oNewDocument; } + function rename($oDocument, $sNewFilename, $oUser) { + $oStorage =& KTStorageManagerUtil::getSingleton(); + + $iPreviousMetadataVersion = $oDocument->getMetadataVersionId(); + $oOldContentVersion = $oDocument->_oDocumentContentVersion; + $oDocument->startNewContentVersion($oUser); + KTDocumentUtil::copyMetadata($oDocument, $iPreviousMetadataVersion); + $res = $oStorage->renameDocument($oDocument, $oOldContentVersion, $sNewFilename); + + if (!$res) { + return PEAR::raiseError(_("An error occurred while storing the new file")); + } + + $oDocument->setLastModifiedDate(getCurrentDateTime()); + $oDocument->setModifiedUserId($oUser->getId()); + $oDocument->setMinorVersionNumber($oDocument->getMinorVersionNumber()+1); + $oDocument->_oDocumentContentVersion->setFilename($sNewFilename); + $bSuccess = $oDocument->update(); + if ($bSuccess !== true) { + if (PEAR::isError($bSuccess)) { + return $bSuccess; + } + return PEAR::raiseError(_("An error occurred while storing this document in the database")); + } + + // create the document transaction record + $oDocumentTransaction = & new DocumentTransaction($oDocument, 'Document renamed', 'ktcore.transactions.update'); + $oDocumentTransaction->create(); + + // fire subscription alerts for the checked in document + $oSubscriptionEvent = new SubscriptionEvent(); + $oFolder = Folder::get($oDocument->getFolderID()); + $oSubscriptionEvent->ModifyDocument($oDocument, $oFolder); + + return true; + } + } class KTMetadataValidationError extends PEAR_Error { diff --git a/lib/foldermanagement/folderutil.inc.php b/lib/foldermanagement/folderutil.inc.php index 5193db2..da86a6f 100644 --- a/lib/foldermanagement/folderutil.inc.php +++ b/lib/foldermanagement/folderutil.inc.php @@ -118,6 +118,46 @@ class KTFolderUtil { } return; } + + function rename($oFolder, $sNewName, $oUser) { + $oStorage =& KTStorageManagerUtil::getSingleton(); + + // First, deal with SQL, as it, at least, is guaranteed to be atomic + $table = "folders"; + + $sQuery = "UPDATE $table SET full_path = CONCAT(?, SUBSTRING(full_path FROM ?)) WHERE full_path LIKE ?"; + $aParams = array( + sprintf("%s/%s", $oFolder->getFullPath(), $sNewName), + strlen($oFolder->getFullPath() . '/' . $oFolder->getName()) + 1, + sprintf("%s/%s%%", $oFolder->getFullPath(), $oFolder->getName()), + ); + $res = DBUtil::runQuery(array($sQuery, $aParams)); + if (PEAR::isError($res)) { + return $res; + } + + $table = "documents"; + $sQuery = "UPDATE $table SET full_path = CONCAT(?, SUBSTRING(full_path FROM ?)) WHERE full_path LIKE ?"; + $aParams = array( + sprintf("%s/%s", $oFolder->getFullPath(), $sNewName), + strlen($oFolder->getFullPath() . '/' . $oFolder->getName()) + 1, + sprintf("%s/%s%%", $oFolder->getFullPath(), $oFolder->getName()), + ); + $res = DBUtil::runQuery(array($sQuery, $aParams)); + if (PEAR::isError($res)) { + return $res; + } + + $res = $oStorage->renameFolder($oFolder, $sNewName); + if (PEAR::isError($res)) { + return $res; + } + + $oFolder->setName($sNewName); + $res = $oFolder->update(); + + return $res; + } function exists($oParentFolder, $sName) { return Folder::folderExistsName($sName, $oParentFolder->getID()); diff --git a/lib/storage/ondiskpathstoragemanager.inc.php b/lib/storage/ondiskpathstoragemanager.inc.php index 6811f09..2755f91 100644 --- a/lib/storage/ondiskpathstoragemanager.inc.php +++ b/lib/storage/ondiskpathstoragemanager.inc.php @@ -239,6 +239,40 @@ class KTOnDiskPathStorageManager extends KTStorageManager { return KTUtil::moveDirectory($sSrc, $sDst); } + function renameFolder($oFolder, $sNewName) { + $table = "document_content_version"; + $sQuery = "UPDATE $table SET storage_path = CONCAT(?, SUBSTRING(storage_path FROM ?)) WHERE storage_path LIKE ?"; + $aParams = array( + sprintf("%s/%s", $oFolder->getFullPath(), $sNewName), + strlen($oFolder->getFullPath() . '/' . $oFolder->getName()) + 1, + sprintf("%s/%s%%", $oFolder->getFullPath(), $oFolder->getName()), + ); + $res = DBUtil::runQuery(array($sQuery, $aParams)); + if (PEAR::isError($res)) { + return $res; + } + + $oConfig =& KTConfig::getSingleton(); + $sSrc = sprintf("%s/%s/%s", + $oConfig->get('urls/documentRoot'), + $oFolder->getFullPath(), + $oFolder->getName() + ); + $sDst = sprintf("%s/%s/%s", + $oConfig->get('urls/documentRoot'), + $oFolder->getFullPath(), + $sNewName + ); + $res = @rename($sSrc, $sDst); + if (PEAR::isError($res) || ($res == false)) { + print '
-- unable to move ' . $sSrc . ' to ' . $sDst . ' '; + return false; + // return PEAR::raiseError('unable to move directory to ' . $sDst); + } + + return true; + } + /** * Perform any storage changes necessary to account for a copied * document object. @@ -260,6 +294,30 @@ class KTOnDiskPathStorageManager extends KTStorageManager { $oVersion->update(); } + /** + * Perform any storage changes necessary to account for a renamed + * document object. + * someone else _must_ call the update on $oDocument + */ + function renameDocument(&$oDocument, $oOldContentVersion, $sNewFilename) { + // we get the Folder object + $oVersion =& $oDocument->_oDocumentContentVersion; + $oConfig =& KTConfig::getSingleton(); + $sDocumentRoot = $oConfig->get('urls/documentRoot'); + + $sOldPath = sprintf("%s/%s-%s", KTDocumentCore::_generateFolderPath($oDocument->getFolderID()), $oOldContentVersion->getId(), $oOldContentVersion->getFileName()); + $sNewPath = sprintf("%s/%s-%s", KTDocumentCore::_generateFolderPath($oDocument->getFolderID()), $oDocument->_oDocumentContentVersion->getId(), $sNewFilename); + $sFullOldPath = sprintf("%s/%s", $sDocumentRoot, $sOldPath); + $sFullNewPath = sprintf("%s/%s", $sDocumentRoot, $sNewPath); + + $res = KTUtil::copyFile($sFullOldPath, $sFullNewPath); + if (PEAR::isError($res)) { return $res; } + + $oVersion->setStoragePath($sNewPath); + // someone else _must_ call the update. + return true; // RES ?= PEAR::raiseError('.'); + } + /** * Deletes a document- moves it to the Deleted/ folder * diff --git a/lib/storage/storagemanager.inc.php b/lib/storage/storagemanager.inc.php index ae07b33..a65f046 100644 --- a/lib/storage/storagemanager.inc.php +++ b/lib/storage/storagemanager.inc.php @@ -65,6 +65,10 @@ class KTStorageManager { function moveFolder ($oFolder, $oDestFolder) { return PEAR::raiseError("Not implemented"); } + + function renameFolder($oFolder, $sNewName) { + return PEAR::raiseError("Not implemented"); + } /** * Perform any storage changes necessary to account for a copied @@ -113,6 +117,10 @@ class KTStorageManager { function createFolder($sFolderPath) { return PEAR::raiseError("Not implemented"); } + + function renameDocument(&$oDocument, $oOldContentVersion, $sNewFilename) { + return PEAR::raiseError("Not implemented"); + } } class KTStorageManagerUtil { diff --git a/tests/storage/renameDocument.php b/tests/storage/renameDocument.php new file mode 100644 index 0000000..fff78cc --- /dev/null +++ b/tests/storage/renameDocument.php @@ -0,0 +1,18 @@ + \ No newline at end of file diff --git a/tests/storage/renameFolder.php b/tests/storage/renameFolder.php new file mode 100644 index 0000000..3fc8b26 --- /dev/null +++ b/tests/storage/renameFolder.php @@ -0,0 +1,25 @@ + \ No newline at end of file