/**
  * Checks out a document
  *
  * <code>
  * $ktapi = new KTAPI();
  * $session = $ktapi->start_system_session();
  * $document = $ktapi->get_document_by_id($documentid);
  * $document->checkout('Reason for document checkout');
  * if($document->is_checked_out()){
  *     continue;
  * }
  * </code>
  *
  * @author KnowledgeTree Team
  * @access public
  * @param string $reason The reason for checking out the document
  * @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
  */
 function checkout($reason)
 {
     $user = $this->can_user_access_object_requiring_permission($this->document, KTAPI_PERMISSION_WRITE);
     if (PEAR::isError($user)) {
         return $user;
     }
     //if the document is checked-out by the current user, just return
     //as no need to check-out again BUT we do need to download
     //returning here will allow download, but skip check-out
     if ($this->document->getIsCheckedOut() && $this->document->getCheckedOutUserID() == $_SESSION['userID']) {
         return;
     }
     DBUtil::startTransaction();
     $res = KTDocumentUtil::checkout($this->document, $reason, $user);
     if (PEAR::isError($res)) {
         DBUtil::rollback();
         return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $res);
     }
     DBUtil::commit();
 }
 function do_checkout()
 {
     $oForm = $this->form_checkout();
     $res = $oForm->validate();
     if (!empty($res['errors'])) {
         return $oForm->handleError();
     }
     $data = $res['results'];
     $oTemplate =& $this->oValidator->validateTemplate('ktcore/action/checkout_final');
     $sReason = $data['reason'];
     $this->startTransaction();
     $res = KTDocumentUtil::checkout($this->oDocument, $sReason, $this->oUser);
     if (PEAR::isError($res)) {
         return $this->errorRedirectToMain(sprintf(_kt('Failed to check out the document: %s'), $res->getMessage()));
     }
     $this->commitTransaction();
     if (!$data['download_file']) {
         $this->addInfoMessage(_kt('Document checked out.'));
         redirect(KTBrowseUtil::getUrlForDocument($this->oDocument));
         exit(0);
     }
     $oTemplate->setData(array('context' => &$this, 'reason' => $sReason));
     return $oTemplate->render();
 }
示例#3
0
 function perform_action($oEntity)
 {
     // checkout document
     $sReason = $this->sReason;
     if (is_a($oEntity, 'Document')) {
         if ($oEntity->getImmutable()) {
             return PEAR::raiseError($oEntity->getName() . ': ' . _kt('Document cannot be checked out as it is immutable'));
         }
         if ($oEntity->getIsCheckedOut()) {
             $checkedOutUser = $oEntity->getCheckedOutUserID();
             $sUserId = $_SESSION['userID'];
             if ($checkedOutUser != $sUserId) {
                 $oCheckedOutUser = User::get($checkedOutUser);
                 return PEAR::raiseError($oEntity->getName() . ': ' . _kt('Document has already been checked out by ') . $oCheckedOutUser->getName());
             }
         } else {
             $res = KTDocumentUtil::checkout($oEntity, $sReason, $this->oUser);
             if (PEAR::isError($res)) {
                 return PEAR::raiseError($oEntity->getName() . ': ' . $res->getMessage());
             }
         }
         if ($this->bDownload) {
             if ($this->bNoisy) {
                 $oDocumentTransaction = new DocumentTransaction($oEntity, "Document part of bulk checkout", 'ktstandard.transactions.check_out', array());
                 $oDocumentTransaction->create();
             }
             $oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
             $aTriggers = $oKTTriggerRegistry->getTriggers('checkoutDownload', 'postValidate');
             foreach ($aTriggers as $aTrigger) {
                 $sTrigger = $aTrigger[0];
                 $oTrigger = new $sTrigger();
                 $aInfo = array('document' => $oEntity);
                 $oTrigger->setInfo($aInfo);
                 $ret = $oTrigger->postValidate();
                 if (PEAR::isError($ret)) {
                     return $ret;
                 }
             }
             $this->oZip->addDocumentToZip($oEntity);
         }
     } else {
         if (is_a($oEntity, 'Folder')) {
             // get documents and subfolders
             $aDocuments = array();
             $oFolder = $oEntity;
             if ($oFolder->isSymbolicLink()) {
                 $oFolder = $oFolder->getLinkedFolder();
             }
             $sFolderId = $oFolder->getId();
             $sFolderDocs = $oFolder->getDocumentIDs($sFolderId);
             // get documents directly in the folder
             if (!empty($sFolderDocs)) {
                 $aDocuments = explode(',', $sFolderDocs);
             }
             // Get all the folders within the current folder
             $sWhereClause = "parent_folder_ids = '{$sFolderId}' OR\n            parent_folder_ids LIKE '{$sFolderId},%' OR\n            parent_folder_ids LIKE '%,{$sFolderId},%' OR\n            parent_folder_ids LIKE '%,{$sFolderId}'";
             $aFolderList = $this->oFolder->getList($sWhereClause);
             $aLinkingFolders = $this->getLinkingEntities($aFolderList);
             $aFolderList = array_merge($aFolderList, $aLinkingFolders);
             $aFolderObjects = array();
             $aFolderObjects[$sFolderId] = $oFolder;
             // Get the documents within the folder
             if (!empty($aFolderList)) {
                 foreach ($aFolderList as $k => $oFolderItem) {
                     if (Permission::userHasFolderReadPermission($oFolderItem)) {
                         // Get documents for each folder
                         if ($oFolderItem->isSymbolicLink()) {
                             $oFolderItem = $oFolderItem->getLinkedFolder();
                         }
                         $sFolderItemId = $oFolderItem->getID();
                         $sFolderItemDocs = $oFolderItem->getDocumentIDs($sFolderItemId);
                         if (!empty($sFolderItemDocs)) {
                             $aFolderDocs = explode(',', $sFolderItemDocs);
                             $aDocuments = array_merge($aDocuments, $aFolderDocs);
                         }
                         // Add the folder to the zip file
                         if ($this->bDownload) {
                             $this->oZip->addFolderToZip($oFolderItem);
                             $aFolderObjects[$oFolderItem->getId()] = $oFolderItem;
                         }
                     }
                 }
             }
             // Checkout each document within the folder structure
             if (!empty($aDocuments)) {
                 foreach ($aDocuments as $sDocId) {
                     $oDocument = Document::get($sDocId);
                     if (PEAR::isError($oDocument)) {
                         // add message, skip document and continue
                         $this->addErrorMessage($oDocument->getName() . ': ' . $oDocument->getMessage());
                         continue;
                     }
                     if ($oDocument->isSymbolicLink()) {
                         $oDocument->switchToLinkedCore();
                     }
                     if ($oDocument->getImmutable()) {
                         $this->addErrorMessage($oDocument->getName() . ': ' . _kt('Document cannot be checked out as it is immutable'));
                         continue;
                     }
                     // Check if the action is restricted by workflow on the document
                     if (!KTWorkflowUtil::actionEnabledForDocument($oDocument, 'ktcore.actions.document.checkout')) {
                         $this->addErrorMessage($oDocument->getName() . ': ' . _kt('Checkout is restricted by the workflow state.'));
                         continue;
                     }
                     // Check if document is already checked out, check the owner.
                     // If the current user is the owner, then include to the download, otherwise ignore.
                     if ($oDocument->getIsCheckedOut()) {
                         $checkedOutUser = $oDocument->getCheckedOutUserID();
                         $sUserId = $_SESSION['userID'];
                         if ($checkedOutUser != $sUserId) {
                             $oCheckedOutUser = User::get($checkedOutUser);
                             $this->addErrorMessage($oDocument->getName() . ': ' . _kt('Document has already been checked out by ') . $oCheckedOutUser->getName());
                             continue;
                         }
                     } else {
                         // Check out document
                         $res = KTDocumentUtil::checkout($oDocument, $sReason, $this->oUser);
                         if (PEAR::isError($res)) {
                             $this->addErrorMessage($oDocument->getName() . ': ' . _kt('Document could not be checked out. ') . $res->getMessage());
                             continue;
                         }
                     }
                     // Add document to the zip file
                     if ($this->bDownload) {
                         if ($this->bNoisy) {
                             $oDocumentTransaction = new DocumentTransaction($oDocument, 'Document part of bulk checkout', 'ktstandard.transactions.check_out', array());
                             $oDocumentTransaction->create();
                         }
                         $oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
                         $aTriggers = $oKTTriggerRegistry->getTriggers('checkoutDownload', 'postValidate');
                         foreach ($aTriggers as $aTrigger) {
                             $sTrigger = $aTrigger[0];
                             $oTrigger = new $sTrigger();
                             $aInfo = array('document' => $oDocument);
                             $oTrigger->setInfo($aInfo);
                             $ret = $oTrigger->postValidate();
                             if (PEAR::isError($ret)) {
                                 return $ret;
                             }
                         }
                         $sDocFolderId = $oDocument->getFolderID();
                         $oFolder = isset($aFolderObjects[$sDocFolderId]) ? $aFolderObjects[$sDocFolderId] : Folder::get($sDocFolderId);
                         $this->oZip->addDocumentToZip($oDocument, $oFolder);
                     }
                 }
             }
         }
     }
     return true;
 }