Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
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 | */ |
38 | class CRM_Admin_Form_MessageTemplates extends CRM_Admin_Form { | |
39 | // which (and whether) mailing workflow this template belongs to | |
430ae6dd | 40 | protected $_workflow_id = NULL; |
90a73810 | 41 | protected $_documentID = NULL; |
430ae6dd | 42 | |
00be9182 | 43 | public function preProcess() { |
6a488035 TO |
44 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
45 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', | |
46 | $this, FALSE, 'add' | |
47 | ); | |
48 | $this->assign('action', $this->_action); | |
49 | ||
c6327d7d | 50 | $this->_BAOName = 'CRM_Core_BAO_MessageTemplate'; |
6a488035 TO |
51 | $this->set('BAOName', $this->_BAOName); |
52 | parent::preProcess(); | |
53 | } | |
54 | ||
55 | /** | |
c490a46a | 56 | * Set default values for the form. |
6a488035 | 57 | * |
ce064e4f | 58 | * The default values are retrieved from the database. |
6a488035 TO |
59 | */ |
60 | public function setDefaultValues() { | |
61 | $defaults = $this->_values; | |
62 | ||
a7488080 | 63 | if (empty($defaults['pdf_format_id'])) { |
6a488035 TO |
64 | $defaults['pdf_format_id'] = 'null'; |
65 | } | |
66 | ||
67 | $this->_workflow_id = CRM_Utils_Array::value('workflow_id', $defaults); | |
68 | $this->assign('workflow_id', $this->_workflow_id); | |
69 | if ($this->_action & CRM_Core_Action::ADD) { | |
70 | $defaults['is_active'] = 1; | |
71 | //set the context for redirection after form submit or cancel | |
72 | $session = CRM_Core_Session::singleton(); | |
73 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/messageTemplates', | |
353ffa53 TO |
74 | 'selectedChild=user&reset=1' |
75 | )); | |
6a488035 TO |
76 | } |
77 | ||
78 | // FIXME: we need to fix the Cancel button here as we don’t know whether it’s a workflow template in buildQuickForm() | |
79 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
80 | if ($this->_workflow_id) { | |
81 | $selectedChild = 'workflow'; | |
82 | } | |
83 | else { | |
84 | $selectedChild = 'user'; | |
85 | } | |
90a73810 | 86 | |
87 | $documentInfo = CRM_Core_BAO_File::getEntityFile('civicrm_msg_template', $this->_id, TRUE); | |
88 | if (!empty($documentInfo)) { | |
89 | foreach ($documentInfo as &$info) { | |
90 | $defaults['file_type'] = array_search($info['mime_type'], CRM_Core_SelectValues::documentApplicationType()); | |
91 | $info['mime_type'] = $defaults['file_type']; | |
92 | $this->_documentID = $info['fileID']; | |
93 | } | |
94 | $this->assign('attachment', $documentInfo); | |
95 | } | |
96 | ||
6a488035 TO |
97 | $cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', "selectedChild={$selectedChild}&reset=1"); |
98 | $cancelURL = str_replace('&', '&', $cancelURL); | |
99 | $this->addButtons( | |
100 | array( | |
101 | array( | |
90a73810 | 102 | 'type' => 'upload', |
6a488035 TO |
103 | 'name' => ts('Save'), |
104 | 'isDefault' => TRUE, | |
105 | ), | |
106 | array( | |
107 | 'type' => 'cancel', | |
108 | 'name' => ts('Cancel'), | |
109 | 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"), | |
110 | ), | |
111 | ) | |
112 | ); | |
113 | } | |
114 | ||
115 | return $defaults; | |
116 | } | |
117 | ||
118 | /** | |
eceb18cc | 119 | * Build the form object. |
6a488035 TO |
120 | */ |
121 | public function buildQuickForm() { | |
122 | ||
123 | // For VIEW we only want Done button | |
124 | if ($this->_action & CRM_Core_Action::VIEW) { | |
125 | // currently, the above action is used solely for previewing default workflow templates | |
126 | $cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'); | |
127 | $cancelURL = str_replace('&', '&', $cancelURL); | |
128 | $this->addButtons(array( | |
129 | array( | |
130 | 'type' => 'cancel', | |
131 | 'name' => ts('Done'), | |
132 | 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"), | |
133 | 'isDefault' => TRUE, | |
134 | ), | |
135 | ) | |
136 | ); | |
137 | } | |
138 | else { | |
90a73810 | 139 | $this->addButtons(array( |
140 | array( | |
141 | 'type' => 'upload', | |
142 | 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'), | |
143 | 'isDefault' => TRUE, | |
144 | ), | |
145 | array( | |
146 | 'type' => 'cancel', | |
147 | 'name' => ts('Cancel'), | |
148 | ), | |
149 | ) | |
150 | ); | |
6a488035 TO |
151 | } |
152 | ||
153 | if ($this->_action & CRM_Core_Action::DELETE) { | |
154 | return; | |
155 | } | |
156 | ||
353ffa53 TO |
157 | $breadCrumb = array( |
158 | array( | |
159 | 'title' => ts('Message Templates'), | |
6a488035 TO |
160 | 'url' => CRM_Utils_System::url('civicrm/admin/messageTemplates', |
161 | 'action=browse&reset=1' | |
162 | ), | |
8d7a9d07 | 163 | ), |
353ffa53 | 164 | ); |
6a488035 TO |
165 | CRM_Utils_System::appendBreadCrumb($breadCrumb); |
166 | ||
167 | $this->applyFilter('__ALL__', 'trim'); | |
c6327d7d | 168 | $this->add('text', 'msg_title', ts('Message Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_title'), TRUE); |
6a488035 | 169 | |
90a73810 | 170 | $this->add('select', 'file_type', ts('File Type'), CRM_Core_SelectValues::documentFormat()); |
171 | ||
172 | $this->addElement('file', "file_id", ts('Upload Document'), 'size=30 maxlength=60'); | |
173 | $this->addUploadElement("file_id"); | |
174 | ||
6a488035 TO |
175 | $this->add('text', 'msg_subject', |
176 | ts('Message Subject'), | |
c6327d7d | 177 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_subject') |
6a488035 TO |
178 | ); |
179 | ||
180 | //get the tokens. | |
181 | $tokens = CRM_Core_SelectValues::contactTokens(); | |
182 | ||
ac0a3db5 | 183 | $this->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens)); |
6a488035 | 184 | |
6a488035 TO |
185 | // if not a system message use a wysiwyg editor, CRM-5971 |
186 | if ($this->_id && | |
c6327d7d | 187 | CRM_Core_DAO::getFieldValue('CRM_Core_DAO_MessageTemplate', |
6a488035 TO |
188 | $this->_id, |
189 | 'workflow_id' | |
190 | ) | |
191 | ) { | |
192 | $this->add('textarea', 'msg_html', ts('HTML Message'), | |
193 | "cols=50 rows=6" | |
194 | ); | |
195 | } | |
196 | else { | |
5d51a2f9 | 197 | $this->add('wysiwyg', 'msg_html', ts('HTML Message'), |
6a488035 | 198 | array( |
02fc859b | 199 | 'cols' => '80', |
353ffa53 | 200 | 'rows' => '8', |
6a488035 | 201 | 'onkeyup' => "return verify(this)", |
cb578dd4 | 202 | 'class' => 'crm-wysiwyg-fullpage', |
6a488035 TO |
203 | ) |
204 | ); | |
205 | } | |
206 | ||
ef4a9bcd | 207 | $this->add('textarea', 'msg_text', ts('Text Message'), |
208 | "cols=50 rows=6" | |
209 | ); | |
210 | ||
6a488035 TO |
211 | $this->add('select', 'pdf_format_id', ts('PDF Page Format'), |
212 | array( | |
8d7a9d07 | 213 | 'null' => ts('- default -'), |
353ffa53 | 214 | ) + CRM_Core_BAO_PdfFormat::getList(TRUE), FALSE |
6a488035 TO |
215 | ); |
216 | ||
217 | $this->add('checkbox', 'is_active', ts('Enabled?')); | |
218 | ||
90a73810 | 219 | $this->addFormRule(array(__CLASS__, 'formRule'), $this); |
220 | ||
6a488035 TO |
221 | if ($this->_action & CRM_Core_Action::VIEW) { |
222 | $this->freeze(); | |
223 | CRM_Utils_System::setTitle(ts('View System Default Message Template')); | |
224 | } | |
225 | } | |
226 | ||
90a73810 | 227 | /** |
228 | * Global form rule. | |
229 | * | |
230 | * @param array $fields | |
231 | * The input form values. | |
232 | * @param array $files | |
233 | * The uploaded files if any. | |
234 | * | |
235 | * @return array | |
236 | * array of errors | |
237 | */ | |
238 | public static function formRule($params, $files, $self) { | |
239 | $errors = array(); | |
240 | ||
241 | //empty file upload validation for odt/docx template | |
242 | if (empty($files['file_id']['tmp_name']) && in_array($params['file_type'], array('odt', 'docx'))) { | |
243 | //On edit page of docx/odt message template if user changes file type but forgot to upload document | |
244 | if (!empty($self->_documentID)) { | |
245 | $fileDAO = new CRM_Core_DAO_File(); | |
246 | $fileDAO->id = $self->_documentID; | |
247 | if ($fileDAO->find(TRUE) && | |
248 | $fileDAO->mime_type != CRM_Utils_Array::value($params['file_type'], CRM_Core_SelectValues::documentApplicationType()) | |
249 | ) { | |
250 | $errors['file_id'] = ts('Please upload document'); | |
251 | } | |
252 | } | |
253 | else { | |
254 | $errors['file_id'] = ts('Please upload document'); | |
255 | } | |
256 | ||
257 | } | |
258 | ||
259 | return $errors; | |
260 | } | |
261 | ||
6a488035 | 262 | /** |
eceb18cc | 263 | * Process the form submission. |
6a488035 TO |
264 | */ |
265 | public function postProcess() { | |
266 | if ($this->_action & CRM_Core_Action::DELETE) { | |
c6327d7d | 267 | CRM_Core_BAO_MessageTemplate::del($this->_id); |
6a488035 TO |
268 | } |
269 | elseif ($this->_action & CRM_Core_Action::VIEW) { | |
270 | // currently, the above action is used solely for previewing default workflow templates | |
271 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1')); | |
272 | } | |
273 | else { | |
274 | $params = array(); | |
275 | ||
276 | // store the submitted values in an array | |
90a73810 | 277 | $params = $this->controller->exportValues($this->_name); |
6a488035 TO |
278 | |
279 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
280 | $params['id'] = $this->_id; | |
281 | } | |
282 | ||
90a73810 | 283 | if (in_array($params['file_type'], array('odt', 'docx'))) { |
284 | unset($params['msg_html']); | |
285 | unset($params['msg_text']); | |
286 | CRM_Utils_File::formatFile($params, 'file_id'); | |
287 | } | |
288 | // delete related file refernces if html/text/pdf template are chosen over document | |
289 | elseif (!empty($this->_id)) { | |
290 | $entityFileDAO = new CRM_Core_DAO_EntityFile(); | |
291 | $entityFileDAO->entity_id = $this->_id; | |
292 | $entityFileDAO->entity_table = 'civicrm_msg_template'; | |
293 | if ($entityFileDAO->find(TRUE)) { | |
294 | $fileDAO = new CRM_Core_DAO_File(); | |
295 | $fileDAO->id = $entityFileDAO->file_id; | |
296 | $fileDAO->find(TRUE); | |
297 | $entityFileDAO->delete(); | |
298 | $fileDAO->delete(); | |
299 | } | |
300 | } | |
301 | ||
6a488035 TO |
302 | if ($this->_workflow_id) { |
303 | $params['workflow_id'] = $this->_workflow_id; | |
304 | $params['is_active'] = TRUE; | |
305 | } | |
306 | ||
c6327d7d | 307 | $messageTemplate = CRM_Core_BAO_MessageTemplate::add($params); |
6a488035 TO |
308 | CRM_Core_Session::setStatus(ts('The Message Template \'%1\' has been saved.', array(1 => $messageTemplate->msg_title)), ts('Saved'), 'success'); |
309 | ||
310 | if ($this->_workflow_id) { | |
311 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1')); | |
312 | } | |
313 | else { | |
314 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=user&reset=1')); | |
315 | } | |
316 | } | |
317 | } | |
96025800 | 318 | |
6a488035 | 319 | } |