Merge pull request #14003 from seamuslee001/sl_fix_4_7_31_upgrade
[civicrm-core.git] / CRM / Admin / Form / MessageTemplates.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * This class generates form components for Message templates
36 * used by membership, contributions, event registrations, etc.
37 */
38 class CRM_Admin_Form_MessageTemplates extends CRM_Admin_Form {
39 /**
40 * which (and whether) mailing workflow this template belongs to
41 * @var int
42 */
43 protected $_workflow_id = NULL;
44
45 /**
46 * Is document file is already loaded as default value?
47 * @var bool
48 */
49 protected $_is_document = FALSE;
50
51 public function preProcess() {
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
58 $this->_BAOName = 'CRM_Core_BAO_MessageTemplate';
59 $this->set('BAOName', $this->_BAOName);
60 parent::preProcess();
61 }
62
63 /**
64 * Set default values for the form.
65 *
66 * The default values are retrieved from the database.
67 */
68 public function setDefaultValues() {
69 $defaults = $this->_values;
70
71 if (empty($defaults['pdf_format_id'])) {
72 $defaults['pdf_format_id'] = 'null';
73 }
74 if (empty($defaults['file_type'])) {
75 $defaults['file_type'] = 0;
76 }
77
78 if ($this->_action & CRM_Core_Action::ADD) {
79 $defaults['is_active'] = 1;
80 }
81
82 if ($this->_action & CRM_Core_Action::UPDATE) {
83 $documentInfo = CRM_Core_BAO_File::getEntityFile('civicrm_msg_template', $this->_id, TRUE);
84 if (!empty($documentInfo)) {
85 $defaults['file_type'] = 1;
86 $this->_is_document = TRUE;
87 $this->assign('attachment', $documentInfo);
88 }
89 }
90
91 return $defaults;
92 }
93
94 /**
95 * Build the form object.
96 */
97 public function buildQuickForm() {
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);
103 $this->addButtons([
104 [
105 'type' => 'cancel',
106 'name' => ts('Done'),
107 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
108 'isDefault' => TRUE,
109 ],
110 ]);
111 }
112 else {
113 $this->_workflow_id = CRM_Utils_Array::value('workflow_id', $this->_values);
114 $this->checkUserPermission($this->_workflow_id);
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);
126 $buttons[] = [
127 'type' => 'upload',
128 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'),
129 'isDefault' => TRUE,
130 ];
131 if (!($this->_action & CRM_Core_Action::DELETE)) {
132 $buttons[] = [
133 'type' => 'submit',
134 'name' => ts('Save and Done'),
135 'subName' => 'done',
136 ];
137 }
138 $buttons[] = [
139 'type' => 'cancel',
140 'name' => ts('Cancel'),
141 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
142 ];
143 $this->addButtons($buttons);
144 }
145
146 if ($this->_action & CRM_Core_Action::DELETE) {
147 $this->assign('msg_title', $this->_values['msg_title']);
148 return;
149 }
150
151 $breadCrumb = [
152 [
153 'title' => ts('Message Templates'),
154 'url' => CRM_Utils_System::url('civicrm/admin/messageTemplates', 'action=browse&reset=1'),
155 ],
156 ];
157 CRM_Utils_System::appendBreadCrumb($breadCrumb);
158
159 $this->applyFilter('__ALL__', 'trim');
160 $this->add('text', 'msg_title', ts('Message Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_title'), TRUE);
161
162 $options = [ts('Compose On-screen'), ts('Upload Document')];
163 $element = $this->addRadio('file_type', ts('Source'), $options);
164 if ($this->_id) {
165 $element->freeze();
166 }
167
168 $this->addElement('file', "file_id", ts('Upload Document'), 'size=30 maxlength=255');
169 $this->addUploadElement("file_id");
170
171 $this->add('text', 'msg_subject',
172 ts('Message Subject'),
173 CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_subject')
174 );
175
176 //get the tokens.
177 $tokens = CRM_Core_SelectValues::contactTokens();
178
179 $this->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens));
180
181 // if not a system message use a wysiwyg editor, CRM-5971
182 if ($this->_id &&
183 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_MessageTemplate',
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 {
193 $this->add('wysiwyg', 'msg_html', ts('HTML Message'),
194 [
195 'cols' => '80',
196 'rows' => '8',
197 'onkeyup' => "return verify(this)",
198 'preset' => 'civimail',
199 ]
200 );
201 }
202
203 $this->add('textarea', 'msg_text', ts('Text Message'),
204 "cols=50 rows=6"
205 );
206
207 $this->add('select', 'pdf_format_id', ts('PDF Page Format'),
208 [
209 'null' => ts('- default -'),
210 ] + CRM_Core_BAO_PdfFormat::getList(TRUE), FALSE
211 );
212
213 $this->add('checkbox', 'is_active', ts('Enabled?'));
214 $this->addFormRule([__CLASS__, 'formRule'], $this);
215
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
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
242 /**
243 * Global form rule.
244 *
245 * @param array $params
246 * The input form values.
247 * @param array $files
248 * The uploaded files if any.
249 * @param array $self
250 *
251 * @return array
252 * array of errors
253 */
254 public static function formRule($params, $files, $self) {
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 ) {
259 $errors['file_id'] = ts('Invalid document file format');
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) {
263 //On edit page of docx/odt message template if user changes file type but forgot to upload document
264 $errors['file_id'] = ts('Please upload document');
265 }
266
267 return empty($errors) ? TRUE : $errors;
268 }
269
270 /**
271 * Process the form submission.
272 */
273 public function postProcess() {
274 if ($this->_action & CRM_Core_Action::DELETE) {
275 CRM_Core_BAO_MessageTemplate::del($this->_id);
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 {
282 // store the submitted values in an array
283 $params = $this->controller->exportValues();
284
285 if ($this->_action & CRM_Core_Action::UPDATE) {
286 $params['id'] = $this->_id;
287 }
288
289 if (!empty($params['file_type'])) {
290 unset($params['msg_html']);
291 unset($params['msg_text']);
292 CRM_Utils_File::formatFile($params, 'file_id');
293 }
294 // delete related file references if html/text/pdf template are chosen over document
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
308 $this->_workflow_id = CRM_Utils_Array::value('workflow_id', $this->_values);
309 if ($this->_workflow_id) {
310 $params['workflow_id'] = $this->_workflow_id;
311 $params['is_active'] = TRUE;
312 }
313
314 $messageTemplate = CRM_Core_BAO_MessageTemplate::add($params);
315 CRM_Core_Session::setStatus(ts('The Message Template \'%1\' has been saved.', [1 => $messageTemplate->msg_title]), ts('Saved'), 'success');
316
317 if (isset($this->_submitValues['_qf_MessageTemplates_upload'])) {
318 // Save button was pressed
319 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates/add', "action=update&id={$messageTemplate->id}&reset=1"));
320 }
321 // Save and done button was pressed
322 if ($this->_workflow_id) {
323 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
324 }
325 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=user&reset=1'));
326 }
327 }
328
329 }