Commit 3eef5e55f45996a4c88677cd94549c0678ba134e
1 parent
5f4b1127
Added "Duplicate PDF" workflow trigger
Showing
3 changed files
with
245 additions
and
3 deletions
plugins/ktstandard/PDFGeneratorAction.php
| @@ -32,7 +32,7 @@ | @@ -32,7 +32,7 @@ | ||
| 32 | * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices | 32 | * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices |
| 33 | * must display the words "Powered by KnowledgeTree" and retain the original | 33 | * must display the words "Powered by KnowledgeTree" and retain the original |
| 34 | * copyright notice. | 34 | * copyright notice. |
| 35 | - * Contributor( s): ______________________________________ | 35 | + * Contributor( s): thePanz (thepanz@gmail.com) |
| 36 | * | 36 | * |
| 37 | */ | 37 | */ |
| 38 | 38 |
plugins/ktstandard/PDFGeneratorPlugin.php
| @@ -32,7 +32,7 @@ | @@ -32,7 +32,7 @@ | ||
| 32 | * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices | 32 | * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices |
| 33 | * must display the words "Powered by KnowledgeTree" and retain the original | 33 | * must display the words "Powered by KnowledgeTree" and retain the original |
| 34 | * copyright notice. | 34 | * copyright notice. |
| 35 | - * Contributor( s): ______________________________________ | 35 | + * Contributor( s): thePanz (thepanz@gmail.com) |
| 36 | * | 36 | * |
| 37 | */ | 37 | */ |
| 38 | 38 | ||
| @@ -52,7 +52,8 @@ require_once('PDFGeneratorAction.php'); | @@ -52,7 +52,8 @@ require_once('PDFGeneratorAction.php'); | ||
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | function setup() { | 54 | function setup() { |
| 55 | - $this->registerAction('documentaction', 'PDFGeneratorAction', 'ktstandard.pdf.generate', $sFilename = null); | 55 | + $this->registerAction('documentaction', 'PDFGeneratorAction', 'ktstandard.pdf.generate', 'PDFGeneratorAction.php'); |
| 56 | + $this->registerWorkflowTrigger('ktcore.workflowtriggers.pdfgenerator.duplicate' , 'PDFGeneratorWorkflowTriggerDuplicatePDF', 'PDFGeneratorWorkflowTriggers.php'); | ||
| 56 | } | 57 | } |
| 57 | } | 58 | } |
| 58 | $oPluginRegistry =& KTPluginRegistry::getSingleton(); | 59 | $oPluginRegistry =& KTPluginRegistry::getSingleton(); |
plugins/ktstandard/PDFGeneratorWorkflowTriggers.php
0 → 100644
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * $Id$ | ||
| 4 | + * | ||
| 5 | + * KnowledgeTree Community Edition | ||
| 6 | + * Document Management Made Simple | ||
| 7 | + * Copyright (C) 2008, 2009 KnowledgeTree Inc. | ||
| 8 | + * | ||
| 9 | + * | ||
| 10 | + * This program is free software; you can redistribute it and/or modify it under | ||
| 11 | + * the terms of the GNU General Public License version 3 as published by the | ||
| 12 | + * Free Software Foundation. | ||
| 13 | + * | ||
| 14 | + * This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 15 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 16 | + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 17 | + * details. | ||
| 18 | + * | ||
| 19 | + * You should have received a copy of the GNU General Public License | ||
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 21 | + * | ||
| 22 | + * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco, | ||
| 23 | + * California 94120-7775, or email info@knowledgetree.com. | ||
| 24 | + * | ||
| 25 | + * The interactive user interfaces in modified source and object code versions | ||
| 26 | + * of this program must display Appropriate Legal Notices, as required under | ||
| 27 | + * Section 5 of the GNU General Public License version 3. | ||
| 28 | + * | ||
| 29 | + * In accordance with Section 7(b) of the GNU General Public License version 3, | ||
| 30 | + * these Appropriate Legal Notices must retain the display of the "Powered by | ||
| 31 | + * KnowledgeTree" logo and retain the original copyright notice. If the display of the | ||
| 32 | + * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices | ||
| 33 | + * must display the words "Powered by KnowledgeTree" and retain the original | ||
| 34 | + * copyright notice. | ||
| 35 | + * Contributor( s): thePanz (thepanz@gmail.com) | ||
| 36 | + * | ||
| 37 | + */ | ||
| 38 | + | ||
| 39 | +require_once(KT_LIB_DIR . '/workflow/workflowtrigger.inc.php'); | ||
| 40 | +require_once(KT_DIR . '/plugins/pdfConverter/pdfConverter.php'); | ||
| 41 | + | ||
| 42 | +class PDFGeneratorWorkflowTriggerDuplicatePDF extends KTWorkflowTrigger { | ||
| 43 | + var $sNamespace; | ||
| 44 | + var $sFriendlyName; | ||
| 45 | + var $sDescription; | ||
| 46 | + var $oTriggerInstance; | ||
| 47 | + var $aConfig = array(); | ||
| 48 | + | ||
| 49 | + // generic requirements - both can be true | ||
| 50 | + var $bIsGuard = false; | ||
| 51 | + var $bIsAction = true; | ||
| 52 | + | ||
| 53 | + public function PDFGeneratorWorkflowTriggerDuplicatePDF() { | ||
| 54 | + $this->sNamespace = 'ktcore.workflowtriggers.pdfgenerator.duplicate'; | ||
| 55 | + $this->sFriendlyName = _kt('Copy the Document as PDF'); | ||
| 56 | + $this->sDescription = _kt('This action will create a pdf copy of the document as a new document.'); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + // perform more expensive checks -before- performTransition. | ||
| 60 | + // Taken from : plugins\ktcore\KTWorkflowTriggers.inc.php | ||
| 61 | + public function precheckTransition($oDocument, $oUser) { | ||
| 62 | + $iFolderId = KTUtil::arrayGet($this->aConfig, 'folder_id'); | ||
| 63 | + $oFolder = Folder::get($iFolderId); | ||
| 64 | + if (PEAR::isError($oFolder)) { | ||
| 65 | + return PEAR::raiseError(_kt('The folder to which this document should be copied as PDF does not exist. Cancelling the transition - please contact a system administrator.')); | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + return true; | ||
| 69 | + } | ||
| 70 | + function performTransition($oDocument, $oUser) { | ||
| 71 | + $iFolderId = KTUtil::arrayGet($this->aConfig, 'folder_id'); | ||
| 72 | + $oToFolder = Folder::get($iFolderId); | ||
| 73 | + if (PEAR::isError($oFolder)) { | ||
| 74 | + return PEAR::raiseError(_kt('The folder to which this document should be copied as PDF does not exist. Cancelling the transition - please contact a system administrator.')); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + // Create the PDF | ||
| 78 | + $pdfFile = $this->createPDF($oDocument); | ||
| 79 | + if (PEAR::isError($pdfFile)) { | ||
| 80 | + return $pdfFile; | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + // Duplicate/Copy the Document | ||
| 84 | + $oNewDocument = KTDocumentUtil::copy($oDocument, $oToFolder); | ||
| 85 | + if (PEAR::isError($oNewDocument)) { | ||
| 86 | + return $oNewDocument; | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + // Associate PDF with the new document | ||
| 90 | + $aOptions = array( | ||
| 91 | + 'temp_filename' => $pdfFile, | ||
| 92 | + 'cleanup_initial_file' => true, | ||
| 93 | + ); | ||
| 94 | + | ||
| 95 | + $res = KTDocumentUtil::storeContents($oNewDocument, $oContents = null, $aOptions); | ||
| 96 | + if (PEAR::isError($res)) { | ||
| 97 | + // Remove the created document (not-correct) | ||
| 98 | + KTDocumentUtil::delete($oNewDocument, _kt("PDF WorkflowTrigger error: can't create associate PDF file to document.")); | ||
| 99 | + return $res; | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + return $oNewDocument; | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + | ||
| 106 | + /** | ||
| 107 | + * Create the PDF file for the given document (or re-use the already created one) | ||
| 108 | + * Returns the PDF filename or PEAR::error | ||
| 109 | + */ | ||
| 110 | + private function createPDF(&$oDocument) { | ||
| 111 | + global $default; | ||
| 112 | + $dir = $default->pdfDirectory; | ||
| 113 | + $file = $dir .'/'. $iDocId . '.pdf'; | ||
| 114 | + print __class__ . '::' . __function__ . '() .'." file=$file<br />"; | ||
| 115 | + if (!file_exists($file)) { | ||
| 116 | + // If not - create one | ||
| 117 | + $converter = new pdfConverter(); | ||
| 118 | + $converter->setDocument($oDocument); | ||
| 119 | + $res = $converter->processDocument(); | ||
| 120 | + if ($res !== true) { | ||
| 121 | + return PEAR::raiseError(_kt('PDF file could not be generated; Please contact your System Administrator for assistance.')); | ||
| 122 | + } | ||
| 123 | + } | ||
| 124 | + return $file; | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + function displayConfiguration($args) { | ||
| 128 | + $oTemplating =& KTTemplating::getSingleton(); | ||
| 129 | + $oTemplate = $oTemplating->loadTemplate('ktcore/workflowtriggers/copyaction'); | ||
| 130 | + | ||
| 131 | + require_once(KT_LIB_DIR . '/browse/DocumentCollection.inc.php'); | ||
| 132 | + require_once(KT_LIB_DIR . '/browse/columnregistry.inc.php'); | ||
| 133 | + | ||
| 134 | + $collection = new AdvancedCollection; | ||
| 135 | + $oColumnRegistry = KTColumnRegistry::getSingleton(); | ||
| 136 | + $aColumns = array(); | ||
| 137 | + $aColumns[] = $oColumnRegistry->getColumn('ktcore.columns.singleselection'); | ||
| 138 | + $aColumns[] = $oColumnRegistry->getColumn('ktcore.columns.title'); | ||
| 139 | + | ||
| 140 | + $collection->addColumns($aColumns); | ||
| 141 | + | ||
| 142 | + $aOptions = $collection->getEnvironOptions(); // extract data from the environment | ||
| 143 | + | ||
| 144 | + | ||
| 145 | + $qsFrag = array(); | ||
| 146 | + foreach ($args as $k => $v) { | ||
| 147 | + if ($k == 'action') { $v = 'editactiontrigger'; } // horrible hack - we really need iframe embedding. | ||
| 148 | + $qsFrag[] = sprintf('%s=%s',urlencode($k), urlencode($v)); | ||
| 149 | + } | ||
| 150 | + $qs = implode('&',$qsFrag); | ||
| 151 | + $aOptions['result_url'] = KTUtil::addQueryStringSelf($qs); | ||
| 152 | + $aOptions['show_documents'] = false; | ||
| 153 | + | ||
| 154 | + $fFolderId = KTUtil::arrayGet($_REQUEST, 'fFolderId', KTUtil::arrayGet($this->aConfig, 'folder_id', 1)); | ||
| 155 | + | ||
| 156 | + $oFolder = Folder::get($fFolderId); | ||
| 157 | + if(PEAR::isError($oFolder)) | ||
| 158 | + { | ||
| 159 | + $iRoot = 1; | ||
| 160 | + $oFolder = Folder::get($iRoot); | ||
| 161 | + $fFolderId = 1; | ||
| 162 | + | ||
| 163 | + } | ||
| 164 | + | ||
| 165 | + $collection->setOptions($aOptions); | ||
| 166 | + $collection->setQueryObject(new BrowseQuery($fFolderId, $this->oUser)); | ||
| 167 | + $collection->setColumnOptions('ktcore.columns.singleselection', array( | ||
| 168 | + 'rangename' => 'folder_id', | ||
| 169 | + 'show_folders' => true, | ||
| 170 | + 'show_documents' => false, | ||
| 171 | + )); | ||
| 172 | + | ||
| 173 | + $collection->setColumnOptions('ktcore.columns.title', array( | ||
| 174 | + 'direct_folder' => false, | ||
| 175 | + 'folder_link' => $aOptions['result_url'], | ||
| 176 | + )); | ||
| 177 | + | ||
| 178 | + | ||
| 179 | + $aBreadcrumbs = array(); | ||
| 180 | + $folder_path_names = $oFolder->getPathArray(); | ||
| 181 | + $folder_path_ids = explode(',', $oFolder->getParentFolderIds()); | ||
| 182 | + $folder_path_ids[] = $oFolder->getId(); | ||
| 183 | + if ($folder_path_ids[0] == 0) { | ||
| 184 | + array_shift($folder_path_ids); | ||
| 185 | + array_shift($folder_path_names); | ||
| 186 | + } | ||
| 187 | + | ||
| 188 | + foreach (range(0, count($folder_path_ids) - 1) as $index) { | ||
| 189 | + $id = $folder_path_ids[$index]; | ||
| 190 | + $qsFrag2 = $qsFrag; | ||
| 191 | + $qsFrag2[] = sprintf('fFolderId=%d', $id); | ||
| 192 | + $qs2 = implode('&',$qsFrag2); | ||
| 193 | + $url = KTUtil::addQueryStringSelf($qs2); | ||
| 194 | + $aBreadcrumbs[] = sprintf('<a href="%s">%s</a>', $url, htmlentities($folder_path_names[$index], ENT_NOQUOTES, 'UTF-8')); | ||
| 195 | + } | ||
| 196 | + | ||
| 197 | + $sBreadcrumbs = implode(' » ', $aBreadcrumbs); | ||
| 198 | + | ||
| 199 | + $aTemplateData = array( | ||
| 200 | + 'context' => $this, | ||
| 201 | + 'breadcrumbs' => $sBreadcrumbs, | ||
| 202 | + 'collection' => $collection, | ||
| 203 | + 'args' => $args, | ||
| 204 | + ); | ||
| 205 | + return $oTemplate->render($aTemplateData); | ||
| 206 | + } | ||
| 207 | + | ||
| 208 | + function saveConfiguration() { | ||
| 209 | + $folder_id = KTUtil::arrayGet($_REQUEST, 'folder_id', null); | ||
| 210 | + $oFolder = Folder::get($folder_id); | ||
| 211 | + if (PEAR::isError($oFolder)) { | ||
| 212 | + // silenty ignore | ||
| 213 | + $folder_id = null; | ||
| 214 | + } | ||
| 215 | + | ||
| 216 | + $config = array(); | ||
| 217 | + $config['folder_id'] = $folder_id; | ||
| 218 | + | ||
| 219 | + $this->oTriggerInstance->setConfig($config); | ||
| 220 | + $res = $this->oTriggerInstance->update(); | ||
| 221 | + | ||
| 222 | + return $res; | ||
| 223 | + } | ||
| 224 | + | ||
| 225 | + function getConfigDescription() { | ||
| 226 | + if (!$this->isLoaded()) { | ||
| 227 | + return _kt('This trigger has no configuration.'); | ||
| 228 | + } | ||
| 229 | + // the actual permissions are stored in the array. | ||
| 230 | + $perms = array(); | ||
| 231 | + if (empty($this->aConfig) || is_null($this->aConfig['folder_id'])) { | ||
| 232 | + return _kt('<strong>This transition cannot be performed: no folder has been selected.</strong>'); | ||
| 233 | + } | ||
| 234 | + $oFolder = Folder::get($this->aConfig['folder_id']); | ||
| 235 | + if (PEAR::isError($oFolder)) { | ||
| 236 | + return _kt('<strong>The folder required for this trigger has been deleted, so the transition cannot be performed.</strong>'); | ||
| 237 | + } else { | ||
| 238 | + return sprintf(_kt('The document will be copied as PDF to folder "<a href="%s">%s</a>".'), KTBrowseUtil::getUrlForFolder($oFolder), htmlentities($oFolder->getName(), ENT_NOQUOTES, 'UTF-8')); | ||
| 239 | + } | ||
| 240 | + } | ||
| 241 | +} | ||
| 0 | \ No newline at end of file | 242 | \ No newline at end of file |