Merge pull request #15978 from civicrm/5.20
[civicrm-core.git] / CRM / Admin / Form / MessageTemplates.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * This class generates form components for Message templates
20 * used by membership, contributions, event registrations, etc.
6a488035
TO
21 */
22class CRM_Admin_Form_MessageTemplates extends CRM_Admin_Form {
62d3ee27
SL
23 /**
24 * which (and whether) mailing workflow this template belongs to
25 * @var int
26 */
430ae6dd 27 protected $_workflow_id = NULL;
04a76231 28
62d3ee27
SL
29 /**
30 * Is document file is already loaded as default value?
31 * @var bool
32 */
a8cd772c 33 protected $_is_document = FALSE;
430ae6dd 34
00be9182 35 public function preProcess() {
6a488035
TO
36 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
37 $this->_action = CRM_Utils_Request::retrieve('action', 'String',
38 $this, FALSE, 'add'
39 );
40 $this->assign('action', $this->_action);
41
c6327d7d 42 $this->_BAOName = 'CRM_Core_BAO_MessageTemplate';
6a488035
TO
43 $this->set('BAOName', $this->_BAOName);
44 parent::preProcess();
45 }
46
47 /**
c490a46a 48 * Set default values for the form.
6a488035 49 *
ce064e4f 50 * The default values are retrieved from the database.
6a488035
TO
51 */
52 public function setDefaultValues() {
53 $defaults = $this->_values;
54
a7488080 55 if (empty($defaults['pdf_format_id'])) {
6a488035
TO
56 $defaults['pdf_format_id'] = 'null';
57 }
a8cd772c 58 if (empty($defaults['file_type'])) {
59 $defaults['file_type'] = 0;
60 }
6a488035 61
6a488035
TO
62 if ($this->_action & CRM_Core_Action::ADD) {
63 $defaults['is_active'] = 1;
6a488035
TO
64 }
65
6a488035 66 if ($this->_action & CRM_Core_Action::UPDATE) {
90a73810 67 $documentInfo = CRM_Core_BAO_File::getEntityFile('civicrm_msg_template', $this->_id, TRUE);
68 if (!empty($documentInfo)) {
a8cd772c 69 $defaults['file_type'] = 1;
70 $this->_is_document = TRUE;
90a73810 71 $this->assign('attachment', $documentInfo);
72 }
6a488035
TO
73 }
74
75 return $defaults;
76 }
77
78 /**
eceb18cc 79 * Build the form object.
6a488035
TO
80 */
81 public function buildQuickForm() {
6a488035
TO
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('&amp;', '&', $cancelURL);
9f462279 87 $this->addButtons([
0d48f1cc
TO
88 [
89 'type' => 'cancel',
90 'name' => ts('Done'),
91 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
92 'isDefault' => TRUE,
93 ],
94 ]);
6a488035
TO
95 }
96 else {
35ca4a12 97 $this->_workflow_id = CRM_Utils_Array::value('workflow_id', $this->_values);
40a732a9 98 $this->checkUserPermission($this->_workflow_id);
35ca4a12
MW
99 $this->assign('workflow_id', $this->_workflow_id);
100
101 if ($this->_workflow_id) {
102 $selectedChild = 'workflow';
103 }
104 else {
105 $selectedChild = 'user';
106 }
107
108 $cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', "selectedChild={$selectedChild}&reset=1");
109 $cancelURL = str_replace('&amp;', '&', $cancelURL);
9f462279 110 $buttons[] = [
ae4b5884
MW
111 'type' => 'upload',
112 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'),
113 'isDefault' => TRUE,
9f462279 114 ];
ae4b5884 115 if (!($this->_action & CRM_Core_Action::DELETE)) {
9f462279 116 $buttons[] = [
ae4b5884
MW
117 'type' => 'submit',
118 'name' => ts('Save and Done'),
119 'subName' => 'done',
9f462279 120 ];
ae4b5884 121 }
9f462279 122 $buttons[] = [
ae4b5884
MW
123 'type' => 'cancel',
124 'name' => ts('Cancel'),
9f462279 125 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
126 ];
ae4b5884 127 $this->addButtons($buttons);
6a488035
TO
128 }
129
130 if ($this->_action & CRM_Core_Action::DELETE) {
5c38109a 131 $this->assign('msg_title', $this->_values['msg_title']);
6a488035
TO
132 return;
133 }
134
9f462279 135 $breadCrumb = [
136 [
353ffa53 137 'title' => ts('Message Templates'),
9f462279 138 'url' => CRM_Utils_System::url('civicrm/admin/messageTemplates', 'action=browse&reset=1'),
139 ],
140 ];
6a488035
TO
141 CRM_Utils_System::appendBreadCrumb($breadCrumb);
142
143 $this->applyFilter('__ALL__', 'trim');
c6327d7d 144 $this->add('text', 'msg_title', ts('Message Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_title'), TRUE);
6a488035 145
9f462279 146 $options = [ts('Compose On-screen'), ts('Upload Document')];
b3088211 147 $element = $this->addRadio('file_type', ts('Source'), $options);
148 if ($this->_id) {
149 $element->freeze();
150 }
a8cd772c 151
1f46c1b9 152 $this->addElement('file', "file_id", ts('Upload Document'), 'size=30 maxlength=255');
90a73810 153 $this->addUploadElement("file_id");
154
6a488035
TO
155 $this->add('text', 'msg_subject',
156 ts('Message Subject'),
c6327d7d 157 CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_subject')
6a488035
TO
158 );
159
160 //get the tokens.
161 $tokens = CRM_Core_SelectValues::contactTokens();
162
ac0a3db5 163 $this->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens));
6a488035 164
6a488035
TO
165 // if not a system message use a wysiwyg editor, CRM-5971
166 if ($this->_id &&
c6327d7d 167 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_MessageTemplate',
6a488035
TO
168 $this->_id,
169 'workflow_id'
170 )
171 ) {
172 $this->add('textarea', 'msg_html', ts('HTML Message'),
173 "cols=50 rows=6"
174 );
175 }
176 else {
5d51a2f9 177 $this->add('wysiwyg', 'msg_html', ts('HTML Message'),
9f462279 178 [
02fc859b 179 'cols' => '80',
353ffa53 180 'rows' => '8',
6a488035 181 'onkeyup' => "return verify(this)",
7ad5ae6a 182 'preset' => 'civimail',
9f462279 183 ]
6a488035
TO
184 );
185 }
186
ef4a9bcd 187 $this->add('textarea', 'msg_text', ts('Text Message'),
188 "cols=50 rows=6"
189 );
190
6a488035 191 $this->add('select', 'pdf_format_id', ts('PDF Page Format'),
9f462279 192 [
8d7a9d07 193 'null' => ts('- default -'),
9f462279 194 ] + CRM_Core_BAO_PdfFormat::getList(TRUE), FALSE
6a488035
TO
195 );
196
197 $this->add('checkbox', 'is_active', ts('Enabled?'));
9f462279 198 $this->addFormRule([__CLASS__, 'formRule'], $this);
90a73810 199
6a488035
TO
200 if ($this->_action & CRM_Core_Action::VIEW) {
201 $this->freeze();
202 CRM_Utils_System::setTitle(ts('View System Default Message Template'));
203 }
204 }
205
40a732a9
SA
206 /**
207 * Restrict users access based on permission
208 *
209 * @param int $workflowId
210 */
211 private function checkUserPermission($workflowId) {
212 if (isset($workflowId)) {
213 $canView = CRM_Core_Permission::check('edit system workflow message templates');
214 }
215 else {
216 $canView = CRM_Core_Permission::check('edit user-driven message templates');
217 }
218
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);
223 }
224 }
225
90a73810 226 /**
227 * Global form rule.
228 *
04a76231 229 * @param array $params
90a73810 230 * The input form values.
231 * @param array $files
232 * The uploaded files if any.
04a76231 233 * @param array $self
90a73810 234 *
235 * @return array
236 * array of errors
237 */
238 public static function formRule($params, $files, $self) {
100e363e 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
242 ) {
b93376a8 243 $errors['file_id'] = ts('Invalid document file format');
100e363e 244 }
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) {
90a73810 247 //On edit page of docx/odt message template if user changes file type but forgot to upload document
a8cd772c 248 $errors['file_id'] = ts('Please upload document');
90a73810 249 }
250
b93376a8 251 return empty($errors) ? TRUE : $errors;
90a73810 252 }
253
6a488035 254 /**
eceb18cc 255 * Process the form submission.
6a488035
TO
256 */
257 public function postProcess() {
258 if ($this->_action & CRM_Core_Action::DELETE) {
c6327d7d 259 CRM_Core_BAO_MessageTemplate::del($this->_id);
6a488035
TO
260 }
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'));
264 }
265 else {
6a488035 266 // store the submitted values in an array
b93376a8 267 $params = $this->controller->exportValues();
6a488035
TO
268
269 if ($this->_action & CRM_Core_Action::UPDATE) {
270 $params['id'] = $this->_id;
271 }
272
a8cd772c 273 if (!empty($params['file_type'])) {
90a73810 274 unset($params['msg_html']);
275 unset($params['msg_text']);
276 CRM_Utils_File::formatFile($params, 'file_id');
277 }
35ca4a12 278 // delete related file references if html/text/pdf template are chosen over document
90a73810 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();
288 $fileDAO->delete();
289 }
290 }
291
35ca4a12 292 $this->_workflow_id = CRM_Utils_Array::value('workflow_id', $this->_values);
6a488035
TO
293 if ($this->_workflow_id) {
294 $params['workflow_id'] = $this->_workflow_id;
295 $params['is_active'] = TRUE;
296 }
297
c6327d7d 298 $messageTemplate = CRM_Core_BAO_MessageTemplate::add($params);
9f462279 299 CRM_Core_Session::setStatus(ts('The Message Template \'%1\' has been saved.', [1 => $messageTemplate->msg_title]), ts('Saved'), 'success');
6a488035 300
b93376a8
MW
301 if (isset($this->_submitValues['_qf_MessageTemplates_upload'])) {
302 // Save button was pressed
ae4b5884
MW
303 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates/add', "action=update&id={$messageTemplate->id}&reset=1"));
304 }
b93376a8 305 // Save and done button was pressed
6a488035
TO
306 if ($this->_workflow_id) {
307 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
308 }
ae4b5884 309 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=user&reset=1'));
6a488035
TO
310 }
311 }
96025800 312
6a488035 313}