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