From: deb.monish Date: Fri, 15 Jul 2016 14:17:56 +0000 (+0530) Subject: fetch content of HTML rather then creating and using the generated HTML file X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=9cbcd63f8150d0bd0fe82a2c371b9fe9b8cfed84;p=civicrm-core.git fetch content of HTML rather then creating and using the generated HTML file --- diff --git a/CRM/Contact/Form/Task/PDFLetterCommon.php b/CRM/Contact/Form/Task/PDFLetterCommon.php index 2fc4b0ee66..5a242814cd 100644 --- a/CRM/Contact/Form/Task/PDFLetterCommon.php +++ b/CRM/Contact/Form/Task/PDFLetterCommon.php @@ -294,13 +294,13 @@ class CRM_Contact_Form_Task_PDFLetterCommon { $documentInfo = CRM_Core_BAO_File::getEntityFile('civicrm_msg_template', $formValues['template']); foreach ((array) $documentInfo as $info) { - list($html_message, $formValues['document_type']) = CRM_Utils_PDF_Document::docReader($info['fullPath'], $info['mime_type'], TRUE); + list($html_message, $formValues['document_type']) = CRM_Utils_PDF_Document::docReader($info['fullPath'], $info['mime_type']); $formValues['document_file_path'] = $info['fullPath']; } } // extract the content of uploaded document file elseif (!empty($formValues['document_file'])) { - list($html_message, $formValues['document_type']) = CRM_Utils_PDF_Document::docReader($formValues['document_file']['name'], $formValues['document_file']['type'], TRUE); + list($html_message, $formValues['document_type']) = CRM_Utils_PDF_Document::docReader($formValues['document_file']['name'], $formValues['document_file']['type']); $formValues['document_file_path'] = $formValues['document_file']['name']; // Fixme: this shouldn't be necessary but the above docReader function is giving blank output for docx files list($html_message) = CRM_Utils_PDF_Document::unzipDoc($formValues['document_file_path'], $formValues['document_type']); diff --git a/CRM/Mailing/Page/AJAX.php b/CRM/Mailing/Page/AJAX.php index a02aadb7b6..075e2d1f8c 100644 --- a/CRM/Mailing/Page/AJAX.php +++ b/CRM/Mailing/Page/AJAX.php @@ -57,7 +57,7 @@ class CRM_Mailing_Page_AJAX { $documentInfo = CRM_Core_BAO_File::getEntityFile('civicrm_msg_template', $templateId); foreach ((array) $documentInfo as $info) { $messages['file_type'] = array_search($info['mime_type'], CRM_Core_SelectValues::documentApplicationType()); - $messages['document_path'] = CRM_Utils_PDF_Document::docReader($info['fullPath'], $messages['file_type']); + $messages['document_body'] = CRM_Utils_PDF_Document::docReader($info['fullPath'], $messages['file_type'], TRUE); $messages['mime_type'] = $info['mime_type']; } diff --git a/CRM/Utils/PDF/Document.php b/CRM/Utils/PDF/Document.php index 5d2c8d6231..8c9fc65978 100644 --- a/CRM/Utils/PDF/Document.php +++ b/CRM/Utils/PDF/Document.php @@ -131,38 +131,25 @@ class CRM_Utils_PDF_Document { /** * @param array $path docx/odt file path * @param string $type File type - * @param bool $returnContent extract content of docx/odt file as a text, later used for token replacement + * @param bool $returnHTMLBody extract entire or only HTNL body content of docx/odt file, + * later used for token replacement or preview * - * @return string - * Return filepath of created html copy of document OR extracted content of document + * @return string|array + * Return extracted content of document in HTML */ - public static function docReader($path, $type, $returnContent = FALSE) { - // get path of civicrm upload directory which is used for temporary file storage - $uploadDir = Civi::settings()->get('uploadDir'); - - // build the path of of new html file - $pathInfo = pathinfo($path); - $newFile = $pathInfo['filename'] . ".html"; - $absPath = Civi::paths()->getPath($uploadDir) . $newFile; - - // cleanup temporary html file created for preview - if (file_exists($absPath)) { - unlink($absPath); - } + public static function docReader($path, $type, $returnHTMLBody = FALSE) { $fileType = ($type == 'docx') ? 'Word2007' : 'ODText'; $phpWord = \PhpOffice\PhpWord\IOFactory::load($path, $fileType); - $phpWord->save($absPath, 'HTML'); + $phpWordHTML = new \PhpOffice\PhpWord\Writer\HTML($phpWord); - // return the html content for tokenreplacment and eventually used for document download - if ($returnContent) { - $filename = fopen($absPath, 'r'); - $content = fread($filename, filesize($absPath)); - fclose($filename); - return array($content, array_search($type, CRM_Core_SelectValues::documentApplicationType())); + //return only the HTML body later used for preview + if ($returnHTMLBody) { + return $phpWordHTML->getWriterPart('Body')->write(); } - return \Civi::paths()->getUrl($uploadDir) . $newFile; + // return the html content for tokenreplacment and eventually used for document download + return array($phpWordHTML->getContent(), array_search($type, CRM_Core_SelectValues::documentApplicationType())); } /** diff --git a/templates/CRM/Mailing/Form/InsertTokens.tpl b/templates/CRM/Mailing/Form/InsertTokens.tpl index c732af7a91..395e140810 100644 --- a/templates/CRM/Mailing/Form/InsertTokens.tpl +++ b/templates/CRM/Mailing/Form/InsertTokens.tpl @@ -140,7 +140,7 @@ function selectValue( val, prefix) { var dataUrl = {/literal}"{crmURL p='civicrm/ajax/template' h=0 }"{literal}; cj.post( dataUrl, {tid: val}, function( data ) { - var hide = (data.document_path && isPDF) ? false : true; + var hide = (data.document_body && isPDF) ? false : true; cj('.crm-html_email-accordion, .crm-pdf-format-accordion').toggle(hide); cj('.crm-document-accordion').toggle(!hide); @@ -153,7 +153,7 @@ function selectValue( val, prefix) { if (!hide) { cj("#subject").val( data.subject ); - document.getElementById("document-preview").innerHTML=''; + cj("#document-preview").html(data.document_body).parent().css({'background': 'white'}); return; }