Whitespace fixes (from jshint)
[civicrm-core.git] / CRM / Admin / Form / MessageTemplates.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates form components for Message templates
38 * used by membership, contributions, event registrations, etc.
39 *
40 */
41class CRM_Admin_Form_MessageTemplates extends CRM_Admin_Form {
42 // which (and whether) mailing workflow this template belongs to
430ae6dd
TO
43 protected $_workflow_id = NULL;
44
45 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 /**
58 * This function sets the default values for the form.
59 * The default values are retrieved from the database.
60 *
61 * @access public
62 *
63 * @return None
64 */
65 public function setDefaultValues() {
66 $defaults = $this->_values;
67
68 if (!CRM_Utils_Array::value('pdf_format_id', $defaults)) {
69 $defaults['pdf_format_id'] = 'null';
70 }
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',
79 'selectedChild=user&reset=1'
80 ));
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 }
91 $cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', "selectedChild={$selectedChild}&reset=1");
92 $cancelURL = str_replace('&amp;', '&', $cancelURL);
93 $this->addButtons(
94 array(
95 array(
96 'type' => 'next',
97 'name' => ts('Save'),
98 'isDefault' => TRUE,
99 ),
100 array(
101 'type' => 'cancel',
102 'name' => ts('Cancel'),
103 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"),
104 ),
105 )
106 );
107 }
108
109 return $defaults;
110 }
111
112 /**
113 * Function to build the form
114 *
115 * @return None
116 * @access public
117 */
118 public function buildQuickForm() {
119
120 // For VIEW we only want Done button
121 if ($this->_action & CRM_Core_Action::VIEW) {
122 // currently, the above action is used solely for previewing default workflow templates
123 $cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1');
124 $cancelURL = str_replace('&amp;', '&', $cancelURL);
125 $this->addButtons(array(
126 array(
127 'type' => 'cancel',
128 'name' => ts('Done'),
129 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"),
130 'isDefault' => TRUE,
131 ),
132 )
133 );
134 }
135 else {
136 parent::buildQuickForm();
137 }
138
139 if ($this->_action & CRM_Core_Action::DELETE) {
140 return;
141 }
142
143 $breadCrumb = array(array('title' => ts('Message Templates'),
144 'url' => CRM_Utils_System::url('civicrm/admin/messageTemplates',
145 'action=browse&reset=1'
146 ),
147 ));
148 CRM_Utils_System::appendBreadCrumb($breadCrumb);
149
150 $this->applyFilter('__ALL__', 'trim');
c6327d7d 151 $this->add('text', 'msg_title', ts('Message Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_title'), TRUE);
6a488035
TO
152
153 $this->add('text', 'msg_subject',
154 ts('Message Subject'),
c6327d7d 155 CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_subject')
6a488035
TO
156 );
157
158 //get the tokens.
159 $tokens = CRM_Core_SelectValues::contactTokens();
160
161 //sorted in ascending order tokens by ignoring word case
162 natcasesort($tokens);
163 $this->assign('tokens', json_encode($tokens));
164
165 $this->add('select', 'token1', ts('Insert Tokens'),
166 $tokens, FALSE,
167 array(
168 'size' => "5",
169 'multiple' => TRUE,
170 'onchange' => "return tokenReplText(this);",
171 )
172 );
173
174 $this->add('select', 'token2', ts('Insert Tokens'),
175 $tokens, FALSE,
176 array(
177 'size' => "5",
178 'multiple' => TRUE,
179 'onchange' => "return tokenReplHtml(this);",
180 )
181 );
182
183 $this->add('select', 'token3', ts('Insert Tokens'),
184 $tokens, FALSE,
185 array(
186 'size' => "5",
187 'multiple' => TRUE,
188 'onchange' => "return tokenReplText(this);",
189 )
190 );
191
192 $this->add('textarea', 'msg_text', ts('Text Message'),
193 "cols=50 rows=6"
194 );
195
196 // if not a system message use a wysiwyg editor, CRM-5971
197 if ($this->_id &&
c6327d7d 198 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_MessageTemplate',
6a488035
TO
199 $this->_id,
200 'workflow_id'
201 )
202 ) {
203 $this->add('textarea', 'msg_html', ts('HTML Message'),
204 "cols=50 rows=6"
205 );
206 }
207 else {
208 $this->addWysiwyg('msg_html', ts('HTML Message'),
209 array(
210 'cols' => '80', 'rows' => '8',
211 'onkeyup' => "return verify(this)",
212 )
213 );
214 }
215
216 $this->add('select', 'pdf_format_id', ts('PDF Page Format'),
217 array(
218 'null' => ts('- default -')) + CRM_Core_BAO_PdfFormat::getList(TRUE), FALSE
219 );
220
221 $this->add('checkbox', 'is_active', ts('Enabled?'));
222
223 if ($this->_action & CRM_Core_Action::VIEW) {
224 $this->freeze();
225 CRM_Utils_System::setTitle(ts('View System Default Message Template'));
226 }
227 }
228
229 /**
230 * Function to process the form
231 *
232 * @access public
233 *
234 * @return None
235 */
236 public function postProcess() {
237 if ($this->_action & CRM_Core_Action::DELETE) {
c6327d7d 238 CRM_Core_BAO_MessageTemplate::del($this->_id);
6a488035
TO
239 }
240 elseif ($this->_action & CRM_Core_Action::VIEW) {
241 // currently, the above action is used solely for previewing default workflow templates
242 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
243 }
244 else {
245 $params = array();
246
247 // store the submitted values in an array
248 $params = $this->exportValues();
249
250 if ($this->_action & CRM_Core_Action::UPDATE) {
251 $params['id'] = $this->_id;
252 }
253
254 if ($this->_workflow_id) {
255 $params['workflow_id'] = $this->_workflow_id;
256 $params['is_active'] = TRUE;
257 }
258
c6327d7d 259 $messageTemplate = CRM_Core_BAO_MessageTemplate::add($params);
6a488035
TO
260 CRM_Core_Session::setStatus(ts('The Message Template \'%1\' has been saved.', array(1 => $messageTemplate->msg_title)), ts('Saved'), 'success');
261
262 if ($this->_workflow_id) {
263 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
264 }
265 else {
266 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=user&reset=1'));
267 }
268 }
269 }
270}
271