Merge pull request #9587 from colemanw/CRM-19802
[civicrm-core.git] / CRM / Admin / Form / MessageTemplates.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
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
TO
71
72 $this->_workflow_id = CRM_Utils_Array::value('workflow_id', $defaults);
73 $this->assign('workflow_id', $this->_workflow_id);
74 if ($this->_action & CRM_Core_Action::ADD) {
75 $defaults['is_active'] = 1;
76 //set the context for redirection after form submit or cancel
77 $session = CRM_Core_Session::singleton();
78 $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/messageTemplates',
353ffa53
TO
79 'selectedChild=user&reset=1'
80 ));
6a488035
TO
81 }
82
83 // FIXME: we need to fix the Cancel button here as we don’t know whether it’s a workflow template in buildQuickForm()
84 if ($this->_action & CRM_Core_Action::UPDATE) {
85 if ($this->_workflow_id) {
86 $selectedChild = 'workflow';
87 }
88 else {
89 $selectedChild = 'user';
90 }
90a73810 91
92 $documentInfo = CRM_Core_BAO_File::getEntityFile('civicrm_msg_template', $this->_id, TRUE);
93 if (!empty($documentInfo)) {
a8cd772c 94 $defaults['file_type'] = 1;
95 $this->_is_document = TRUE;
90a73810 96 $this->assign('attachment', $documentInfo);
97 }
98
6a488035
TO
99 $cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', "selectedChild={$selectedChild}&reset=1");
100 $cancelURL = str_replace('&amp;', '&', $cancelURL);
101 $this->addButtons(
102 array(
103 array(
90a73810 104 'type' => 'upload',
6a488035
TO
105 'name' => ts('Save'),
106 'isDefault' => TRUE,
107 ),
108 array(
109 'type' => 'cancel',
110 'name' => ts('Cancel'),
111 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"),
112 ),
113 )
114 );
115 }
116
117 return $defaults;
118 }
119
120 /**
eceb18cc 121 * Build the form object.
6a488035
TO
122 */
123 public function buildQuickForm() {
124
125 // For VIEW we only want Done button
126 if ($this->_action & CRM_Core_Action::VIEW) {
127 // currently, the above action is used solely for previewing default workflow templates
128 $cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1');
129 $cancelURL = str_replace('&amp;', '&', $cancelURL);
130 $this->addButtons(array(
131 array(
132 'type' => 'cancel',
133 'name' => ts('Done'),
134 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"),
135 'isDefault' => TRUE,
136 ),
137 )
138 );
139 }
140 else {
90a73810 141 $this->addButtons(array(
142 array(
143 'type' => 'upload',
144 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'),
145 'isDefault' => TRUE,
146 ),
147 array(
148 'type' => 'cancel',
149 'name' => ts('Cancel'),
150 ),
151 )
152 );
6a488035
TO
153 }
154
155 if ($this->_action & CRM_Core_Action::DELETE) {
156 return;
157 }
158
353ffa53
TO
159 $breadCrumb = array(
160 array(
161 'title' => ts('Message Templates'),
6a488035
TO
162 'url' => CRM_Utils_System::url('civicrm/admin/messageTemplates',
163 'action=browse&reset=1'
164 ),
8d7a9d07 165 ),
353ffa53 166 );
6a488035
TO
167 CRM_Utils_System::appendBreadCrumb($breadCrumb);
168
169 $this->applyFilter('__ALL__', 'trim');
c6327d7d 170 $this->add('text', 'msg_title', ts('Message Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_title'), TRUE);
6a488035 171
a8cd772c 172 $options = array(ts('Compose On-screen'), ts('Upload Document'));
b3088211 173 $element = $this->addRadio('file_type', ts('Source'), $options);
174 if ($this->_id) {
175 $element->freeze();
176 }
a8cd772c 177
1f46c1b9 178 $this->addElement('file', "file_id", ts('Upload Document'), 'size=30 maxlength=255');
90a73810 179 $this->addUploadElement("file_id");
180
6a488035
TO
181 $this->add('text', 'msg_subject',
182 ts('Message Subject'),
c6327d7d 183 CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_subject')
6a488035
TO
184 );
185
186 //get the tokens.
187 $tokens = CRM_Core_SelectValues::contactTokens();
188
ac0a3db5 189 $this->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens));
6a488035 190
6a488035
TO
191 // if not a system message use a wysiwyg editor, CRM-5971
192 if ($this->_id &&
c6327d7d 193 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_MessageTemplate',
6a488035
TO
194 $this->_id,
195 'workflow_id'
196 )
197 ) {
198 $this->add('textarea', 'msg_html', ts('HTML Message'),
199 "cols=50 rows=6"
200 );
201 }
202 else {
5d51a2f9 203 $this->add('wysiwyg', 'msg_html', ts('HTML Message'),
6a488035 204 array(
02fc859b 205 'cols' => '80',
353ffa53 206 'rows' => '8',
6a488035 207 'onkeyup' => "return verify(this)",
7ad5ae6a 208 'preset' => 'civimail',
6a488035
TO
209 )
210 );
211 }
212
ef4a9bcd 213 $this->add('textarea', 'msg_text', ts('Text Message'),
214 "cols=50 rows=6"
215 );
216
6a488035
TO
217 $this->add('select', 'pdf_format_id', ts('PDF Page Format'),
218 array(
8d7a9d07 219 'null' => ts('- default -'),
353ffa53 220 ) + CRM_Core_BAO_PdfFormat::getList(TRUE), FALSE
6a488035
TO
221 );
222
223 $this->add('checkbox', 'is_active', ts('Enabled?'));
224
90a73810 225 $this->addFormRule(array(__CLASS__, 'formRule'), $this);
226
6a488035
TO
227 if ($this->_action & CRM_Core_Action::VIEW) {
228 $this->freeze();
229 CRM_Utils_System::setTitle(ts('View System Default Message Template'));
230 }
231 }
232
90a73810 233 /**
234 * Global form rule.
235 *
04a76231 236 * @param array $params
90a73810 237 * The input form values.
238 * @param array $files
239 * The uploaded files if any.
04a76231 240 * @param array $self
90a73810 241 *
242 * @return array
243 * array of errors
244 */
245 public static function formRule($params, $files, $self) {
246 $errors = array();
247
100e363e 248 // If user uploads non-document file other than odt/docx
249 if (!empty($files['file_id']['tmp_name']) &&
250 array_search($files['file_id']['type'], CRM_Core_SelectValues::documentApplicationType()) == NULL
251 ) {
252 $error['file_id'] = ts('Invalid document file format');
253 }
254 // If default is not set and no document file is uploaded
255 elseif (empty($files['file_id']['tmp_name']) && !empty($params['file_type']) && !$self->_is_document) {
90a73810 256 //On edit page of docx/odt message template if user changes file type but forgot to upload document
a8cd772c 257 $errors['file_id'] = ts('Please upload document');
90a73810 258 }
259
260 return $errors;
261 }
262
6a488035 263 /**
eceb18cc 264 * Process the form submission.
6a488035
TO
265 */
266 public function postProcess() {
267 if ($this->_action & CRM_Core_Action::DELETE) {
c6327d7d 268 CRM_Core_BAO_MessageTemplate::del($this->_id);
6a488035
TO
269 }
270 elseif ($this->_action & CRM_Core_Action::VIEW) {
271 // currently, the above action is used solely for previewing default workflow templates
272 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
273 }
274 else {
275 $params = array();
276
277 // store the submitted values in an array
90a73810 278 $params = $this->controller->exportValues($this->_name);
6a488035
TO
279
280 if ($this->_action & CRM_Core_Action::UPDATE) {
281 $params['id'] = $this->_id;
282 }
283
a8cd772c 284 if (!empty($params['file_type'])) {
90a73810 285 unset($params['msg_html']);
286 unset($params['msg_text']);
287 CRM_Utils_File::formatFile($params, 'file_id');
288 }
289 // delete related file refernces 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
6a488035
TO
303 if ($this->_workflow_id) {
304 $params['workflow_id'] = $this->_workflow_id;
305 $params['is_active'] = TRUE;
306 }
307
c6327d7d 308 $messageTemplate = CRM_Core_BAO_MessageTemplate::add($params);
6a488035
TO
309 CRM_Core_Session::setStatus(ts('The Message Template \'%1\' has been saved.', array(1 => $messageTemplate->msg_title)), ts('Saved'), 'success');
310
311 if ($this->_workflow_id) {
312 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
313 }
314 else {
315 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=user&reset=1'));
316 }
317 }
318 }
96025800 319
6a488035 320}