Example #1
0
 /**
  * Get Option Group ID for PDF Page Formats.
  *
  * @return int
  *   Group ID (null if Group ID doesn't exist)
  */
 private static function _getGid()
 {
     if (!self::$_gid) {
         self::$_gid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'pdf_format', 'id', 'name');
         if (!self::$_gid) {
             CRM_Core_Error::fatal(ts('PDF Format Option Group not found in database.'));
         }
     }
     return self::$_gid;
 }
Example #2
0
 /**
  * @param $text
  * @param string $fileName
  * @param bool $output
  * @param null $pdfFormat
  *
  * @return string|void
  */
 public static function html2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL)
 {
     if (is_array($text)) {
         $pages =& $text;
     } else {
         $pages = array($text);
     }
     // Get PDF Page Format
     $format = CRM_Core_BAO_PdfFormat::getDefaultValues();
     if (is_array($pdfFormat)) {
         // PDF Page Format parameters passed in
         $format = array_merge($format, $pdfFormat);
     } else {
         // PDF Page Format ID passed in
         $format = CRM_Core_BAO_PdfFormat::getById($pdfFormat);
     }
     $paperSize = CRM_Core_BAO_PaperSize::getByName($format['paper_size']);
     $paper_width = self::convertMetric($paperSize['width'], $paperSize['metric'], 'pt');
     $paper_height = self::convertMetric($paperSize['height'], $paperSize['metric'], 'pt');
     // dompdf requires dimensions in points
     $paper_size = array(0, 0, $paper_width, $paper_height);
     $orientation = CRM_Core_BAO_PdfFormat::getValue('orientation', $format);
     $metric = CRM_Core_BAO_PdfFormat::getValue('metric', $format);
     $t = CRM_Core_BAO_PdfFormat::getValue('margin_top', $format);
     $r = CRM_Core_BAO_PdfFormat::getValue('margin_right', $format);
     $b = CRM_Core_BAO_PdfFormat::getValue('margin_bottom', $format);
     $l = CRM_Core_BAO_PdfFormat::getValue('margin_left', $format);
     $stationery_path_partial = CRM_Core_BAO_PdfFormat::getValue('stationery', $format);
     $stationery_path = NULL;
     if (strlen($stationery_path_partial)) {
         $doc_root = $_SERVER['DOCUMENT_ROOT'];
         $stationery_path = $doc_root . "/" . $stationery_path_partial;
     }
     $margins = array($metric, $t, $r, $b, $l);
     $config = CRM_Core_Config::singleton();
     // Add a special region for the HTML header of PDF files:
     $pdfHeaderRegion = CRM_Core_Region::instance('export-document-header', FALSE);
     $htmlHeader = $pdfHeaderRegion ? $pdfHeaderRegion->render('', FALSE) : '';
     $html = "\n<html>\n  <head>\n    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n    <style>@page { margin: {$t}{$metric} {$r}{$metric} {$b}{$metric} {$l}{$metric}; }</style>\n    <style type=\"text/css\">@import url({$config->userFrameworkResourceURL}css/print.css);</style>\n    {$htmlHeader}\n  </head>\n  <body>\n    <div id=\"crm-container\">\n";
     // Strip <html>, <header>, and <body> tags from each page
     $htmlElementstoStrip = array('@<head[^>]*?>.*?</head>@siu', '@<script[^>]*?>.*?</script>@siu', '@<body>@siu', '@</body>@siu', '@<html[^>]*?>@siu', '@</html>@siu', '@<!DOCTYPE[^>]*?>@siu');
     $htmlElementsInstead = array('', '', '', '', '', '');
     foreach ($pages as &$page) {
         $page = preg_replace($htmlElementstoStrip, $htmlElementsInstead, $page);
     }
     // Glue the pages together
     $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages);
     $html .= "\n    </div>\n  </body>\n</html>";
     if ($config->wkhtmltopdfPath) {
         return self::_html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName);
     } else {
         return self::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName);
         //return self::_html2pdf_tcpdf($paper_size, $orientation, $margins, $html, $output, $fileName,  $stationery_path);
     }
 }
 static function html2pdf(&$text, $fileName = 'civicrm.pdf', $output = FALSE, $pdfFormat = NULL)
 {
     if (is_array($text)) {
         $pages =& $text;
     } else {
         $pages = array($text);
     }
     // Get PDF Page Format
     $format = CRM_Core_BAO_PdfFormat::getDefaultValues();
     if (is_array($pdfFormat)) {
         // PDF Page Format parameters passed in
         $format = array_merge($format, $pdfFormat);
     } else {
         // PDF Page Format ID passed in
         $format = CRM_Core_BAO_PdfFormat::getById($pdfFormat);
     }
     $paperSize = CRM_Core_BAO_PaperSize::getByName($format['paper_size']);
     $paper_width = self::convertMetric($paperSize['width'], $paperSize['metric'], 'pt');
     $paper_height = self::convertMetric($paperSize['height'], $paperSize['metric'], 'pt');
     // dompdf requires dimensions in points
     $paper_size = array(0, 0, $paper_width, $paper_height);
     $orientation = CRM_Core_BAO_PdfFormat::getValue('orientation', $format);
     $metric = CRM_Core_BAO_PdfFormat::getValue('metric', $format);
     $t = CRM_Core_BAO_PdfFormat::getValue('margin_top', $format);
     $r = CRM_Core_BAO_PdfFormat::getValue('margin_right', $format);
     $b = CRM_Core_BAO_PdfFormat::getValue('margin_bottom', $format);
     $l = CRM_Core_BAO_PdfFormat::getValue('margin_left', $format);
     $config = CRM_Core_Config::singleton();
     $html = "\n<html>\n  <head>\n    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n    <style>@page { margin: {$t}{$metric} {$r}{$metric} {$b}{$metric} {$l}{$metric}; }</style>\n    <style type=\"text/css\">@import url({$config->userFrameworkResourceURL}css/print.css);</style>\n  </head>\n  <body>\n    <div id=\"crm-container\">\n";
     // Strip <html>, <header>, and <body> tags from each page
     $htmlElementstoStrip = array('@<head[^>]*?>.*?</head>@siu', '@<body>@siu', '@</body>@siu', '@<html[^>]*?>@siu', '@</html>@siu', '@<!DOCTYPE[^>]*?>@siu');
     $htmlElementsInstead = array('', '', '', '', '', '');
     foreach ($pages as &$page) {
         $page = preg_replace($htmlElementstoStrip, $htmlElementsInstead, $page);
     }
     // Glue the pages together
     $html .= implode("\n<div style=\"page-break-after: always\"></div>\n", $pages);
     $html .= "\n    </div>\n  </body>\n</html>";
     if ($config->wkhtmltopdfPath) {
         return self::_html2pdf_wkhtmltopdf($paper_size, $orientation, $html, $output, $fileName);
     } else {
         return self::_html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName);
     }
 }
Example #4
0
 /**
  * @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);
     }
     self::printDoc($phpWord, $ext, $fileName);
 }
Example #5
0
 /**
  * Set default values.
  */
 public function setDefaultValues()
 {
     $defaultFormat = CRM_Core_BAO_PdfFormat::getDefaultValues();
     return array('pdf_format_id' => $defaultFormat['id'], 'receipt_update' => 1, 'override_privacy' => 0);
 }
Example #6
0
 /**
  * Retrieve a PDF Page Format for the PDF Letter form.
  */
 public function pdfFormat()
 {
     $formatId = CRM_Utils_Type::escape($_REQUEST['formatId'], 'Integer');
     $pdfFormat = CRM_Core_BAO_PdfFormat::getById($formatId);
     CRM_Utils_JSON::output($pdfFormat);
 }
Example #7
0
 /**
  * Build the form object.
  *
  * @return void
  */
 public function buildQuickForm()
 {
     // For VIEW we only want Done button
     if ($this->_action & CRM_Core_Action::VIEW) {
         // currently, the above action is used solely for previewing default workflow templates
         $cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1');
         $cancelURL = str_replace('&amp;', '&', $cancelURL);
         $this->addButtons(array(array('type' => 'cancel', 'name' => ts('Done'), 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"), 'isDefault' => TRUE)));
     } else {
         parent::buildQuickForm();
     }
     if ($this->_action & CRM_Core_Action::DELETE) {
         return;
     }
     $breadCrumb = array(array('title' => ts('Message Templates'), 'url' => CRM_Utils_System::url('civicrm/admin/messageTemplates', 'action=browse&reset=1')));
     CRM_Utils_System::appendBreadCrumb($breadCrumb);
     $this->applyFilter('__ALL__', 'trim');
     $this->add('text', 'msg_title', ts('Message Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_title'), TRUE);
     $this->add('text', 'msg_subject', ts('Message Subject'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_subject'));
     //get the tokens.
     $tokens = CRM_Core_SelectValues::contactTokens();
     $this->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens));
     // if not a system message use a wysiwyg editor, CRM-5971
     if ($this->_id && CRM_Core_DAO::getFieldValue('CRM_Core_DAO_MessageTemplate', $this->_id, 'workflow_id')) {
         $this->add('textarea', 'msg_html', ts('HTML Message'), "cols=50 rows=6");
     } else {
         $this->addWysiwyg('msg_html', ts('HTML Message'), array('cols' => '80', 'rows' => '8', 'onkeyup' => "return verify(this)"));
     }
     $this->add('textarea', 'msg_text', ts('Text Message'), "cols=50 rows=6");
     $this->add('select', 'pdf_format_id', ts('PDF Page Format'), array('null' => ts('- default -')) + CRM_Core_BAO_PdfFormat::getList(TRUE), FALSE);
     $this->add('checkbox', 'is_active', ts('Enabled?'));
     if ($this->_action & CRM_Core_Action::VIEW) {
         $this->freeze();
         CRM_Utils_System::setTitle(ts('View System Default Message Template'));
     }
 }
Example #8
0
 /**
  * Set default values
  */
 function setDefaultValues()
 {
     $defaultFormat = CRM_Core_BAO_PdfFormat::getDefaultValues();
     return array('pdf_format_id' => $defaultFormat['id']);
 }
Example #9
0
 /**
  * Process the form submission.
  */
 public function postProcess()
 {
     if ($this->_action & CRM_Core_Action::DELETE) {
         // delete PDF Page Format
         CRM_Core_BAO_PdfFormat::del($this->_id);
         CRM_Core_Session::setStatus(ts('Selected PDF Page Format has been deleted.'), ts('Record Deleted'), 'success');
         return;
     }
     $values = $this->controller->exportValues($this->getName());
     $values['is_default'] = isset($values['is_default']);
     $bao = new CRM_Core_BAO_PdfFormat();
     $bao->savePdfFormat($values, $this->_id);
     $status = ts('Your new PDF Page Format titled <strong>%1</strong> has been saved.', array(1 => $values['name']), ts('Saved'), 'success');
     if ($this->_action & CRM_Core_Action::UPDATE) {
         $status = ts('Your PDF Page Format titled <strong>%1</strong> has been updated.', array(1 => $values['name']), ts('Saved'), 'success');
     }
     CRM_Core_Session::setStatus($status);
 }
Example #10
0
 /**
  * Browse all PDF Page Formats.
  *
  * @param null $action
  *
  * @return void
  */
 public function browse($action = NULL)
 {
     // Get list of configured PDF Page Formats
     $pdfFormatList = CRM_Core_BAO_PdfFormat::getList();
     // Add action links to each of the PDF Page Formats
     $action = array_sum(array_keys($this->links()));
     foreach ($pdfFormatList as &$format) {
         $format['action'] = CRM_Core_Action::formLink(self::links(), $action, array('id' => $format['id']), ts('more'), FALSE, 'pdfFormat.manage.action', 'PdfFormat', $format['id']);
     }
     // Order Label Formats by weight
     $returnURL = CRM_Utils_System::url(self::userContext());
     CRM_Core_BAO_PdfFormat::addOrder($pdfFormatList, $returnURL);
     $this->assign('rows', $pdfFormatList);
 }
 /**
  * Function to retrieve a PDF Page Format for the PDF Letter form
  */
 function pdfFormat()
 {
     $formatId = CRM_Utils_Type::escape($_POST['formatId'], 'Integer');
     $pdfFormat = CRM_Core_BAO_PdfFormat::getById($formatId);
     echo json_encode($pdfFormat);
     CRM_Utils_System::civiExit();
 }
 /**
  * part of the post process which prepare and extract information from the template
  *
  * @access protected
  *
  * @return array( $categories, $html_message, $messageToken, $returnProperties )
  */
 protected static function processMessageTemplate(&$form)
 {
     $formValues = $form->controller->exportValues($form->getName());
     // process message template
     if (CRM_Utils_Array::value('saveTemplate', $formValues) || CRM_Utils_Array::value('updateTemplate', $formValues)) {
         $messageTemplate = array('msg_text' => NULL, 'msg_html' => $formValues['html_message'], 'msg_subject' => NULL, 'is_active' => TRUE);
         $messageTemplate['pdf_format_id'] = 'null';
         if (CRM_Utils_Array::value('bind_format', $formValues) && $formValues['format_id'] > 0) {
             $messageTemplate['pdf_format_id'] = $formValues['format_id'];
         }
         if (CRM_Utils_Array::value('saveTemplate', $formValues) && $formValues['saveTemplate']) {
             $messageTemplate['msg_title'] = $formValues['saveTemplateName'];
             CRM_Core_BAO_MessageTemplates::add($messageTemplate);
         }
         if (CRM_Utils_Array::value('updateTemplate', $formValues) && $formValues['template'] && $formValues['updateTemplate']) {
             $messageTemplate['id'] = $formValues['template'];
             unset($messageTemplate['msg_title']);
             CRM_Core_BAO_MessageTemplates::add($messageTemplate);
         }
     } elseif (CRM_Utils_Array::value('template', $formValues) > 0) {
         if (CRM_Utils_Array::value('bind_format', $formValues) && $formValues['format_id'] > 0) {
             $query = "UPDATE civicrm_msg_template SET pdf_format_id = {$formValues['format_id']} WHERE id = {$formValues['template']}";
         } else {
             $query = "UPDATE civicrm_msg_template SET pdf_format_id = NULL WHERE id = {$formValues['template']}";
         }
         CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
     }
     if (CRM_Utils_Array::value('update_format', $formValues)) {
         $bao = new CRM_Core_BAO_PdfFormat();
         $bao->savePdfFormat($formValues, $formValues['format_id']);
     }
     $html = array();
     $tokens = array();
     CRM_Utils_Hook::tokens($tokens);
     $categories = array_keys($tokens);
     $html_message = $formValues['html_message'];
     //time being hack to strip '&nbsp;'
     //from particular letter line, CRM-6798
     self::formatMessage($html_message);
     $messageToken = CRM_Utils_Token::getTokens($html_message);
     $returnProperties = array();
     if (isset($messageToken['contact'])) {
         foreach ($messageToken['contact'] as $key => $value) {
             $returnProperties[$value] = 1;
         }
     }
     return array($formValues, $categories, $html_message, $messageToken, $returnProperties);
 }
 /**
  * Part of the post process which prepare and extract information from the template.
  *
  *
  * @param array $formValues
  *
  * @return array
  *   [$categories, $html_message, $messageToken, $returnProperties]
  */
 public static function processMessageTemplate($formValues)
 {
     $html_message = CRM_Utils_Array::value('html_message', $formValues);
     // process message template
     if (!empty($formValues['saveTemplate']) || !empty($formValues['updateTemplate'])) {
         $messageTemplate = array('msg_text' => NULL, 'msg_html' => $formValues['html_message'], 'msg_subject' => NULL, 'is_active' => TRUE);
         $messageTemplate['pdf_format_id'] = 'null';
         if (!empty($formValues['bind_format']) && $formValues['format_id']) {
             $messageTemplate['pdf_format_id'] = $formValues['format_id'];
         }
         if (!empty($formValues['saveTemplate']) && $formValues['saveTemplate']) {
             $messageTemplate['msg_title'] = $formValues['saveTemplateName'];
             CRM_Core_BAO_MessageTemplate::add($messageTemplate);
         }
         if (!empty($formValues['updateTemplate']) && $formValues['template'] && $formValues['updateTemplate']) {
             $messageTemplate['id'] = $formValues['template'];
             unset($messageTemplate['msg_title']);
             CRM_Core_BAO_MessageTemplate::add($messageTemplate);
         }
     } elseif (CRM_Utils_Array::value('template', $formValues) > 0) {
         if (!empty($formValues['bind_format']) && $formValues['format_id']) {
             $query = "UPDATE civicrm_msg_template SET pdf_format_id = {$formValues['format_id']} WHERE id = {$formValues['template']}";
         } else {
             $query = "UPDATE civicrm_msg_template SET pdf_format_id = NULL WHERE id = {$formValues['template']}";
         }
         CRM_Core_DAO::executeQuery($query);
         $documentInfo = CRM_Core_BAO_File::getEntityFile('civicrm_msg_template', $formValues['template']);
         foreach ((array) $documentInfo as $info) {
             list($html_message, $formValues['document_type']) = CRM_Utils_PDF_Document::docReader($info['fullPath'], $info['mime_type']);
             $formValues['document_file_path'] = $info['fullPath'];
         }
     } elseif (!empty($formValues['document_file'])) {
         list($html_message, $formValues['document_type']) = CRM_Utils_PDF_Document::docReader($formValues['document_file']['name'], $formValues['document_file']['type']);
         $formValues['document_file_path'] = $formValues['document_file']['name'];
     }
     if (!empty($formValues['update_format'])) {
         $bao = new CRM_Core_BAO_PdfFormat();
         $bao->savePdfFormat($formValues, $formValues['format_id']);
     }
     $tokens = array();
     CRM_Utils_Hook::tokens($tokens);
     $categories = array_keys($tokens);
     //time being hack to strip '&nbsp;'
     //from particular letter line, CRM-6798
     self::formatMessage($html_message);
     $messageToken = CRM_Utils_Token::getTokens($html_message);
     $returnProperties = array();
     if (isset($messageToken['contact'])) {
         foreach ($messageToken['contact'] as $key => $value) {
             $returnProperties[$value] = 1;
         }
     }
     return array($formValues, $categories, $html_message, $messageToken, $returnProperties);
 }