/**
  * Moves 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");
  * $document->move($newFolder, 'Reason for moving the document');
  * </code>
  *
  * @author KnowledgeTree Team
  * @access public
  * @param KTAPI_Folder $ktapi_target_folder The folder object where the document is being moved into
  * @param string $reason The reason for the move
  * @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 void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
  */
 function move(&$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->can_user_access_object_requiring_permission($this->document, KTAPI_PERMISSION_DOCUMENT_MOVE);
     if (PEAR::isError($user)) {
         return $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;
     }
     if (!KTDocumentUtil::canBeMoved($this->document)) {
         return new PEAR_Error('Document cannot be moved.');
     }
     $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) {
         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 moved 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) {
         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 moved document.');
     }
     DBUtil::startTransaction();
     $res = KTDocumentUtil::move($this->document, $target_folder, $user, $reason);
     if (PEAR::isError($res)) {
         DBUtil::rollback();
         return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $res);
     }
     $this->document->setName($name);
     $this->document->setFilename($filename);
     $res = $this->document->update();
     if (PEAR::isError($res)) {
         DBUtil::rollback();
         return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $res);
     }
     DBUtil::commit();
 }
Esempio n. 2
0
 function do_copy()
 {
     $oForm = $this->form_copyselection();
     $res = $oForm->validate();
     $errors = $res['errors'];
     $data = $res['results'];
     $sReason = $data['reason'];
     $extra_errors = array();
     if (!is_null($data['browse'])) {
         $bNameClash = KTDocumentUtil::nameExists($data['browse'], $this->oDocument->getName());
         if ($bNameClash && isset($data['name'])) {
             $name = $data['name'];
             $bNameClash = KTDocumentUtil::nameExists($data['browse'], $name);
         } else {
             $name = $this->oDocument->getName();
         }
         if ($bNameClash) {
             $extra_errors['name'] = _kt('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.');
         }
         $bFileClash = KTDocumentUtil::fileExists($data['browse'], $this->oDocument->getFilename());
         if ($bFileClash && isset($data['filename'])) {
             $filename = $data['filename'];
             $bFileClash = KTDocumentUtil::fileExists($data['browse'], $filename);
         } else {
             $filename = $this->oDocument->getFilename();
         }
         if ($bFileClash) {
             $extra_errors['filename'] = _kt('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.');
         }
         if (!Permission::userHasFolderWritePermission($data['browse'])) {
             $extra_errors['browse'] = _kt('You do not have permission to create new documents in that folder.');
         }
     }
     if (!empty($errors) || !empty($extra_errors)) {
         return $oForm->handleError(null, $extra_errors);
     }
     // FIXME agree on document-duplication rules re: naming, etc.
     $this->startTransaction();
     // now try update it.
     $oNewDoc = KTDocumentUtil::copy($this->oDocument, $data['browse'], $sReason);
     if (PEAR::isError($oNewDoc)) {
         $this->errorRedirectTo('main', _kt('Failed to copy document: ') . $oNewDoc->getMessage(), sprintf('fDocumentId=%d&fFolderId=%d', $this->oDocument->getId(), $this->oFolder->getId()));
         exit(0);
     }
     $oNewDoc->setName($name);
     $oNewDoc->setFilename($filename);
     $res = $oNewDoc->update();
     if (PEAR::isError($res)) {
         return $this->errorRedirectTo('main', _kt('Failed to copy document: ') . $res->getMessage(), sprintf('fDocumentId=%d&fFolderId=%d', $this->oDocument->getId(), $this->oFolder->getId()));
     }
     $this->commitTransaction();
     $_SESSION['KTInfoMessage'][] = _kt('Document copied.');
     controllerRedirect('viewDocument', 'fDocumentId=' . $oNewDoc->getId());
     exit(0);
 }
 function getUniqueDocumentName($oFolder, $sFilename)
 {
     // this is just a quick refactoring. We should look at a more optimal way of doing this as there are
     // quite a lot of queries.
     $iFolderId = $oFolder->getId();
     while (KTDocumentUtil::nameExists($oFolder, $sFilename)) {
         $oDoc = Document::getByNameAndFolder($sFilename, $iFolderId);
         $sFilename = KTDocumentUtil::generateNewDocumentName($oDoc->getName());
     }
     return $sFilename;
 }