From: Coleman Watts Date: Wed, 24 Feb 2016 14:09:59 +0000 (-0500) Subject: CRM-17607 - Support multiple document formats for export X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=0aeb5a1ecad20360888f54741afba2d76cdf423e;p=civicrm-core.git CRM-17607 - Support multiple document formats for export --- diff --git a/CRM/Contact/Form/Task/PDFLetterCommon.php b/CRM/Contact/Form/Task/PDFLetterCommon.php index 819e6b6ec1..dbcacbeca6 100644 --- a/CRM/Contact/Form/Task/PDFLetterCommon.php +++ b/CRM/Contact/Form/Task/PDFLetterCommon.php @@ -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(); diff --git a/CRM/Core/SelectValues.php b/CRM/Core/SelectValues.php index dc37e86cc2..ed65f23c96 100644 --- a/CRM/Core/SelectValues.php +++ b/CRM/Core/SelectValues.php @@ -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 index 0000000000..4b990cf74a --- /dev/null +++ b/CRM/Utils/PDF/Document.php @@ -0,0 +1,98 @@ + 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); + } + +} diff --git a/templates/CRM/Contact/Form/Task/PDFLetterCommon.tpl b/templates/CRM/Contact/Form/Task/PDFLetterCommon.tpl index 2a53210188..97e033548c 100644 --- a/templates/CRM/Contact/Form/Task/PDFLetterCommon.tpl +++ b/templates/CRM/Contact/Form/Task/PDFLetterCommon.tpl @@ -116,6 +116,13 @@ + + + + + +
{$form.document_type.label}{$form.document_type.html}
+ {include file="CRM/Mailing/Form/InsertTokens.tpl"} {literal}