$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']);
$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'];
}
/**
* @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()));
}
/**
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);
if (!hide) {
cj("#subject").val( data.subject );
- document.getElementById("document-preview").innerHTML='<object type="text/html" data='+ data.document_path +' width="680" height="400" style="background:white;"></object>';
+ cj("#document-preview").html(data.document_body).parent().css({'background': 'white'});
return;
}