From: deb.monish Date: Wed, 1 Jun 2016 06:21:29 +0000 (+0530) Subject: additional fixes X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=04a762316c9d5de23d910ee35c22728c19d0dc66;p=civicrm-core.git additional fixes --- diff --git a/CRM/Admin/Form/MessageTemplates.php b/CRM/Admin/Form/MessageTemplates.php index f4d995eb32..e5bf6c7902 100644 --- a/CRM/Admin/Form/MessageTemplates.php +++ b/CRM/Admin/Form/MessageTemplates.php @@ -38,7 +38,9 @@ class CRM_Admin_Form_MessageTemplates extends CRM_Admin_Form { // which (and whether) mailing workflow this template belongs to protected $_workflow_id = NULL; - protected $_documentID = NULL; + + // document ID set during loading default values and later used in formRule + protected $_document_id = NULL; public function preProcess() { $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); @@ -89,7 +91,7 @@ class CRM_Admin_Form_MessageTemplates extends CRM_Admin_Form { foreach ($documentInfo as &$info) { $defaults['file_type'] = array_search($info['mime_type'], CRM_Core_SelectValues::documentApplicationType()); $info['mime_type'] = $defaults['file_type']; - $this->_documentID = $info['fileID']; + $this->_document_id = $info['fileID']; } $this->assign('attachment', $documentInfo); } @@ -168,7 +170,6 @@ class CRM_Admin_Form_MessageTemplates extends CRM_Admin_Form { $this->add('text', 'msg_title', ts('Message Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_title'), TRUE); $this->add('select', 'file_type', ts('File Type'), CRM_Core_SelectValues::documentFormat()); - $this->addElement('file', "file_id", ts('Upload Document'), 'size=30 maxlength=60'); $this->addUploadElement("file_id"); @@ -227,10 +228,11 @@ class CRM_Admin_Form_MessageTemplates extends CRM_Admin_Form { /** * Global form rule. * - * @param array $fields + * @param array $params * The input form values. * @param array $files * The uploaded files if any. + * @param array $self * * @return array * array of errors @@ -241,9 +243,9 @@ class CRM_Admin_Form_MessageTemplates extends CRM_Admin_Form { //empty file upload validation for odt/docx template if (empty($files['file_id']['tmp_name']) && in_array($params['file_type'], array('odt', 'docx'))) { //On edit page of docx/odt message template if user changes file type but forgot to upload document - if (!empty($self->_documentID)) { + if (!empty($self->_document_id)) { $fileDAO = new CRM_Core_DAO_File(); - $fileDAO->id = $self->_documentID; + $fileDAO->id = $self->_document_id; if ($fileDAO->find(TRUE) && $fileDAO->mime_type != CRM_Utils_Array::value($params['file_type'], CRM_Core_SelectValues::documentApplicationType()) ) { diff --git a/CRM/Contact/Form/Task/PDFLetterCommon.php b/CRM/Contact/Form/Task/PDFLetterCommon.php index fecc8fa6f1..667b4f3aa3 100644 --- a/CRM/Contact/Form/Task/PDFLetterCommon.php +++ b/CRM/Contact/Form/Task/PDFLetterCommon.php @@ -252,6 +252,8 @@ class CRM_Contact_Form_Task_PDFLetterCommon { static protected function processMessageTemplate(&$form) { $formValues = $form->controller->exportValues($form->getName()); + $html_message = $formValues['html_message']; + // process message template if (!empty($formValues['saveTemplate']) || !empty($formValues['updateTemplate'])) { $messageTemplate = array( @@ -285,6 +287,12 @@ class CRM_Contact_Form_Task_PDFLetterCommon { $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) { + $docType = array_search($info['mime_type'], CRM_Core_SelectValues::documentApplicationType()); + $html_message = CRM_Utils_PDF_Document::docReader($info['fullPath'], $docType, TRUE); + } } if (!empty($formValues['update_format'])) { $bao = new CRM_Core_BAO_PdfFormat(); @@ -297,8 +305,6 @@ class CRM_Contact_Form_Task_PDFLetterCommon { CRM_Utils_Hook::tokens($tokens); $categories = array_keys($tokens); - $html_message = $formValues['html_message']; - //time being hack to strip ' ' //from particular letter line, CRM-6798 self::formatMessage($html_message); diff --git a/CRM/Core/BAO/File.php b/CRM/Core/BAO/File.php index 8f1fcf6bdb..b6deb13ace 100644 --- a/CRM/Core/BAO/File.php +++ b/CRM/Core/BAO/File.php @@ -170,8 +170,6 @@ class CRM_Core_BAO_File extends CRM_Core_DAO_File { // lets call the post hook here so attachments code can do the right stuff CRM_Utils_Hook::post($op, 'File', $fileDAO->id, $fileDAO); - - return $fileDAO; } /** diff --git a/CRM/Core/BAO/MessageTemplate.php b/CRM/Core/BAO/MessageTemplate.php index 4df5bed666..45ebaeabf9 100644 --- a/CRM/Core/BAO/MessageTemplate.php +++ b/CRM/Core/BAO/MessageTemplate.php @@ -97,7 +97,7 @@ class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate { if (!empty($fileParams)) { $params['file_id'] = $fileParams; - $fileDao = CRM_Core_BAO_File::filePostProcess( + CRM_Core_BAO_File::filePostProcess( $params['file_id']['location'], NULL, 'civicrm_msg_template', @@ -108,8 +108,6 @@ class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate { 'file_id', $params['file_id']['type'] ); - - $messageTemplates->save(); } CRM_Utils_Hook::post($hook, 'MessageTemplate', $messageTemplates->id, $messageTemplates); diff --git a/CRM/Core/SelectValues.php b/CRM/Core/SelectValues.php index 005b13086a..af7f6510b8 100644 --- a/CRM/Core/SelectValues.php +++ b/CRM/Core/SelectValues.php @@ -1099,5 +1099,4 @@ class CRM_Core_SelectValues { ); } - } diff --git a/CRM/Mailing/Page/AJAX.php b/CRM/Mailing/Page/AJAX.php index 799c71953b..a02aadb7b6 100644 --- a/CRM/Mailing/Page/AJAX.php +++ b/CRM/Mailing/Page/AJAX.php @@ -57,7 +57,7 @@ class CRM_Mailing_Page_AJAX { $documentInfo = CRM_Core_BAO_File::getEntityFile('civicrm_msg_template', $templateId); foreach ((array) $documentInfo as $info) { $messages['file_type'] = array_search($info['mime_type'], CRM_Core_SelectValues::documentApplicationType()); - $messages['document_path'] = $info['fullPath']; + $messages['document_path'] = CRM_Utils_PDF_Document::docReader($info['fullPath'], $messages['file_type']); $messages['mime_type'] = $info['mime_type']; } diff --git a/CRM/Upgrade/Incremental/sql/4.7.9.mysql.tpl b/CRM/Upgrade/Incremental/sql/4.7.9.mysql.tpl index 3be47b33cb..c2e5cfd4d0 100644 --- a/CRM/Upgrade/Incremental/sql/4.7.9.mysql.tpl +++ b/CRM/Upgrade/Incremental/sql/4.7.9.mysql.tpl @@ -7,6 +7,15 @@ UPDATE civicrm_option_value SET {localize field="description"}description = '{ts escape="sql"}Export letters and other printable documents.{/ts}'{/localize} WHERE name = 'Print PDF Letter' AND option_group_id = @option_group_id_act; +-- CRM-17608 Merge to DOCx or ODT template +SELECT @option_group_id_ext := max(id) from civicrm_option_group where name = 'safe_file_extension'; +SELECT @option_group_id_ext_wt := MAX(weight) FROM civicrm_option_value WHERE option_group_id = @option_group_id_ext; +SELECT @option_group_id_ext_val := MAX(CAST( `value` AS UNSIGNED )) FROM civicrm_option_value WHERE option_group_id = @option_group_id_ext; +INSERT INTO + `civicrm_option_value` (`option_group_id`, {localize field='label'}label{/localize}, `value`, `name`, `grouping`, `filter`, `is_default`, `weight`, `is_optgroup`, `is_reserved`, `is_active`) +VALUES +(@option_group_id_ext, {localize}'{ts escape="sql"}odt{/ts}'{/localize}, @option_group_id_ext_val+1, 'odt', NULL, 0, 0, @option_group_id_ext_wt+1, 0, 1, 1); + -- CRM-18699 Fix Wake Island misspelling, was Wake Ialand UPDATE civicrm_state_province SET name="Wake Island" WHERE name="Wake Ialand"; diff --git a/CRM/Utils/PDF/Document.php b/CRM/Utils/PDF/Document.php index 4dba35403f..3fc6d9c701 100644 --- a/CRM/Utils/PDF/Document.php +++ b/CRM/Utils/PDF/Document.php @@ -95,8 +95,63 @@ class CRM_Utils_PDF_Document { return \PhpOffice\PhpWord\Shared\Converter::pointToTwip($point); } - public static function docReader($path) { - return \PhpOffice\PhpWord\IOFactory::load($path); + /** + * @param array $path docx/odt file path + * @param string $type File type + * @param bool $returnContent extract content of docx/odt file as a text, later used for token replacement + * + * @return string + * Return filepath of created html copy of document OR extracted content of document + */ + public static function docReader($path, $type, $returnContent = FALSE) { + if ($returnContent) { + return self::doc2Text($path, $type); + } + + $newpath = str_replace('.' . $type, '.html', $path); + $fileType = ($type == 'docx') ? 'Word2007' : 'ODText'; + if (!file_exists($newpath)) { + $phpWord = \PhpOffice\PhpWord\IOFactory::load($path, $fileType); + $phpWord->save($newpath, 'HTML'); + } + + $relPath = str_replace('/Users/monish/www/civicrm-master/', CRM_Utils_System::baseURL(), $newpath); + return $relPath; + } + + /** + * Extract content of docx/odt file as text and later used for token replacement + * @param string $filePath Document file path + * @param string $type File type of document + * + * @return string + * File content of document as text + */ + public static function doc2Text($filePath, $type) { + $content = ''; + + $zip = zip_open($filePath); + $dataFile = ($type == 'docx') ? "word/document.xml" : "content.xml"; + if (!$zip || is_numeric($zip)) { + return $content; + } + + while ($zip_entry = zip_read($zip)) { + if (zip_entry_open($zip, $zip_entry) == FALSE || zip_entry_name($zip_entry) != $dataFile) { + continue; + } + $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); + zip_entry_close($zip_entry); + } + + zip_close($zip); + + $content = str_replace('', " ", $content); + $content = str_replace('', "\r\n", $content); + $striped_content = strip_tags($content); + + return nl2br($striped_content); } + } diff --git a/templates/CRM/Contact/Form/Task/PDFLetterCommon.tpl b/templates/CRM/Contact/Form/Task/PDFLetterCommon.tpl index 97e033548c..f7560b2fe5 100644 --- a/templates/CRM/Contact/Form/Task/PDFLetterCommon.tpl +++ b/templates/CRM/Contact/Form/Task/PDFLetterCommon.tpl @@ -85,6 +85,15 @@ +
+
+ {$form.html_message.label} +
+
+
+
+
+
{$form.html_message.label} @@ -129,6 +138,12 @@