Merge pull request #4851 from totten/master-createtest-bao
[civicrm-core.git] / CRM / Admin / Form / MessageTemplates.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
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
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
TO
59 * The default values are retrieved from the database.
60 *
6a488035 61 *
355ba699 62 * @return void
6a488035
TO
63 */
64 public function setDefaultValues() {
65 $defaults = $this->_values;
66
a7488080 67 if (empty($defaults['pdf_format_id'])) {
6a488035
TO
68 $defaults['pdf_format_id'] = 'null';
69 }
70
71 $this->_workflow_id = CRM_Utils_Array::value('workflow_id', $defaults);
72 $this->assign('workflow_id', $this->_workflow_id);
73 if ($this->_action & CRM_Core_Action::ADD) {
74 $defaults['is_active'] = 1;
75 //set the context for redirection after form submit or cancel
76 $session = CRM_Core_Session::singleton();
77 $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/messageTemplates',
78 'selectedChild=user&reset=1'
79 ));
80 }
81
82 // FIXME: we need to fix the Cancel button here as we don’t know whether it’s a workflow template in buildQuickForm()
83 if ($this->_action & CRM_Core_Action::UPDATE) {
84 if ($this->_workflow_id) {
85 $selectedChild = 'workflow';
86 }
87 else {
88 $selectedChild = 'user';
89 }
90 $cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', "selectedChild={$selectedChild}&reset=1");
91 $cancelURL = str_replace('&amp;', '&', $cancelURL);
92 $this->addButtons(
93 array(
94 array(
95 'type' => 'next',
96 'name' => ts('Save'),
97 'isDefault' => TRUE,
98 ),
99 array(
100 'type' => 'cancel',
101 'name' => ts('Cancel'),
102 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"),
103 ),
104 )
105 );
106 }
107
108 return $defaults;
109 }
110
111 /**
c490a46a 112 * Build the form object
6a488035 113 *
355ba699 114 * @return void
6a488035
TO
115 */
116 public function buildQuickForm() {
117
118 // For VIEW we only want Done button
119 if ($this->_action & CRM_Core_Action::VIEW) {
120 // currently, the above action is used solely for previewing default workflow templates
121 $cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1');
122 $cancelURL = str_replace('&amp;', '&', $cancelURL);
123 $this->addButtons(array(
124 array(
125 'type' => 'cancel',
126 'name' => ts('Done'),
127 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"),
128 'isDefault' => TRUE,
129 ),
130 )
131 );
132 }
133 else {
134 parent::buildQuickForm();
135 }
136
137 if ($this->_action & CRM_Core_Action::DELETE) {
138 return;
139 }
140
02fc859b
TO
141 $breadCrumb = array(array(
142 'title' => ts('Message Templates'),
6a488035
TO
143 'url' => CRM_Utils_System::url('civicrm/admin/messageTemplates',
144 'action=browse&reset=1'
145 ),
146 ));
147 CRM_Utils_System::appendBreadCrumb($breadCrumb);
148
149 $this->applyFilter('__ALL__', 'trim');
c6327d7d 150 $this->add('text', 'msg_title', ts('Message Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_title'), TRUE);
6a488035
TO
151
152 $this->add('text', 'msg_subject',
153 ts('Message Subject'),
c6327d7d 154 CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_subject')
6a488035
TO
155 );
156
157 //get the tokens.
158 $tokens = CRM_Core_SelectValues::contactTokens();
159
ac0a3db5 160 $this->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens));
6a488035 161
6a488035
TO
162 // if not a system message use a wysiwyg editor, CRM-5971
163 if ($this->_id &&
c6327d7d 164 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_MessageTemplate',
6a488035
TO
165 $this->_id,
166 'workflow_id'
167 )
168 ) {
169 $this->add('textarea', 'msg_html', ts('HTML Message'),
170 "cols=50 rows=6"
171 );
172 }
173 else {
174 $this->addWysiwyg('msg_html', ts('HTML Message'),
175 array(
02fc859b
TO
176 'cols' => '80',
177 'rows' => '8',
6a488035
TO
178 'onkeyup' => "return verify(this)",
179 )
180 );
181 }
182
ef4a9bcd 183 $this->add('textarea', 'msg_text', ts('Text Message'),
184 "cols=50 rows=6"
185 );
186
6a488035
TO
187 $this->add('select', 'pdf_format_id', ts('PDF Page Format'),
188 array(
189 'null' => ts('- default -')) + CRM_Core_BAO_PdfFormat::getList(TRUE), FALSE
190 );
191
192 $this->add('checkbox', 'is_active', ts('Enabled?'));
193
194 if ($this->_action & CRM_Core_Action::VIEW) {
195 $this->freeze();
196 CRM_Utils_System::setTitle(ts('View System Default Message Template'));
197 }
198 }
199
200 /**
c490a46a 201 * Process the form submission
6a488035 202 *
6a488035 203 *
355ba699 204 * @return void
6a488035
TO
205 */
206 public function postProcess() {
207 if ($this->_action & CRM_Core_Action::DELETE) {
c6327d7d 208 CRM_Core_BAO_MessageTemplate::del($this->_id);
6a488035
TO
209 }
210 elseif ($this->_action & CRM_Core_Action::VIEW) {
211 // currently, the above action is used solely for previewing default workflow templates
212 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
213 }
214 else {
215 $params = array();
216
217 // store the submitted values in an array
218 $params = $this->exportValues();
219
220 if ($this->_action & CRM_Core_Action::UPDATE) {
221 $params['id'] = $this->_id;
222 }
223
224 if ($this->_workflow_id) {
225 $params['workflow_id'] = $this->_workflow_id;
226 $params['is_active'] = TRUE;
227 }
228
c6327d7d 229 $messageTemplate = CRM_Core_BAO_MessageTemplate::add($params);
6a488035
TO
230 CRM_Core_Session::setStatus(ts('The Message Template \'%1\' has been saved.', array(1 => $messageTemplate->msg_title)), ts('Saved'), 'success');
231
232 if ($this->_workflow_id) {
233 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
234 }
235 else {
236 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=user&reset=1'));
237 }
238 }
239 }
240}