From 90a73810f982767ef8875e47c82485a46e6daa48 Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Tue, 31 May 2016 12:49:34 +0530 Subject: [PATCH] Merge to DOCx or ODT template --- CRM/Admin/Form/MessageTemplates.php | 90 ++++++++++++++++++- CRM/Core/BAO/File.php | 12 +-- CRM/Core/BAO/MessageTemplate.php | 22 +++++ CRM/Core/SelectValues.php | 13 +++ CRM/Mailing/Page/AJAX.php | 7 ++ CRM/Utils/File.php | 15 ++++ CRM/Utils/PDF/Document.php | 4 + templates/CRM/Admin/Form/MessageTemplates.tpl | 65 +++++++++++++- templates/CRM/Form/attachmentjs.tpl | 3 + templates/CRM/Mailing/Form/InsertTokens.tpl | 9 ++ 10 files changed, 227 insertions(+), 13 deletions(-) diff --git a/CRM/Admin/Form/MessageTemplates.php b/CRM/Admin/Form/MessageTemplates.php index 4ee63fc444..f4d995eb32 100644 --- a/CRM/Admin/Form/MessageTemplates.php +++ b/CRM/Admin/Form/MessageTemplates.php @@ -38,6 +38,7 @@ 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; public function preProcess() { $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); @@ -82,12 +83,23 @@ class CRM_Admin_Form_MessageTemplates extends CRM_Admin_Form { else { $selectedChild = 'user'; } + + $documentInfo = CRM_Core_BAO_File::getEntityFile('civicrm_msg_template', $this->_id, TRUE); + if (!empty($documentInfo)) { + 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->assign('attachment', $documentInfo); + } + $cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', "selectedChild={$selectedChild}&reset=1"); $cancelURL = str_replace('&', '&', $cancelURL); $this->addButtons( array( array( - 'type' => 'next', + 'type' => 'upload', 'name' => ts('Save'), 'isDefault' => TRUE, ), @@ -124,7 +136,18 @@ class CRM_Admin_Form_MessageTemplates extends CRM_Admin_Form { ); } else { - parent::buildQuickForm(); + $this->addButtons(array( + array( + 'type' => 'upload', + 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'), + 'isDefault' => TRUE, + ), + array( + 'type' => 'cancel', + 'name' => ts('Cancel'), + ), + ) + ); } if ($this->_action & CRM_Core_Action::DELETE) { @@ -144,6 +167,11 @@ class CRM_Admin_Form_MessageTemplates extends CRM_Admin_Form { $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('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"); + $this->add('text', 'msg_subject', ts('Message Subject'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_subject') @@ -188,12 +216,49 @@ class CRM_Admin_Form_MessageTemplates extends CRM_Admin_Form { $this->add('checkbox', 'is_active', ts('Enabled?')); + $this->addFormRule(array(__CLASS__, 'formRule'), $this); + if ($this->_action & CRM_Core_Action::VIEW) { $this->freeze(); CRM_Utils_System::setTitle(ts('View System Default Message Template')); } } + /** + * Global form rule. + * + * @param array $fields + * The input form values. + * @param array $files + * The uploaded files if any. + * + * @return array + * array of errors + */ + public static function formRule($params, $files, $self) { + $errors = array(); + + //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)) { + $fileDAO = new CRM_Core_DAO_File(); + $fileDAO->id = $self->_documentID; + if ($fileDAO->find(TRUE) && + $fileDAO->mime_type != CRM_Utils_Array::value($params['file_type'], CRM_Core_SelectValues::documentApplicationType()) + ) { + $errors['file_id'] = ts('Please upload document'); + } + } + else { + $errors['file_id'] = ts('Please upload document'); + } + + } + + return $errors; + } + /** * Process the form submission. */ @@ -209,12 +274,31 @@ class CRM_Admin_Form_MessageTemplates extends CRM_Admin_Form { $params = array(); // store the submitted values in an array - $params = $this->exportValues(); + $params = $this->controller->exportValues($this->_name); if ($this->_action & CRM_Core_Action::UPDATE) { $params['id'] = $this->_id; } + if (in_array($params['file_type'], array('odt', 'docx'))) { + unset($params['msg_html']); + unset($params['msg_text']); + CRM_Utils_File::formatFile($params, 'file_id'); + } + // delete related file refernces if html/text/pdf template are chosen over document + elseif (!empty($this->_id)) { + $entityFileDAO = new CRM_Core_DAO_EntityFile(); + $entityFileDAO->entity_id = $this->_id; + $entityFileDAO->entity_table = 'civicrm_msg_template'; + if ($entityFileDAO->find(TRUE)) { + $fileDAO = new CRM_Core_DAO_File(); + $fileDAO->id = $entityFileDAO->file_id; + $fileDAO->find(TRUE); + $entityFileDAO->delete(); + $fileDAO->delete(); + } + } + if ($this->_workflow_id) { $params['workflow_id'] = $this->_workflow_id; $params['is_active'] = TRUE; diff --git a/CRM/Core/BAO/File.php b/CRM/Core/BAO/File.php index 9514b2044b..8f1fcf6bdb 100644 --- a/CRM/Core/BAO/File.php +++ b/CRM/Core/BAO/File.php @@ -170,6 +170,8 @@ 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; } /** @@ -532,8 +534,6 @@ AND CEF.entity_id = %2"; $numAttachments = Civi::settings()->get('max_attachments'); - $now = date('YmdHis'); - // setup all attachments for ($i = 1; $i <= $numAttachments; $i++) { $attachName = "attachFile_$i"; @@ -551,17 +551,13 @@ AND CEF.entity_id = %2"; // we dont care if the file is empty or not // CRM-7448 - $fileParams = array( - 'uri' => $formValues[$attachName]['name'], - 'type' => $formValues[$attachName]['type'], - 'location' => $formValues[$attachName]['name'], + $extraParams = array( 'description' => $formValues[$attachDesc], - 'upload_date' => $now, 'tag' => $tagParams, 'attachment_taglist' => CRM_Utils_Array::value($attachFreeTags, $formValues, array()), ); - $params[$attachName] = $fileParams; + CRM_Utils_File::formatFile($formValues, $attachName, $extraParams); } } } diff --git a/CRM/Core/BAO/MessageTemplate.php b/CRM/Core/BAO/MessageTemplate.php index b2ee6bded4..4df5bed666 100644 --- a/CRM/Core/BAO/MessageTemplate.php +++ b/CRM/Core/BAO/MessageTemplate.php @@ -86,10 +86,32 @@ class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate { $hook = empty($params['id']) ? 'create' : 'edit'; CRM_Utils_Hook::pre($hook, 'MessageTemplate', CRM_Utils_Array::value('id', $params), $params); + if (!empty($params['file_id']) && is_array($params['file_id']) && count($params['file_id'])) { + $fileParams = $params['file_id']; + unset($params['file_id']); + } + $messageTemplates = new CRM_Core_DAO_MessageTemplate(); $messageTemplates->copyValues($params); $messageTemplates->save(); + if (!empty($fileParams)) { + $params['file_id'] = $fileParams; + $fileDao = CRM_Core_BAO_File::filePostProcess( + $params['file_id']['location'], + NULL, + 'civicrm_msg_template', + $messageTemplates->id, + NULL, + TRUE, + $params['file_id'], + 'file_id', + $params['file_id']['type'] + ); + + $messageTemplates->save(); + } + CRM_Utils_Hook::post($hook, 'MessageTemplate', $messageTemplates->id, $messageTemplates); return $messageTemplates; } diff --git a/CRM/Core/SelectValues.php b/CRM/Core/SelectValues.php index ed65f23c96..005b13086a 100644 --- a/CRM/Core/SelectValues.php +++ b/CRM/Core/SelectValues.php @@ -1087,4 +1087,17 @@ class CRM_Core_SelectValues { ); } + /** + * Application type of document. + * + * @return array + */ + public static function documentApplicationType() { + return array( + 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'odt' => 'application/vnd.oasis.opendocument.text', + ); + } + + } diff --git a/CRM/Mailing/Page/AJAX.php b/CRM/Mailing/Page/AJAX.php index 855967c01c..799c71953b 100644 --- a/CRM/Mailing/Page/AJAX.php +++ b/CRM/Mailing/Page/AJAX.php @@ -54,6 +54,13 @@ class CRM_Mailing_Page_AJAX { 'pdf_format_id' => $messageTemplate->pdf_format_id, ); + $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['mime_type'] = $info['mime_type']; + } + CRM_Utils_JSON::output($messages); } diff --git a/CRM/Utils/File.php b/CRM/Utils/File.php index 16996e8f24..087a484746 100644 --- a/CRM/Utils/File.php +++ b/CRM/Utils/File.php @@ -766,4 +766,19 @@ HTACCESS; return TRUE; } + public static function formatFile(&$param, $fileName, $extraParams = array()) { + if (empty($param[$fileName])) { + return; + } + + $fileParams = array( + 'uri' => $param[$fileName]['name'], + 'type' => $param[$fileName]['type'], + 'location' => $param[$fileName]['name'], + 'upload_date' => date('YmdHis'), + ) + $extraParams; + + $param[$fileName] = $fileParams; + } + } diff --git a/CRM/Utils/PDF/Document.php b/CRM/Utils/PDF/Document.php index 4b990cf74a..4dba35403f 100644 --- a/CRM/Utils/PDF/Document.php +++ b/CRM/Utils/PDF/Document.php @@ -95,4 +95,8 @@ class CRM_Utils_PDF_Document { return \PhpOffice\PhpWord\Shared\Converter::pointToTwip($point); } + public static function docReader($path) { + return \PhpOffice\PhpWord\IOFactory::load($path); + + } } diff --git a/templates/CRM/Admin/Form/MessageTemplates.tpl b/templates/CRM/Admin/Form/MessageTemplates.tpl index 1b33dc60fe..dd440e15eb 100644 --- a/templates/CRM/Admin/Form/MessageTemplates.tpl +++ b/templates/CRM/Admin/Form/MessageTemplates.tpl @@ -49,6 +49,12 @@
{ts}Descriptive title of message - used for template selection.{/ts} + + {$form.file_type.label} + {$form.file_type.html} +
{ts}Choose the file type{/ts} + + {$form.msg_subject.label} @@ -59,9 +65,26 @@ + {$form.file_id.label} + {$form.file_id.html} + {if $attachment} + {foreach from=$attachment key=attKey item=attVal} +
+ {$attVal.cleanName} + {if $attVal.description} - {$attVal.description}{/if} + {if $attVal.deleteURLArgs} + + {/if} + {include file="CRM/Form/attachmentjs.tpl" context='MessageTemplate'} + {/foreach} + {/if} +
{ts}Upload the document file{/ts} + + + -
+
{ts}HTML Format{/ts} {help id="id-message-text" file="CRM/Contact/Form/Task/Email.hlp"} @@ -79,7 +102,7 @@
-
+
{ts}Plain-Text Format{/ts}
@@ -124,3 +147,41 @@
{include file="CRM/Mailing/Form/InsertTokens.tpl"} + +{literal} + +{/literal} diff --git a/templates/CRM/Form/attachmentjs.tpl b/templates/CRM/Form/attachmentjs.tpl index 19823c2637..378aaf1919 100644 --- a/templates/CRM/Form/attachmentjs.tpl +++ b/templates/CRM/Form/attachmentjs.tpl @@ -15,6 +15,9 @@ request.done(function() { $el.trigger('crmPopupFormSuccess'); $row.remove(); + {/literal}{if $context EQ 'MessageTemplate'}{literal} + $('#file_id').show(); + {/literal}{/if}{literal} }); }); e.preventDefault(); diff --git a/templates/CRM/Mailing/Form/InsertTokens.tpl b/templates/CRM/Mailing/Form/InsertTokens.tpl index 64b5000b88..75a18fc419 100644 --- a/templates/CRM/Mailing/Form/InsertTokens.tpl +++ b/templates/CRM/Mailing/Form/InsertTokens.tpl @@ -131,10 +131,19 @@ function selectValue( val, prefix) { 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); + } + 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(); -- 2.25.1