3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This class generates form components for Message templates
36 * used by membership, contributions, event registrations, etc.
38 class CRM_Admin_Form_MessageTemplates
extends CRM_Admin_Form
{
39 // which (and whether) mailing workflow this template belongs to
40 protected $_workflow_id = NULL;
42 // Is document file is already loaded as default value?
43 protected $_is_document = FALSE;
45 public function preProcess() {
46 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
47 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String',
50 $this->assign('action', $this->_action
);
52 $this->_BAOName
= 'CRM_Core_BAO_MessageTemplate';
53 $this->set('BAOName', $this->_BAOName
);
58 * Set default values for the form.
60 * The default values are retrieved from the database.
62 public function setDefaultValues() {
63 $defaults = $this->_values
;
65 if (empty($defaults['pdf_format_id'])) {
66 $defaults['pdf_format_id'] = 'null';
68 if (empty($defaults['file_type'])) {
69 $defaults['file_type'] = 0;
72 if ($this->_action
& CRM_Core_Action
::ADD
) {
73 $defaults['is_active'] = 1;
76 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
77 $documentInfo = CRM_Core_BAO_File
::getEntityFile('civicrm_msg_template', $this->_id
, TRUE);
78 if (!empty($documentInfo)) {
79 $defaults['file_type'] = 1;
80 $this->_is_document
= TRUE;
81 $this->assign('attachment', $documentInfo);
89 * Build the form object.
91 public function buildQuickForm() {
92 // For VIEW we only want Done button
93 if ($this->_action
& CRM_Core_Action
::VIEW
) {
94 // currently, the above action is used solely for previewing default workflow templates
95 $cancelURL = CRM_Utils_System
::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1');
96 $cancelURL = str_replace('&', '&', $cancelURL);
100 'name' => ts('Done'),
101 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
107 $this->_workflow_id
= CRM_Utils_Array
::value('workflow_id', $this->_values
);
108 $this->checkUserPermission($this->_workflow_id
);
109 $this->assign('workflow_id', $this->_workflow_id
);
111 if ($this->_workflow_id
) {
112 $selectedChild = 'workflow';
115 $selectedChild = 'user';
118 $cancelURL = CRM_Utils_System
::url('civicrm/admin/messageTemplates', "selectedChild={$selectedChild}&reset=1");
119 $cancelURL = str_replace('&', '&', $cancelURL);
122 'name' => $this->_action
& CRM_Core_Action
::DELETE ?
ts('Delete') : ts('Save'),
125 if (!($this->_action
& CRM_Core_Action
::DELETE
)) {
128 'name' => ts('Save and Done'),
134 'name' => ts('Cancel'),
135 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
137 $this->addButtons($buttons);
140 if ($this->_action
& CRM_Core_Action
::DELETE
) {
141 $this->assign('msg_title', $this->_values
['msg_title']);
147 'title' => ts('Message Templates'),
148 'url' => CRM_Utils_System
::url('civicrm/admin/messageTemplates', 'action=browse&reset=1'),
151 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
153 $this->applyFilter('__ALL__', 'trim');
154 $this->add('text', 'msg_title', ts('Message Title'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_title'), TRUE);
156 $options = [ts('Compose On-screen'), ts('Upload Document')];
157 $element = $this->addRadio('file_type', ts('Source'), $options);
162 $this->addElement('file', "file_id", ts('Upload Document'), 'size=30 maxlength=255');
163 $this->addUploadElement("file_id");
165 $this->add('text', 'msg_subject',
166 ts('Message Subject'),
167 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_subject')
171 $tokens = CRM_Core_SelectValues
::contactTokens();
173 $this->assign('tokens', CRM_Utils_Token
::formatTokensForDisplay($tokens));
175 // if not a system message use a wysiwyg editor, CRM-5971
177 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_MessageTemplate',
182 $this->add('textarea', 'msg_html', ts('HTML Message'),
187 $this->add('wysiwyg', 'msg_html', ts('HTML Message'),
191 'onkeyup' => "return verify(this)",
192 'preset' => 'civimail',
197 $this->add('textarea', 'msg_text', ts('Text Message'),
201 $this->add('select', 'pdf_format_id', ts('PDF Page Format'),
203 'null' => ts('- default -'),
204 ] + CRM_Core_BAO_PdfFormat
::getList(TRUE), FALSE
207 $this->add('checkbox', 'is_active', ts('Enabled?'));
208 $this->addFormRule([__CLASS__
, 'formRule'], $this);
210 if ($this->_action
& CRM_Core_Action
::VIEW
) {
212 CRM_Utils_System
::setTitle(ts('View System Default Message Template'));
217 * Restrict users access based on permission
219 * @param int $workflowId
221 private function checkUserPermission($workflowId) {
222 if (isset($workflowId)) {
223 $canView = CRM_Core_Permission
::check('edit system workflow message templates');
226 $canView = CRM_Core_Permission
::check('edit user-driven message templates');
229 if (!$canView && !CRM_Core_Permission
::check('edit message templates')) {
230 CRM_Core_Session
::setStatus(ts('You do not have permission to view requested page.'), ts('Access Denied'));
231 $url = CRM_Utils_System
::url('civicrm/admin/messageTemplates', "reset=1");
232 CRM_Utils_System
::redirect($url);
239 * @param array $params
240 * The input form values.
241 * @param array $files
242 * The uploaded files if any.
248 public static function formRule($params, $files, $self) {
249 // If user uploads non-document file other than odt/docx
250 if (!empty($files['file_id']['tmp_name']) &&
251 array_search($files['file_id']['type'], CRM_Core_SelectValues
::documentApplicationType()) == NULL
253 $errors['file_id'] = ts('Invalid document file format');
255 // If default is not set and no document file is uploaded
256 elseif (empty($files['file_id']['tmp_name']) && !empty($params['file_type']) && !$self->_is_document
) {
257 //On edit page of docx/odt message template if user changes file type but forgot to upload document
258 $errors['file_id'] = ts('Please upload document');
261 return empty($errors) ?
TRUE : $errors;
265 * Process the form submission.
267 public function postProcess() {
268 if ($this->_action
& CRM_Core_Action
::DELETE
) {
269 CRM_Core_BAO_MessageTemplate
::del($this->_id
);
271 elseif ($this->_action
& CRM_Core_Action
::VIEW
) {
272 // currently, the above action is used solely for previewing default workflow templates
273 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
276 // store the submitted values in an array
277 $params = $this->controller
->exportValues();
279 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
280 $params['id'] = $this->_id
;
283 if (!empty($params['file_type'])) {
284 unset($params['msg_html']);
285 unset($params['msg_text']);
286 CRM_Utils_File
::formatFile($params, 'file_id');
288 // delete related file references if html/text/pdf template are chosen over document
289 elseif (!empty($this->_id
)) {
290 $entityFileDAO = new CRM_Core_DAO_EntityFile();
291 $entityFileDAO->entity_id
= $this->_id
;
292 $entityFileDAO->entity_table
= 'civicrm_msg_template';
293 if ($entityFileDAO->find(TRUE)) {
294 $fileDAO = new CRM_Core_DAO_File();
295 $fileDAO->id
= $entityFileDAO->file_id
;
296 $fileDAO->find(TRUE);
297 $entityFileDAO->delete();
302 $this->_workflow_id
= CRM_Utils_Array
::value('workflow_id', $this->_values
);
303 if ($this->_workflow_id
) {
304 $params['workflow_id'] = $this->_workflow_id
;
305 $params['is_active'] = TRUE;
308 $messageTemplate = CRM_Core_BAO_MessageTemplate
::add($params);
309 CRM_Core_Session
::setStatus(ts('The Message Template \'%1\' has been saved.', [1 => $messageTemplate->msg_title
]), ts('Saved'), 'success');
311 if (isset($this->_submitValues
['_qf_MessageTemplates_upload'])) {
312 // Save button was pressed
313 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/messageTemplates/add', "action=update&id={$messageTemplate->id}&reset=1"));
315 // Save and done button was pressed
316 if ($this->_workflow_id
) {
317 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
319 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/messageTemplates', 'selectedChild=user&reset=1'));