Commit 0365199e730c735d701e83bea47e4ddf65c7fecb

Authored by Brad Shuttleworth
1 parent dce3e4ac

incorporate rename support.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@4900 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/documentmanagement/documentutil.inc.php
... ... @@ -709,6 +709,43 @@ class KTDocumentUtil {
709 709 return $oNewDocument;
710 710 }
711 711  
  712 + function rename($oDocument, $sNewFilename, $oUser) {
  713 + $oStorage =& KTStorageManagerUtil::getSingleton();
  714 +
  715 + $iPreviousMetadataVersion = $oDocument->getMetadataVersionId();
  716 + $oOldContentVersion = $oDocument->_oDocumentContentVersion;
  717 + $oDocument->startNewContentVersion($oUser);
  718 + KTDocumentUtil::copyMetadata($oDocument, $iPreviousMetadataVersion);
  719 + $res = $oStorage->renameDocument($oDocument, $oOldContentVersion, $sNewFilename);
  720 +
  721 + if (!$res) {
  722 + return PEAR::raiseError(_("An error occurred while storing the new file"));
  723 + }
  724 +
  725 + $oDocument->setLastModifiedDate(getCurrentDateTime());
  726 + $oDocument->setModifiedUserId($oUser->getId());
  727 + $oDocument->setMinorVersionNumber($oDocument->getMinorVersionNumber()+1);
  728 + $oDocument->_oDocumentContentVersion->setFilename($sNewFilename);
  729 + $bSuccess = $oDocument->update();
  730 + if ($bSuccess !== true) {
  731 + if (PEAR::isError($bSuccess)) {
  732 + return $bSuccess;
  733 + }
  734 + return PEAR::raiseError(_("An error occurred while storing this document in the database"));
  735 + }
  736 +
  737 + // create the document transaction record
  738 + $oDocumentTransaction = & new DocumentTransaction($oDocument, 'Document renamed', 'ktcore.transactions.update');
  739 + $oDocumentTransaction->create();
  740 +
  741 + // fire subscription alerts for the checked in document
  742 + $oSubscriptionEvent = new SubscriptionEvent();
  743 + $oFolder = Folder::get($oDocument->getFolderID());
  744 + $oSubscriptionEvent->ModifyDocument($oDocument, $oFolder);
  745 +
  746 + return true;
  747 + }
  748 +
712 749 }
713 750  
714 751 class KTMetadataValidationError extends PEAR_Error {
... ...
lib/foldermanagement/folderutil.inc.php
... ... @@ -118,6 +118,46 @@ class KTFolderUtil {
118 118 }
119 119 return;
120 120 }
  121 +
  122 + function rename($oFolder, $sNewName, $oUser) {
  123 + $oStorage =& KTStorageManagerUtil::getSingleton();
  124 +
  125 + // First, deal with SQL, as it, at least, is guaranteed to be atomic
  126 + $table = "folders";
  127 +
  128 + $sQuery = "UPDATE $table SET full_path = CONCAT(?, SUBSTRING(full_path FROM ?)) WHERE full_path LIKE ?";
  129 + $aParams = array(
  130 + sprintf("%s/%s", $oFolder->getFullPath(), $sNewName),
  131 + strlen($oFolder->getFullPath() . '/' . $oFolder->getName()) + 1,
  132 + sprintf("%s/%s%%", $oFolder->getFullPath(), $oFolder->getName()),
  133 + );
  134 + $res = DBUtil::runQuery(array($sQuery, $aParams));
  135 + if (PEAR::isError($res)) {
  136 + return $res;
  137 + }
  138 +
  139 + $table = "documents";
  140 + $sQuery = "UPDATE $table SET full_path = CONCAT(?, SUBSTRING(full_path FROM ?)) WHERE full_path LIKE ?";
  141 + $aParams = array(
  142 + sprintf("%s/%s", $oFolder->getFullPath(), $sNewName),
  143 + strlen($oFolder->getFullPath() . '/' . $oFolder->getName()) + 1,
  144 + sprintf("%s/%s%%", $oFolder->getFullPath(), $oFolder->getName()),
  145 + );
  146 + $res = DBUtil::runQuery(array($sQuery, $aParams));
  147 + if (PEAR::isError($res)) {
  148 + return $res;
  149 + }
  150 +
  151 + $res = $oStorage->renameFolder($oFolder, $sNewName);
  152 + if (PEAR::isError($res)) {
  153 + return $res;
  154 + }
  155 +
  156 + $oFolder->setName($sNewName);
  157 + $res = $oFolder->update();
  158 +
  159 + return $res;
  160 + }
121 161  
122 162 function exists($oParentFolder, $sName) {
123 163 return Folder::folderExistsName($sName, $oParentFolder->getID());
... ...
lib/storage/ondiskpathstoragemanager.inc.php
... ... @@ -239,6 +239,40 @@ class KTOnDiskPathStorageManager extends KTStorageManager {
239 239 return KTUtil::moveDirectory($sSrc, $sDst);
240 240 }
241 241  
  242 + function renameFolder($oFolder, $sNewName) {
  243 + $table = "document_content_version";
  244 + $sQuery = "UPDATE $table SET storage_path = CONCAT(?, SUBSTRING(storage_path FROM ?)) WHERE storage_path LIKE ?";
  245 + $aParams = array(
  246 + sprintf("%s/%s", $oFolder->getFullPath(), $sNewName),
  247 + strlen($oFolder->getFullPath() . '/' . $oFolder->getName()) + 1,
  248 + sprintf("%s/%s%%", $oFolder->getFullPath(), $oFolder->getName()),
  249 + );
  250 + $res = DBUtil::runQuery(array($sQuery, $aParams));
  251 + if (PEAR::isError($res)) {
  252 + return $res;
  253 + }
  254 +
  255 + $oConfig =& KTConfig::getSingleton();
  256 + $sSrc = sprintf("%s/%s/%s",
  257 + $oConfig->get('urls/documentRoot'),
  258 + $oFolder->getFullPath(),
  259 + $oFolder->getName()
  260 + );
  261 + $sDst = sprintf("%s/%s/%s",
  262 + $oConfig->get('urls/documentRoot'),
  263 + $oFolder->getFullPath(),
  264 + $sNewName
  265 + );
  266 + $res = @rename($sSrc, $sDst);
  267 + if (PEAR::isError($res) || ($res == false)) {
  268 + print '<br /> -- unable to move ' . $sSrc . ' to ' . $sDst . ' ';
  269 + return false;
  270 + // return PEAR::raiseError('unable to move directory to ' . $sDst);
  271 + }
  272 +
  273 + return true;
  274 + }
  275 +
242 276 /**
243 277 * Perform any storage changes necessary to account for a copied
244 278 * document object.
... ... @@ -260,6 +294,30 @@ class KTOnDiskPathStorageManager extends KTStorageManager {
260 294 $oVersion->update();
261 295 }
262 296  
  297 + /**
  298 + * Perform any storage changes necessary to account for a renamed
  299 + * document object.
  300 + * someone else _must_ call the update on $oDocument
  301 + */
  302 + function renameDocument(&$oDocument, $oOldContentVersion, $sNewFilename) {
  303 + // we get the Folder object
  304 + $oVersion =& $oDocument->_oDocumentContentVersion;
  305 + $oConfig =& KTConfig::getSingleton();
  306 + $sDocumentRoot = $oConfig->get('urls/documentRoot');
  307 +
  308 + $sOldPath = sprintf("%s/%s-%s", KTDocumentCore::_generateFolderPath($oDocument->getFolderID()), $oOldContentVersion->getId(), $oOldContentVersion->getFileName());
  309 + $sNewPath = sprintf("%s/%s-%s", KTDocumentCore::_generateFolderPath($oDocument->getFolderID()), $oDocument->_oDocumentContentVersion->getId(), $sNewFilename);
  310 + $sFullOldPath = sprintf("%s/%s", $sDocumentRoot, $sOldPath);
  311 + $sFullNewPath = sprintf("%s/%s", $sDocumentRoot, $sNewPath);
  312 +
  313 + $res = KTUtil::copyFile($sFullOldPath, $sFullNewPath);
  314 + if (PEAR::isError($res)) { return $res; }
  315 +
  316 + $oVersion->setStoragePath($sNewPath);
  317 + // someone else _must_ call the update.
  318 + return true; // RES ?= PEAR::raiseError('.');
  319 + }
  320 +
263 321 /**
264 322 * Deletes a document- moves it to the Deleted/ folder
265 323 *
... ...
lib/storage/storagemanager.inc.php
... ... @@ -65,6 +65,10 @@ class KTStorageManager {
65 65 function moveFolder ($oFolder, $oDestFolder) {
66 66 return PEAR::raiseError("Not implemented");
67 67 }
  68 +
  69 + function renameFolder($oFolder, $sNewName) {
  70 + return PEAR::raiseError("Not implemented");
  71 + }
68 72  
69 73 /**
70 74 * Perform any storage changes necessary to account for a copied
... ... @@ -113,6 +117,10 @@ class KTStorageManager {
113 117 function createFolder($sFolderPath) {
114 118 return PEAR::raiseError("Not implemented");
115 119 }
  120 +
  121 + function renameDocument(&$oDocument, $oOldContentVersion, $sNewFilename) {
  122 + return PEAR::raiseError("Not implemented");
  123 + }
116 124 }
117 125  
118 126 class KTStorageManagerUtil {
... ...
tests/storage/renameDocument.php 0 → 100644
  1 +<?php
  2 +
  3 +require_once("../../config/dmsDefaults.php");
  4 +require_once(KT_LIB_DIR . '/documentmanagement/documentutil.inc.php');
  5 +require_once(KT_LIB_DIR . '/users/User.inc');
  6 +require_once(KT_LIB_DIR . '/database/dbutil.inc');
  7 +
  8 +error_reporting(E_ALL);
  9 +
  10 +$oDocument = Document::get(28);
  11 +$oUser = User::get(1);
  12 +
  13 +var_dump(KTDocumentUtil::rename($oDocument, 'bob1', $oUser));
  14 +var_dump(KTDocumentUtil::rename($oDocument, 'bob2', $oUser));
  15 +var_dump(KTDocumentUtil::rename($oDocument, 'bob3', $oUser));
  16 +var_dump(KTDocumentUtil::rename($oDocument, 'bob4', $oUser));
  17 +
  18 +?>
0 19 \ No newline at end of file
... ...
tests/storage/renameFolder.php 0 → 100644
  1 +<?php
  2 +
  3 +require_once("../../config/dmsDefaults.php");
  4 +require_once(KT_LIB_DIR . '/foldermanagement/folderutil.inc.php');
  5 +require_once(KT_LIB_DIR . '/users/User.inc');
  6 +require_once(KT_LIB_DIR . '/database/dbutil.inc');
  7 +
  8 +error_reporting(E_ALL);
  9 +
  10 +$iFolderId = 43;
  11 +
  12 +$oUser = User::get(1);
  13 +
  14 +$j = 'll';
  15 +
  16 +$oFolder = Folder::get($iFolderId);
  17 +var_dump(KTFolderUtil::rename($oFolder, $j . '1', $oUser));
  18 +$oFolder = Folder::get($iFolderId);
  19 +var_dump(KTFolderUtil::rename($oFolder, $j . '2', $oUser));
  20 +$oFolder = Folder::get($iFolderId);
  21 +var_dump(KTFolderUtil::rename($oFolder, $j . '3', $oUser));
  22 +$oFolder = Folder::get($iFolderId);
  23 +var_dump(KTFolderUtil::rename($oFolder, $j . '4', $oUser));
  24 +
  25 +?>
0 26 \ No newline at end of file
... ...