function do_main()
 {
     $oStorage =& KTStorageManagerUtil::getSingleton();
     $aOptions = array();
     $iVersion = KTUtil::arrayGet($_REQUEST, 'version');
     session_write_close();
     if ($iVersion) {
         $oVersion = KTDocumentContentVersion::get($iVersion);
         $aOptions['version'] = sprintf('%d.%d', $oVersion->getMajorVersionNumber(), $oVersion->getMinorVersionNumber());
         $res = $oStorage->downloadVersion($this->oDocument, $iVersion);
     } else {
         $res = $oStorage->download($this->oDocument);
     }
     if ($res === false) {
         $this->addErrorMessage(_kt('The file you requested is not available - please contact the system administrator if this is incorrect.'));
         redirect(generateControllerLink('viewDocument', sprintf(_kt('fDocumentId=%d'), $this->oDocument->getId())));
         exit(0);
     }
     $oDocumentTransaction =& new DocumentTransaction($this->oDocument, _kt('Document downloaded'), 'ktcore.transactions.download', $aOptions);
     $oDocumentTransaction->create();
     // fire subscription alerts for the downloaded document
     $oKTConfig =& KTConfig::getSingleton();
     $bNotifications = $oKTConfig->get('export/enablenotifications', 'on') == 'on' ? true : false;
     if ($bNotifications) {
         $oSubscriptionEvent = new SubscriptionEvent();
         $oFolder = Folder::get($this->oDocument->getFolderID());
         $oSubscriptionEvent->DownloadDocument($this->oDocument, $oFolder);
     }
     exit(0);
 }
Example #2
0
 function perform_action($oEntity)
 {
     if (is_a($oEntity, 'Document')) {
         $oDocument = $oEntity;
         if ($oDocument->isSymbolicLink()) {
             $oDocument->switchToLinkedCore();
         }
         if ($this->bNoisy) {
             $oDocumentTransaction = new DocumentTransaction($oDocument, "Document part of bulk export", 'ktstandard.transactions.bulk_export', array());
             $oDocumentTransaction->create();
         }
         // fire subscription alerts for the downloaded document - if global config is set
         if ($this->bNotifications) {
             $oSubscriptionEvent = new SubscriptionEvent();
             $oFolder = Folder::get($oDocument->getFolderID());
             $oSubscriptionEvent->DownloadDocument($oDocument, $oFolder);
         }
         $this->oZip->addDocumentToZip($oDocument);
     } else {
         if (is_a($oEntity, 'Folder')) {
             $aDocuments = array();
             $oFolder = $oEntity;
             if ($oFolder->isSymbolicLink()) {
                 $oFolder = $oFolder->getLinkedFolder();
             }
             $sFolderId = $oFolder->getId();
             $sFolderDocs = $oFolder->getDocumentIDs($sFolderId);
             // Add folder to zip
             $this->oZip->addFolderToZip($oFolder);
             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;
             // Export the folder structure to ensure the export of empty directories
             if (!empty($aFolderList)) {
                 foreach ($aFolderList as $k => $oFolderItem) {
                     if ($oFolderItem->isSymbolicLink()) {
                         $oFolderItem = $oFolderItem->getLinkedFolder();
                     }
                     if (Permission::userHasFolderReadPermission($oFolderItem)) {
                         // Get documents for each folder
                         $sFolderItemId = $oFolderItem->getID();
                         $sFolderItemDocs = $oFolderItem->getDocumentIDs($sFolderItemId);
                         if (!empty($sFolderItemDocs)) {
                             $aFolderDocs = explode(',', $sFolderItemDocs);
                             $aDocuments = array_merge($aDocuments, $aFolderDocs);
                         }
                         $this->oZip->addFolderToZip($oFolderItem);
                         $aFolderObjects[$oFolderItem->getId()] = $oFolderItem;
                     }
                 }
             }
             // Add all documents to the export
             if (!empty($aDocuments)) {
                 foreach ($aDocuments as $sDocumentId) {
                     $oDocument = Document::get($sDocumentId);
                     if ($oDocument->isSymbolicLink()) {
                         $oDocument->switchToLinkedCore();
                     }
                     if (Permission::userHasDocumentReadPermission($oDocument)) {
                         if (!KTWorkflowUtil::actionEnabledForDocument($oDocument, 'ktcore.actions.document.view')) {
                             $this->addErrorMessage($oDocument->getName() . ': ' . _kt('Document cannot be exported as it is restricted by the workflow.'));
                             continue;
                         }
                         $sDocFolderId = $oDocument->getFolderID();
                         $oFolder = isset($aFolderObjects[$sDocFolderId]) ? $aFolderObjects[$sDocFolderId] : Folder::get($sDocFolderId);
                         if ($this->bNoisy) {
                             $oDocumentTransaction = new DocumentTransaction($oDocument, "Document part of bulk export", 'ktstandard.transactions.bulk_export', array());
                             $oDocumentTransaction->create();
                         }
                         // fire subscription alerts for the downloaded document
                         if ($this->bNotifications) {
                             $oSubscriptionEvent = new SubscriptionEvent();
                             $oSubscriptionEvent->DownloadDocument($oDocument, $oFolder);
                         }
                         $this->oZip->addDocumentToZip($oDocument, $oFolder);
                     }
                 }
             }
         }
     }
     return true;
 }
 /**
  * Add a folder to the archive
  *
  * @param unknown_type $zip
  * @param unknown_type $folderId
  * @return unknown
  */
 public function addFolder(&$zip, $folderId)
 {
     $oFolder = Folder::get($folderId);
     if (PEAR::isError($oFolder)) {
         $this->errors[] = _kt('Folder cannot be exported, an error occurred: ') . $oFolder->getMessage();
         return $oFolder;
     }
     $sFolderDocs = $oFolder->getDocumentIDs($folderId);
     if (PEAR::isError($sFolderDocs)) {
         $default->log->error('Download Queue: get document ids for folder caused an error: ' . $sFolderDocs->getMessage());
         $sFolderDocs = '';
     }
     // Add folder to zip
     $zip->addFolderToZip($oFolder);
     $aDocuments = array();
     if (!empty($sFolderDocs)) {
         $aDocuments = explode(',', $sFolderDocs);
     }
     // Get all the folders within the current folder
     $sWhereClause = "parent_folder_ids like '%,{$folderId}'\n            OR parent_folder_ids like '%,{$folderId},%'\n            OR parent_folder_ids like '{$folderId},%'\n            OR parent_id = {$folderId}";
     $aFolderList = $oFolder->getList($sWhereClause);
     $aLinkingFolders = $this->getLinkingEntities($aFolderList);
     $aFolderList = array_merge($aFolderList, $aLinkingFolders);
     $aFolderObjects = array();
     $aFolderObjects[$folderId] = $oFolder;
     // Export the folder structure to ensure the export of empty directories
     if (!empty($aFolderList)) {
         foreach ($aFolderList as $k => $oFolderItem) {
             if ($oFolderItem->isSymbolicLink()) {
                 $oFolderItem = $oFolderItem->getLinkedFolder();
             }
             if (Permission::userHasFolderReadPermission($oFolderItem)) {
                 // Get documents for each folder
                 $sFolderItemId = $oFolderItem->getID();
                 $sFolderItemDocs = $oFolderItem->getDocumentIDs($sFolderItemId);
                 if (!empty($sFolderItemDocs)) {
                     $aFolderDocs = explode(',', $sFolderItemDocs);
                     $aDocuments = array_merge($aDocuments, $aFolderDocs);
                 }
                 $zip->addFolderToZip($oFolderItem);
                 $aFolderObjects[$oFolderItem->getId()] = $oFolderItem;
             }
         }
     }
     // Add all documents to the export
     if (!empty($aDocuments)) {
         foreach ($aDocuments as $sDocumentId) {
             $oDocument = Document::get($sDocumentId);
             if ($oDocument->isSymbolicLink()) {
                 $oDocument->switchToLinkedCore();
             }
             if (Permission::userHasDocumentReadPermission($oDocument)) {
                 if (!KTWorkflowUtil::actionEnabledForDocument($oDocument, 'ktcore.actions.document.view')) {
                     $this->errors[] = $oDocument->getName() . ': ' . _kt('Document cannot be exported as it is restricted by the workflow.');
                     continue;
                 }
                 $sDocFolderId = $oDocument->getFolderID();
                 $oFolder = isset($aFolderObjects[$sDocFolderId]) ? $aFolderObjects[$sDocFolderId] : Folder::get($sDocFolderId);
                 if ($this->bNoisy) {
                     $oDocumentTransaction = new DocumentTransaction($oDocument, "Document part of bulk export", 'ktstandard.transactions.bulk_export', array());
                     $oDocumentTransaction->create();
                 }
                 // fire subscription alerts for the downloaded document
                 if ($this->bNotifications) {
                     $oSubscriptionEvent = new SubscriptionEvent();
                     $oSubscriptionEvent->DownloadDocument($oDocument, $oFolder);
                 }
                 $zip->addDocumentToZip($oDocument, $oFolder);
             }
         }
     }
 }
    function do_main()
    {
        $folderName = $this->oFolder->getName();
        $this->oZip = new ZipFolder($folderName);
        if (!$this->oZip->checkConvertEncoding()) {
            redirect(KTBrowseUtil::getUrlForFolder($this->oFolder));
            exit(0);
        }
        $oKTConfig =& KTConfig::getSingleton();
        $bNoisy = $oKTConfig->get("tweaks/noisyBulkOperations");
        $bNotifications = $oKTConfig->get('export/enablenotifications', 'on') == 'on' ? true : false;
        // Get all folders and sub-folders
        $sCurrentFolderId = $this->oFolder->getId();
        $sWhereClause = "parent_folder_ids = '{$sCurrentFolderId}' OR\n        parent_folder_ids LIKE '{$sCurrentFolderId},%' OR\n        parent_folder_ids LIKE '%,{$sCurrentFolderId},%' OR\n        parent_folder_ids LIKE '%,{$sCurrentFolderId}'";
        $aFolderList = $this->oFolder->getList($sWhereClause);
        // Get any folder shortcuts within the folders
        $aLinkedFolders = KTBulkAction::getLinkingEntities($aFolderList);
        $aFolderList = array_merge($aFolderList, $aLinkedFolders);
        // Add the folders to the zip file
        $aFolderObjects = array($sCurrentFolderId => $this->oFolder);
        if (!empty($aFolderList)) {
            foreach ($aFolderList as $oFolderItem) {
                $itemId = $oFolderItem->getId();
                $linkedFolder = $oFolderItem->getLinkedFolderId();
                // If the folder has been added or is a shortcut then skip
                // The shortcut folders don't need to be added as their targets will be added.
                if (array_key_exists($itemId, $aFolderObjects) || !empty($linkedFolder)) {
                    continue;
                }
                $this->oZip->addFolderToZip($oFolderItem);
                $aFolderObjects[$oFolderItem->getId()] = $oFolderItem;
            }
        }
        // Get the list of folder ids
        $aFolderIds = array_keys($aFolderObjects);
        // Get all documents in the folder list
        $aQuery = $this->buildQuery($aFolderIds);
        $aDocumentIds = DBUtil::getResultArrayKey($aQuery, 'id');
        if (PEAR::isError($aDocumentIds)) {
            $this->addErrorMessage(_kt('There was a problem exporting the documents: ') . $aDocumentIds->getMessage());
            redirect(KTBrowseUtil::getUrlForFolder($this->oFolder));
            exit(0);
        }
        // Redirect if there are no documents and no folders to export
        if (empty($aDocumentIds) && empty($aFolderList)) {
            $this->addErrorMessage(_kt("No documents found to export"));
            redirect(KTBrowseUtil::getUrlForFolder($this->oFolder));
            exit(0);
        }
        $this->oPage->template = "kt3/minimal_page";
        $this->handleOutput("");
        // Add the documents to the zip file
        if (!empty($aDocumentIds)) {
            foreach ($aDocumentIds as $iId) {
                $oDocument = Document::get($iId);
                $sFolderId = $oDocument->getFolderID();
                if (!KTWorkflowUtil::actionEnabledForDocument($oDocument, 'ktcore.actions.document.view')) {
                    $this->addErrorMessage($oDocument->getName() . ': ' . _kt('Document cannot be exported as it is restricted by the workflow.'));
                    continue;
                }
                $oFolder = isset($aFolderObjects[$sFolderId]) ? $aFolderObjects[$sFolderId] : Folder::get($sFolderId);
                if ($bNoisy) {
                    $oDocumentTransaction =& new DocumentTransaction($oDocument, "Document part of bulk export", 'ktstandard.transactions.bulk_export', array());
                    $oDocumentTransaction->create();
                }
                // fire subscription alerts for the downloaded document
                if ($bNotifications) {
                    $oSubscriptionEvent = new SubscriptionEvent();
                    $oSubscriptionEvent->DownloadDocument($oDocument, $oFolder);
                }
                $this->oZip->addDocumentToZip($oDocument, $oFolder);
            }
        }
        $sExportCode = $this->oZip->createZipFile(TRUE);
        $oTransaction = KTFolderTransaction::createFromArray(array('folderid' => $this->oFolder->getId(), 'comment' => "Bulk export", 'transactionNS' => 'ktstandard.transactions.bulk_export', 'userid' => $_SESSION['userID'], 'ip' => Session::getClientIP()));
        $url = KTUtil::addQueryStringSelf(sprintf('action=downloadZipFile&fFolderId=%d&exportcode=%s', $this->oFolder->getId(), $sExportCode));
        printf('<p>' . _kt('Your download will begin shortly. If you are not automatically redirected to your download, please click <a href="%s">here</a> ') . "</p>\n", $url);
        $folderurl = KTBrowseUtil::getUrlForFolder($this->oFolder);
        printf('<p>' . _kt('Once your download is complete, click <a href="%s">here</a> to return to the original folder') . "</p>\n", $folderurl);
        printf("</div></div></body></html>\n");
        printf('<script language="JavaScript">
                function kt_bulkexport_redirect() {
                    document.location.href = "%s";
                }
                callLater(1, kt_bulkexport_redirect);

                </script>', $url);
        exit(0);
    }