}
// extract the content of uploaded document file
elseif (!empty($formValues['document_file'])) {
- $html_message = 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'], TRUE);
}
elseif (CRM_Utils_Array::value('template', $formValues) > 0) {
if (!empty($formValues['bind_format']) && $formValues['format_id']) {
$documentInfo = CRM_Core_BAO_File::getEntityFile('civicrm_msg_template', $formValues['template']);
foreach ((array) $documentInfo as $info) {
- $html_message = 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'], TRUE);
}
}
if (!empty($formValues['update_format'])) {
$messageToken,
'CRM_Contact_Form_Task_PDFLetterCommon'
);
+
if (civicrm_error($contact)) {
$notSent[] = $contactId;
continue;
// todo
'pdf' => 'PDF',
);
+
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, $formats[$ext]);
// TODO: Split document generation and output into separate functions
unlink($absPath);
}
- if ($returnContent) {
- return self::doc2Text($path, $type);
- }
-
$fileType = ($type == 'docx') ? 'Word2007' : 'ODText';
$phpWord = \PhpOffice\PhpWord\IOFactory::load($path, $fileType);
$phpWord->save($absPath, 'HTML');
+ // return the html content for tokenreplacment and eventually dused 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 \Civi::paths()->getUrl($uploadDir) . $newFile;
}
public static function doc2Text($filePath, $type) {
$content = '';
$docType = array_search($type, CRM_Core_SelectValues::documentApplicationType());
- $dataFile = ($docType == 'docx') ? "word/document.xml" : "content.xml";
+
+ // for reference on document entry type check http://phpword.readthedocs.io/en/latest/writersreaders.html
+ $dataFiles = array(
+ 'odt' => array(
+ 'content.xml',
+ 'styles.xml',
+ 'Pictures/',
+ ),
+ 'docx' => array(
+ 'word/document.xml',
+ 'word/styles.xml',
+ 'docProps/custom.xml',
+ 'word/numbering.xml',
+ 'word/settings.xml',
+ 'word/webSettings.xml',
+ 'word/fontTable.xml',
+ 'word/theme/theme1.xml',
+ ),
+ );
$zip = zip_open($filePath);
}
while ($zip_entry = zip_read($zip)) {
- if (zip_entry_open($zip, $zip_entry) == FALSE || zip_entry_name($zip_entry) != $dataFile) {
+ if (zip_entry_open($zip, $zip_entry) == FALSE || !in_array(zip_entry_name($zip_entry), $dataFiles[$docType])) {
continue;
}
$content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
zip_close($zip);
- $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
+ $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
$content = str_replace('</w:r></w:p>', "\r\n", $content);
- $striped_content = strip_tags($content);
+ $striped_content = nl2br(strip_tags($content));
- return nl2br($striped_content);
+ return array($striped_content, $docType);
}
}
</div><!-- /.crm-accordion-body -->
</div><!-- /.crm-accordion-wrapper -->
- <div class="crm-accordion-wrapper crm-html_email-accordion ">
+ <div id="pdf_format" class="crm-accordion-wrapper crm-html_email-accordion ">
<div class="crm-accordion-header">
{$form.pdf_format_id.label}
</div><!-- /.crm-accordion-header -->
function toggleByFileType(type) {
var show = (type == 'odt' || type == 'docx') ? false : true;
$("#msg_html").toggle(show);
- $("#msg_text").toggle(show);
+ $("#msg_text, #pdf_format").toggle(show);
$("#file_id").parent().parent().toggle(!show);
// auto file type validation
else {
cj('.crm-html_email-accordion').show();
cj('.crm-document-accordion').hide();
+ cj('#document_type').closest('tr').show();
}
CRM.wysiwyg.setVal('#' + html_message, '');
var hide = (data.document_path && isPDF) ? false : true;
cj('.crm-html_email-accordion').toggle(hide);
cj('.crm-document-accordion').toggle(!hide);
+ cj('#document_type').closest('tr').toggle(hide);
// Unset any uploaded document when any template is chosen
if (cj('#document.file').length) {
}
if (!hide) {
- cj("#document_type").val( data.file_type );
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>';
return;