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 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
c4d7103b | 18 | use Civi\Api4\MessageTemplate; |
19 | ||
6a488035 | 20 | require_once 'Mail/mime.php'; |
b5c2afd0 EM |
21 | |
22 | /** | |
8eedd10a | 23 | * Class CRM_Core_BAO_MessageTemplate. |
b5c2afd0 | 24 | */ |
c6327d7d | 25 | class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate { |
6a488035 TO |
26 | |
27 | /** | |
fe482240 | 28 | * Fetch object based on array of properties. |
6a488035 | 29 | * |
6a0b768e TO |
30 | * @param array $params |
31 | * (reference ) an assoc array of name/value pairs. | |
32 | * @param array $defaults | |
33 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 34 | * |
8f86a9e5 | 35 | * @return CRM_Core_DAO_MessageTemplate |
6a488035 | 36 | */ |
00be9182 | 37 | public static function retrieve(&$params, &$defaults) { |
c6327d7d | 38 | $messageTemplates = new CRM_Core_DAO_MessageTemplate(); |
6a488035 TO |
39 | $messageTemplates->copyValues($params); |
40 | if ($messageTemplates->find(TRUE)) { | |
41 | CRM_Core_DAO::storeValues($messageTemplates, $defaults); | |
42 | return $messageTemplates; | |
43 | } | |
44 | return NULL; | |
45 | } | |
46 | ||
47 | /** | |
fe482240 | 48 | * Update the is_active flag in the db. |
6a488035 | 49 | * |
6a0b768e TO |
50 | * @param int $id |
51 | * Id of the database record. | |
52 | * @param bool $is_active | |
53 | * Value we want to set the is_active field. | |
6a488035 | 54 | * |
8a4fede3 | 55 | * @return bool |
56 | * true if we found and updated the object, else false | |
6a488035 | 57 | */ |
00be9182 | 58 | public static function setIsActive($id, $is_active) { |
c6327d7d | 59 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_MessageTemplate', $id, 'is_active', $is_active); |
6a488035 TO |
60 | } |
61 | ||
62 | /** | |
fe482240 | 63 | * Add the Message Templates. |
6a488035 | 64 | * |
6a0b768e TO |
65 | * @param array $params |
66 | * Reference array contains the values submitted by the form. | |
6a488035 | 67 | * |
6a488035 TO |
68 | * |
69 | * @return object | |
8f86a9e5 | 70 | * @throws \CiviCRM_API3_Exception |
71 | * @throws \Civi\API\Exception\UnauthorizedException | |
6a488035 | 72 | */ |
00be9182 | 73 | public static function add(&$params) { |
781ed314 SL |
74 | // System Workflow Templates have a specific wodkflow_id in them but normal user end message templates don't |
75 | // If we have an id check to see if we are update, and need to check if original is a system workflow or not. | |
76 | $systemWorkflowPermissionDeniedMessage = 'Editing or creating system workflow messages requires edit system workflow message templates permission or the edit message templates permission'; | |
77 | $userWorkflowPermissionDeniedMessage = 'Editing or creating user driven workflow messages requires edit user-driven message templates or the edit message templates permission'; | |
78 | if (!empty($params['check_permissions'])) { | |
79 | if (!CRM_Core_Permission::check('edit message templates')) { | |
80 | if (!empty($params['id'])) { | |
81 | $details = civicrm_api3('MessageTemplate', 'getSingle', ['id' => $params['id']]); | |
0c5781ae | 82 | if (!empty($details['workflow_id']) || !empty($details['workflow_name'])) { |
781ed314 SL |
83 | if (!CRM_Core_Permission::check('edit system workflow message templates')) { |
84 | throw new \Civi\API\Exception\UnauthorizedException(ts('%1', [1 => $systemWorkflowPermissionDeniedMessage])); | |
85 | } | |
86 | } | |
87 | elseif (!CRM_Core_Permission::check('edit user-driven message templates')) { | |
88 | throw new \Civi\API\Exception\UnauthorizedException(ts('%1', [1 => $userWorkflowPermissionDeniedMessage])); | |
89 | } | |
90 | } | |
91 | else { | |
0c5781ae | 92 | if (!empty($params['workflow_id']) || !empty($params['workflow_name'])) { |
36111e9f SL |
93 | if (!CRM_Core_Permission::check('edit system workflow message templates')) { |
94 | throw new \Civi\API\Exception\UnauthorizedException(ts('%1', [1 => $systemWorkflowPermissionDeniedMessage])); | |
95 | } | |
781ed314 | 96 | } |
36111e9f | 97 | elseif (!CRM_Core_Permission::check('edit user-driven message templates')) { |
781ed314 SL |
98 | throw new \Civi\API\Exception\UnauthorizedException(ts('%1', [1 => $userWorkflowPermissionDeniedMessage])); |
99 | } | |
100 | } | |
101 | } | |
102 | } | |
3c8059c8 KM |
103 | $hook = empty($params['id']) ? 'create' : 'edit'; |
104 | CRM_Utils_Hook::pre($hook, 'MessageTemplate', CRM_Utils_Array::value('id', $params), $params); | |
c39c0aa1 | 105 | |
90a73810 | 106 | if (!empty($params['file_id']) && is_array($params['file_id']) && count($params['file_id'])) { |
107 | $fileParams = $params['file_id']; | |
108 | unset($params['file_id']); | |
109 | } | |
110 | ||
0c5781ae TO |
111 | // The workflow_id and workflow_name should be sync'd. But what mix of inputs do we have to work with? |
112 | switch ((empty($params['workflow_id']) ? '' : 'id') . (empty($params['workflow_name']) ? '' : 'name')) { | |
113 | case 'id': | |
114 | $params['workflow_name'] = array_search($params['workflow_id'], self::getWorkflowNameIdMap()); | |
115 | break; | |
116 | ||
117 | case 'name': | |
118 | $params['workflow_id'] = self::getWorkflowNameIdMap()[$params['workflow_name']] ?? NULL; | |
119 | break; | |
120 | ||
121 | case 'idname': | |
122 | $map = self::getWorkflowNameIdMap(); | |
123 | if ($map[$params['workflow_name']] != $params['workflow_id']) { | |
124 | throw new CRM_Core_Exception("The workflow_id and workflow_name are mismatched. Note: You only need to submit one or the other."); | |
125 | } | |
126 | break; | |
127 | ||
128 | case '': | |
129 | // OK, don't care. | |
130 | break; | |
131 | ||
132 | default: | |
133 | throw new \RuntimeException("Bad code"); | |
134 | } | |
135 | ||
c6327d7d | 136 | $messageTemplates = new CRM_Core_DAO_MessageTemplate(); |
6a488035 | 137 | $messageTemplates->copyValues($params); |
6a488035 | 138 | $messageTemplates->save(); |
c39c0aa1 | 139 | |
90a73810 | 140 | if (!empty($fileParams)) { |
141 | $params['file_id'] = $fileParams; | |
04a76231 | 142 | CRM_Core_BAO_File::filePostProcess( |
90a73810 | 143 | $params['file_id']['location'], |
144 | NULL, | |
145 | 'civicrm_msg_template', | |
146 | $messageTemplates->id, | |
147 | NULL, | |
148 | TRUE, | |
149 | $params['file_id'], | |
150 | 'file_id', | |
151 | $params['file_id']['type'] | |
152 | ); | |
90a73810 | 153 | } |
154 | ||
c9606a53 | 155 | CRM_Utils_Hook::post($hook, 'MessageTemplate', $messageTemplates->id, $messageTemplates); |
6a488035 TO |
156 | return $messageTemplates; |
157 | } | |
158 | ||
159 | /** | |
fe482240 | 160 | * Delete the Message Templates. |
6a488035 | 161 | * |
100fef9d | 162 | * @param int $messageTemplatesID |
8f86a9e5 | 163 | * |
164 | * @throws \CRM_Core_Exception | |
6a488035 | 165 | */ |
00be9182 | 166 | public static function del($messageTemplatesID) { |
6a488035 TO |
167 | // make sure messageTemplatesID is an integer |
168 | if (!CRM_Utils_Rule::positiveInteger($messageTemplatesID)) { | |
8f86a9e5 | 169 | throw new CRM_Core_Exception(ts('Invalid Message template')); |
6a488035 TO |
170 | } |
171 | ||
172 | // Set mailing msg template col to NULL | |
173 | $query = "UPDATE civicrm_mailing | |
174 | SET msg_template_id = NULL | |
175 | WHERE msg_template_id = %1"; | |
5f351616 | 176 | |
be2fb01f | 177 | $params = [1 => [$messageTemplatesID, 'Integer']]; |
6a488035 TO |
178 | CRM_Core_DAO::executeQuery($query, $params); |
179 | ||
c6327d7d | 180 | $messageTemplates = new CRM_Core_DAO_MessageTemplate(); |
6a488035 TO |
181 | $messageTemplates->id = $messageTemplatesID; |
182 | $messageTemplates->delete(); | |
183 | CRM_Core_Session::setStatus(ts('Selected message template has been deleted.'), ts('Deleted'), 'success'); | |
184 | } | |
185 | ||
186 | /** | |
fe482240 | 187 | * Get the Message Templates. |
6a488035 | 188 | * |
6a488035 | 189 | * |
dd244018 EM |
190 | * @param bool $all |
191 | * | |
ad37ac8e | 192 | * @param bool $isSMS |
193 | * | |
8f86a9e5 | 194 | * @return array |
6a488035 | 195 | */ |
00be9182 | 196 | public static function getMessageTemplates($all = TRUE, $isSMS = FALSE) { |
be2fb01f | 197 | $msgTpls = []; |
6a488035 | 198 | |
c6327d7d | 199 | $messageTemplates = new CRM_Core_DAO_MessageTemplate(); |
6a488035 | 200 | $messageTemplates->is_active = 1; |
1e035d58 | 201 | $messageTemplates->is_sms = $isSMS; |
6a488035 TO |
202 | |
203 | if (!$all) { | |
204 | $messageTemplates->workflow_id = 'NULL'; | |
205 | } | |
206 | $messageTemplates->find(); | |
207 | while ($messageTemplates->fetch()) { | |
208 | $msgTpls[$messageTemplates->id] = $messageTemplates->msg_title; | |
209 | } | |
210 | asort($msgTpls); | |
211 | return $msgTpls; | |
212 | } | |
213 | ||
b5c2afd0 | 214 | /** |
100fef9d | 215 | * @param int $contactId |
b5c2afd0 | 216 | * @param $email |
100fef9d | 217 | * @param int $messageTemplateID |
b5c2afd0 EM |
218 | * @param $from |
219 | * | |
e60f24eb | 220 | * @return bool|NULL |
8f86a9e5 | 221 | * @throws \CRM_Core_Exception |
b5c2afd0 | 222 | */ |
00be9182 | 223 | public static function sendReminder($contactId, $email, $messageTemplateID, $from) { |
8fbb09d7 | 224 | CRM_Core_Error::deprecatedWarning('CRM_Core_BAO_MessageTemplate::sendReminder is deprecated and will be removed in a future version of CiviCRM'); |
6a488035 | 225 | |
c6327d7d | 226 | $messageTemplates = new CRM_Core_DAO_MessageTemplate(); |
6a488035 TO |
227 | $messageTemplates->id = $messageTemplateID; |
228 | ||
353ffa53 TO |
229 | $domain = CRM_Core_BAO_Domain::getDomain(); |
230 | $result = NULL; | |
be2fb01f | 231 | $hookTokens = []; |
6a488035 TO |
232 | |
233 | if ($messageTemplates->find(TRUE)) { | |
353ffa53 TO |
234 | $body_text = $messageTemplates->msg_text; |
235 | $body_html = $messageTemplates->msg_html; | |
6a488035 TO |
236 | $body_subject = $messageTemplates->msg_subject; |
237 | if (!$body_text) { | |
238 | $body_text = CRM_Utils_String::htmlToText($body_html); | |
239 | } | |
240 | ||
be2fb01f | 241 | $params = [['contact_id', '=', $contactId, 0, 0]]; |
676a6ff4 | 242 | [$contact] = CRM_Contact_BAO_Query::apiQuery($params); |
6a488035 TO |
243 | |
244 | //CRM-4524 | |
245 | $contact = reset($contact); | |
246 | ||
247 | if (!$contact || is_a($contact, 'CRM_Core_Error')) { | |
248 | return NULL; | |
249 | } | |
250 | ||
251 | //CRM-5734 | |
252 | ||
253 | // get tokens to be replaced | |
254 | $tokens = array_merge(CRM_Utils_Token::getTokens($body_text), | |
353ffa53 TO |
255 | CRM_Utils_Token::getTokens($body_html), |
256 | CRM_Utils_Token::getTokens($body_subject)); | |
6a488035 TO |
257 | |
258 | // get replacement text for these tokens | |
be2fb01f | 259 | $returnProperties = ["preferred_mail_format" => 1]; |
c39c0aa1 | 260 | if (isset($tokens['contact'])) { |
6a488035 | 261 | foreach ($tokens['contact'] as $key => $value) { |
c39c0aa1 | 262 | $returnProperties[$value] = 1; |
6a488035 TO |
263 | } |
264 | } | |
a4965e0c | 265 | [$details] = CRM_Utils_Token::getTokenDetails([$contactId], |
353ffa53 TO |
266 | $returnProperties, |
267 | NULL, NULL, FALSE, | |
268 | $tokens, | |
269 | 'CRM_Core_BAO_MessageTemplate'); | |
481a74f4 | 270 | $contact = reset($details); |
6a488035 TO |
271 | |
272 | // call token hook | |
be2fb01f | 273 | $hookTokens = []; |
6a488035 TO |
274 | CRM_Utils_Hook::tokens($hookTokens); |
275 | $categories = array_keys($hookTokens); | |
276 | ||
c39c0aa1 | 277 | // do replacements in text and html body |
be2fb01f | 278 | $type = ['html', 'text']; |
6a488035 TO |
279 | foreach ($type as $key => $value) { |
280 | $bodyType = "body_{$value}"; | |
281 | if ($$bodyType) { | |
282 | CRM_Utils_Token::replaceGreetingTokens($$bodyType, NULL, $contact['contact_id']); | |
4eeb9a5b | 283 | $$bodyType = CRM_Utils_Token::replaceDomainTokens($$bodyType, $domain, TRUE, $tokens, TRUE); |
ab8a593e | 284 | $$bodyType = CRM_Utils_Token::replaceContactTokens($$bodyType, $contact, FALSE, $tokens, FALSE, TRUE); |
4eeb9a5b | 285 | $$bodyType = CRM_Utils_Token::replaceComponentTokens($$bodyType, $contact, $tokens, TRUE); |
d3e86119 | 286 | $$bodyType = CRM_Utils_Token::replaceHookTokens($$bodyType, $contact, $categories, TRUE); |
6a488035 TO |
287 | } |
288 | } | |
289 | $html = $body_html; | |
290 | $text = $body_text; | |
291 | ||
4ee279f4 | 292 | $smarty = CRM_Core_Smarty::singleton(); |
be2fb01f | 293 | foreach ([ |
518fa0ee SL |
294 | 'text', |
295 | 'html', | |
296 | ] as $elem) { | |
4ee279f4 | 297 | $$elem = $smarty->fetch("string:{$$elem}"); |
298 | } | |
299 | ||
6a488035 | 300 | // do replacements in message subject |
ab8a593e | 301 | $messageSubject = CRM_Utils_Token::replaceContactTokens($body_subject, $contact, FALSE, $tokens); |
4eeb9a5b TO |
302 | $messageSubject = CRM_Utils_Token::replaceDomainTokens($messageSubject, $domain, TRUE, $tokens); |
303 | $messageSubject = CRM_Utils_Token::replaceComponentTokens($messageSubject, $contact, $tokens, TRUE); | |
304 | $messageSubject = CRM_Utils_Token::replaceHookTokens($messageSubject, $contact, $categories, TRUE); | |
6a488035 | 305 | |
4ee279f4 | 306 | $messageSubject = $smarty->fetch("string:{$messageSubject}"); |
307 | ||
6a488035 | 308 | // set up the parameters for CRM_Utils_Mail::send |
be2fb01f | 309 | $mailParams = [ |
6a488035 TO |
310 | 'groupName' => 'Scheduled Reminder Sender', |
311 | 'from' => $from, | |
312 | 'toName' => $contact['display_name'], | |
313 | 'toEmail' => $email, | |
314 | 'subject' => $messageSubject, | |
be2fb01f | 315 | ]; |
6a488035 TO |
316 | if (!$html || $contact['preferred_mail_format'] == 'Text' || |
317 | $contact['preferred_mail_format'] == 'Both' | |
318 | ) { | |
319 | // render the & entities in text mode, so that the links work | |
320 | $mailParams['text'] = str_replace('&', '&', $text); | |
321 | } | |
322 | if ($html && ($contact['preferred_mail_format'] == 'HTML' || | |
323 | $contact['preferred_mail_format'] == 'Both' | |
353ffa53 TO |
324 | ) |
325 | ) { | |
6a488035 TO |
326 | $mailParams['html'] = $html; |
327 | } | |
328 | ||
329 | $result = CRM_Utils_Mail::send($mailParams); | |
330 | } | |
331 | ||
6a488035 TO |
332 | return $result; |
333 | } | |
334 | ||
335 | /** | |
8eedd10a | 336 | * Revert a message template to its default subject+text+HTML state. |
6a488035 | 337 | * |
608e6658 | 338 | * @param int $id id of the template |
8f86a9e5 | 339 | * |
340 | * @throws \CRM_Core_Exception | |
6a488035 | 341 | */ |
00be9182 | 342 | public static function revert($id) { |
608e6658 | 343 | $diverted = new CRM_Core_BAO_MessageTemplate(); |
6a488035 TO |
344 | $diverted->id = (int) $id; |
345 | $diverted->find(1); | |
346 | ||
347 | if ($diverted->N != 1) { | |
8f86a9e5 | 348 | throw new CRM_Core_Exception(ts('Did not find a message template with id of %1.', [1 => $id])); |
6a488035 TO |
349 | } |
350 | ||
608e6658 | 351 | $orig = new CRM_Core_BAO_MessageTemplate(); |
6a488035 TO |
352 | $orig->workflow_id = $diverted->workflow_id; |
353 | $orig->is_reserved = 1; | |
354 | $orig->find(1); | |
355 | ||
356 | if ($orig->N != 1) { | |
8f86a9e5 | 357 | throw new CRM_Core_Exception(ts('Message template with id of %1 does not have a default to revert to.', [1 => $id])); |
6a488035 TO |
358 | } |
359 | ||
360 | $diverted->msg_subject = $orig->msg_subject; | |
361 | $diverted->msg_text = $orig->msg_text; | |
362 | $diverted->msg_html = $orig->msg_html; | |
363 | $diverted->pdf_format_id = is_null($orig->pdf_format_id) ? 'null' : $orig->pdf_format_id; | |
364 | $diverted->save(); | |
365 | } | |
366 | ||
367 | /** | |
fe482240 | 368 | * Send an email from the specified template based on an array of params. |
6a488035 | 369 | * |
6a0b768e TO |
370 | * @param array $params |
371 | * A string-keyed array of function params, see function body for details. | |
6a488035 | 372 | * |
a6c01b45 | 373 | * @return array |
16b10e64 | 374 | * Array of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates |
8f86a9e5 | 375 | * @throws \CRM_Core_Exception |
c4d7103b | 376 | * @throws \API_Exception |
6a488035 | 377 | */ |
00be9182 | 378 | public static function sendTemplate($params) { |
be2fb01f | 379 | $defaults = [ |
6a488035 TO |
380 | // option value name of the template |
381 | 'valueName' => NULL, | |
382 | // ID of the template | |
383 | 'messageTemplateID' => NULL, | |
384 | // contact id if contact tokens are to be replaced | |
385 | 'contactId' => NULL, | |
386 | // additional template params (other than the ones already set in the template singleton) | |
be2fb01f | 387 | 'tplParams' => [], |
6a488035 TO |
388 | // the From: header |
389 | 'from' => NULL, | |
390 | // the recipient’s name | |
391 | 'toName' => NULL, | |
392 | // the recipient’s email - mail is sent only if set | |
393 | 'toEmail' => NULL, | |
394 | // the Cc: header | |
395 | 'cc' => NULL, | |
396 | // the Bcc: header | |
397 | 'bcc' => NULL, | |
398 | // the Reply-To: header | |
399 | 'replyTo' => NULL, | |
400 | // email attachments | |
401 | 'attachments' => NULL, | |
402 | // whether this is a test email (and hence should include the test banner) | |
403 | 'isTest' => FALSE, | |
404 | // filename of optional PDF version to add as attachment (do not include path) | |
405 | 'PDFFilename' => NULL, | |
56f523f5 RLAR |
406 | // Disable Smarty? |
407 | 'disableSmarty' => FALSE, | |
be2fb01f | 408 | ]; |
6a488035 TO |
409 | $params = array_merge($defaults, $params); |
410 | ||
44a2f017 | 411 | // Core#644 - handle Email ID passed as "From". |
4335f229 JG |
412 | if (isset($params['from'])) { |
413 | $params['from'] = CRM_Utils_Mail::formatFromAddress($params['from']); | |
414 | } | |
415 | ||
ededcdab | 416 | CRM_Utils_Hook::alterMailParams($params, 'messageTemplate'); |
a4965e0c | 417 | if (!is_int($params['messageTemplateID']) && !is_null($params['messageTemplateID'])) { |
418 | CRM_Core_Error::deprecatedWarning('message template id should be an integer'); | |
419 | $params['messageTemplateID'] = (int) $params['messageTemplateID']; | |
6a488035 | 420 | } |
a4965e0c | 421 | $mailContent = self::loadTemplate((string) $params['valueName'], $params['isTest'], $params['messageTemplateID'] ?? NULL, $params['groupName'] ?? ''); |
6a488035 | 422 | |
c8025280 SP |
423 | // Overwrite subject from form field |
424 | if (!empty($params['subject'])) { | |
425 | $mailContent['subject'] = $params['subject']; | |
426 | } | |
427 | ||
6f4976a2 | 428 | $mailContent = self::renderMessageTemplate($mailContent, $params['disableSmarty'], $params['contactId'] ?? NULL, $params['tplParams']); |
4ee279f4 | 429 | |
6a488035 TO |
430 | // send the template, honouring the target user’s preferences (if any) |
431 | $sent = FALSE; | |
432 | ||
433 | // create the params array | |
94993cf9 | 434 | $params['subject'] = $mailContent['subject']; |
14bf6806 AF |
435 | $params['text'] = $mailContent['text']; |
436 | $params['html'] = $mailContent['html']; | |
6a488035 TO |
437 | |
438 | if ($params['toEmail']) { | |
be2fb01f | 439 | $contactParams = [['email', 'LIKE', $params['toEmail'], 0, 1]]; |
676a6ff4 | 440 | [$contact] = CRM_Contact_BAO_Query::apiQuery($contactParams); |
6a488035 TO |
441 | |
442 | $prefs = array_pop($contact); | |
443 | ||
8f86a9e5 | 444 | if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] === 'HTML') { |
6a488035 TO |
445 | $params['text'] = NULL; |
446 | } | |
447 | ||
8f86a9e5 | 448 | if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] === 'Text') { |
6a488035 TO |
449 | $params['html'] = NULL; |
450 | } | |
451 | ||
452 | $config = CRM_Core_Config::singleton(); | |
9161952c | 453 | if (isset($params['isEmailPdf']) && $params['isEmailPdf'] == 1) { |
d141946b | 454 | $pdfHtml = CRM_Contribute_BAO_ContributionPage::addInvoicePdfToEmail($params['contributionId'], $params['contactId']); |
9161952c | 455 | if (empty($params['attachments'])) { |
be2fb01f | 456 | $params['attachments'] = []; |
9161952c | 457 | } |
14bf6806 | 458 | $params['attachments'][] = CRM_Utils_Mail::appendPDF('Invoice.pdf', $pdfHtml, $mailContent['format']); |
9161952c | 459 | } |
6a488035 TO |
460 | $pdf_filename = ''; |
461 | if ($config->doNotAttachPDFReceipt && | |
462 | $params['PDFFilename'] && | |
463 | $params['html'] | |
464 | ) { | |
6a488035 | 465 | if (empty($params['attachments'])) { |
be2fb01f | 466 | $params['attachments'] = []; |
6a488035 | 467 | } |
14bf6806 | 468 | $params['attachments'][] = CRM_Utils_Mail::appendPDF($params['PDFFilename'], $params['html'], $mailContent['format']); |
9849720e RK |
469 | if (isset($params['tplParams']['email_comment'])) { |
470 | $params['html'] = $params['tplParams']['email_comment']; | |
d9e4ebe7 | 471 | $params['text'] = strip_tags($params['tplParams']['email_comment']); |
9849720e | 472 | } |
6a488035 TO |
473 | } |
474 | ||
475 | $sent = CRM_Utils_Mail::send($params); | |
476 | ||
477 | if ($pdf_filename) { | |
478 | unlink($pdf_filename); | |
479 | } | |
480 | } | |
481 | ||
be2fb01f | 482 | return [$sent, $mailContent['subject'], $mailContent['text'], $mailContent['html']]; |
6a488035 | 483 | } |
96025800 | 484 | |
0c5781ae TO |
485 | /** |
486 | * Create a map between workflow_name and workflow_id. | |
487 | * | |
488 | * @return array | |
489 | * Array(string $workflowName => int $workflowId) | |
490 | */ | |
491 | protected static function getWorkflowNameIdMap() { | |
492 | // There's probably some more clever way to do this, but this seems simple. | |
493 | return CRM_Core_DAO::executeQuery('SELECT cov.name as name, cov.id as id FROM civicrm_option_group cog INNER JOIN civicrm_option_value cov on cov.option_group_id=cog.id WHERE cog.name LIKE %1', [ | |
494 | 1 => ['msg_tpl_workflow_%', 'String'], | |
495 | ])->fetchMap('name', 'id'); | |
496 | } | |
497 | ||
a4965e0c | 498 | /** |
499 | * Load the specified template. | |
500 | * | |
501 | * @param string $workflowName | |
502 | * @param bool $isTest | |
503 | * @param int|null $messageTemplateID | |
504 | * @param string $groupName | |
505 | * | |
506 | * @return array | |
507 | * @throws \API_Exception | |
508 | * @throws \CRM_Core_Exception | |
509 | */ | |
510 | protected static function loadTemplate(string $workflowName, bool $isTest, int $messageTemplateID = NULL, $groupName = NULL): array { | |
511 | if (!$workflowName && !$messageTemplateID) { | |
512 | throw new CRM_Core_Exception(ts("Message template's option value or ID missing.")); | |
513 | } | |
514 | ||
515 | $apiCall = MessageTemplate::get(FALSE) | |
516 | ->addSelect('msg_subject', 'msg_text', 'msg_html', 'pdf_format_id', 'id') | |
517 | ->addWhere('is_default', '=', 1); | |
518 | ||
519 | if ($messageTemplateID) { | |
520 | $apiCall->addWhere('id', '=', (int) $messageTemplateID); | |
521 | } | |
522 | else { | |
523 | $apiCall->addWhere('workflow_name', '=', $workflowName); | |
524 | } | |
525 | $messageTemplate = $apiCall->execute()->first(); | |
526 | if (empty($messageTemplate['id'])) { | |
527 | if ($messageTemplateID) { | |
528 | throw new CRM_Core_Exception(ts('No such message template: id=%1.', [1 => $messageTemplateID])); | |
529 | } | |
530 | throw new CRM_Core_Exception(ts('No message template with workflow name %2.', [2 => $workflowName])); | |
531 | } | |
532 | ||
533 | $mailContent = [ | |
534 | 'subject' => $messageTemplate['msg_subject'], | |
535 | 'text' => $messageTemplate['msg_text'], | |
536 | 'html' => $messageTemplate['msg_html'], | |
537 | 'format' => $messageTemplate['pdf_format_id'], | |
87c4c149 | 538 | // Workflow name is the field in the message templates table that denotes the |
539 | // workflow the template is used for. This is intended to eventually | |
540 | // replace the non-standard option value/group implementation - see | |
541 | // https://github.com/civicrm/civicrm-core/pull/17227 and the longer | |
542 | // discussion on https://github.com/civicrm/civicrm-core/pull/17180 | |
a4965e0c | 543 | 'workflow_name' => $workflowName, |
544 | // Note messageTemplateID is the id but when present we also know it was specifically requested. | |
545 | 'messageTemplateID' => $messageTemplateID, | |
546 | // Group name & valueName are deprecated parameters. At some point it will not be passed out. | |
547 | // https://github.com/civicrm/civicrm-core/pull/17180 | |
548 | 'groupName' => $groupName, | |
549 | 'valueName' => $workflowName, | |
550 | ]; | |
551 | ||
552 | CRM_Utils_Hook::alterMailContent($mailContent); | |
553 | ||
554 | // add the test banner (if requested) | |
555 | if ($isTest) { | |
556 | $testText = MessageTemplate::get(FALSE) | |
557 | ->setSelect(['msg_subject', 'msg_text', 'msg_html']) | |
558 | ->addWhere('workflow_name', '=', 'test_preview') | |
559 | ->addWhere('is_default', '=', TRUE) | |
560 | ->execute()->first(); | |
561 | ||
562 | $mailContent['subject'] = $testText['msg_subject'] . $mailContent['subject']; | |
563 | $mailContent['text'] = $testText['msg_text'] . $mailContent['text']; | |
564 | $mailContent['html'] = preg_replace('/<body(.*)$/im', "<body\\1\n{$testText['msg_html']}", $mailContent['html']); | |
565 | } | |
566 | ||
567 | return $mailContent; | |
568 | } | |
569 | ||
ab9d3560 | 570 | /** |
571 | * Get an array of the tokens ito be resolved in the template. | |
572 | * | |
573 | * @param array $html | |
574 | * | |
575 | * @return array | |
576 | */ | |
577 | protected static function getTokensToResolve(array $html): array { | |
578 | $mailing = new CRM_Mailing_BAO_Mailing(); | |
579 | $mailing->subject = $html['subject']; | |
580 | $mailing->body_text = $html['text']; | |
581 | $mailing->body_html = $html['html']; | |
582 | return $mailing->getTokens(); | |
583 | } | |
584 | ||
585 | /** | |
586 | * @param array $mailContent | |
587 | * @param array $tokens | |
588 | * @param bool $escapeSmarty | |
589 | * | |
590 | * @return array | |
591 | * @throws \CRM_Core_Exception | |
592 | */ | |
593 | protected static function resolveDomainTokens(array $mailContent, array $tokens, bool $escapeSmarty): array { | |
594 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
595 | $mailContent['subject'] = CRM_Utils_Token::replaceDomainTokens($mailContent['subject'], $domain, FALSE, $tokens['subject'], $escapeSmarty); | |
596 | $mailContent['text'] = CRM_Utils_Token::replaceDomainTokens($mailContent['text'], $domain, FALSE, $tokens['text'], $escapeSmarty); | |
160e7328 | 597 | $mailContent['html'] = CRM_Utils_Token::replaceDomainTokens($mailContent['html'], $domain, TRUE, $tokens['html'], $escapeSmarty); |
ab9d3560 | 598 | return $mailContent; |
599 | } | |
600 | ||
601 | /** | |
602 | * @param $contactID | |
603 | * @param array|null $tokens | |
604 | * @param array $mailContent | |
605 | * @param bool $escapeSmarty | |
606 | * | |
607 | * @return array | |
608 | */ | |
609 | protected static function resolveContactTokens($contactID, ?array $tokens, array $mailContent, bool $escapeSmarty): array { | |
610 | $contactParams = ['contact_id' => $contactID]; | |
611 | $returnProperties = []; | |
612 | ||
613 | if (isset($tokens['subject']['contact'])) { | |
614 | foreach ($tokens['subject']['contact'] as $name) { | |
615 | $returnProperties[$name] = 1; | |
616 | } | |
617 | } | |
618 | ||
619 | if (isset($tokens['text']['contact'])) { | |
620 | foreach ($tokens['text']['contact'] as $name) { | |
621 | $returnProperties[$name] = 1; | |
622 | } | |
623 | } | |
624 | ||
625 | if (isset($tokens['html']['contact'])) { | |
626 | foreach ($tokens['html']['contact'] as $name) { | |
627 | $returnProperties[$name] = 1; | |
628 | } | |
629 | } | |
630 | ||
631 | // @todo CRM-17253 don't resolve contact details if there are no tokens | |
632 | // effectively comment out this next (performance-expensive) line | |
633 | // but unfortunately testing is a bit think on the ground to that needs to | |
634 | // be added. | |
635 | [$contact] = CRM_Utils_Token::getTokenDetails($contactParams, | |
636 | $returnProperties, | |
637 | FALSE, FALSE, NULL, | |
638 | CRM_Utils_Token::flattenTokens($tokens), | |
639 | // we should consider adding valueName here | |
640 | 'CRM_Core_BAO_MessageTemplate' | |
641 | ); | |
642 | $contact = $contact[$contactID]; | |
643 | $mailContent['subject'] = CRM_Utils_Token::replaceContactTokens($mailContent['subject'], $contact, FALSE, $tokens['subject'], FALSE, $escapeSmarty); | |
644 | $mailContent['text'] = CRM_Utils_Token::replaceContactTokens($mailContent['text'], $contact, FALSE, $tokens['text'], FALSE, $escapeSmarty); | |
645 | $mailContent['html'] = CRM_Utils_Token::replaceContactTokens($mailContent['html'], $contact, FALSE, $tokens['html'], FALSE, $escapeSmarty); | |
646 | ||
647 | $contactArray = [$contactID => $contact]; | |
648 | CRM_Utils_Hook::tokenValues($contactArray, | |
649 | [$contactID], | |
650 | NULL, | |
651 | CRM_Utils_Token::flattenTokens($tokens), | |
652 | // we should consider adding valueName here | |
653 | 'CRM_Core_BAO_MessageTemplate' | |
654 | ); | |
655 | $contact = $contactArray[$contactID]; | |
656 | ||
657 | $hookTokens = []; | |
658 | CRM_Utils_Hook::tokens($hookTokens); | |
659 | $categories = array_keys($hookTokens); | |
660 | $mailContent['subject'] = CRM_Utils_Token::replaceHookTokens($mailContent['subject'], $contact, $categories, TRUE); | |
661 | $mailContent['text'] = CRM_Utils_Token::replaceHookTokens($mailContent['text'], $contact, $categories, TRUE); | |
662 | $mailContent['html'] = CRM_Utils_Token::replaceHookTokens($mailContent['html'], $contact, $categories, TRUE); | |
663 | return $mailContent; | |
664 | } | |
665 | ||
666 | /** | |
667 | * @param array $mailContent | |
668 | * @param $tplParams | |
669 | * | |
670 | * @return array | |
671 | */ | |
672 | protected static function parseThroughSmarty(array $mailContent, $tplParams): array { | |
673 | // strip whitespace from ends and turn into a single line | |
674 | $mailContent['subject'] = "{strip}{$mailContent['subject']}{/strip}"; | |
675 | ||
676 | // parse the three elements with Smarty | |
677 | $smarty = CRM_Core_Smarty::singleton(); | |
678 | foreach ($tplParams as $name => $value) { | |
679 | $smarty->assign($name, $value); | |
680 | } | |
681 | foreach (['subject', 'text', 'html'] as $elem) { | |
682 | $mailContent[$elem] = $smarty->fetch("string:{$mailContent[$elem]}"); | |
683 | } | |
684 | return $mailContent; | |
685 | } | |
686 | ||
6f4976a2 | 687 | /** |
688 | * Render the message template, resolving tokens and smarty tokens. | |
689 | * | |
160e7328 | 690 | * As with all BAO methods this should not be called directly outside |
691 | * of tested core code and is highly likely to change. | |
692 | * | |
6f4976a2 | 693 | * @param array $mailContent |
694 | * @param bool $disableSmarty | |
695 | * @param int $contactID | |
696 | * @param array $smartyAssigns | |
697 | * | |
698 | * @return array | |
699 | * @throws \CRM_Core_Exception | |
700 | */ | |
160e7328 | 701 | public static function renderMessageTemplate(array $mailContent, $disableSmarty, $contactID, $smartyAssigns): array { |
6f4976a2 | 702 | $tokens = self::getTokensToResolve($mailContent); |
703 | ||
704 | // When using Smarty we need to pass the $escapeSmarty parameter. | |
705 | $escapeSmarty = !$disableSmarty; | |
706 | ||
707 | $mailContent = self::resolveDomainTokens($mailContent, $tokens, $escapeSmarty); | |
708 | ||
709 | if ($contactID) { | |
710 | $mailContent = self::resolveContactTokens($contactID, $tokens, $mailContent, $escapeSmarty); | |
711 | } | |
712 | ||
713 | // Normally Smarty is run, but it can be disabled using the disableSmarty | |
714 | // parameter, which may be useful for non-core uses of MessageTemplate.send | |
715 | // In particular it helps with the mosaicomsgtpl extension. | |
716 | if (!$disableSmarty) { | |
717 | $mailContent = self::parseThroughSmarty($mailContent, $smartyAssigns); | |
718 | } | |
719 | else { | |
720 | // Since we're not relying on Smarty for this function, we DIY. | |
721 | // strip whitespace from ends and turn into a single line | |
722 | $mailContent['subject'] = trim(preg_replace('/[\r\n]+/', ' ', $mailContent['subject'])); | |
723 | } | |
724 | return $mailContent; | |
725 | } | |
726 | ||
6a488035 | 727 | } |