3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for Message templates
20 * used by membership, contributions, event registrations, etc.
22 class CRM_Admin_Form_MessageTemplates
extends CRM_Admin_Form
{
24 * which (and whether) mailing workflow this template belongs to
27 protected $_workflow_id = NULL;
30 * Is document file is already loaded as default value?
33 protected $_is_document = FALSE;
35 public function preProcess() {
36 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
37 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String',
40 $this->assign('action', $this->_action
);
42 $this->_BAOName
= 'CRM_Core_BAO_MessageTemplate';
43 $this->set('BAOName', $this->_BAOName
);
48 * Set default values for the form.
50 * The default values are retrieved from the database.
52 public function setDefaultValues() {
53 $defaults = $this->_values
;
55 if (empty($defaults['pdf_format_id'])) {
56 $defaults['pdf_format_id'] = 'null';
58 if (empty($defaults['file_type'])) {
59 $defaults['file_type'] = 0;
62 if ($this->_action
& CRM_Core_Action
::ADD
) {
63 $defaults['is_active'] = 1;
66 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
67 $documentInfo = CRM_Core_BAO_File
::getEntityFile('civicrm_msg_template', $this->_id
, TRUE);
68 if (!empty($documentInfo)) {
69 $defaults['file_type'] = 1;
70 $this->_is_document
= TRUE;
71 $this->assign('attachment', $documentInfo);
79 * Build the form object.
81 public function buildQuickForm() {
82 // For VIEW we only want Done button
83 if ($this->_action
& CRM_Core_Action
::VIEW
) {
84 // currently, the above action is used solely for previewing default workflow templates
85 $cancelURL = CRM_Utils_System
::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1');
86 $cancelURL = str_replace('&', '&', $cancelURL);
91 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
97 $this->_workflow_id
= CRM_Utils_Array
::value('workflow_id', $this->_values
);
98 $this->checkUserPermission($this->_workflow_id
);
99 $this->assign('workflow_id', $this->_workflow_id
);
101 if ($this->_workflow_id
) {
102 $selectedChild = 'workflow';
105 $selectedChild = 'user';
108 $cancelURL = CRM_Utils_System
::url('civicrm/admin/messageTemplates', "selectedChild={$selectedChild}&reset=1");
109 $cancelURL = str_replace('&', '&', $cancelURL);
112 'name' => $this->_action
& CRM_Core_Action
::DELETE ?
ts('Delete') : ts('Save'),
115 if (!($this->_action
& CRM_Core_Action
::DELETE
)) {
118 'name' => ts('Save and Done'),
124 'name' => ts('Cancel'),
125 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
127 $this->addButtons($buttons);
130 if ($this->_action
& CRM_Core_Action
::DELETE
) {
131 $this->assign('msg_title', $this->_values
['msg_title']);
137 'title' => ts('Message Templates'),
138 'url' => CRM_Utils_System
::url('civicrm/admin/messageTemplates', 'action=browse&reset=1'),
141 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
143 $this->applyFilter('__ALL__', 'trim');
144 $this->add('text', 'msg_title', ts('Message Title'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_title'), TRUE);
146 $options = [ts('Compose On-screen'), ts('Upload Document')];
147 $element = $this->addRadio('file_type', ts('Source'), $options);
152 $this->addElement('file', "file_id", ts('Upload Document'), 'size=30 maxlength=255');
153 $this->addUploadElement("file_id");
155 $this->add('text', 'msg_subject',
156 ts('Message Subject'),
157 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_subject')
161 $tokens = CRM_Core_SelectValues
::contactTokens();
163 $this->assign('tokens', CRM_Utils_Token
::formatTokensForDisplay($tokens));
165 // if not a system message use a wysiwyg editor, CRM-5971
167 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_MessageTemplate',
172 $this->add('textarea', 'msg_html', ts('HTML Message'),
177 $this->add('wysiwyg', 'msg_html', ts('HTML Message'),
181 'onkeyup' => "return verify(this)",
182 'preset' => 'civimail',
187 $this->add('textarea', 'msg_text', ts('Text Message'),
191 $this->add('select', 'pdf_format_id', ts('PDF Page Format'),
193 'null' => ts('- default -'),
194 ] + CRM_Core_BAO_PdfFormat
::getList(TRUE), FALSE
197 $this->add('checkbox', 'is_active', ts('Enabled?'));
198 $this->addFormRule([__CLASS__
, 'formRule'], $this);
200 if ($this->_action
& CRM_Core_Action
::VIEW
) {
202 CRM_Utils_System
::setTitle(ts('View System Default Message Template'));
207 * Restrict users access based on permission
209 * @param int $workflowId
211 private function checkUserPermission($workflowId) {
212 if (isset($workflowId)) {
213 $canView = CRM_Core_Permission
::check('edit system workflow message templates');
216 $canView = CRM_Core_Permission
::check('edit user-driven message templates');
219 if (!$canView && !CRM_Core_Permission
::check('edit message templates')) {
220 CRM_Core_Session
::setStatus(ts('You do not have permission to view requested page.'), ts('Access Denied'));
221 $url = CRM_Utils_System
::url('civicrm/admin/messageTemplates', "reset=1");
222 CRM_Utils_System
::redirect($url);
229 * @param array $params
230 * The input form values.
231 * @param array $files
232 * The uploaded files if any.
238 public static function formRule($params, $files, $self) {
239 // If user uploads non-document file other than odt/docx
240 if (!empty($files['file_id']['tmp_name']) &&
241 array_search($files['file_id']['type'], CRM_Core_SelectValues
::documentApplicationType()) == NULL
243 $errors['file_id'] = ts('Invalid document file format');
245 // If default is not set and no document file is uploaded
246 elseif (empty($files['file_id']['tmp_name']) && !empty($params['file_type']) && !$self->_is_document
) {
247 //On edit page of docx/odt message template if user changes file type but forgot to upload document
248 $errors['file_id'] = ts('Please upload document');
251 return empty($errors) ?
TRUE : $errors;
255 * Process the form submission.
257 public function postProcess() {
258 if ($this->_action
& CRM_Core_Action
::DELETE
) {
259 CRM_Core_BAO_MessageTemplate
::del($this->_id
);
261 elseif ($this->_action
& CRM_Core_Action
::VIEW
) {
262 // currently, the above action is used solely for previewing default workflow templates
263 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
266 // store the submitted values in an array
267 $params = $this->controller
->exportValues();
269 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
270 $params['id'] = $this->_id
;
273 if (!empty($params['file_type'])) {
274 unset($params['msg_html']);
275 unset($params['msg_text']);
276 CRM_Utils_File
::formatFile($params, 'file_id');
278 // delete related file references if html/text/pdf template are chosen over document
279 elseif (!empty($this->_id
)) {
280 $entityFileDAO = new CRM_Core_DAO_EntityFile();
281 $entityFileDAO->entity_id
= $this->_id
;
282 $entityFileDAO->entity_table
= 'civicrm_msg_template';
283 if ($entityFileDAO->find(TRUE)) {
284 $fileDAO = new CRM_Core_DAO_File();
285 $fileDAO->id
= $entityFileDAO->file_id
;
286 $fileDAO->find(TRUE);
287 $entityFileDAO->delete();
292 $this->_workflow_id
= CRM_Utils_Array
::value('workflow_id', $this->_values
);
293 if ($this->_workflow_id
) {
294 $params['workflow_id'] = $this->_workflow_id
;
295 $params['is_active'] = TRUE;
298 $messageTemplate = CRM_Core_BAO_MessageTemplate
::add($params);
299 CRM_Core_Session
::setStatus(ts('The Message Template \'%1\' has been saved.', [1 => $messageTemplate->msg_title
]), ts('Saved'), 'success');
301 if (isset($this->_submitValues
['_qf_MessageTemplates_upload'])) {
302 // Save button was pressed
303 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/messageTemplates/add', "action=update&id={$messageTemplate->id}&reset=1"));
305 // Save and done button was pressed
306 if ($this->_workflow_id
) {
307 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
309 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/messageTemplates', 'selectedChild=user&reset=1'));