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);
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);
}
$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");
/**
* 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
//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())
) {
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(
$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();
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);
// 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;
}
/**
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',
'file_id',
$params['file_id']['type']
);
-
- $messageTemplates->save();
}
CRM_Utils_Hook::post($hook, 'MessageTemplate', $messageTemplates->id, $messageTemplates);
$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'];
}
{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";
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('</w:r></w:p></w:tc><w:tc>', " ", $content);
+ $content = str_replace('</w:r></w:p>', "\r\n", $content);
+ $striped_content = strip_tags($content);
+
+ return nl2br($striped_content);
}
+
}
</div>
</div>
+<div class="crm-accordion-wrapper crm-document-accordion ">
+ <div class="crm-accordion-header">
+ {$form.html_message.label}
+ </div><!-- /.crm-accordion-header -->
+ <div class="crm-accordion-body">
+ <div id='document'></div>
+ </div><!-- /.crm-accordion-body -->
+</div><!-- /.crm-accordion-wrapper -->
+
<div class="crm-accordion-wrapper crm-html_email-accordion ">
<div class="crm-accordion-header">
{$form.html_message.label}
<script type="text/javascript">
CRM.$(function($) {
var $form = $('form.{/literal}{$form.formClass}{literal}');
+
+ {/literal}{if $form.formName eq 'PDF'}{literal}
+ cj('.crm-document-accordion').hide();
+ {/literal}{/if}{literal}
+
+
$('#format_id', $form).on('change', function() {
selectFormat($(this).val());
});
document.getElementsByName(prefix + "updateTemplate")[0].checked = false;
showSaveUpdateChkBox(prefix);
if ( !val ) {
+ if (document.getElementById("subject").length) {
+ document.getElementById("subject").value ="";
+ }
if (document.getElementById("subject").length) {
document.getElementById("subject").value ="";
}
document.getElementById("text_message").value ="";
}
}
+ else {
+ cj('.crm-html_email-accordion').show();
+ cj('.crm-document-accordion').hide();
+ }
+
CRM.wysiwyg.setVal('#' + html_message, '');
if ( isPDF ) {
showBindFormatChkBox();
var dataUrl = {/literal}"{crmURL p='civicrm/ajax/template' h=0 }"{literal};
cj.post( dataUrl, {tid: val}, function( data ) {
- if (data.document_path) {
- cj("#document").attr('data', data.document_path);
- cj("#document").attr('type', data.mime_type);
- cj("#document embed").attr('type', data.mime_type);
- cj('#document embed').attr('src', data.document_path);
- console.log(data.document_path);
+ var hide = (data.document_path && isPDF) ? false : true;
+ cj('.crm-html_email-accordion').toggle(hide);
+ cj('.crm-document-accordion').toggle(!hide);
+
+ if (!hide) {
+ cj("#document_type").val( data.file_type );
+ cj("#subject").val( data.subject );
+ document.getElementById("document").innerHTML='<object type="text/html" data='+ data.document_path +' width="680" height="400" style="background:white;"></object>';
+ return;
}
if ( !isPDF ) {
if (prefix == "SMS") {
text_message = "sms_text_message";
}
-
if ( data.msg_text ) {
cj("#"+text_message).val( data.msg_text );
cj("div.text").show();
(@option_group_id_sfe, 'ppt', 11, 'ppt', NULL, 0, 0, 11, NULL, 0, 0, 1, NULL, NULL),
(@option_group_id_sfe, 'docx', 12, 'docx', NULL, 0, 0, 12, NULL, 0, 0, 1, NULL, NULL),
(@option_group_id_sfe, 'xlsx', 13, 'xlsx', NULL, 0, 0, 13, NULL, 0, 0, 1, NULL, NULL),
+ (@option_group_id_sfe, 'odt', 14, 'odt', NULL, 0, 0, 14, NULL, 0, 0, 1, NULL, NULL),
(@option_group_id_we, '{ts escape="sql"}Textarea{/ts}', 1, 'Textarea', NULL, 0, NULL, 1, NULL, 0, 1, 1, NULL, NULL),
(@option_group_id_we, 'CKEditor', 2, 'CKEditor', NULL, 0, NULL, 2, NULL, 0, 1, 1, NULL, NULL),