Commit fd05866d8a30daace28d0bbf33cb38161f215525
Merge branch 'master' of git@github.com:ktgit/knowledgetree
Showing
5 changed files
with
165 additions
and
11 deletions
lib/documentmanagement/documentutil.inc.php
| @@ -751,6 +751,9 @@ $sourceDocument->getName(), | @@ -751,6 +751,9 @@ $sourceDocument->getName(), | ||
| 751 | } | 751 | } |
| 752 | } | 752 | } |
| 753 | // refresh the document object | 753 | // refresh the document object |
| 754 | + DBUtil::commit(); | ||
| 755 | + | ||
| 756 | + $oDocument->clearAllCaches(); | ||
| 754 | 757 | ||
| 755 | // NEW SEARCH | 758 | // NEW SEARCH |
| 756 | 759 | ||
| @@ -787,8 +790,6 @@ $sourceDocument->getName(), | @@ -787,8 +790,6 @@ $sourceDocument->getName(), | ||
| 787 | 790 | ||
| 788 | } | 791 | } |
| 789 | 792 | ||
| 790 | - DBUtil::commit(); | ||
| 791 | - | ||
| 792 | // update document object with additional fields / data from the triggers | 793 | // update document object with additional fields / data from the triggers |
| 793 | $oDocument = Document::get($oDocument->iId); | 794 | $oDocument = Document::get($oDocument->iId); |
| 794 | 795 |
plugins/ktstandard/PDFGeneratorAction.php
| @@ -46,6 +46,8 @@ require_once(KT_LIB_DIR . '/plugins/pluginregistry.inc.php'); | @@ -46,6 +46,8 @@ require_once(KT_LIB_DIR . '/plugins/pluginregistry.inc.php'); | ||
| 46 | 46 | ||
| 47 | require_once(KT_LIB_DIR . '/roles/Role.inc'); | 47 | require_once(KT_LIB_DIR . '/roles/Role.inc'); |
| 48 | 48 | ||
| 49 | +require_once(KT_DIR . '/plugins/pdfConverter/pdfConverter.php'); | ||
| 50 | + | ||
| 49 | class PDFGeneratorAction extends KTDocumentAction { | 51 | class PDFGeneratorAction extends KTDocumentAction { |
| 50 | var $sName = 'ktstandard.pdf.generate'; | 52 | var $sName = 'ktstandard.pdf.generate'; |
| 51 | var $_sShowPermission = "ktcore.permissions.read"; | 53 | var $_sShowPermission = "ktcore.permissions.read"; |
| @@ -60,7 +62,48 @@ class PDFGeneratorAction extends KTDocumentAction { | @@ -60,7 +62,48 @@ class PDFGeneratorAction extends KTDocumentAction { | ||
| 60 | 'otg', 'std', 'asc'); | 62 | 'otg', 'std', 'asc'); |
| 61 | 63 | ||
| 62 | function getDisplayName() { | 64 | function getDisplayName() { |
| 63 | - $cmdpath = KTUtil::findCommand('externalBinary/python'); | 65 | + global $default; |
| 66 | + // The generation of the pdf is done through the PDF Converter plugin. | ||
| 67 | + // The PDF's are generated in the background by the document processor | ||
| 68 | + | ||
| 69 | + if(!empty($this->oDocument)){ | ||
| 70 | + $iDocId = $this->oDocument->iId; | ||
| 71 | + | ||
| 72 | + // Build the display name and url | ||
| 73 | + $sDisplayName = _kt('Generate PDF'); | ||
| 74 | + | ||
| 75 | + $sHostPath = KTUtil::kt_url(); | ||
| 76 | + $icon = "<img src='{$sHostPath}/resources/mimetypes/pdf.gif' alt='PDF' border=0 />"; | ||
| 77 | + $link = KTUtil::ktLink('action.php', 'ktstandard.pdf.generate', array( 'fDocumentId' => $this->oDocument->getId(), 'action' => 'pdfdownload')); | ||
| 78 | + $sDisplayLink = " <a href=\"{$link}\">{$icon}</a>"; | ||
| 79 | + | ||
| 80 | + // First check if the pdf has already been generated | ||
| 81 | + $dir = $default->pdfDirectory; | ||
| 82 | + $file = $dir .'/'. $iDocId . '.pdf'; | ||
| 83 | + | ||
| 84 | + if(file_exists($file)){ | ||
| 85 | + // Display the download link | ||
| 86 | + return $sDisplayName . $sDisplayLink; | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + // If the file does not exist, check if the document has the correct mimetype | ||
| 90 | + $converter = new pdfConverter(); | ||
| 91 | + $mimeTypes = $converter->getSupportedMimeTypes(); | ||
| 92 | + $docType = $this->getMimeExtension(); | ||
| 93 | + | ||
| 94 | + if($mimeTypes === true || in_array($docType, $mimeTypes)){ | ||
| 95 | + // Display the download link | ||
| 96 | + return $sDisplayName . $sDisplayLink; | ||
| 97 | + } | ||
| 98 | + }else{ | ||
| 99 | + // If the document is empty then we are probably in the workflow admin - action restrictions section, so we can display the name. | ||
| 100 | + return $sDisplayName; | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + return ''; | ||
| 104 | + | ||
| 105 | + /* | ||
| 106 | + //$cmdpath = KTUtil::findCommand('externalBinary/python'); | ||
| 64 | // Check if openoffice and python are available | 107 | // Check if openoffice and python are available |
| 65 | 108 | ||
| 66 | if($cmdpath != false && file_exists($cmdpath) && !empty($cmdpath)) { | 109 | if($cmdpath != false && file_exists($cmdpath) && !empty($cmdpath)) { |
| @@ -82,6 +125,7 @@ class PDFGeneratorAction extends KTDocumentAction { | @@ -82,6 +125,7 @@ class PDFGeneratorAction extends KTDocumentAction { | ||
| 82 | } | 125 | } |
| 83 | } | 126 | } |
| 84 | return ''; | 127 | return ''; |
| 128 | + */ | ||
| 85 | } | 129 | } |
| 86 | 130 | ||
| 87 | function form_main() { | 131 | function form_main() { |
| @@ -170,11 +214,66 @@ class PDFGeneratorAction extends KTDocumentAction { | @@ -170,11 +214,66 @@ class PDFGeneratorAction extends KTDocumentAction { | ||
| 170 | } | 214 | } |
| 171 | 215 | ||
| 172 | /** | 216 | /** |
| 217 | + * Method to download the pdf. | ||
| 218 | + * | ||
| 219 | + * @author KnowledgeTree Team | ||
| 220 | + * @access public | ||
| 221 | + */ | ||
| 222 | + public function do_pdfdownload() | ||
| 223 | + { | ||
| 224 | + global $default; | ||
| 225 | + $iDocId = $this->oDocument->iId; | ||
| 226 | + | ||
| 227 | + // Check if pdf has already been created | ||
| 228 | + $dir = $default->pdfDirectory; | ||
| 229 | + $file = $dir .'/'. $iDocId . '.pdf'; | ||
| 230 | + $mimetype = 'application/pdf'; | ||
| 231 | + $size = filesize($file); | ||
| 232 | + | ||
| 233 | + // Set the filename | ||
| 234 | + $name = $this->oDocument->getFileName(); | ||
| 235 | + $aName = explode('.', $name); | ||
| 236 | + array_pop($aName); | ||
| 237 | + $name = implode('.', $aName) . '.pdf'; | ||
| 238 | + | ||
| 239 | + | ||
| 240 | + if(file_exists($file)){ | ||
| 241 | + if(KTUtil::download($file, $mimetype, $size, $name) === false){ | ||
| 242 | + $default->log->error('PDF Generator: PDF file could not be downloaded because it doesn\'t exist'); | ||
| 243 | + $this->errorRedirectToMain(_kt('PDF file could not be downloaded because it doesn\'t exist')); | ||
| 244 | + } | ||
| 245 | + exit(); | ||
| 246 | + } | ||
| 247 | + | ||
| 248 | + // If not - create one | ||
| 249 | + $converter = new pdfConverter(); | ||
| 250 | + $converter->setDocument($this->oDocument); | ||
| 251 | + $res = $converter->processDocument(); | ||
| 252 | + | ||
| 253 | + if(!$res){ | ||
| 254 | + $default->log->error('PDF Generator: PDF file could not be generated'); | ||
| 255 | + $this->errorRedirectToMain(_kt('PDF file could not be generated, the file may be of an unsupported mime type.')); | ||
| 256 | + exit(); | ||
| 257 | + } | ||
| 258 | + | ||
| 259 | + if(file_exists($file)){ | ||
| 260 | + if(KTUtil::download($file, $mimetype, $size, $name) === false){ | ||
| 261 | + $default->log->error('PDF Generator: PDF file could not be downloaded because it doesn\'t exist'); | ||
| 262 | + $this->errorRedirectToMain(_kt('PDF file could not be downloaded because it doesn\'t exist')); | ||
| 263 | + } | ||
| 264 | + exit(); | ||
| 265 | + } | ||
| 266 | + $this->errorRedirectToMain(_kt('PDF file could not be generated')); | ||
| 267 | + exit(); | ||
| 268 | + } | ||
| 269 | + | ||
| 270 | + /** | ||
| 173 | * Method for downloading the document as a pdf. | 271 | * Method for downloading the document as a pdf. |
| 174 | * | 272 | * |
| 273 | + * @deprecated | ||
| 175 | * @return true on success else false | 274 | * @return true on success else false |
| 176 | */ | 275 | */ |
| 177 | - function do_pdfdownload() { | 276 | + function do_pdfdownload_deprecated() { |
| 178 | 277 | ||
| 179 | $oDocument = $this->oDocument; | 278 | $oDocument = $this->oDocument; |
| 180 | $oStorage =& KTStorageManagerUtil::getSingleton(); | 279 | $oStorage =& KTStorageManagerUtil::getSingleton(); |
plugins/pdfConverter/pdfConverter.php
| @@ -70,28 +70,47 @@ class pdfConverter extends BaseProcessor | @@ -70,28 +70,47 @@ class pdfConverter extends BaseProcessor | ||
| 70 | */ | 70 | */ |
| 71 | public function getSupportedMimeTypes() | 71 | public function getSupportedMimeTypes() |
| 72 | { | 72 | { |
| 73 | - // support all for now... | ||
| 74 | - return true; //array(); | 73 | + $aAcceptedMimeTypes = array('doc', 'ods', 'odt', 'ott', 'txt', 'rtf', 'sxw', 'stw', |
| 74 | + // 'html', 'htm', | ||
| 75 | + 'xml' , 'pdb', 'psw', 'ods', 'ots', 'sxc', | ||
| 76 | + 'stc', 'dif', 'dbf', 'xls', 'xlt', 'slk', 'csv', 'pxl', | ||
| 77 | + 'odp', 'otp', 'sxi', 'sti', 'ppt', 'pot', 'sxd', 'odg', | ||
| 78 | + 'otg', 'std', 'asc'); | ||
| 79 | + | ||
| 80 | + return $aAcceptedMimeTypes; | ||
| 75 | } | 81 | } |
| 76 | 82 | ||
| 77 | function convertFile($filename) | 83 | function convertFile($filename) |
| 78 | { | 84 | { |
| 85 | + global $default; | ||
| 86 | + | ||
| 79 | // Get contents and send to converter | 87 | // Get contents and send to converter |
| 80 | $buffer = file_get_contents($filename); | 88 | $buffer = file_get_contents($filename); |
| 81 | $buffer = $this->xmlrpc->convertDocument($buffer, 'pdf'); | 89 | $buffer = $this->xmlrpc->convertDocument($buffer, 'pdf'); |
| 82 | 90 | ||
| 83 | if($buffer === false){ | 91 | if($buffer === false){ |
| 92 | + $default->log->error('PDF Converter Plugin: Conversion to PDF Failed'); | ||
| 84 | return false; | 93 | return false; |
| 85 | } | 94 | } |
| 86 | 95 | ||
| 87 | - global $default; | ||
| 88 | - $dir = $default->varDirectory . '/pdf/'; | ||
| 89 | - $filename = $dir . $this->document->iId.'.pdf'; | 96 | + $dir = $default->pdfDirectory; |
| 97 | + | ||
| 98 | + // Ensure the PDF directory exists | ||
| 99 | + if(!file_exists($dir)){ | ||
| 100 | + mkdir($dir, '0755'); | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + $pdfFile = $dir .'/'. $this->document->iId.'.pdf'; | ||
| 104 | + | ||
| 105 | + // if a previous version of the pdf exists - delete it | ||
| 106 | + if(file_exists($pdfFile)){ | ||
| 107 | + @unlink($pdfFile); | ||
| 108 | + } | ||
| 90 | 109 | ||
| 91 | - file_put_contents($filename, $buffer); | 110 | + file_put_contents($pdfFile, $buffer); |
| 92 | unset($buffer); | 111 | unset($buffer); |
| 93 | 112 | ||
| 94 | - return $filename; | 113 | + return $pdfFile; |
| 95 | 114 | ||
| 96 | } | 115 | } |
| 97 | } | 116 | } |
plugins/pdfConverter/pdfConverterPlugin.php
| @@ -24,19 +24,52 @@ | @@ -24,19 +24,52 @@ | ||
| 24 | require_once(KT_LIB_DIR . '/plugins/plugin.inc.php'); | 24 | require_once(KT_LIB_DIR . '/plugins/plugin.inc.php'); |
| 25 | require_once(KT_LIB_DIR . '/plugins/pluginregistry.inc.php'); | 25 | require_once(KT_LIB_DIR . '/plugins/pluginregistry.inc.php'); |
| 26 | 26 | ||
| 27 | +class DeletePDFTrigger { | ||
| 28 | + var $namespace = 'pdf.converter.triggers.delete'; | ||
| 29 | + var $aInfo = null; | ||
| 30 | + | ||
| 31 | + function setInfo($aInfo) { | ||
| 32 | + $this->aInfo = $aInfo; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + /** | ||
| 36 | + * On deleting a document, send the document owner and alert creator a notification email | ||
| 37 | + */ | ||
| 38 | + function postValidate() { | ||
| 39 | + $oDoc = $this->aInfo['document']; | ||
| 40 | + $docId = $oDoc->getId(); | ||
| 41 | + $docInfo = array('id' => $docId, 'name' => $oDoc->getName()); | ||
| 42 | + | ||
| 43 | + // Delete the pdf document | ||
| 44 | + global $default; | ||
| 45 | + $pdfDirectory = $default->pdfDirectory; | ||
| 46 | + | ||
| 47 | + $file = $pdfDirectory .'/'.$docId.'.pdf'; | ||
| 48 | + | ||
| 49 | + if(file_exists($file)){ | ||
| 50 | + @unlink($file); | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | +} | ||
| 54 | + | ||
| 27 | class pdfConverterPlugin extends KTPlugin { | 55 | class pdfConverterPlugin extends KTPlugin { |
| 28 | var $sNamespace = 'pdf.converter.processor.plugin'; | 56 | var $sNamespace = 'pdf.converter.processor.plugin'; |
| 29 | var $iVersion = 0; | 57 | var $iVersion = 0; |
| 58 | + var $autoRegister = true; | ||
| 59 | + var $createSQL = true; | ||
| 30 | 60 | ||
| 31 | function pdfConverterPlugin($sFilename = null) { | 61 | function pdfConverterPlugin($sFilename = null) { |
| 32 | $res = parent::KTPlugin($sFilename); | 62 | $res = parent::KTPlugin($sFilename); |
| 33 | $this->sFriendlyName = _kt('Document PDF Converter'); | 63 | $this->sFriendlyName = _kt('Document PDF Converter'); |
| 64 | + $this->dir = dirname(__FILE__) . DIRECTORY_SEPARATOR; | ||
| 65 | + $this->sSQLDir = $this->dir . 'sql' . DIRECTORY_SEPARATOR; | ||
| 34 | return $res; | 66 | return $res; |
| 35 | } | 67 | } |
| 36 | 68 | ||
| 37 | function setup() { | 69 | function setup() { |
| 38 | $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pdfConverter.php'; | 70 | $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pdfConverter.php'; |
| 39 | $this->registerProcessor('PDFConverter', 'pdf.converter.processor', $dir); | 71 | $this->registerProcessor('PDFConverter', 'pdf.converter.processor', $dir); |
| 72 | + $this->registerTrigger('delete', 'postValidate', 'DeletePDFTrigger','pdf.converter.triggers.delete', __FILE__); | ||
| 40 | } | 73 | } |
| 41 | } | 74 | } |
| 42 | 75 |
plugins/pdfConverter/sql/upgradeto0.sql
0 → 100644