3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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-2020
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
{
40 * which (and whether) mailing workflow this template belongs to
43 protected $_workflow_id = NULL;
46 * Is document file is already loaded as default value?
49 protected $_is_document = FALSE;
51 public function preProcess() {
52 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
53 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String',
56 $this->assign('action', $this->_action
);
58 $this->_BAOName
= 'CRM_Core_BAO_MessageTemplate';
59 $this->set('BAOName', $this->_BAOName
);
64 * Set default values for the form.
66 * The default values are retrieved from the database.
68 public function setDefaultValues() {
69 $defaults = $this->_values
;
71 if (empty($defaults['pdf_format_id'])) {
72 $defaults['pdf_format_id'] = 'null';
74 if (empty($defaults['file_type'])) {
75 $defaults['file_type'] = 0;
78 if ($this->_action
& CRM_Core_Action
::ADD
) {
79 $defaults['is_active'] = 1;
82 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
83 $documentInfo = CRM_Core_BAO_File
::getEntityFile('civicrm_msg_template', $this->_id
, TRUE);
84 if (!empty($documentInfo)) {
85 $defaults['file_type'] = 1;
86 $this->_is_document
= TRUE;
87 $this->assign('attachment', $documentInfo);
95 * Build the form object.
97 public function buildQuickForm() {
98 // For VIEW we only want Done button
99 if ($this->_action
& CRM_Core_Action
::VIEW
) {
100 // currently, the above action is used solely for previewing default workflow templates
101 $cancelURL = CRM_Utils_System
::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1');
102 $cancelURL = str_replace('&', '&', $cancelURL);
106 'name' => ts('Done'),
107 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
113 $this->_workflow_id
= CRM_Utils_Array
::value('workflow_id', $this->_values
);
114 $this->checkUserPermission($this->_workflow_id
);
115 $this->assign('workflow_id', $this->_workflow_id
);
117 if ($this->_workflow_id
) {
118 $selectedChild = 'workflow';
121 $selectedChild = 'user';
124 $cancelURL = CRM_Utils_System
::url('civicrm/admin/messageTemplates', "selectedChild={$selectedChild}&reset=1");
125 $cancelURL = str_replace('&', '&', $cancelURL);
128 'name' => $this->_action
& CRM_Core_Action
::DELETE ?
ts('Delete') : ts('Save'),
131 if (!($this->_action
& CRM_Core_Action
::DELETE
)) {
134 'name' => ts('Save and Done'),
140 'name' => ts('Cancel'),
141 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
143 $this->addButtons($buttons);
146 if ($this->_action
& CRM_Core_Action
::DELETE
) {
147 $this->assign('msg_title', $this->_values
['msg_title']);
153 'title' => ts('Message Templates'),
154 'url' => CRM_Utils_System
::url('civicrm/admin/messageTemplates', 'action=browse&reset=1'),
157 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
159 $this->applyFilter('__ALL__', 'trim');
160 $this->add('text', 'msg_title', ts('Message Title'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_title'), TRUE);
162 $options = [ts('Compose On-screen'), ts('Upload Document')];
163 $element = $this->addRadio('file_type', ts('Source'), $options);
168 $this->addElement('file', "file_id", ts('Upload Document'), 'size=30 maxlength=255');
169 $this->addUploadElement("file_id");
171 $this->add('text', 'msg_subject',
172 ts('Message Subject'),
173 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_subject')
177 $tokens = CRM_Core_SelectValues
::contactTokens();
179 $this->assign('tokens', CRM_Utils_Token
::formatTokensForDisplay($tokens));
181 // if not a system message use a wysiwyg editor, CRM-5971
183 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_MessageTemplate',
188 $this->add('textarea', 'msg_html', ts('HTML Message'),
193 $this->add('wysiwyg', 'msg_html', ts('HTML Message'),
197 'onkeyup' => "return verify(this)",
198 'preset' => 'civimail',
203 $this->add('textarea', 'msg_text', ts('Text Message'),
207 $this->add('select', 'pdf_format_id', ts('PDF Page Format'),
209 'null' => ts('- default -'),
210 ] + CRM_Core_BAO_PdfFormat
::getList(TRUE), FALSE
213 $this->add('checkbox', 'is_active', ts('Enabled?'));
214 $this->addFormRule([__CLASS__
, 'formRule'], $this);
216 if ($this->_action
& CRM_Core_Action
::VIEW
) {
218 CRM_Utils_System
::setTitle(ts('View System Default Message Template'));
223 * Restrict users access based on permission
225 * @param int $workflowId
227 private function checkUserPermission($workflowId) {
228 if (isset($workflowId)) {
229 $canView = CRM_Core_Permission
::check('edit system workflow message templates');
232 $canView = CRM_Core_Permission
::check('edit user-driven message templates');
235 if (!$canView && !CRM_Core_Permission
::check('edit message templates')) {
236 CRM_Core_Session
::setStatus(ts('You do not have permission to view requested page.'), ts('Access Denied'));
237 $url = CRM_Utils_System
::url('civicrm/admin/messageTemplates', "reset=1");
238 CRM_Utils_System
::redirect($url);
245 * @param array $params
246 * The input form values.
247 * @param array $files
248 * The uploaded files if any.
254 public static function formRule($params, $files, $self) {
255 // If user uploads non-document file other than odt/docx
256 if (!empty($files['file_id']['tmp_name']) &&
257 array_search($files['file_id']['type'], CRM_Core_SelectValues
::documentApplicationType()) == NULL
259 $errors['file_id'] = ts('Invalid document file format');
261 // If default is not set and no document file is uploaded
262 elseif (empty($files['file_id']['tmp_name']) && !empty($params['file_type']) && !$self->_is_document
) {
263 //On edit page of docx/odt message template if user changes file type but forgot to upload document
264 $errors['file_id'] = ts('Please upload document');
267 return empty($errors) ?
TRUE : $errors;
271 * Process the form submission.
273 public function postProcess() {
274 if ($this->_action
& CRM_Core_Action
::DELETE
) {
275 CRM_Core_BAO_MessageTemplate
::del($this->_id
);
277 elseif ($this->_action
& CRM_Core_Action
::VIEW
) {
278 // currently, the above action is used solely for previewing default workflow templates
279 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
282 // store the submitted values in an array
283 $params = $this->controller
->exportValues();
285 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
286 $params['id'] = $this->_id
;
289 if (!empty($params['file_type'])) {
290 unset($params['msg_html']);
291 unset($params['msg_text']);
292 CRM_Utils_File
::formatFile($params, 'file_id');
294 // delete related file references if html/text/pdf template are chosen over document
295 elseif (!empty($this->_id
)) {
296 $entityFileDAO = new CRM_Core_DAO_EntityFile();
297 $entityFileDAO->entity_id
= $this->_id
;
298 $entityFileDAO->entity_table
= 'civicrm_msg_template';
299 if ($entityFileDAO->find(TRUE)) {
300 $fileDAO = new CRM_Core_DAO_File();
301 $fileDAO->id
= $entityFileDAO->file_id
;
302 $fileDAO->find(TRUE);
303 $entityFileDAO->delete();
308 $this->_workflow_id
= CRM_Utils_Array
::value('workflow_id', $this->_values
);
309 if ($this->_workflow_id
) {
310 $params['workflow_id'] = $this->_workflow_id
;
311 $params['is_active'] = TRUE;
314 $messageTemplate = CRM_Core_BAO_MessageTemplate
::add($params);
315 CRM_Core_Session
::setStatus(ts('The Message Template \'%1\' has been saved.', [1 => $messageTemplate->msg_title
]), ts('Saved'), 'success');
317 if (isset($this->_submitValues
['_qf_MessageTemplates_upload'])) {
318 // Save button was pressed
319 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/messageTemplates/add', "action=update&id={$messageTemplate->id}&reset=1"));
321 // Save and done button was pressed
322 if ($this->_workflow_id
) {
323 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
325 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/messageTemplates', 'selectedChild=user&reset=1'));