-
-
Notifications
You must be signed in to change notification settings - Fork 825
/
Copy pathMessageTemplates.php
359 lines (315 loc) · 11.4 KB
/
MessageTemplates.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
use Civi\Api4\MessageTemplate;
use Civi\Token\TokenProcessor;
/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
/**
* This class generates form components for Message templates
* used by membership, contributions, event registrations, etc.
*/
class CRM_Admin_Form_MessageTemplates extends CRM_Core_Form {
/**
* which (and whether) mailing workflow this template belongs to
* @var int
*/
protected $_workflow_id = NULL;
/**
* Is document file is already loaded as default value?
*
* @var bool
*/
protected $_is_document = FALSE;
/**
* @var bool
*/
public $submitOnce = TRUE;
/**
* The ID of the message template being edited
*
* @var int
*/
protected $_id;
/**
* The (current) message template database values
*
* @var array
*/
protected $_values;
/**
* PreProcess form - load existing values.
*
* @throws \CRM_Core_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
public function preProcess() {
$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
$this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add');
$this->assign('action', $this->_action);
$this->_values = [];
if ($this->_id) {
$this->_values = (array) MessageTemplate::get()->addWhere('id', '=', $this->_id)->setSelect(['*'])->execute()->first();
}
}
/**
* Set default values for the form.
*
* The default values are retrieved from the database.
*/
public function setDefaultValues() {
$defaults = $this->_values;
if (empty($defaults['pdf_format_id'])) {
$defaults['pdf_format_id'] = 'null';
}
if (empty($defaults['file_type'])) {
$defaults['file_type'] = 0;
}
if ($this->_action & CRM_Core_Action::ADD) {
$defaults['is_active'] = 1;
}
if ($this->_action & CRM_Core_Action::UPDATE) {
$documentInfo = CRM_Core_BAO_File::getEntityFile('civicrm_msg_template', $this->_id, TRUE);
if (!empty($documentInfo)) {
$defaults['file_type'] = 1;
$this->_is_document = TRUE;
$this->assign('attachment', $documentInfo);
}
}
return $defaults;
}
/**
* Build the form object.
*/
public function buildQuickForm() {
// For VIEW we only want Done button
if ($this->_action & CRM_Core_Action::VIEW) {
// currently, the above action is used solely for previewing default workflow templates
$cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1');
$cancelURL = str_replace('&', '&', $cancelURL);
$this->addButtons([
[
'type' => 'cancel',
'name' => ts('Done'),
'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
'isDefault' => TRUE,
],
]);
}
else {
$this->_workflow_id = $this->_values['workflow_id'] ?? NULL;
$this->checkUserPermission($this->_workflow_id);
$this->assign('isWorkflow', (bool) ($this->_values['workflow_id'] ?? NULL));
if ($this->_workflow_id) {
$selectedChild = 'workflow';
}
else {
$selectedChild = 'user';
}
$cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', "selectedChild={$selectedChild}&reset=1");
$cancelURL = str_replace('&', '&', $cancelURL);
$buttons[] = [
'type' => 'upload',
'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'),
'isDefault' => TRUE,
];
if (!($this->_action & CRM_Core_Action::DELETE)) {
$buttons[] = [
'type' => 'upload',
'name' => ts('Save and Done'),
'subName' => 'done',
];
}
$buttons[] = [
'type' => 'cancel',
'name' => ts('Cancel'),
'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
];
$this->addButtons($buttons);
}
if ($this->_action & CRM_Core_Action::DELETE) {
$this->assign('msg_title', $this->_values['msg_title']);
return;
}
$breadCrumb = [
[
'title' => ts('Message Templates'),
'url' => CRM_Utils_System::url('civicrm/admin/messageTemplates', 'action=browse&reset=1'),
],
];
CRM_Utils_System::appendBreadCrumb($breadCrumb);
$this->applyFilter('__ALL__', 'trim');
$this->add('text', 'msg_title', ts('Message Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_title'), TRUE);
$options = [ts('Compose On-screen'), ts('Upload Document')];
$element = $this->addRadio('file_type', ts('Source'), $options);
if ($this->_id) {
$element->freeze();
}
$this->addElement('file', "file_id", ts('Upload Document'), 'size=30 maxlength=255');
$this->addUploadElement("file_id");
$this->add('text', 'msg_subject',
ts('Message Subject'),
CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_subject')
);
$tokenProcessor = new TokenProcessor(Civi::dispatcher(), ['schema' => ['contactId']]);
$tokens = $tokenProcessor->listTokens();
$this->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens));
// if not a system message use a wysiwyg editor, CRM-5971 and dev/core#5154
if (
$this->_id && (
CRM_Core_DAO::getFieldValue('CRM_Core_DAO_MessageTemplate', $this->_id, 'workflow_id') ||
CRM_Core_DAO::getFieldValue('CRM_Core_DAO_MessageTemplate', $this->_id, 'workflow_name')
)
) {
$this->add('textarea', 'msg_html', ts('HTML Message'),
['cols' => 50, 'rows' => 6]
);
}
else {
$this->add('wysiwyg', 'msg_html', ts('HTML Message'),
[
'cols' => '80',
'rows' => '8',
'onkeyup' => "return verify(this)",
'preset' => 'civimail',
]
);
}
$this->add('textarea', 'msg_text', ts('Text Message'),
['cols' => 50, 'rows' => 6]
);
$this->add('select', 'pdf_format_id', ts('PDF Page Format'),
[
'null' => ts('- default -'),
] + CRM_Core_BAO_PdfFormat::getList(TRUE), FALSE
);
$this->add('advcheckbox', 'is_active', ts('Enabled?'));
$this->addFormRule([__CLASS__, 'formRule'], $this);
if ($this->_action & CRM_Core_Action::VIEW) {
$this->freeze();
$this->setTitle(ts('View System Default Message Template'));
}
}
/**
* Restrict users access based on permission
*
* @param int $workflowId
*/
private function checkUserPermission($workflowId) {
if (isset($workflowId)) {
$canView = CRM_Core_Permission::check('edit system workflow message templates');
}
else {
$canView = CRM_Core_Permission::check('edit user-driven message templates');
}
if (!$canView && !CRM_Core_Permission::check('edit message templates')) {
CRM_Core_Session::setStatus(ts('You do not have permission to view requested page.'), ts('Access Denied'));
$url = CRM_Utils_System::url('civicrm/admin/messageTemplates', "reset=1");
CRM_Utils_System::redirect($url);
}
}
/**
* Global form rule.
*
* @param array $params
* The input form values.
* @param array $files
* The uploaded files if any.
* @param self $self
*
* @return array
* array of errors
*/
public static function formRule($params, $files, $self) {
// If user uploads non-document file other than odt/docx
if (!empty($files['file_id']['tmp_name']) &&
array_search($files['file_id']['type'], CRM_Core_SelectValues::documentApplicationType()) == NULL
) {
$errors['file_id'] = ts('Invalid document file format');
}
// If default is not set and no document file is uploaded
elseif (empty($files['file_id']['tmp_name']) && !empty($params['file_type']) && !$self->_is_document) {
//On edit page of docx/odt message template if user changes file type but forgot to upload document
$errors['file_id'] = ts('Please upload document');
}
return empty($errors) ? TRUE : $errors;
}
/**
* Process the form submission.
*
* @throws \CRM_Core_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
public function postProcess() {
if ($this->_action & CRM_Core_Action::DELETE) {
CRM_Core_BAO_MessageTemplate::deleteRecord(['id' => $this->_id]);
CRM_Core_Session::setStatus(ts('Selected message template has been deleted.'), ts('Deleted'), 'success');
$this->postProcessHook();
}
elseif ($this->_action & CRM_Core_Action::VIEW) {
// currently, the above action is used solely for previewing default workflow templates
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
}
else {
// store the submitted values in an array
$params = $this->controller->exportValues();
if ($this->_action & CRM_Core_Action::UPDATE) {
$params['id'] = $this->_id;
}
if (!empty($params['file_type'])) {
unset($params['msg_html']);
unset($params['msg_text']);
CRM_Utils_File::formatFile($params, 'file_id');
}
// delete related file references 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();
}
}
$this->_workflow_id = $this->_values['workflow_id'] ?? NULL;
if ($this->_workflow_id) {
$params['workflow_id'] = $this->_workflow_id;
$params['is_active'] = TRUE;
}
$messageTemplate = MessageTemplate::save()->setDefaults($params)->setRecords([['id' => $this->_id]])->execute()->first();
// set the id on save, so it can be used in a extension using the posProcess hook
$this->_id = $messageTemplate['id'];
$this->postProcessHook();
CRM_Core_Session::setStatus(ts('The Message Template \'%1\' has been saved.', [1 => $messageTemplate['msg_title']]), ts('Saved'), 'success');
if (isset($this->_submitValues['_qf_MessageTemplates_upload'])) {
// Save button was pressed
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates/add', "action=update&id={$messageTemplate['id']}&reset=1"));
}
// Save and done button was pressed
if ($this->_workflow_id) {
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
}
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=user&reset=1'));
}
}
/**
* Override
* @return array
*/
protected function getFieldsToExcludeFromPurification(): array {
return ['msg_html'];
}
}