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