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);
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,
),
);
}
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) {
$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')
$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.
*/
$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;
// 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;
}
/**
$numAttachments = Civi::settings()->get('max_attachments');
- $now = date('YmdHis');
-
// setup all attachments
for ($i = 1; $i <= $numAttachments; $i++) {
$attachName = "attachFile_$i";
// 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);
}
}
}
$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;
}
);
}
+ /**
+ * 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',
+ );
+ }
+
+
}
'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);
}
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;
+ }
+
}
return \PhpOffice\PhpWord\Shared\Converter::pointToTwip($point);
}
+ public static function docReader($path) {
+ return \PhpOffice\PhpWord\IOFactory::load($path);
+
+ }
}
<br /><span class="description html-adjust">{ts}Descriptive title of message - used for template selection.{/ts}</span>
</td>
</tr>
+ <tr>
+ <td class="label-left">{$form.file_type.label}</td>
+ <td>{$form.file_type.html}
+ <br /><span class="description html-adjust">{ts}Choose the file type{/ts}</span>
+ </td>
+ </tr>
<tr>
<td class="label-left">{$form.msg_subject.label}</td>
<td>
</td>
</tr>
<tr>
+ <td class="label-left">{$form.file_id.label}</td>
+ <td>{$form.file_id.html}
+ {if $attachment}
+ {foreach from=$attachment key=attKey item=attVal}
+ <div class="crm-attachment-wrapper crm-entity" id="file_{$attVal.fileID}">
+ <strong><a class="crm-attachment" href="{$attVal.url}">{$attVal.cleanName}</a></strong>
+ {if $attVal.description} - {$attVal.description}{/if}
+ {if $attVal.deleteURLArgs}
+ <a href="#" class="crm-hover-button delete-attachment" data-mimetype="{$attVal.mime_type}" data-filename="{$attVal.cleanName}" data-args="{$attVal.deleteURLArgs}" title="{ts}Delete File{/ts}"><span class="icon delete-icon"></span></a>
+ {/if}
+ {include file="CRM/Form/attachmentjs.tpl" context='MessageTemplate'}
+ {/foreach}
+ {/if}
+ <br /><span class="description html-adjust">{ts}Upload the document file{/ts}</span>
+ </td>
+ </tr>
+ <tr>
</table>
- <div class="crm-accordion-wrapper crm-html_email-accordion ">
+ <div id="msg_html" class="crm-accordion-wrapper crm-html_email-accordion ">
<div class="crm-accordion-header">
{ts}HTML Format{/ts}
{help id="id-message-text" file="CRM/Contact/Form/Task/Email.hlp"}
</div><!-- /.crm-accordion-body -->
</div><!-- /.crm-accordion-wrapper -->
- <div class="crm-accordion-wrapper crm-plaint_text_email-accordion ">
+ <div id="msg_text" class="crm-accordion-wrapper crm-plaint_text_email-accordion ">
<div class="crm-accordion-header">
{ts}Plain-Text Format{/ts}
</div><!-- /.crm-accordion-header -->
</div>
</div> <!-- end of crm-form-block -->
{include file="CRM/Mailing/Form/InsertTokens.tpl"}
+
+{literal}
+ <script type='text/javascript'>
+ CRM.$(function($) {
+ var mimeType = null;
+ // if default file is selected then hide the file upload field
+ if ($('a.delete-attachment').length) {
+ $('#file_id').hide();
+ mimeType = $('a.delete-attachment').data('mimetype');
+ }
+
+ $('#file_type').on('change', function(){
+ toggleByFileType(this.value);
+ });
+ toggleByFileType($('#file_type').val());
+
+ function toggleByFileType(type) {
+ var show = (type == 'odt' || type == 'docx') ? false : true;
+ $("#msg_html").toggle(show);
+ $("#msg_text").toggle(show);
+ $("#file_id").parent().parent().toggle(!show);
+
+ // auto file type validation
+ if (!show) {
+ var validType = (type == 'odt') ? 'application/vnd.oasis.opendocument.text' : 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
+ $("#file_id").attr('accept', validType);
+ }
+
+ // when you change file type other than the type of default uploaded document
+ if (mimeType) {
+ var hide = (mimeType != type) ? true : false;
+ $("#file_id").toggle(hide);
+ $('.crm-attachment-wrapper').toggle(!hide);
+ }
+ }
+ });
+ </script>
+{/literal}
request.done(function() {
$el.trigger('crmPopupFormSuccess');
$row.remove();
+ {/literal}{if $context EQ 'MessageTemplate'}{literal}
+ $('#file_id').show();
+ {/literal}{/if}{literal}
});
});
e.preventDefault();
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();