Merge pull request #15135 from mattwire/case_links_refactor_report
[civicrm-core.git] / CRM / Admin / Form / MessageTemplates.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components for Message templates
20 * used by membership, contributions, event registrations, etc.
21 */
22 class CRM_Admin_Form_MessageTemplates extends CRM_Admin_Form {
23 /**
24 * which (and whether) mailing workflow this template belongs to
25 * @var int
26 */
27 protected $_workflow_id = NULL;
28
29 /**
30 * Is document file is already loaded as default value?
31 * @var bool
32 */
33 protected $_is_document = FALSE;
34
35 public function preProcess() {
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
42 $this->_BAOName = 'CRM_Core_BAO_MessageTemplate';
43 $this->set('BAOName', $this->_BAOName);
44 parent::preProcess();
45 }
46
47 /**
48 * Set default values for the form.
49 *
50 * The default values are retrieved from the database.
51 */
52 public function setDefaultValues() {
53 $defaults = $this->_values;
54
55 if (empty($defaults['pdf_format_id'])) {
56 $defaults['pdf_format_id'] = 'null';
57 }
58 if (empty($defaults['file_type'])) {
59 $defaults['file_type'] = 0;
60 }
61
62 if ($this->_action & CRM_Core_Action::ADD) {
63 $defaults['is_active'] = 1;
64 }
65
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);
72 }
73 }
74
75 return $defaults;
76 }
77
78 /**
79 * Build the form object.
80 */
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('&amp;', '&', $cancelURL);
87 $this->addButtons([
88 [
89 'type' => 'cancel',
90 'name' => ts('Done'),
91 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
92 'isDefault' => TRUE,
93 ],
94 ]);
95 }
96 else {
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);
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);
110 $buttons[] = [
111 'type' => 'upload',
112 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'),
113 'isDefault' => TRUE,
114 ];
115 if (!($this->_action & CRM_Core_Action::DELETE)) {
116 $buttons[] = [
117 'type' => 'submit',
118 'name' => ts('Save and Done'),
119 'subName' => 'done',
120 ];
121 }
122 $buttons[] = [
123 'type' => 'cancel',
124 'name' => ts('Cancel'),
125 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
126 ];
127 $this->addButtons($buttons);
128 }
129
130 if ($this->_action & CRM_Core_Action::DELETE) {
131 $this->assign('msg_title', $this->_values['msg_title']);
132 return;
133 }
134
135 $breadCrumb = [
136 [
137 'title' => ts('Message Templates'),
138 'url' => CRM_Utils_System::url('civicrm/admin/messageTemplates', 'action=browse&reset=1'),
139 ],
140 ];
141 CRM_Utils_System::appendBreadCrumb($breadCrumb);
142
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);
145
146 $options = [ts('Compose On-screen'), ts('Upload Document')];
147 $element = $this->addRadio('file_type', ts('Source'), $options);
148 if ($this->_id) {
149 $element->freeze();
150 }
151
152 $this->addElement('file', "file_id", ts('Upload Document'), 'size=30 maxlength=255');
153 $this->addUploadElement("file_id");
154
155 $this->add('text', 'msg_subject',
156 ts('Message Subject'),
157 CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_subject')
158 );
159
160 //get the tokens.
161 $tokens = CRM_Core_SelectValues::contactTokens();
162
163 $this->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens));
164
165 // if not a system message use a wysiwyg editor, CRM-5971
166 if ($this->_id &&
167 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_MessageTemplate',
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 {
177 $this->add('wysiwyg', 'msg_html', ts('HTML Message'),
178 [
179 'cols' => '80',
180 'rows' => '8',
181 'onkeyup' => "return verify(this)",
182 'preset' => 'civimail',
183 ]
184 );
185 }
186
187 $this->add('textarea', 'msg_text', ts('Text Message'),
188 "cols=50 rows=6"
189 );
190
191 $this->add('select', 'pdf_format_id', ts('PDF Page Format'),
192 [
193 'null' => ts('- default -'),
194 ] + CRM_Core_BAO_PdfFormat::getList(TRUE), FALSE
195 );
196
197 $this->add('checkbox', 'is_active', ts('Enabled?'));
198 $this->addFormRule([__CLASS__, 'formRule'], $this);
199
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
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
226 /**
227 * Global form rule.
228 *
229 * @param array $params
230 * The input form values.
231 * @param array $files
232 * The uploaded files if any.
233 * @param array $self
234 *
235 * @return array
236 * array of errors
237 */
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
242 ) {
243 $errors['file_id'] = ts('Invalid document file format');
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) {
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');
249 }
250
251 return empty($errors) ? TRUE : $errors;
252 }
253
254 /**
255 * Process the form submission.
256 */
257 public function postProcess() {
258 if ($this->_action & CRM_Core_Action::DELETE) {
259 CRM_Core_BAO_MessageTemplate::del($this->_id);
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 {
266 // store the submitted values in an array
267 $params = $this->controller->exportValues();
268
269 if ($this->_action & CRM_Core_Action::UPDATE) {
270 $params['id'] = $this->_id;
271 }
272
273 if (!empty($params['file_type'])) {
274 unset($params['msg_html']);
275 unset($params['msg_text']);
276 CRM_Utils_File::formatFile($params, 'file_id');
277 }
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();
288 $fileDAO->delete();
289 }
290 }
291
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;
296 }
297
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');
300
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"));
304 }
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'));
308 }
309 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=user&reset=1'));
310 }
311 }
312
313 }