CRM-17607 - Support multiple document formats for export
authorColeman Watts <coleman@civicrm.org>
Wed, 24 Feb 2016 14:09:59 +0000 (09:09 -0500)
committerColeman Watts <coleman@civicrm.org>
Wed, 25 May 2016 02:31:30 +0000 (22:31 -0400)
CRM/Contact/Form/Task/PDFLetterCommon.php
CRM/Core/SelectValues.php
CRM/Utils/PDF/Document.php [new file with mode: 0644]
templates/CRM/Contact/Form/Task/PDFLetterCommon.tpl

index 819e6b6ec1e48ee23a470933126b46442497e484..dbcacbeca682633b6a8d1081585df5ac8428e226 100644 (file)
@@ -167,6 +167,8 @@ class CRM_Contact_Form_Task_PDFLetterCommon {
     $form->assign('useSelectedPageFormat', ts('Should the new template always use the selected Page Format?'));
     $form->assign('totalSelectedContacts', count($form->_contactIds));
 
+    $form->add('select', 'document_type', ts('Document Type'), CRM_Core_SelectValues::documentFormat());
+
     CRM_Mailing_BAO_Mailing::commonCompose($form);
 
     $buttons = array();
@@ -175,6 +177,7 @@ class CRM_Contact_Form_Task_PDFLetterCommon {
         'type' => 'submit',
         'name' => ts('Download Document'),
         'isDefault' => TRUE,
+        'icon' => 'fa-download',
       );
       $buttons[] = array(
         'type' => 'submit',
@@ -322,6 +325,7 @@ class CRM_Contact_Form_Task_PDFLetterCommon {
     $buttonName = $form->controller->getButtonName();
     $skipOnHold = isset($form->skipOnHold) ? $form->skipOnHold : FALSE;
     $skipDeceased = isset($form->skipDeceased) ? $form->skipDeceased : TRUE;
+    $html = array();
 
     foreach ($form->_contactIds as $item => $contactId) {
       $params = array('contact_id' => $contactId);
@@ -360,7 +364,14 @@ class CRM_Contact_Form_Task_PDFLetterCommon {
       self::createActivities($form, $html_message, $form->_contactIds);
     }
 
-    CRM_Utils_PDF_Utils::html2pdf($html, "CiviLetter.pdf", FALSE, $formValues);
+    $type = $formValues['document_type'];
+
+    if ($type == 'pdf') {
+      CRM_Utils_PDF_Utils::html2pdf($html, "CiviLetter.pdf", FALSE, $formValues);
+    }
+    else {
+      CRM_Utils_PDF_Document::html2doc($html, "CiviLetter.$type", $formValues);
+    }
 
     $form->postProcessHook();
 
index dc37e86cc2cf3067130d43fc042495c76d22b519..ed65f23c96a0b6fc6d576f2ae9092a4f0074749f 100644 (file)
@@ -1073,4 +1073,18 @@ class CRM_Core_SelectValues {
     );
   }
 
+  /**
+   * Exportable document formats.
+   *
+   * @return array
+   */
+  public static function documentFormat() {
+    return array(
+      'pdf' => ts('Portable Document Format (.pdf)'),
+      'docx' => ts('MS Word (.docx)'),
+      'odt' => ts('Open Office (.odt)'),
+      'html' => ts('Webpage (.html)'),
+    );
+  }
+
 }
diff --git a/CRM/Utils/PDF/Document.php b/CRM/Utils/PDF/Document.php
new file mode 100644 (file)
index 0000000..4b990cf
--- /dev/null
@@ -0,0 +1,98 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC (c) 2004-2015
+ */
+class CRM_Utils_PDF_Document {
+
+  /**
+   * @param array $pages
+   * @param string $fileName
+   * @param array|int $format
+   */
+  public static function html2doc($pages, $fileName, $format = array()) {
+    if (is_array($format)) {
+      // PDF Page Format parameters passed in - merge with defaults
+      $format += CRM_Core_BAO_PdfFormat::getDefaultValues();
+    }
+    else {
+      // PDF Page Format ID passed in
+      $format = CRM_Core_BAO_PdfFormat::getById($format);
+    }
+    $paperSize = CRM_Core_BAO_PaperSize::getByName($format['paper_size']);
+
+    $metric = CRM_Core_BAO_PdfFormat::getValue('metric', $format);
+    $pageStyle = array(
+      'orientation' => CRM_Core_BAO_PdfFormat::getValue('orientation', $format),
+      'pageSizeW' => self::toTwip($paperSize['width'], $paperSize['metric']),
+      'pageSizeH' => self::toTwip($paperSize['height'], $paperSize['metric']),
+      'marginTop' => self::toTwip(CRM_Core_BAO_PdfFormat::getValue('margin_top', $format), $metric),
+      'marginRight' => self::toTwip(CRM_Core_BAO_PdfFormat::getValue('margin_right', $format), $metric),
+      'marginBottom' => self::toTwip(CRM_Core_BAO_PdfFormat::getValue('margin_bottom', $format), $metric),
+      'marginLeft' => self::toTwip(CRM_Core_BAO_PdfFormat::getValue('margin_left', $format), $metric),
+    );
+
+    $ext = pathinfo($fileName, PATHINFO_EXTENSION);
+
+    $phpWord = new \PhpOffice\PhpWord\PhpWord();
+
+    $phpWord->getDocInfo()
+      ->setCreator(CRM_Core_DAO::getFieldValue('CRM_Contact_BAO_Contact', CRM_Core_Session::getLoggedInContactID(), 'display_name'));
+
+    foreach ((array) $pages as $page => $html) {
+      $section = $phpWord->addSection($pageStyle + array('breakType' => 'nextPage'));
+      \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);
+    }
+    $formats = array(
+      'docx' => 'Word2007',
+      'odt' => 'ODText',
+      'html' => 'HTML',
+      // todo
+      'pdf' => 'PDF',
+    );
+    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, $formats[$ext]);
+
+    // TODO: Split document generation and output into separate functions
+    CRM_Utils_System::setHttpHeader('Content-Type', "application/$ext");
+    CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"');
+    $objWriter->save("php://output");
+  }
+
+  /**
+   * @param $value
+   * @param $metric
+   * @return int
+   */
+  public static function toTwip($value, $metric) {
+    $point = CRM_Utils_PDF_Utils::convertMetric($value, $metric, 'pt');
+    return \PhpOffice\PhpWord\Shared\Converter::pointToTwip($point);
+  }
+
+}
index 2a53210188d6604ad0e6788a13294cfe2299d05d..97e033548c2af141f8ba1682346f8a9756459fa5 100644 (file)
   </div><!-- /.crm-accordion-body -->
 </div><!-- /.crm-accordion-wrapper -->
 
+<table class="form-layout-compressed">
+  <tr>
+    <td class="label-left">{$form.document_type.label}</td>
+    <td>{$form.document_type.html}</td>
+  </tr>
+</table>
+
 {include file="CRM/Mailing/Form/InsertTokens.tpl"}
 
 {literal}