From 2c8a6e633e6a5edd11e730455fd6a22acfcf4ffa Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Thu, 9 Jun 2016 20:53:43 +0530 Subject: [PATCH] hide document type on upload and improve template processing --- CRM/Contact/Form/Task/PDFLetterCommon.php | 5 ++- CRM/Utils/PDF/Document.php | 41 +++++++++++++++---- templates/CRM/Admin/Form/MessageTemplates.tpl | 4 +- templates/CRM/Mailing/Form/InsertTokens.tpl | 3 +- 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/CRM/Contact/Form/Task/PDFLetterCommon.php b/CRM/Contact/Form/Task/PDFLetterCommon.php index 3811d97543..bafc8b8902 100644 --- a/CRM/Contact/Form/Task/PDFLetterCommon.php +++ b/CRM/Contact/Form/Task/PDFLetterCommon.php @@ -285,7 +285,7 @@ class CRM_Contact_Form_Task_PDFLetterCommon { } // 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']) { @@ -298,7 +298,7 @@ class CRM_Contact_Form_Task_PDFLetterCommon { $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'])) { @@ -351,6 +351,7 @@ class CRM_Contact_Form_Task_PDFLetterCommon { $messageToken, 'CRM_Contact_Form_Task_PDFLetterCommon' ); + if (civicrm_error($contact)) { $notSent[] = $contactId; continue; diff --git a/CRM/Utils/PDF/Document.php b/CRM/Utils/PDF/Document.php index 81da9b58ea..ba7c77370d 100644 --- a/CRM/Utils/PDF/Document.php +++ b/CRM/Utils/PDF/Document.php @@ -77,6 +77,7 @@ class CRM_Utils_PDF_Document { // todo 'pdf' => 'PDF', ); + $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, $formats[$ext]); // TODO: Split document generation and output into separate functions @@ -117,14 +118,18 @@ class CRM_Utils_PDF_Document { 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; } @@ -139,7 +144,25 @@ class CRM_Utils_PDF_Document { 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); @@ -148,7 +171,7 @@ class CRM_Utils_PDF_Document { } 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)); @@ -157,11 +180,11 @@ class CRM_Utils_PDF_Document { zip_close($zip); - $content = str_replace('', " ", $content); + $content = str_replace('', "  ", $content); $content = str_replace('', "\r\n", $content); - $striped_content = strip_tags($content); + $striped_content = nl2br(strip_tags($content)); - return nl2br($striped_content); + return array($striped_content, $docType); } } diff --git a/templates/CRM/Admin/Form/MessageTemplates.tpl b/templates/CRM/Admin/Form/MessageTemplates.tpl index dd440e15eb..ecd834f9c7 100644 --- a/templates/CRM/Admin/Form/MessageTemplates.tpl +++ b/templates/CRM/Admin/Form/MessageTemplates.tpl @@ -119,7 +119,7 @@ -
+
{$form.pdf_format_id.label}
@@ -166,7 +166,7 @@ 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 diff --git a/templates/CRM/Mailing/Form/InsertTokens.tpl b/templates/CRM/Mailing/Form/InsertTokens.tpl index bbecb1f6fb..4cf363963c 100644 --- a/templates/CRM/Mailing/Form/InsertTokens.tpl +++ b/templates/CRM/Mailing/Form/InsertTokens.tpl @@ -127,6 +127,7 @@ function selectValue( val, prefix) { else { cj('.crm-html_email-accordion').show(); cj('.crm-document-accordion').hide(); + cj('#document_type').closest('tr').show(); } CRM.wysiwyg.setVal('#' + html_message, ''); @@ -142,6 +143,7 @@ function selectValue( val, prefix) { 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) { @@ -149,7 +151,6 @@ function selectValue( val, prefix) { } if (!hide) { - cj("#document_type").val( data.file_type ); cj("#subject").val( data.subject ); document.getElementById("document-preview").innerHTML=''; return; -- 2.25.1