hide document type on upload and improve template processing
authordeb.monish <monish.deb@webaccessglobal.com>
Thu, 9 Jun 2016 15:23:43 +0000 (20:53 +0530)
committerdeb.monish <monish.deb@webaccessglobal.com>
Thu, 14 Jul 2016 18:05:43 +0000 (23:35 +0530)
CRM/Contact/Form/Task/PDFLetterCommon.php
CRM/Utils/PDF/Document.php
templates/CRM/Admin/Form/MessageTemplates.tpl
templates/CRM/Mailing/Form/InsertTokens.tpl

index 3811d97543d372263db16d1c6ec00f96305ce228..bafc8b89021910a9ef85a97dc13816817903fb86 100644 (file)
@@ -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;
index 81da9b58ea5b603de2ed1273afa6ae657b31b847..ba7c77370d9e9a715131e0213b19f297548687f2 100644 (file)
@@ -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('</w:r></w:p></w:tc><w:tc>', " ", $content);
+    $content = str_replace('</w:r></w:p></w:tc><w:tc>', "&nbsp;&nbsp;", $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);
   }
 
 }
index dd440e15eb2f2bb5a5ca0e7ff194af97bd03d4d7..ecd834f9c7be1b793ebac6cd83946a4789690485 100644 (file)
             </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
index bbecb1f6fb69c28c53bc0993a9a8a077017dfe85..4cf363963c589089513bb778e703e302b0dfd6f4 100644 (file)
@@ -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='<object type="text/html" data='+ data.document_path +' width="680" height="400" style="background:white;"></object>';
       return;