Merge pull request #13969 from eileenmcnaughton/array_format2
[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 // which (and whether) mailing workflow this template belongs to
40 protected $_workflow_id = NULL;
41
42 // Is document file is already loaded as default value?
43 protected $_is_document = FALSE;
44
45 public function preProcess() {
46 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
47 $this->_action = CRM_Utils_Request::retrieve('action', 'String',
48 $this, FALSE, 'add'
49 );
50 $this->assign('action', $this->_action);
51
52 $this->_BAOName = 'CRM_Core_BAO_MessageTemplate';
53 $this->set('BAOName', $this->_BAOName);
54 parent::preProcess();
55 }
56
57 /**
58 * Set default values for the form.
59 *
60 * The default values are retrieved from the database.
61 */
62 public function setDefaultValues() {
63 $defaults = $this->_values;
64
65 if (empty($defaults['pdf_format_id'])) {
66 $defaults['pdf_format_id'] = 'null';
67 }
68 if (empty($defaults['file_type'])) {
69 $defaults['file_type'] = 0;
70 }
71
72 if ($this->_action & CRM_Core_Action::ADD) {
73 $defaults['is_active'] = 1;
74 }
75
76 if ($this->_action & CRM_Core_Action::UPDATE) {
77 $documentInfo = CRM_Core_BAO_File::getEntityFile('civicrm_msg_template', $this->_id, TRUE);
78 if (!empty($documentInfo)) {
79 $defaults['file_type'] = 1;
80 $this->_is_document = TRUE;
81 $this->assign('attachment', $documentInfo);
82 }
83 }
84
85 return $defaults;
86 }
87
88 /**
89 * Build the form object.
90 */
91 public function buildQuickForm() {
92 // For VIEW we only want Done button
93 if ($this->_action & CRM_Core_Action::VIEW) {
94 // currently, the above action is used solely for previewing default workflow templates
95 $cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1');
96 $cancelURL = str_replace('&amp;', '&', $cancelURL);
97 $this->addButtons([
98 [
99 'type' => 'cancel',
100 'name' => ts('Done'),
101 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
102 'isDefault' => TRUE,
103 ],
104 ]
105 );
106 }
107 else {
108 $this->_workflow_id = CRM_Utils_Array::value('workflow_id', $this->_values);
109 $this->checkUserPermission($this->_workflow_id);
110 $this->assign('workflow_id', $this->_workflow_id);
111
112 if ($this->_workflow_id) {
113 $selectedChild = 'workflow';
114 }
115 else {
116 $selectedChild = 'user';
117 }
118
119 $cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', "selectedChild={$selectedChild}&reset=1");
120 $cancelURL = str_replace('&amp;', '&', $cancelURL);
121 $buttons[] = [
122 'type' => 'upload',
123 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'),
124 'isDefault' => TRUE,
125 ];
126 if (!($this->_action & CRM_Core_Action::DELETE)) {
127 $buttons[] = [
128 'type' => 'submit',
129 'name' => ts('Save and Done'),
130 'subName' => 'done',
131 ];
132 }
133 $buttons[] = [
134 'type' => 'cancel',
135 'name' => ts('Cancel'),
136 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
137 ];
138 $this->addButtons($buttons);
139 }
140
141 if ($this->_action & CRM_Core_Action::DELETE) {
142 $this->assign('msg_title', $this->_values['msg_title']);
143 return;
144 }
145
146 $breadCrumb = [
147 [
148 'title' => ts('Message Templates'),
149 'url' => CRM_Utils_System::url('civicrm/admin/messageTemplates', 'action=browse&reset=1'),
150 ],
151 ];
152 CRM_Utils_System::appendBreadCrumb($breadCrumb);
153
154 $this->applyFilter('__ALL__', 'trim');
155 $this->add('text', 'msg_title', ts('Message Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_title'), TRUE);
156
157 $options = [ts('Compose On-screen'), ts('Upload Document')];
158 $element = $this->addRadio('file_type', ts('Source'), $options);
159 if ($this->_id) {
160 $element->freeze();
161 }
162
163 $this->addElement('file', "file_id", ts('Upload Document'), 'size=30 maxlength=255');
164 $this->addUploadElement("file_id");
165
166 $this->add('text', 'msg_subject',
167 ts('Message Subject'),
168 CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_subject')
169 );
170
171 //get the tokens.
172 $tokens = CRM_Core_SelectValues::contactTokens();
173
174 $this->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens));
175
176 // if not a system message use a wysiwyg editor, CRM-5971
177 if ($this->_id &&
178 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_MessageTemplate',
179 $this->_id,
180 'workflow_id'
181 )
182 ) {
183 $this->add('textarea', 'msg_html', ts('HTML Message'),
184 "cols=50 rows=6"
185 );
186 }
187 else {
188 $this->add('wysiwyg', 'msg_html', ts('HTML Message'),
189 [
190 'cols' => '80',
191 'rows' => '8',
192 'onkeyup' => "return verify(this)",
193 'preset' => 'civimail',
194 ]
195 );
196 }
197
198 $this->add('textarea', 'msg_text', ts('Text Message'),
199 "cols=50 rows=6"
200 );
201
202 $this->add('select', 'pdf_format_id', ts('PDF Page Format'),
203 [
204 'null' => ts('- default -'),
205 ] + CRM_Core_BAO_PdfFormat::getList(TRUE), FALSE
206 );
207
208 $this->add('checkbox', 'is_active', ts('Enabled?'));
209 $this->addFormRule([__CLASS__, 'formRule'], $this);
210
211 if ($this->_action & CRM_Core_Action::VIEW) {
212 $this->freeze();
213 CRM_Utils_System::setTitle(ts('View System Default Message Template'));
214 }
215 }
216
217 /**
218 * Restrict users access based on permission
219 *
220 * @param int $workflowId
221 */
222 private function checkUserPermission($workflowId) {
223 if (isset($workflowId)) {
224 $canView = CRM_Core_Permission::check('edit system workflow message templates');
225 }
226 else {
227 $canView = CRM_Core_Permission::check('edit user-driven message templates');
228 }
229
230 if (!$canView && !CRM_Core_Permission::check('edit message templates')) {
231 CRM_Core_Session::setStatus(ts('You do not have permission to view requested page.'), ts('Access Denied'));
232 $url = CRM_Utils_System::url('civicrm/admin/messageTemplates', "reset=1");
233 CRM_Utils_System::redirect($url);
234 }
235 }
236
237 /**
238 * Global form rule.
239 *
240 * @param array $params
241 * The input form values.
242 * @param array $files
243 * The uploaded files if any.
244 * @param array $self
245 *
246 * @return array
247 * array of errors
248 */
249 public static function formRule($params, $files, $self) {
250 // If user uploads non-document file other than odt/docx
251 if (!empty($files['file_id']['tmp_name']) &&
252 array_search($files['file_id']['type'], CRM_Core_SelectValues::documentApplicationType()) == NULL
253 ) {
254 $errors['file_id'] = ts('Invalid document file format');
255 }
256 // If default is not set and no document file is uploaded
257 elseif (empty($files['file_id']['tmp_name']) && !empty($params['file_type']) && !$self->_is_document) {
258 //On edit page of docx/odt message template if user changes file type but forgot to upload document
259 $errors['file_id'] = ts('Please upload document');
260 }
261
262 return empty($errors) ? TRUE : $errors;
263 }
264
265 /**
266 * Process the form submission.
267 */
268 public function postProcess() {
269 if ($this->_action & CRM_Core_Action::DELETE) {
270 CRM_Core_BAO_MessageTemplate::del($this->_id);
271 }
272 elseif ($this->_action & CRM_Core_Action::VIEW) {
273 // currently, the above action is used solely for previewing default workflow templates
274 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
275 }
276 else {
277 // store the submitted values in an array
278 $params = $this->controller->exportValues();
279
280 if ($this->_action & CRM_Core_Action::UPDATE) {
281 $params['id'] = $this->_id;
282 }
283
284 if (!empty($params['file_type'])) {
285 unset($params['msg_html']);
286 unset($params['msg_text']);
287 CRM_Utils_File::formatFile($params, 'file_id');
288 }
289 // delete related file references if html/text/pdf template are chosen over document
290 elseif (!empty($this->_id)) {
291 $entityFileDAO = new CRM_Core_DAO_EntityFile();
292 $entityFileDAO->entity_id = $this->_id;
293 $entityFileDAO->entity_table = 'civicrm_msg_template';
294 if ($entityFileDAO->find(TRUE)) {
295 $fileDAO = new CRM_Core_DAO_File();
296 $fileDAO->id = $entityFileDAO->file_id;
297 $fileDAO->find(TRUE);
298 $entityFileDAO->delete();
299 $fileDAO->delete();
300 }
301 }
302
303 $this->_workflow_id = CRM_Utils_Array::value('workflow_id', $this->_values);
304 if ($this->_workflow_id) {
305 $params['workflow_id'] = $this->_workflow_id;
306 $params['is_active'] = TRUE;
307 }
308
309 $messageTemplate = CRM_Core_BAO_MessageTemplate::add($params);
310 CRM_Core_Session::setStatus(ts('The Message Template \'%1\' has been saved.', [1 => $messageTemplate->msg_title]), ts('Saved'), 'success');
311
312 if (isset($this->_submitValues['_qf_MessageTemplates_upload'])) {
313 // Save button was pressed
314 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates/add', "action=update&id={$messageTemplate->id}&reset=1"));
315 }
316 // Save and done button was pressed
317 if ($this->_workflow_id) {
318 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
319 }
320 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=user&reset=1'));
321 }
322 }
323
324 }