diff --git a/lib/documentmanagement/documentutil.inc.php b/lib/documentmanagement/documentutil.inc.php index 8569f3b..b06b46b 100644 --- a/lib/documentmanagement/documentutil.inc.php +++ b/lib/documentmanagement/documentutil.inc.php @@ -751,6 +751,9 @@ $sourceDocument->getName(), } } // refresh the document object + DBUtil::commit(); + + $oDocument->clearAllCaches(); // NEW SEARCH @@ -787,8 +790,6 @@ $sourceDocument->getName(), } - DBUtil::commit(); - // update document object with additional fields / data from the triggers $oDocument = Document::get($oDocument->iId); diff --git a/plugins/ktstandard/PDFGeneratorAction.php b/plugins/ktstandard/PDFGeneratorAction.php index 89030ef..5868384 100644 --- a/plugins/ktstandard/PDFGeneratorAction.php +++ b/plugins/ktstandard/PDFGeneratorAction.php @@ -46,6 +46,8 @@ require_once(KT_LIB_DIR . '/plugins/pluginregistry.inc.php'); require_once(KT_LIB_DIR . '/roles/Role.inc'); +require_once(KT_DIR . '/plugins/pdfConverter/pdfConverter.php'); + class PDFGeneratorAction extends KTDocumentAction { var $sName = 'ktstandard.pdf.generate'; var $_sShowPermission = "ktcore.permissions.read"; @@ -60,7 +62,48 @@ class PDFGeneratorAction extends KTDocumentAction { 'otg', 'std', 'asc'); function getDisplayName() { - $cmdpath = KTUtil::findCommand('externalBinary/python'); + global $default; + // The generation of the pdf is done through the PDF Converter plugin. + // The PDF's are generated in the background by the document processor + + if(!empty($this->oDocument)){ + $iDocId = $this->oDocument->iId; + + // Build the display name and url + $sDisplayName = _kt('Generate PDF'); + + $sHostPath = KTUtil::kt_url(); + $icon = "PDF"; + $link = KTUtil::ktLink('action.php', 'ktstandard.pdf.generate', array( 'fDocumentId' => $this->oDocument->getId(), 'action' => 'pdfdownload')); + $sDisplayLink = " {$icon}"; + + // First check if the pdf has already been generated + $dir = $default->pdfDirectory; + $file = $dir .'/'. $iDocId . '.pdf'; + + if(file_exists($file)){ + // Display the download link + return $sDisplayName . $sDisplayLink; + } + + // If the file does not exist, check if the document has the correct mimetype + $converter = new pdfConverter(); + $mimeTypes = $converter->getSupportedMimeTypes(); + $docType = $this->getMimeExtension(); + + if($mimeTypes === true || in_array($docType, $mimeTypes)){ + // Display the download link + return $sDisplayName . $sDisplayLink; + } + }else{ + // If the document is empty then we are probably in the workflow admin - action restrictions section, so we can display the name. + return $sDisplayName; + } + + return ''; + + /* + //$cmdpath = KTUtil::findCommand('externalBinary/python'); // Check if openoffice and python are available if($cmdpath != false && file_exists($cmdpath) && !empty($cmdpath)) { @@ -82,6 +125,7 @@ class PDFGeneratorAction extends KTDocumentAction { } } return ''; + */ } function form_main() { @@ -170,11 +214,66 @@ class PDFGeneratorAction extends KTDocumentAction { } /** + * Method to download the pdf. + * + * @author KnowledgeTree Team + * @access public + */ + public function do_pdfdownload() + { + global $default; + $iDocId = $this->oDocument->iId; + + // Check if pdf has already been created + $dir = $default->pdfDirectory; + $file = $dir .'/'. $iDocId . '.pdf'; + $mimetype = 'application/pdf'; + $size = filesize($file); + + // Set the filename + $name = $this->oDocument->getFileName(); + $aName = explode('.', $name); + array_pop($aName); + $name = implode('.', $aName) . '.pdf'; + + + if(file_exists($file)){ + if(KTUtil::download($file, $mimetype, $size, $name) === false){ + $default->log->error('PDF Generator: PDF file could not be downloaded because it doesn\'t exist'); + $this->errorRedirectToMain(_kt('PDF file could not be downloaded because it doesn\'t exist')); + } + exit(); + } + + // If not - create one + $converter = new pdfConverter(); + $converter->setDocument($this->oDocument); + $res = $converter->processDocument(); + + if(!$res){ + $default->log->error('PDF Generator: PDF file could not be generated'); + $this->errorRedirectToMain(_kt('PDF file could not be generated, the file may be of an unsupported mime type.')); + exit(); + } + + if(file_exists($file)){ + if(KTUtil::download($file, $mimetype, $size, $name) === false){ + $default->log->error('PDF Generator: PDF file could not be downloaded because it doesn\'t exist'); + $this->errorRedirectToMain(_kt('PDF file could not be downloaded because it doesn\'t exist')); + } + exit(); + } + $this->errorRedirectToMain(_kt('PDF file could not be generated')); + exit(); + } + + /** * Method for downloading the document as a pdf. * + * @deprecated * @return true on success else false */ - function do_pdfdownload() { + function do_pdfdownload_deprecated() { $oDocument = $this->oDocument; $oStorage =& KTStorageManagerUtil::getSingleton(); diff --git a/plugins/pdfConverter/pdfConverter.php b/plugins/pdfConverter/pdfConverter.php index cbf9ac3..4930c3e 100644 --- a/plugins/pdfConverter/pdfConverter.php +++ b/plugins/pdfConverter/pdfConverter.php @@ -70,28 +70,47 @@ class pdfConverter extends BaseProcessor */ public function getSupportedMimeTypes() { - // support all for now... - return true; //array(); + $aAcceptedMimeTypes = array('doc', 'ods', 'odt', 'ott', 'txt', 'rtf', 'sxw', 'stw', + // 'html', 'htm', + 'xml' , 'pdb', 'psw', 'ods', 'ots', 'sxc', + 'stc', 'dif', 'dbf', 'xls', 'xlt', 'slk', 'csv', 'pxl', + 'odp', 'otp', 'sxi', 'sti', 'ppt', 'pot', 'sxd', 'odg', + 'otg', 'std', 'asc'); + + return $aAcceptedMimeTypes; } function convertFile($filename) { + global $default; + // Get contents and send to converter $buffer = file_get_contents($filename); $buffer = $this->xmlrpc->convertDocument($buffer, 'pdf'); if($buffer === false){ + $default->log->error('PDF Converter Plugin: Conversion to PDF Failed'); return false; } - global $default; - $dir = $default->varDirectory . '/pdf/'; - $filename = $dir . $this->document->iId.'.pdf'; + $dir = $default->pdfDirectory; + + // Ensure the PDF directory exists + if(!file_exists($dir)){ + mkdir($dir, '0755'); + } + + $pdfFile = $dir .'/'. $this->document->iId.'.pdf'; + + // if a previous version of the pdf exists - delete it + if(file_exists($pdfFile)){ + @unlink($pdfFile); + } - file_put_contents($filename, $buffer); + file_put_contents($pdfFile, $buffer); unset($buffer); - return $filename; + return $pdfFile; } } diff --git a/plugins/pdfConverter/pdfConverterPlugin.php b/plugins/pdfConverter/pdfConverterPlugin.php index d24aa34..5664ed1 100644 --- a/plugins/pdfConverter/pdfConverterPlugin.php +++ b/plugins/pdfConverter/pdfConverterPlugin.php @@ -24,19 +24,52 @@ require_once(KT_LIB_DIR . '/plugins/plugin.inc.php'); require_once(KT_LIB_DIR . '/plugins/pluginregistry.inc.php'); +class DeletePDFTrigger { + var $namespace = 'pdf.converter.triggers.delete'; + var $aInfo = null; + + function setInfo($aInfo) { + $this->aInfo = $aInfo; + } + + /** + * On deleting a document, send the document owner and alert creator a notification email + */ + function postValidate() { + $oDoc = $this->aInfo['document']; + $docId = $oDoc->getId(); + $docInfo = array('id' => $docId, 'name' => $oDoc->getName()); + + // Delete the pdf document + global $default; + $pdfDirectory = $default->pdfDirectory; + + $file = $pdfDirectory .'/'.$docId.'.pdf'; + + if(file_exists($file)){ + @unlink($file); + } + } +} + class pdfConverterPlugin extends KTPlugin { var $sNamespace = 'pdf.converter.processor.plugin'; var $iVersion = 0; + var $autoRegister = true; + var $createSQL = true; function pdfConverterPlugin($sFilename = null) { $res = parent::KTPlugin($sFilename); $this->sFriendlyName = _kt('Document PDF Converter'); + $this->dir = dirname(__FILE__) . DIRECTORY_SEPARATOR; + $this->sSQLDir = $this->dir . 'sql' . DIRECTORY_SEPARATOR; return $res; } function setup() { $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pdfConverter.php'; $this->registerProcessor('PDFConverter', 'pdf.converter.processor', $dir); + $this->registerTrigger('delete', 'postValidate', 'DeletePDFTrigger','pdf.converter.triggers.delete', __FILE__); } } diff --git a/plugins/pdfConverter/sql/upgradeto0.sql b/plugins/pdfConverter/sql/upgradeto0.sql new file mode 100644 index 0000000..1818947 --- /dev/null +++ b/plugins/pdfConverter/sql/upgradeto0.sql @@ -0,0 +1,2 @@ +INSERT INTO config_settings (group_name, display_name, description, item, value, default_value, type, options, can_edit) +VALUES ('urls', 'PDF Directoy', 'The path for storing the generated PDF Documents', 'pdfDirectory', 'default', '${varDirectory}/Pdf', 'string', '', 1);