From: Coleman Watts Date: Thu, 14 Jul 2016 16:54:55 +0000 (-0400) Subject: CRM-17608 - Only unzip file once X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=9eae09f1e2bbaf5c983d9ff592955ee566747ab8;p=civicrm-core.git CRM-17608 - Only unzip file once --- diff --git a/CRM/Contact/Form/Task/PDFLetterCommon.php b/CRM/Contact/Form/Task/PDFLetterCommon.php index e10478bfba..bb2755d1b7 100644 --- a/CRM/Contact/Form/Task/PDFLetterCommon.php +++ b/CRM/Contact/Form/Task/PDFLetterCommon.php @@ -349,7 +349,7 @@ class CRM_Contact_Form_Task_PDFLetterCommon { } if (!empty($formValues['document_file_path'])) { - $html_message = CRM_Utils_PDF_Document::doc2Text($formValues['document_file_path'], $formValues['document_type']); + list($html_message, $zip) = CRM_Utils_PDF_Document::unzipDoc($formValues['document_file_path'], $formValues['document_type']); } foreach ($form->_contactIds as $item => $contactId) { @@ -391,7 +391,7 @@ class CRM_Contact_Form_Task_PDFLetterCommon { CRM_Utils_PDF_Utils::html2pdf($html, "CiviLetter.pdf", FALSE, $formValues); } elseif (!empty($formValues['document_file_path'])) { - CRM_Utils_PDF_Document::printDocuments($formValues['document_file_path'], $html, $type); + CRM_Utils_PDF_Document::printDocuments($formValues['document_file_path'], $html, $type, $zip); } else { CRM_Utils_PDF_Document::html2doc($html, "CiviLetter.$type", $formValues); diff --git a/CRM/Utils/PDF/Document.php b/CRM/Utils/PDF/Document.php index e20ae65242..5d2c8d6231 100644 --- a/CRM/Utils/PDF/Document.php +++ b/CRM/Utils/PDF/Document.php @@ -35,6 +35,22 @@ require_once 'TbsZip/tbszip.php'; class CRM_Utils_PDF_Document { + public static $ooxmlMap = array( + 'docx' => array( + 'dataFile' => 'word/document.xml', + 'startTag' => '', + // TODO need to provide proper ooxml tag for pagebreak + 'pageBreak' => '', + 'endTag' => '', + ), + 'odt' => array( + 'dataFile' => 'content.xml', + 'startTag' => '', + 'pageBreak' => '', + 'endTag' => '', + ), + ); + /** * @param array $pages * @param string $fileName @@ -150,56 +166,40 @@ class CRM_Utils_PDF_Document { } /** - * Extract content of docx/odt file as text and later used for token replacement + * Extract content of docx/odt file + * * @param string $filePath Document file path * @param string $docType File type of document - * @param bool $returnZipObj Return clsTbsZip object along with content? * - * @return string|array - * File content of document as text or array of content and clsTbsZip object + * @return array + * [string, clsTbsZip] */ - public static function doc2Text($filePath, $docType, $returnZipObj = FALSE) { - $dataFile = ($docType == 'docx') ? 'word/document.xml' : 'content.xml'; + public static function unzipDoc($filePath, $docType) { + $dataFile = SELF::$ooxmlMap[$docType]['dataFile']; $zip = new clsTbsZip(); $zip->Open($filePath); $content = $zip->FileRead($dataFile); - if ($returnZipObj) { - return array($content, $zip); - } - - return $content; + return array($content, $zip); } /** * Modify contents of docx/odt file(s) and later merged into one final document * - * @param string $filePath Document file path - * @param array $contents content of formatted/token-replaced document - * @param string $docType Document type e.g. odt/docx + * @param string $filePath + * Document file path + * @param array $contents + * Content of formatted/token-replaced document + * @param string $docType + * Document type e.g. odt/docx + * @param clsTbsZip $zip + * Zip archive */ - public static function printDocuments($filePath, $contents, $docType) { - $ooxmlMap = array( - 'docx' => array( - 'dataFile' => 'word/document.xml', - 'startTag' => '', - // TODO need to provide proper ooxml tag for pagebreak - 'pageBreak' => '', - 'endTag' => '', - ), - 'odt' => array( - 'dataFile' => 'content.xml', - 'startTag' => '', - 'pageBreak' => '', - 'endTag' => '', - ), - ); - - $dataMap = $ooxmlMap[$docType]; + public static function printDocuments($filePath, $contents, $docType, $zip) { + $dataMap = SELF::$ooxmlMap[$docType]; - /* @var clsTbsZip $zip */ - list($finalContent, $zip) = self::doc2Text($filePath, $docType, TRUE); + $finalContent = $zip->FileRead($dataMap['dataFile']); // token-replaced document contents of each contact will be merged into final document foreach ($contents as $key => $content) {