Commit 6f3966ab1aab41bc345b6fbcbe6bd8518f6e62c7
1 parent
ceaaa4eb
FIx: Mime type corrected for PDF file
Showing
1 changed file
with
67 additions
and
47 deletions
plugins/ktstandard/PDFGeneratorWorkflowTriggers.php
| @@ -40,22 +40,22 @@ require_once(KT_LIB_DIR . '/workflow/workflowtrigger.inc.php'); | @@ -40,22 +40,22 @@ require_once(KT_LIB_DIR . '/workflow/workflowtrigger.inc.php'); | ||
| 40 | require_once(KT_DIR . '/plugins/pdfConverter/pdfConverter.php'); | 40 | require_once(KT_DIR . '/plugins/pdfConverter/pdfConverter.php'); |
| 41 | 41 | ||
| 42 | class PDFGeneratorWorkflowTriggerDuplicatePDF extends KTWorkflowTrigger { | 42 | class PDFGeneratorWorkflowTriggerDuplicatePDF extends KTWorkflowTrigger { |
| 43 | - var $sNamespace; | ||
| 44 | - var $sFriendlyName; | ||
| 45 | - var $sDescription; | ||
| 46 | - var $oTriggerInstance; | ||
| 47 | - var $aConfig = array(); | 43 | + var $sNamespace; |
| 44 | + var $sFriendlyName; | ||
| 45 | + var $sDescription; | ||
| 46 | + var $oTriggerInstance; | ||
| 47 | + var $aConfig = array(); | ||
| 48 | 48 | ||
| 49 | - // generic requirements - both can be true | ||
| 50 | - var $bIsGuard = false; | ||
| 51 | - var $bIsAction = true; | 49 | + // generic requirements - both can be true |
| 50 | + var $bIsGuard = false; | ||
| 51 | + var $bIsAction = true; | ||
| 52 | 52 | ||
| 53 | public function PDFGeneratorWorkflowTriggerDuplicatePDF() { | 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.'); | 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 | } | 57 | } |
| 58 | - | 58 | + |
| 59 | // perform more expensive checks -before- performTransition. | 59 | // perform more expensive checks -before- performTransition. |
| 60 | // Taken from : plugins\ktcore\KTWorkflowTriggers.inc.php | 60 | // Taken from : plugins\ktcore\KTWorkflowTriggers.inc.php |
| 61 | public function precheckTransition($oDocument, $oUser) { | 61 | public function precheckTransition($oDocument, $oUser) { |
| @@ -67,51 +67,72 @@ class PDFGeneratorWorkflowTriggerDuplicatePDF extends KTWorkflowTrigger { | @@ -67,51 +67,72 @@ class PDFGeneratorWorkflowTriggerDuplicatePDF extends KTWorkflowTrigger { | ||
| 67 | 67 | ||
| 68 | return true; | 68 | return true; |
| 69 | } | 69 | } |
| 70 | + | ||
| 71 | + /** | ||
| 72 | + * Actually duplicate the Document: PDF creation, document copy, and PDF association | ||
| 73 | + */ | ||
| 70 | function performTransition($oDocument, $oUser) { | 74 | function performTransition($oDocument, $oUser) { |
| 75 | + global $default; | ||
| 71 | $iFolderId = KTUtil::arrayGet($this->aConfig, 'folder_id'); | 76 | $iFolderId = KTUtil::arrayGet($this->aConfig, 'folder_id'); |
| 72 | $oToFolder = Folder::get($iFolderId); | 77 | $oToFolder = Folder::get($iFolderId); |
| 73 | if (PEAR::isError($oFolder)) { | 78 | 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.')); | 79 | 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 | } | 80 | } |
| 76 | - | 81 | + |
| 77 | // Create the PDF | 82 | // Create the PDF |
| 83 | + $default->log->error("Create the PDF"); | ||
| 78 | $pdfFile = $this->createPDF($oDocument); | 84 | $pdfFile = $this->createPDF($oDocument); |
| 79 | if (PEAR::isError($pdfFile)) { | 85 | if (PEAR::isError($pdfFile)) { |
| 80 | return $pdfFile; | 86 | return $pdfFile; |
| 81 | } | 87 | } |
| 82 | - | 88 | + |
| 89 | + $default->log->error("Duplicate_Copy the Document"); | ||
| 83 | // Duplicate/Copy the Document | 90 | // Duplicate/Copy the Document |
| 84 | $oNewDocument = KTDocumentUtil::copy($oDocument, $oToFolder); | 91 | $oNewDocument = KTDocumentUtil::copy($oDocument, $oToFolder); |
| 85 | if (PEAR::isError($oNewDocument)) { | 92 | if (PEAR::isError($oNewDocument)) { |
| 86 | return $oNewDocument; | 93 | return $oNewDocument; |
| 87 | - } | ||
| 88 | - | 94 | + } |
| 95 | + | ||
| 96 | + $default->log->error("Associate PDF with the new document [$pdfFile]"); | ||
| 89 | // Associate PDF with the new document | 97 | // Associate PDF with the new document |
| 90 | $aOptions = array( | 98 | $aOptions = array( |
| 91 | - 'temp_filename' => $pdfFile, | ||
| 92 | - 'cleanup_initial_file' => true, | 99 | + 'temp_file' => $pdfFile, |
| 100 | + 'cleanup_initial_file' => false, | ||
| 93 | ); | 101 | ); |
| 94 | - | 102 | + |
| 95 | $res = KTDocumentUtil::storeContents($oNewDocument, $oContents = null, $aOptions); | 103 | $res = KTDocumentUtil::storeContents($oNewDocument, $oContents = null, $aOptions); |
| 96 | if (PEAR::isError($res)) { | 104 | if (PEAR::isError($res)) { |
| 105 | + $default->log->error("PDF WorkflowTrigger error: can't associate PDF file to document." . $res->message); | ||
| 97 | // Remove the created document (not-correct) | 106 | // Remove the created document (not-correct) |
| 98 | KTDocumentUtil::delete($oNewDocument, _kt("PDF WorkflowTrigger error: can't create associate PDF file to document.")); | 107 | KTDocumentUtil::delete($oNewDocument, _kt("PDF WorkflowTrigger error: can't create associate PDF file to document.")); |
| 99 | return $res; | 108 | return $res; |
| 100 | } | 109 | } |
| 101 | 110 | ||
| 111 | + // Changing MIME-Filetype | ||
| 112 | + // TODO: maybe use the KTDocumentUtil::rename($oDocument, $sFilename, $oUser) ? | ||
| 113 | + $iMimeTypeId = KTMime::getMimeTypeID('application/pdf'); | ||
| 114 | + $oNewDocument->setMimeTypeId($iMimeTypeId); | ||
| 115 | + | ||
| 116 | + $bSuccess = $oNewDocument->update(); | ||
| 117 | + if ($bSuccess !== true) { | ||
| 118 | + return PEAR::raiseError(_kt("PDF WorkflowTrigger error: can't finalize document data as PDF.")); | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + // Returns the new document, so other plugin can extend this class to further handle the new PDF document | ||
| 102 | return $oNewDocument; | 122 | return $oNewDocument; |
| 103 | } | 123 | } |
| 104 | - | ||
| 105 | - | 124 | + |
| 125 | + | ||
| 106 | /** | 126 | /** |
| 107 | * Create the PDF file for the given document (or re-use the already created one) | 127 | * Create the PDF file for the given document (or re-use the already created one) |
| 108 | * Returns the PDF filename or PEAR::error | 128 | * Returns the PDF filename or PEAR::error |
| 109 | */ | 129 | */ |
| 110 | - private function createPDF(&$oDocument) { | 130 | + private function createPDF(&$oDocument) { |
| 111 | global $default; | 131 | global $default; |
| 112 | $dir = $default->pdfDirectory; | 132 | $dir = $default->pdfDirectory; |
| 133 | + $iDocId = $oDocument->iId; | ||
| 113 | $file = $dir .'/'. $iDocId . '.pdf'; | 134 | $file = $dir .'/'. $iDocId . '.pdf'; |
| 114 | - print __class__ . '::' . __function__ . '() .'." file=$file<br />"; | 135 | + $default->log->error(__class__ . '::' . __function__ . '() PRE-CONV: '." file=$file'"); |
| 115 | if (!file_exists($file)) { | 136 | if (!file_exists($file)) { |
| 116 | // If not - create one | 137 | // If not - create one |
| 117 | $converter = new pdfConverter(); | 138 | $converter = new pdfConverter(); |
| @@ -121,6 +142,7 @@ class PDFGeneratorWorkflowTriggerDuplicatePDF extends KTWorkflowTrigger { | @@ -121,6 +142,7 @@ class PDFGeneratorWorkflowTriggerDuplicatePDF extends KTWorkflowTrigger { | ||
| 121 | return PEAR::raiseError(_kt('PDF file could not be generated; Please contact your System Administrator for assistance.')); | 142 | return PEAR::raiseError(_kt('PDF file could not be generated; Please contact your System Administrator for assistance.')); |
| 122 | } | 143 | } |
| 123 | } | 144 | } |
| 145 | + $default->log->error(__class__ . '::' . __function__ . '() CONVERTED! : '." file=$file'"); | ||
| 124 | return $file; | 146 | return $file; |
| 125 | } | 147 | } |
| 126 | 148 | ||
| @@ -144,8 +166,8 @@ class PDFGeneratorWorkflowTriggerDuplicatePDF extends KTWorkflowTrigger { | @@ -144,8 +166,8 @@ class PDFGeneratorWorkflowTriggerDuplicatePDF extends KTWorkflowTrigger { | ||
| 144 | 166 | ||
| 145 | $qsFrag = array(); | 167 | $qsFrag = array(); |
| 146 | foreach ($args as $k => $v) { | 168 | 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)); | 169 | + if ($k == 'action') { $v = 'editactiontrigger'; } // horrible hack - we really need iframe embedding. |
| 170 | + $qsFrag[] = sprintf('%s=%s',urlencode($k), urlencode($v)); | ||
| 149 | } | 171 | } |
| 150 | $qs = implode('&',$qsFrag); | 172 | $qs = implode('&',$qsFrag); |
| 151 | $aOptions['result_url'] = KTUtil::addQueryStringSelf($qs); | 173 | $aOptions['result_url'] = KTUtil::addQueryStringSelf($qs); |
| @@ -154,25 +176,23 @@ class PDFGeneratorWorkflowTriggerDuplicatePDF extends KTWorkflowTrigger { | @@ -154,25 +176,23 @@ class PDFGeneratorWorkflowTriggerDuplicatePDF extends KTWorkflowTrigger { | ||
| 154 | $fFolderId = KTUtil::arrayGet($_REQUEST, 'fFolderId', KTUtil::arrayGet($this->aConfig, 'folder_id', 1)); | 176 | $fFolderId = KTUtil::arrayGet($_REQUEST, 'fFolderId', KTUtil::arrayGet($this->aConfig, 'folder_id', 1)); |
| 155 | 177 | ||
| 156 | $oFolder = Folder::get($fFolderId); | 178 | $oFolder = Folder::get($fFolderId); |
| 157 | - if(PEAR::isError($oFolder)) | ||
| 158 | - { | 179 | + if(PEAR::isError($oFolder)) { |
| 159 | $iRoot = 1; | 180 | $iRoot = 1; |
| 160 | $oFolder = Folder::get($iRoot); | 181 | $oFolder = Folder::get($iRoot); |
| 161 | $fFolderId = 1; | 182 | $fFolderId = 1; |
| 162 | - | ||
| 163 | } | 183 | } |
| 164 | 184 | ||
| 165 | $collection->setOptions($aOptions); | 185 | $collection->setOptions($aOptions); |
| 166 | $collection->setQueryObject(new BrowseQuery($fFolderId, $this->oUser)); | 186 | $collection->setQueryObject(new BrowseQuery($fFolderId, $this->oUser)); |
| 167 | $collection->setColumnOptions('ktcore.columns.singleselection', array( | 187 | $collection->setColumnOptions('ktcore.columns.singleselection', array( |
| 168 | - 'rangename' => 'folder_id', | ||
| 169 | - 'show_folders' => true, | ||
| 170 | - 'show_documents' => false, | 188 | + 'rangename' => 'folder_id', |
| 189 | + 'show_folders' => true, | ||
| 190 | + 'show_documents' => false, | ||
| 171 | )); | 191 | )); |
| 172 | 192 | ||
| 173 | $collection->setColumnOptions('ktcore.columns.title', array( | 193 | $collection->setColumnOptions('ktcore.columns.title', array( |
| 174 | - 'direct_folder' => false, | ||
| 175 | - 'folder_link' => $aOptions['result_url'], | 194 | + 'direct_folder' => false, |
| 195 | + 'folder_link' => $aOptions['result_url'], | ||
| 176 | )); | 196 | )); |
| 177 | 197 | ||
| 178 | 198 | ||
| @@ -181,26 +201,26 @@ class PDFGeneratorWorkflowTriggerDuplicatePDF extends KTWorkflowTrigger { | @@ -181,26 +201,26 @@ class PDFGeneratorWorkflowTriggerDuplicatePDF extends KTWorkflowTrigger { | ||
| 181 | $folder_path_ids = explode(',', $oFolder->getParentFolderIds()); | 201 | $folder_path_ids = explode(',', $oFolder->getParentFolderIds()); |
| 182 | $folder_path_ids[] = $oFolder->getId(); | 202 | $folder_path_ids[] = $oFolder->getId(); |
| 183 | if ($folder_path_ids[0] == 0) { | 203 | if ($folder_path_ids[0] == 0) { |
| 184 | - array_shift($folder_path_ids); | ||
| 185 | - array_shift($folder_path_names); | 204 | + array_shift($folder_path_ids); |
| 205 | + array_shift($folder_path_names); | ||
| 186 | } | 206 | } |
| 187 | 207 | ||
| 188 | foreach (range(0, count($folder_path_ids) - 1) as $index) { | 208 | 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')); | 209 | + $id = $folder_path_ids[$index]; |
| 210 | + $qsFrag2 = $qsFrag; | ||
| 211 | + $qsFrag2[] = sprintf('fFolderId=%d', $id); | ||
| 212 | + $qs2 = implode('&',$qsFrag2); | ||
| 213 | + $url = KTUtil::addQueryStringSelf($qs2); | ||
| 214 | + $aBreadcrumbs[] = sprintf('<a href="%s">%s</a>', $url, htmlentities($folder_path_names[$index], ENT_NOQUOTES, 'UTF-8')); | ||
| 195 | } | 215 | } |
| 196 | 216 | ||
| 197 | $sBreadcrumbs = implode(' » ', $aBreadcrumbs); | 217 | $sBreadcrumbs = implode(' » ', $aBreadcrumbs); |
| 198 | 218 | ||
| 199 | $aTemplateData = array( | 219 | $aTemplateData = array( |
| 200 | - 'context' => $this, | ||
| 201 | - 'breadcrumbs' => $sBreadcrumbs, | ||
| 202 | - 'collection' => $collection, | ||
| 203 | - 'args' => $args, | 220 | + 'context' => $this, |
| 221 | + 'breadcrumbs' => $sBreadcrumbs, | ||
| 222 | + 'collection' => $collection, | ||
| 223 | + 'args' => $args, | ||
| 204 | ); | 224 | ); |
| 205 | return $oTemplate->render($aTemplateData); | 225 | return $oTemplate->render($aTemplateData); |
| 206 | } | 226 | } |
| @@ -209,8 +229,8 @@ class PDFGeneratorWorkflowTriggerDuplicatePDF extends KTWorkflowTrigger { | @@ -209,8 +229,8 @@ class PDFGeneratorWorkflowTriggerDuplicatePDF extends KTWorkflowTrigger { | ||
| 209 | $folder_id = KTUtil::arrayGet($_REQUEST, 'folder_id', null); | 229 | $folder_id = KTUtil::arrayGet($_REQUEST, 'folder_id', null); |
| 210 | $oFolder = Folder::get($folder_id); | 230 | $oFolder = Folder::get($folder_id); |
| 211 | if (PEAR::isError($oFolder)) { | 231 | if (PEAR::isError($oFolder)) { |
| 212 | - // silenty ignore | ||
| 213 | - $folder_id = null; | 232 | + // silenty ignore |
| 233 | + $folder_id = null; | ||
| 214 | } | 234 | } |
| 215 | 235 | ||
| 216 | $config = array(); | 236 | $config = array(); |