/** * Copies the document from one folder to another. * * <code> * $ktapi = new KTAPI(); * $session = $ktapi->start_system_session(); * $document = $ktapi->get_document_by_id($documentid); * $newFolder = $this->root->add_folder("New folder"); * $copyOfDoc = $document->copy($newFolder, 'Reason for copying document'); * </code> * * @author KnowledgeTree Team * @access public * @param KTAPI_Folder $ktapi_target_folder The new parent folder where the document is being copied into * @param string $reason The reason for the copy * @param string $newname Optional. The title of the document to be used in the case of a name clash * @param string $newfilename Optional. The filename of the document to be used in the case of a name clash * @return KTAPI_Document|PEAR_Error Returns the new KTAPI Document object | a PEAR_Error on failure */ function copy(&$ktapi_target_folder, $reason, $newname = null, $newfilename = null) { assert(!is_null($ktapi_target_folder)); assert($ktapi_target_folder instanceof KTAPI_FOLDER); //is_a($ktapi_target_folder,'KTAPI_Folder')); if (empty($newname)) { $newname = null; } if (empty($newfilename)) { $newfilename = null; } $user = $this->ktapi->get_user(); if ($this->document->getIsCheckedOut()) { return new PEAR_Error(KTAPI_ERROR_DOCUMENT_CHECKED_OUT); } $target_folder =& $ktapi_target_folder->get_folder(); $result = $this->can_user_access_object_requiring_permission($target_folder, KTAPI_PERMISSION_WRITE); if (PEAR::isError($result)) { return $result; } $name = $this->document->getName(); $clash = KTDocumentUtil::nameExists($target_folder, $name); if ($clash && !is_null($newname)) { $name = $newname; $clash = KTDocumentUtil::nameExists($target_folder, $name); } if ($clash) { if (is_null($newname)) { $name = KTDocumentUtil::getUniqueDocumentName($target_folder, $name); } else { return new PEAR_Error('A document with this title already exists in your chosen folder. Please choose a different folder, or specify a new title for the copied document.'); } } $filename = $this->document->getFilename(); $clash = KTDocumentUtil::fileExists($target_folder, $filename); if ($clash && !is_null($newname)) { $filename = $newfilename; $clash = KTDocumentUtil::fileExists($target_folder, $filename); } if ($clash) { if (is_null($newfilename)) { $filename = KTDocumentUtil::getUniqueFilename($target_folder, $newfilename); } else { return new PEAR_Error('A document with this filename already exists in your chosen folder. Please choose a different folder, or specify a new filename for the copied document.'); } } DBUtil::startTransaction(); $new_document = KTDocumentUtil::copy($this->document, $target_folder, $reason); if (PEAR::isError($new_document)) { DBUtil::rollback(); return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $new_document); } $new_document->setName($name); $new_document->setFilename($filename); $res = $new_document->update(); if (PEAR::isError($res)) { DBUtil::rollback(); return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $res); } DBUtil::commit(); /* // FIXME do we need to refactor all trigger usage into the util function? $oKTTriggerRegistry = KTTriggerRegistry::getSingleton(); $aTriggers = $oKTTriggerRegistry->getTriggers('copyDocument', 'postValidate'); foreach ($aTriggers as $aTrigger) { $sTrigger = $aTrigger[0]; $oTrigger = new $sTrigger; $aInfo = array( 'document' => $new_document, 'old_folder' => $this->ktapi_folder->get_folder(), 'new_folder' => $target_folder, ); $oTrigger->setInfo($aInfo); $ret = $oTrigger->postValidate(); } */ return KTAPI_Document::get($this->ktapi, $new_document->getId()); }
function move($oDocument, $oToFolder, $oUser = null, $sReason = null) { //make sure we move the symlink, and the document it's linking to if ($oDocument->isSymbolicLink()) { $oDocument->switchToRealCore(); } else { $oDocument->switchToLinkedCore(); } $oFolder = $oToFolder; // alias. $oOriginalFolder = Folder::get($oDocument->getFolderId()); $iOriginalFolderPermissionObjectId = $oOriginalFolder->getPermissionObjectId(); $iDocumentPermissionObjectId = $oDocument->getPermissionObjectId(); if ($iDocumentPermissionObjectId === $iOriginalFolderPermissionObjectId) { $oDocument->setPermissionObjectId($oFolder->getPermissionObjectId()); } //put the document in the new folder $oDocument->setFolderID($oFolder->getId()); $sName = $oDocument->getName(); $sFilename = $oDocument->getFileName(); $oDocument->setFileName(KTDocumentUtil::getUniqueFilename($oToFolder, $sFilename)); $oDocument->setName(KTDocumentUtil::getUniqueDocumentName($oToFolder, $sName)); $res = $oDocument->update(); if (PEAR::isError($res)) { return $res; } //move the document on the file system(not if it's a symlink) if (!$oDocument->isSymbolicLink()) { $oStorage =& KTStorageManagerUtil::getSingleton(); $res = $oStorage->moveDocument($oDocument, $oFolder, $oOriginalFolder); if (PEAR::isError($res) || $res === false) { $oDocument->setFolderID($oOriginalFolder->getId()); $res = $oDocument->update(); if (PEAR::isError($res)) { return $res; } return $res; // we failed, bail. } } $sMoveMessage = sprintf(_kt("Moved from %s/%s to %s/%s. %s"), $oOriginalFolder->getFullPath(), $oOriginalFolder->getName(), $oFolder->getFullPath(), $oFolder->getName(), $sReason); // create the document transaction record $oDocumentTransaction = new DocumentTransaction($oDocument, $sMoveMessage, 'ktcore.transactions.move'); $oDocumentTransaction->create(); $oKTTriggerRegistry = KTTriggerRegistry::getSingleton(); $aTriggers = $oKTTriggerRegistry->getTriggers('moveDocument', 'postValidate'); foreach ($aTriggers as $aTrigger) { $sTrigger = $aTrigger[0]; $oTrigger = new $sTrigger(); $aInfo = array('document' => $oDocument, 'old_folder' => $oOriginalFolder, 'new_folder' => $oFolder); $oTrigger->setInfo($aInfo); $ret = $oTrigger->postValidate(); if (PEAR::isError($ret)) { return $ret; } } // fire subscription alerts for the moved document $oSubscriptionEvent = new SubscriptionEvent(); $oSubscriptionEvent->MoveDocument($oDocument, $oFolder, $oOriginalFolder); return KTPermissionUtil::updatePermissionLookup($oDocument); }