Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 | 11 | |
33b33abc | 12 | use Civi\Api4\MessageTemplate; |
3c78698e | 13 | use Civi\Token\TokenProcessor; |
33b33abc | 14 | |
6a488035 TO |
15 | /** |
16 | * | |
17 | * @package CRM | |
ca5cec67 | 18 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
19 | */ |
20 | ||
21 | /** | |
22 | * This class generates form components for Message templates | |
23 | * used by membership, contributions, event registrations, etc. | |
6a488035 | 24 | */ |
33b33abc | 25 | class CRM_Admin_Form_MessageTemplates extends CRM_Core_Form { |
62d3ee27 SL |
26 | /** |
27 | * which (and whether) mailing workflow this template belongs to | |
28 | * @var int | |
29 | */ | |
430ae6dd | 30 | protected $_workflow_id = NULL; |
04a76231 | 31 | |
62d3ee27 SL |
32 | /** |
33 | * Is document file is already loaded as default value? | |
33b33abc | 34 | * |
62d3ee27 SL |
35 | * @var bool |
36 | */ | |
a8cd772c | 37 | protected $_is_document = FALSE; |
430ae6dd | 38 | |
88aae6d4 A |
39 | /** |
40 | * @var bool | |
41 | */ | |
42 | public $submitOnce = TRUE; | |
43 | ||
33b33abc | 44 | /** |
45 | * PreProcess form - load existing values. | |
46 | * | |
47 | * @throws \API_Exception | |
48 | * @throws \CRM_Core_Exception | |
49 | * @throws \Civi\API\Exception\UnauthorizedException | |
50 | */ | |
00be9182 | 51 | public function preProcess() { |
6a488035 | 52 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
33b33abc | 53 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); |
6a488035 | 54 | $this->assign('action', $this->_action); |
33b33abc | 55 | $this->_values = []; |
56 | if ($this->_id) { | |
57 | $this->_values = (array) MessageTemplate::get()->addWhere('id', '=', $this->_id)->setSelect(['*'])->execute()->first(); | |
58 | } | |
6a488035 TO |
59 | } |
60 | ||
61 | /** | |
c490a46a | 62 | * Set default values for the form. |
6a488035 | 63 | * |
ce064e4f | 64 | * The default values are retrieved from the database. |
6a488035 TO |
65 | */ |
66 | public function setDefaultValues() { | |
67 | $defaults = $this->_values; | |
68 | ||
a7488080 | 69 | if (empty($defaults['pdf_format_id'])) { |
6a488035 TO |
70 | $defaults['pdf_format_id'] = 'null'; |
71 | } | |
a8cd772c | 72 | if (empty($defaults['file_type'])) { |
73 | $defaults['file_type'] = 0; | |
74 | } | |
6a488035 | 75 | |
6a488035 TO |
76 | if ($this->_action & CRM_Core_Action::ADD) { |
77 | $defaults['is_active'] = 1; | |
6a488035 TO |
78 | } |
79 | ||
6a488035 | 80 | if ($this->_action & CRM_Core_Action::UPDATE) { |
90a73810 | 81 | $documentInfo = CRM_Core_BAO_File::getEntityFile('civicrm_msg_template', $this->_id, TRUE); |
82 | if (!empty($documentInfo)) { | |
a8cd772c | 83 | $defaults['file_type'] = 1; |
84 | $this->_is_document = TRUE; | |
90a73810 | 85 | $this->assign('attachment', $documentInfo); |
86 | } | |
6a488035 TO |
87 | } |
88 | ||
89 | return $defaults; | |
90 | } | |
91 | ||
92 | /** | |
eceb18cc | 93 | * Build the form object. |
6a488035 TO |
94 | */ |
95 | public function buildQuickForm() { | |
6a488035 TO |
96 | // For VIEW we only want Done button |
97 | if ($this->_action & CRM_Core_Action::VIEW) { | |
98 | // currently, the above action is used solely for previewing default workflow templates | |
99 | $cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'); | |
100 | $cancelURL = str_replace('&', '&', $cancelURL); | |
9f462279 | 101 | $this->addButtons([ |
0d48f1cc TO |
102 | [ |
103 | 'type' => 'cancel', | |
104 | 'name' => ts('Done'), | |
105 | 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"], | |
106 | 'isDefault' => TRUE, | |
107 | ], | |
108 | ]); | |
6a488035 TO |
109 | } |
110 | else { | |
9c1bc317 | 111 | $this->_workflow_id = $this->_values['workflow_id'] ?? NULL; |
40a732a9 | 112 | $this->checkUserPermission($this->_workflow_id); |
31340b59 | 113 | $this->assign('isWorkflow', (bool) ($this->_values['workflow_id'] ?? NULL)); |
35ca4a12 MW |
114 | |
115 | if ($this->_workflow_id) { | |
116 | $selectedChild = 'workflow'; | |
117 | } | |
118 | else { | |
119 | $selectedChild = 'user'; | |
120 | } | |
121 | ||
122 | $cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', "selectedChild={$selectedChild}&reset=1"); | |
123 | $cancelURL = str_replace('&', '&', $cancelURL); | |
9f462279 | 124 | $buttons[] = [ |
ae4b5884 MW |
125 | 'type' => 'upload', |
126 | 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'), | |
127 | 'isDefault' => TRUE, | |
9f462279 | 128 | ]; |
ae4b5884 | 129 | if (!($this->_action & CRM_Core_Action::DELETE)) { |
9f462279 | 130 | $buttons[] = [ |
ae4b5884 MW |
131 | 'type' => 'submit', |
132 | 'name' => ts('Save and Done'), | |
133 | 'subName' => 'done', | |
9f462279 | 134 | ]; |
ae4b5884 | 135 | } |
9f462279 | 136 | $buttons[] = [ |
ae4b5884 MW |
137 | 'type' => 'cancel', |
138 | 'name' => ts('Cancel'), | |
9f462279 | 139 | 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"], |
140 | ]; | |
ae4b5884 | 141 | $this->addButtons($buttons); |
6a488035 TO |
142 | } |
143 | ||
144 | if ($this->_action & CRM_Core_Action::DELETE) { | |
5c38109a | 145 | $this->assign('msg_title', $this->_values['msg_title']); |
6a488035 TO |
146 | return; |
147 | } | |
148 | ||
9f462279 | 149 | $breadCrumb = [ |
150 | [ | |
353ffa53 | 151 | 'title' => ts('Message Templates'), |
9f462279 | 152 | 'url' => CRM_Utils_System::url('civicrm/admin/messageTemplates', 'action=browse&reset=1'), |
153 | ], | |
154 | ]; | |
6a488035 TO |
155 | CRM_Utils_System::appendBreadCrumb($breadCrumb); |
156 | ||
157 | $this->applyFilter('__ALL__', 'trim'); | |
c6327d7d | 158 | $this->add('text', 'msg_title', ts('Message Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_title'), TRUE); |
6a488035 | 159 | |
9f462279 | 160 | $options = [ts('Compose On-screen'), ts('Upload Document')]; |
b3088211 | 161 | $element = $this->addRadio('file_type', ts('Source'), $options); |
162 | if ($this->_id) { | |
163 | $element->freeze(); | |
164 | } | |
a8cd772c | 165 | |
1f46c1b9 | 166 | $this->addElement('file', "file_id", ts('Upload Document'), 'size=30 maxlength=255'); |
90a73810 | 167 | $this->addUploadElement("file_id"); |
168 | ||
6a488035 TO |
169 | $this->add('text', 'msg_subject', |
170 | ts('Message Subject'), | |
c6327d7d | 171 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_subject') |
6a488035 TO |
172 | ); |
173 | ||
3c78698e EM |
174 | $tokenProcessor = new TokenProcessor(Civi::dispatcher(), ['schema' => ['contactId']]); |
175 | $tokens = $tokenProcessor->listTokens(); | |
6a488035 | 176 | |
ac0a3db5 | 177 | $this->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens)); |
6a488035 | 178 | |
6a488035 TO |
179 | // if not a system message use a wysiwyg editor, CRM-5971 |
180 | if ($this->_id && | |
c6327d7d | 181 | CRM_Core_DAO::getFieldValue('CRM_Core_DAO_MessageTemplate', |
6a488035 TO |
182 | $this->_id, |
183 | 'workflow_id' | |
184 | ) | |
185 | ) { | |
186 | $this->add('textarea', 'msg_html', ts('HTML Message'), | |
4367e964 | 187 | ['cols' => 50, 'rows' => 6] |
6a488035 TO |
188 | ); |
189 | } | |
190 | else { | |
5d51a2f9 | 191 | $this->add('wysiwyg', 'msg_html', ts('HTML Message'), |
9f462279 | 192 | [ |
02fc859b | 193 | 'cols' => '80', |
353ffa53 | 194 | 'rows' => '8', |
6a488035 | 195 | 'onkeyup' => "return verify(this)", |
7ad5ae6a | 196 | 'preset' => 'civimail', |
9f462279 | 197 | ] |
6a488035 TO |
198 | ); |
199 | } | |
200 | ||
ef4a9bcd | 201 | $this->add('textarea', 'msg_text', ts('Text Message'), |
4367e964 | 202 | ['cols' => 50, 'rows' => 6] |
ef4a9bcd | 203 | ); |
204 | ||
6a488035 | 205 | $this->add('select', 'pdf_format_id', ts('PDF Page Format'), |
9f462279 | 206 | [ |
8d7a9d07 | 207 | 'null' => ts('- default -'), |
9f462279 | 208 | ] + CRM_Core_BAO_PdfFormat::getList(TRUE), FALSE |
6a488035 TO |
209 | ); |
210 | ||
211 | $this->add('checkbox', 'is_active', ts('Enabled?')); | |
9f462279 | 212 | $this->addFormRule([__CLASS__, 'formRule'], $this); |
90a73810 | 213 | |
6a488035 TO |
214 | if ($this->_action & CRM_Core_Action::VIEW) { |
215 | $this->freeze(); | |
483cfbc4 | 216 | $this->setTitle(ts('View System Default Message Template')); |
6a488035 TO |
217 | } |
218 | } | |
219 | ||
40a732a9 SA |
220 | /** |
221 | * Restrict users access based on permission | |
222 | * | |
223 | * @param int $workflowId | |
224 | */ | |
225 | private function checkUserPermission($workflowId) { | |
226 | if (isset($workflowId)) { | |
227 | $canView = CRM_Core_Permission::check('edit system workflow message templates'); | |
228 | } | |
229 | else { | |
230 | $canView = CRM_Core_Permission::check('edit user-driven message templates'); | |
231 | } | |
232 | ||
233 | if (!$canView && !CRM_Core_Permission::check('edit message templates')) { | |
234 | CRM_Core_Session::setStatus(ts('You do not have permission to view requested page.'), ts('Access Denied')); | |
235 | $url = CRM_Utils_System::url('civicrm/admin/messageTemplates', "reset=1"); | |
236 | CRM_Utils_System::redirect($url); | |
237 | } | |
238 | } | |
239 | ||
90a73810 | 240 | /** |
241 | * Global form rule. | |
242 | * | |
04a76231 | 243 | * @param array $params |
90a73810 | 244 | * The input form values. |
245 | * @param array $files | |
246 | * The uploaded files if any. | |
04a76231 | 247 | * @param array $self |
90a73810 | 248 | * |
249 | * @return array | |
250 | * array of errors | |
251 | */ | |
252 | public static function formRule($params, $files, $self) { | |
100e363e | 253 | // If user uploads non-document file other than odt/docx |
254 | if (!empty($files['file_id']['tmp_name']) && | |
255 | array_search($files['file_id']['type'], CRM_Core_SelectValues::documentApplicationType()) == NULL | |
256 | ) { | |
b93376a8 | 257 | $errors['file_id'] = ts('Invalid document file format'); |
100e363e | 258 | } |
259 | // If default is not set and no document file is uploaded | |
260 | elseif (empty($files['file_id']['tmp_name']) && !empty($params['file_type']) && !$self->_is_document) { | |
90a73810 | 261 | //On edit page of docx/odt message template if user changes file type but forgot to upload document |
a8cd772c | 262 | $errors['file_id'] = ts('Please upload document'); |
90a73810 | 263 | } |
264 | ||
b93376a8 | 265 | return empty($errors) ? TRUE : $errors; |
90a73810 | 266 | } |
267 | ||
6a488035 | 268 | /** |
eceb18cc | 269 | * Process the form submission. |
c74478c8 | 270 | * |
271 | * @throws \API_Exception | |
272 | * @throws \CRM_Core_Exception | |
273 | * @throws \Civi\API\Exception\UnauthorizedException | |
6a488035 TO |
274 | */ |
275 | public function postProcess() { | |
276 | if ($this->_action & CRM_Core_Action::DELETE) { | |
c6327d7d | 277 | CRM_Core_BAO_MessageTemplate::del($this->_id); |
b2d5760e D |
278 | |
279 | $this->postProcessHook(); | |
6a488035 TO |
280 | } |
281 | elseif ($this->_action & CRM_Core_Action::VIEW) { | |
282 | // currently, the above action is used solely for previewing default workflow templates | |
283 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1')); | |
284 | } | |
285 | else { | |
6a488035 | 286 | // store the submitted values in an array |
b93376a8 | 287 | $params = $this->controller->exportValues(); |
6a488035 TO |
288 | |
289 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
290 | $params['id'] = $this->_id; | |
291 | } | |
292 | ||
a8cd772c | 293 | if (!empty($params['file_type'])) { |
90a73810 | 294 | unset($params['msg_html']); |
295 | unset($params['msg_text']); | |
296 | CRM_Utils_File::formatFile($params, 'file_id'); | |
297 | } | |
35ca4a12 | 298 | // delete related file references if html/text/pdf template are chosen over document |
90a73810 | 299 | elseif (!empty($this->_id)) { |
300 | $entityFileDAO = new CRM_Core_DAO_EntityFile(); | |
301 | $entityFileDAO->entity_id = $this->_id; | |
302 | $entityFileDAO->entity_table = 'civicrm_msg_template'; | |
303 | if ($entityFileDAO->find(TRUE)) { | |
304 | $fileDAO = new CRM_Core_DAO_File(); | |
305 | $fileDAO->id = $entityFileDAO->file_id; | |
306 | $fileDAO->find(TRUE); | |
307 | $entityFileDAO->delete(); | |
308 | $fileDAO->delete(); | |
309 | } | |
310 | } | |
311 | ||
9c1bc317 | 312 | $this->_workflow_id = $this->_values['workflow_id'] ?? NULL; |
6a488035 TO |
313 | if ($this->_workflow_id) { |
314 | $params['workflow_id'] = $this->_workflow_id; | |
315 | $params['is_active'] = TRUE; | |
316 | } | |
317 | ||
c74478c8 | 318 | $messageTemplate = MessageTemplate::save()->setDefaults($params)->setRecords([['id' => $this->_id]])->execute()->first(); |
b2d5760e D |
319 | |
320 | // set the id on save, so it can be used in a extension using the posProcess hook | |
321 | $this->_id = $messageTemplate['id']; | |
322 | $this->postProcessHook(); | |
323 | ||
c74478c8 | 324 | CRM_Core_Session::setStatus(ts('The Message Template \'%1\' has been saved.', [1 => $messageTemplate['msg_title']]), ts('Saved'), 'success'); |
6a488035 | 325 | |
b93376a8 MW |
326 | if (isset($this->_submitValues['_qf_MessageTemplates_upload'])) { |
327 | // Save button was pressed | |
c74478c8 | 328 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates/add', "action=update&id={$messageTemplate['id']}&reset=1")); |
ae4b5884 | 329 | } |
b93376a8 | 330 | // Save and done button was pressed |
6a488035 TO |
331 | if ($this->_workflow_id) { |
332 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1')); | |
333 | } | |
ae4b5884 | 334 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=user&reset=1')); |
6a488035 TO |
335 | } |
336 | } | |
96025800 | 337 | |
6a488035 | 338 | } |