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