Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | require_once 'Mail/mime.php'; | |
b5c2afd0 EM |
35 | |
36 | /** | |
8eedd10a | 37 | * Class CRM_Core_BAO_MessageTemplate. |
b5c2afd0 | 38 | */ |
c6327d7d | 39 | class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate { |
6a488035 TO |
40 | |
41 | /** | |
fe482240 | 42 | * Fetch object based on array of properties. |
6a488035 | 43 | * |
6a0b768e TO |
44 | * @param array $params |
45 | * (reference ) an assoc array of name/value pairs. | |
46 | * @param array $defaults | |
47 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 48 | * |
16b10e64 | 49 | * @return CRM_Core_BAO_MessageTemplate |
6a488035 | 50 | */ |
00be9182 | 51 | public static function retrieve(&$params, &$defaults) { |
c6327d7d | 52 | $messageTemplates = new CRM_Core_DAO_MessageTemplate(); |
6a488035 TO |
53 | $messageTemplates->copyValues($params); |
54 | if ($messageTemplates->find(TRUE)) { | |
55 | CRM_Core_DAO::storeValues($messageTemplates, $defaults); | |
56 | return $messageTemplates; | |
57 | } | |
58 | return NULL; | |
59 | } | |
60 | ||
61 | /** | |
fe482240 | 62 | * Update the is_active flag in the db. |
6a488035 | 63 | * |
6a0b768e TO |
64 | * @param int $id |
65 | * Id of the database record. | |
66 | * @param bool $is_active | |
67 | * Value we want to set the is_active field. | |
6a488035 | 68 | * |
8a4fede3 | 69 | * @return bool |
70 | * true if we found and updated the object, else false | |
6a488035 | 71 | */ |
00be9182 | 72 | public static function setIsActive($id, $is_active) { |
c6327d7d | 73 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_MessageTemplate', $id, 'is_active', $is_active); |
6a488035 TO |
74 | } |
75 | ||
76 | /** | |
fe482240 | 77 | * Add the Message Templates. |
6a488035 | 78 | * |
6a0b768e TO |
79 | * @param array $params |
80 | * Reference array contains the values submitted by the form. | |
6a488035 | 81 | * |
6a488035 TO |
82 | * |
83 | * @return object | |
84 | */ | |
00be9182 | 85 | public static function add(&$params) { |
781ed314 SL |
86 | // System Workflow Templates have a specific wodkflow_id in them but normal user end message templates don't |
87 | // If we have an id check to see if we are update, and need to check if original is a system workflow or not. | |
88 | $systemWorkflowPermissionDeniedMessage = 'Editing or creating system workflow messages requires edit system workflow message templates permission or the edit message templates permission'; | |
89 | $userWorkflowPermissionDeniedMessage = 'Editing or creating user driven workflow messages requires edit user-driven message templates or the edit message templates permission'; | |
90 | if (!empty($params['check_permissions'])) { | |
91 | if (!CRM_Core_Permission::check('edit message templates')) { | |
92 | if (!empty($params['id'])) { | |
93 | $details = civicrm_api3('MessageTemplate', 'getSingle', ['id' => $params['id']]); | |
94 | if (!empty($details['workflow_id'])) { | |
95 | if (!CRM_Core_Permission::check('edit system workflow message templates')) { | |
96 | throw new \Civi\API\Exception\UnauthorizedException(ts('%1', [1 => $systemWorkflowPermissionDeniedMessage])); | |
97 | } | |
98 | } | |
99 | elseif (!CRM_Core_Permission::check('edit user-driven message templates')) { | |
100 | throw new \Civi\API\Exception\UnauthorizedException(ts('%1', [1 => $userWorkflowPermissionDeniedMessage])); | |
101 | } | |
102 | } | |
103 | else { | |
104 | if (!empty($params['workflow_id']) && !CRM_Core_Permission::check('edit system workflow message templates')) { | |
105 | throw new \Civi\API\Exception\UnauthorizedException(ts('%1', [1 => $systemWorkflowPermissionDeniedMessage])); | |
106 | } | |
107 | elseif (!CRM_Core_Permission::check('edit user-driven message templates')) { | |
108 | throw new \Civi\API\Exception\UnauthorizedException(ts('%1', [1 => $userWorkflowPermissionDeniedMessage])); | |
109 | } | |
110 | } | |
111 | } | |
112 | } | |
3c8059c8 KM |
113 | $hook = empty($params['id']) ? 'create' : 'edit'; |
114 | CRM_Utils_Hook::pre($hook, 'MessageTemplate', CRM_Utils_Array::value('id', $params), $params); | |
c39c0aa1 | 115 | |
90a73810 | 116 | if (!empty($params['file_id']) && is_array($params['file_id']) && count($params['file_id'])) { |
117 | $fileParams = $params['file_id']; | |
118 | unset($params['file_id']); | |
119 | } | |
120 | ||
c6327d7d | 121 | $messageTemplates = new CRM_Core_DAO_MessageTemplate(); |
6a488035 | 122 | $messageTemplates->copyValues($params); |
6a488035 | 123 | $messageTemplates->save(); |
c39c0aa1 | 124 | |
90a73810 | 125 | if (!empty($fileParams)) { |
126 | $params['file_id'] = $fileParams; | |
04a76231 | 127 | CRM_Core_BAO_File::filePostProcess( |
90a73810 | 128 | $params['file_id']['location'], |
129 | NULL, | |
130 | 'civicrm_msg_template', | |
131 | $messageTemplates->id, | |
132 | NULL, | |
133 | TRUE, | |
134 | $params['file_id'], | |
135 | 'file_id', | |
136 | $params['file_id']['type'] | |
137 | ); | |
90a73810 | 138 | } |
139 | ||
c9606a53 | 140 | CRM_Utils_Hook::post($hook, 'MessageTemplate', $messageTemplates->id, $messageTemplates); |
6a488035 TO |
141 | return $messageTemplates; |
142 | } | |
143 | ||
144 | /** | |
fe482240 | 145 | * Delete the Message Templates. |
6a488035 | 146 | * |
100fef9d | 147 | * @param int $messageTemplatesID |
6a488035 | 148 | */ |
00be9182 | 149 | public static function del($messageTemplatesID) { |
6a488035 TO |
150 | // make sure messageTemplatesID is an integer |
151 | if (!CRM_Utils_Rule::positiveInteger($messageTemplatesID)) { | |
152 | CRM_Core_Error::fatal(ts('Invalid Message template')); | |
153 | } | |
154 | ||
155 | // Set mailing msg template col to NULL | |
156 | $query = "UPDATE civicrm_mailing | |
157 | SET msg_template_id = NULL | |
158 | WHERE msg_template_id = %1"; | |
5f351616 | 159 | |
be2fb01f | 160 | $params = [1 => [$messageTemplatesID, 'Integer']]; |
6a488035 TO |
161 | CRM_Core_DAO::executeQuery($query, $params); |
162 | ||
c6327d7d | 163 | $messageTemplates = new CRM_Core_DAO_MessageTemplate(); |
6a488035 TO |
164 | $messageTemplates->id = $messageTemplatesID; |
165 | $messageTemplates->delete(); | |
166 | CRM_Core_Session::setStatus(ts('Selected message template has been deleted.'), ts('Deleted'), 'success'); | |
167 | } | |
168 | ||
169 | /** | |
fe482240 | 170 | * Get the Message Templates. |
6a488035 | 171 | * |
6a488035 | 172 | * |
dd244018 EM |
173 | * @param bool $all |
174 | * | |
ad37ac8e | 175 | * @param bool $isSMS |
176 | * | |
6a488035 TO |
177 | * @return object |
178 | */ | |
00be9182 | 179 | public static function getMessageTemplates($all = TRUE, $isSMS = FALSE) { |
be2fb01f | 180 | $msgTpls = []; |
6a488035 | 181 | |
c6327d7d | 182 | $messageTemplates = new CRM_Core_DAO_MessageTemplate(); |
6a488035 | 183 | $messageTemplates->is_active = 1; |
1e035d58 | 184 | $messageTemplates->is_sms = $isSMS; |
6a488035 TO |
185 | |
186 | if (!$all) { | |
187 | $messageTemplates->workflow_id = 'NULL'; | |
188 | } | |
189 | $messageTemplates->find(); | |
190 | while ($messageTemplates->fetch()) { | |
191 | $msgTpls[$messageTemplates->id] = $messageTemplates->msg_title; | |
192 | } | |
193 | asort($msgTpls); | |
194 | return $msgTpls; | |
195 | } | |
196 | ||
b5c2afd0 | 197 | /** |
100fef9d | 198 | * @param int $contactId |
b5c2afd0 | 199 | * @param $email |
100fef9d | 200 | * @param int $messageTemplateID |
b5c2afd0 EM |
201 | * @param $from |
202 | * | |
e60f24eb | 203 | * @return bool|NULL |
b5c2afd0 | 204 | */ |
00be9182 | 205 | public static function sendReminder($contactId, $email, $messageTemplateID, $from) { |
6a488035 | 206 | |
c6327d7d | 207 | $messageTemplates = new CRM_Core_DAO_MessageTemplate(); |
6a488035 TO |
208 | $messageTemplates->id = $messageTemplateID; |
209 | ||
353ffa53 TO |
210 | $domain = CRM_Core_BAO_Domain::getDomain(); |
211 | $result = NULL; | |
be2fb01f | 212 | $hookTokens = []; |
6a488035 TO |
213 | |
214 | if ($messageTemplates->find(TRUE)) { | |
353ffa53 TO |
215 | $body_text = $messageTemplates->msg_text; |
216 | $body_html = $messageTemplates->msg_html; | |
6a488035 TO |
217 | $body_subject = $messageTemplates->msg_subject; |
218 | if (!$body_text) { | |
219 | $body_text = CRM_Utils_String::htmlToText($body_html); | |
220 | } | |
221 | ||
be2fb01f | 222 | $params = [['contact_id', '=', $contactId, 0, 0]]; |
6a488035 TO |
223 | list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($params); |
224 | ||
225 | //CRM-4524 | |
226 | $contact = reset($contact); | |
227 | ||
228 | if (!$contact || is_a($contact, 'CRM_Core_Error')) { | |
229 | return NULL; | |
230 | } | |
231 | ||
232 | //CRM-5734 | |
233 | ||
234 | // get tokens to be replaced | |
235 | $tokens = array_merge(CRM_Utils_Token::getTokens($body_text), | |
353ffa53 TO |
236 | CRM_Utils_Token::getTokens($body_html), |
237 | CRM_Utils_Token::getTokens($body_subject)); | |
6a488035 TO |
238 | |
239 | // get replacement text for these tokens | |
be2fb01f | 240 | $returnProperties = ["preferred_mail_format" => 1]; |
c39c0aa1 | 241 | if (isset($tokens['contact'])) { |
6a488035 | 242 | foreach ($tokens['contact'] as $key => $value) { |
c39c0aa1 | 243 | $returnProperties[$value] = 1; |
6a488035 TO |
244 | } |
245 | } | |
be2fb01f | 246 | list($details) = CRM_Utils_Token::getTokenDetails([$contactId], |
353ffa53 TO |
247 | $returnProperties, |
248 | NULL, NULL, FALSE, | |
249 | $tokens, | |
250 | 'CRM_Core_BAO_MessageTemplate'); | |
481a74f4 | 251 | $contact = reset($details); |
6a488035 TO |
252 | |
253 | // call token hook | |
be2fb01f | 254 | $hookTokens = []; |
6a488035 TO |
255 | CRM_Utils_Hook::tokens($hookTokens); |
256 | $categories = array_keys($hookTokens); | |
257 | ||
c39c0aa1 | 258 | // do replacements in text and html body |
be2fb01f | 259 | $type = ['html', 'text']; |
6a488035 TO |
260 | foreach ($type as $key => $value) { |
261 | $bodyType = "body_{$value}"; | |
262 | if ($$bodyType) { | |
263 | CRM_Utils_Token::replaceGreetingTokens($$bodyType, NULL, $contact['contact_id']); | |
4eeb9a5b | 264 | $$bodyType = CRM_Utils_Token::replaceDomainTokens($$bodyType, $domain, TRUE, $tokens, TRUE); |
ab8a593e | 265 | $$bodyType = CRM_Utils_Token::replaceContactTokens($$bodyType, $contact, FALSE, $tokens, FALSE, TRUE); |
4eeb9a5b | 266 | $$bodyType = CRM_Utils_Token::replaceComponentTokens($$bodyType, $contact, $tokens, TRUE); |
d3e86119 | 267 | $$bodyType = CRM_Utils_Token::replaceHookTokens($$bodyType, $contact, $categories, TRUE); |
6a488035 TO |
268 | } |
269 | } | |
270 | $html = $body_html; | |
271 | $text = $body_text; | |
272 | ||
4ee279f4 | 273 | $smarty = CRM_Core_Smarty::singleton(); |
be2fb01f | 274 | foreach ([ |
353ffa53 | 275 | 'text', |
608e6658 | 276 | 'html', |
be2fb01f | 277 | ] as $elem) { |
4ee279f4 | 278 | $$elem = $smarty->fetch("string:{$$elem}"); |
279 | } | |
280 | ||
6a488035 | 281 | // do replacements in message subject |
ab8a593e | 282 | $messageSubject = CRM_Utils_Token::replaceContactTokens($body_subject, $contact, FALSE, $tokens); |
4eeb9a5b TO |
283 | $messageSubject = CRM_Utils_Token::replaceDomainTokens($messageSubject, $domain, TRUE, $tokens); |
284 | $messageSubject = CRM_Utils_Token::replaceComponentTokens($messageSubject, $contact, $tokens, TRUE); | |
285 | $messageSubject = CRM_Utils_Token::replaceHookTokens($messageSubject, $contact, $categories, TRUE); | |
6a488035 | 286 | |
4ee279f4 | 287 | $messageSubject = $smarty->fetch("string:{$messageSubject}"); |
288 | ||
6a488035 | 289 | // set up the parameters for CRM_Utils_Mail::send |
be2fb01f | 290 | $mailParams = [ |
6a488035 TO |
291 | 'groupName' => 'Scheduled Reminder Sender', |
292 | 'from' => $from, | |
293 | 'toName' => $contact['display_name'], | |
294 | 'toEmail' => $email, | |
295 | 'subject' => $messageSubject, | |
be2fb01f | 296 | ]; |
6a488035 TO |
297 | if (!$html || $contact['preferred_mail_format'] == 'Text' || |
298 | $contact['preferred_mail_format'] == 'Both' | |
299 | ) { | |
300 | // render the & entities in text mode, so that the links work | |
301 | $mailParams['text'] = str_replace('&', '&', $text); | |
302 | } | |
303 | if ($html && ($contact['preferred_mail_format'] == 'HTML' || | |
304 | $contact['preferred_mail_format'] == 'Both' | |
353ffa53 TO |
305 | ) |
306 | ) { | |
6a488035 TO |
307 | $mailParams['html'] = $html; |
308 | } | |
309 | ||
310 | $result = CRM_Utils_Mail::send($mailParams); | |
311 | } | |
312 | ||
313 | $messageTemplates->free(); | |
314 | ||
315 | return $result; | |
316 | } | |
317 | ||
318 | /** | |
8eedd10a | 319 | * Revert a message template to its default subject+text+HTML state. |
6a488035 | 320 | * |
608e6658 | 321 | * @param int $id id of the template |
6a488035 | 322 | */ |
00be9182 | 323 | public static function revert($id) { |
608e6658 | 324 | $diverted = new CRM_Core_BAO_MessageTemplate(); |
6a488035 TO |
325 | $diverted->id = (int) $id; |
326 | $diverted->find(1); | |
327 | ||
328 | if ($diverted->N != 1) { | |
be2fb01f | 329 | CRM_Core_Error::fatal(ts('Did not find a message template with id of %1.', [1 => $id])); |
6a488035 TO |
330 | } |
331 | ||
608e6658 | 332 | $orig = new CRM_Core_BAO_MessageTemplate(); |
6a488035 TO |
333 | $orig->workflow_id = $diverted->workflow_id; |
334 | $orig->is_reserved = 1; | |
335 | $orig->find(1); | |
336 | ||
337 | if ($orig->N != 1) { | |
be2fb01f | 338 | CRM_Core_Error::fatal(ts('Message template with id of %1 does not have a default to revert to.', [1 => $id])); |
6a488035 TO |
339 | } |
340 | ||
341 | $diverted->msg_subject = $orig->msg_subject; | |
342 | $diverted->msg_text = $orig->msg_text; | |
343 | $diverted->msg_html = $orig->msg_html; | |
344 | $diverted->pdf_format_id = is_null($orig->pdf_format_id) ? 'null' : $orig->pdf_format_id; | |
345 | $diverted->save(); | |
346 | } | |
347 | ||
348 | /** | |
fe482240 | 349 | * Send an email from the specified template based on an array of params. |
6a488035 | 350 | * |
6a0b768e TO |
351 | * @param array $params |
352 | * A string-keyed array of function params, see function body for details. | |
6a488035 | 353 | * |
a6c01b45 | 354 | * @return array |
16b10e64 | 355 | * Array of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates |
6a488035 | 356 | */ |
00be9182 | 357 | public static function sendTemplate($params) { |
be2fb01f | 358 | $defaults = [ |
6a488035 TO |
359 | // option group name of the template |
360 | 'groupName' => NULL, | |
361 | // option value name of the template | |
362 | 'valueName' => NULL, | |
363 | // ID of the template | |
364 | 'messageTemplateID' => NULL, | |
365 | // contact id if contact tokens are to be replaced | |
366 | 'contactId' => NULL, | |
367 | // additional template params (other than the ones already set in the template singleton) | |
be2fb01f | 368 | 'tplParams' => [], |
6a488035 TO |
369 | // the From: header |
370 | 'from' => NULL, | |
371 | // the recipient’s name | |
372 | 'toName' => NULL, | |
373 | // the recipient’s email - mail is sent only if set | |
374 | 'toEmail' => NULL, | |
375 | // the Cc: header | |
376 | 'cc' => NULL, | |
377 | // the Bcc: header | |
378 | 'bcc' => NULL, | |
379 | // the Reply-To: header | |
380 | 'replyTo' => NULL, | |
381 | // email attachments | |
382 | 'attachments' => NULL, | |
383 | // whether this is a test email (and hence should include the test banner) | |
384 | 'isTest' => FALSE, | |
385 | // filename of optional PDF version to add as attachment (do not include path) | |
386 | 'PDFFilename' => NULL, | |
be2fb01f | 387 | ]; |
6a488035 TO |
388 | $params = array_merge($defaults, $params); |
389 | ||
4335f229 JG |
390 | // Core#644 - handle contact ID passed as "From". |
391 | if (isset($params['from'])) { | |
392 | $params['from'] = CRM_Utils_Mail::formatFromAddress($params['from']); | |
393 | } | |
394 | ||
ededcdab BS |
395 | CRM_Utils_Hook::alterMailParams($params, 'messageTemplate'); |
396 | ||
6a488035 TO |
397 | if ((!$params['groupName'] || |
398 | !$params['valueName'] | |
399 | ) && | |
400 | !$params['messageTemplateID'] | |
401 | ) { | |
402 | CRM_Core_Error::fatal(ts("Message template's option group and/or option value or ID missing.")); | |
403 | } | |
404 | ||
405 | if ($params['messageTemplateID']) { | |
406 | // fetch the three elements from the db based on id | |
407 | $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format | |
408 | FROM civicrm_msg_template mt | |
409 | WHERE mt.id = %1 AND mt.is_default = 1'; | |
be2fb01f | 410 | $sqlParams = [1 => [$params['messageTemplateID'], 'String']]; |
6a488035 TO |
411 | } |
412 | else { | |
413 | // fetch the three elements from the db based on option_group and option_value names | |
414 | $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format | |
415 | FROM civicrm_msg_template mt | |
416 | JOIN civicrm_option_value ov ON workflow_id = ov.id | |
417 | JOIN civicrm_option_group og ON ov.option_group_id = og.id | |
418 | WHERE og.name = %1 AND ov.name = %2 AND mt.is_default = 1'; | |
be2fb01f | 419 | $sqlParams = [1 => [$params['groupName'], 'String'], 2 => [$params['valueName'], 'String']]; |
6a488035 TO |
420 | } |
421 | $dao = CRM_Core_DAO::executeQuery($query, $sqlParams); | |
422 | $dao->fetch(); | |
423 | ||
424 | if (!$dao->N) { | |
425 | if ($params['messageTemplateID']) { | |
be2fb01f | 426 | CRM_Core_Error::fatal(ts('No such message template: id=%1.', [1 => $params['messageTemplateID']])); |
6a488035 TO |
427 | } |
428 | else { | |
be2fb01f | 429 | CRM_Core_Error::fatal(ts('No such message template: option group %1, option value %2.', [ |
353ffa53 | 430 | 1 => $params['groupName'], |
608e6658 | 431 | 2 => $params['valueName'], |
be2fb01f | 432 | ])); |
6a488035 TO |
433 | } |
434 | } | |
435 | ||
be2fb01f | 436 | $mailContent = [ |
14bf6806 AF |
437 | 'subject' => $dao->subject, |
438 | 'text' => $dao->text, | |
439 | 'html' => $dao->html, | |
440 | 'format' => $dao->format, | |
87098536 BS |
441 | 'groupName' => $params['groupName'], |
442 | 'valueName' => $params['valueName'], | |
443 | 'messageTemplateID' => $params['messageTemplateID'], | |
be2fb01f | 444 | ]; |
6a488035 TO |
445 | $dao->free(); |
446 | ||
14bf6806 AF |
447 | CRM_Utils_Hook::alterMailContent($mailContent); |
448 | ||
6a488035 TO |
449 | // add the test banner (if requested) |
450 | if ($params['isTest']) { | |
451 | $query = "SELECT msg_subject subject, msg_text text, msg_html html | |
452 | FROM civicrm_msg_template mt | |
453 | JOIN civicrm_option_value ov ON workflow_id = ov.id | |
454 | JOIN civicrm_option_group og ON ov.option_group_id = og.id | |
455 | WHERE og.name = 'msg_tpl_workflow_meta' AND ov.name = 'test_preview' AND mt.is_default = 1"; | |
456 | $testDao = CRM_Core_DAO::executeQuery($query); | |
457 | $testDao->fetch(); | |
458 | ||
14bf6806 AF |
459 | $mailContent['subject'] = $testDao->subject . $mailContent['subject']; |
460 | $mailContent['text'] = $testDao->text . $mailContent['text']; | |
461 | $mailContent['html'] = preg_replace('/<body(.*)$/im', "<body\\1\n{$testDao->html}", $mailContent['html']); | |
6a488035 TO |
462 | $testDao->free(); |
463 | } | |
464 | ||
465 | // replace tokens in the three elements (in subject as if it was the text body) | |
353ffa53 | 466 | $domain = CRM_Core_BAO_Domain::getDomain(); |
be2fb01f | 467 | $hookTokens = []; |
608e6658 | 468 | $mailing = new CRM_Mailing_BAO_Mailing(); |
94993cf9 | 469 | $mailing->subject = $mailContent['subject']; |
14bf6806 AF |
470 | $mailing->body_text = $mailContent['text']; |
471 | $mailing->body_html = $mailContent['html']; | |
353ffa53 | 472 | $tokens = $mailing->getTokens(); |
6a488035 TO |
473 | CRM_Utils_Hook::tokens($hookTokens); |
474 | $categories = array_keys($hookTokens); | |
475 | ||
476 | $contactID = CRM_Utils_Array::value('contactId', $params); | |
477 | ||
478 | if ($contactID) { | |
be2fb01f CW |
479 | $contactParams = ['contact_id' => $contactID]; |
480 | $returnProperties = []; | |
6a488035 | 481 | |
a7c64830 | 482 | if (isset($tokens['subject']['contact'])) { |
483 | foreach ($tokens['subject']['contact'] as $name) { | |
484 | $returnProperties[$name] = 1; | |
485 | } | |
486 | } | |
487 | ||
6a488035 TO |
488 | if (isset($tokens['text']['contact'])) { |
489 | foreach ($tokens['text']['contact'] as $name) { | |
490 | $returnProperties[$name] = 1; | |
491 | } | |
492 | } | |
493 | ||
494 | if (isset($tokens['html']['contact'])) { | |
495 | foreach ($tokens['html']['contact'] as $name) { | |
496 | $returnProperties[$name] = 1; | |
497 | } | |
498 | } | |
b8a4ead8 | 499 | |
500 | // @todo CRM-17253 don't resolve contact details if there are no tokens | |
501 | // effectively comment out this next (performance-expensive) line | |
502 | // but unfortunately testing is a bit think on the ground to that needs to | |
503 | // be added. | |
6a488035 TO |
504 | list($contact) = CRM_Utils_Token::getTokenDetails($contactParams, |
505 | $returnProperties, | |
506 | FALSE, FALSE, NULL, | |
507 | CRM_Utils_Token::flattenTokens($tokens), | |
508 | // we should consider adding groupName and valueName here | |
509 | 'CRM_Core_BAO_MessageTemplate' | |
510 | ); | |
511 | $contact = $contact[$contactID]; | |
512 | } | |
513 | ||
ccb438e6 | 514 | $mailContent['subject'] = CRM_Utils_Token::replaceDomainTokens($mailContent['subject'], $domain, FALSE, $tokens['subject'], TRUE); |
14bf6806 AF |
515 | $mailContent['text'] = CRM_Utils_Token::replaceDomainTokens($mailContent['text'], $domain, FALSE, $tokens['text'], TRUE); |
516 | $mailContent['html'] = CRM_Utils_Token::replaceDomainTokens($mailContent['html'], $domain, TRUE, $tokens['html'], TRUE); | |
6a488035 TO |
517 | |
518 | if ($contactID) { | |
ccb438e6 | 519 | $mailContent['subject'] = CRM_Utils_Token::replaceContactTokens($mailContent['subject'], $contact, FALSE, $tokens['subject'], FALSE, TRUE); |
14bf6806 AF |
520 | $mailContent['text'] = CRM_Utils_Token::replaceContactTokens($mailContent['text'], $contact, FALSE, $tokens['text'], FALSE, TRUE); |
521 | $mailContent['html'] = CRM_Utils_Token::replaceContactTokens($mailContent['html'], $contact, FALSE, $tokens['html'], FALSE, TRUE); | |
6a488035 | 522 | |
be2fb01f | 523 | $contactArray = [$contactID => $contact]; |
6a488035 | 524 | CRM_Utils_Hook::tokenValues($contactArray, |
be2fb01f | 525 | [$contactID], |
6a488035 TO |
526 | NULL, |
527 | CRM_Utils_Token::flattenTokens($tokens), | |
528 | // we should consider adding groupName and valueName here | |
529 | 'CRM_Core_BAO_MessageTemplate' | |
530 | ); | |
531 | $contact = $contactArray[$contactID]; | |
532 | ||
94993cf9 | 533 | $mailContent['subject'] = CRM_Utils_Token::replaceHookTokens($mailContent['subject'], $contact, $categories, TRUE); |
14bf6806 AF |
534 | $mailContent['text'] = CRM_Utils_Token::replaceHookTokens($mailContent['text'], $contact, $categories, TRUE); |
535 | $mailContent['html'] = CRM_Utils_Token::replaceHookTokens($mailContent['html'], $contact, $categories, TRUE); | |
6a488035 TO |
536 | } |
537 | ||
4ee279f4 | 538 | // strip whitespace from ends and turn into a single line |
94993cf9 | 539 | $mailContent['subject'] = "{strip}{$mailContent['subject']}{/strip}"; |
4ee279f4 | 540 | |
541 | // parse the three elements with Smarty | |
4ee279f4 | 542 | $smarty = CRM_Core_Smarty::singleton(); |
543 | foreach ($params['tplParams'] as $name => $value) { | |
544 | $smarty->assign($name, $value); | |
545 | } | |
be2fb01f | 546 | foreach ([ |
eff0a941 AF |
547 | 'subject', |
548 | 'text', | |
549 | 'html', | |
be2fb01f | 550 | ] as $elem) { |
eff0a941 | 551 | $mailContent[$elem] = $smarty->fetch("string:{$mailContent[$elem]}"); |
4ee279f4 | 552 | } |
553 | ||
6a488035 TO |
554 | // send the template, honouring the target user’s preferences (if any) |
555 | $sent = FALSE; | |
556 | ||
557 | // create the params array | |
94993cf9 | 558 | $params['subject'] = $mailContent['subject']; |
14bf6806 AF |
559 | $params['text'] = $mailContent['text']; |
560 | $params['html'] = $mailContent['html']; | |
6a488035 TO |
561 | |
562 | if ($params['toEmail']) { | |
be2fb01f | 563 | $contactParams = [['email', 'LIKE', $params['toEmail'], 0, 1]]; |
6a488035 TO |
564 | list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($contactParams); |
565 | ||
566 | $prefs = array_pop($contact); | |
567 | ||
568 | if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'HTML') { | |
569 | $params['text'] = NULL; | |
570 | } | |
571 | ||
572 | if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'Text') { | |
573 | $params['html'] = NULL; | |
574 | } | |
575 | ||
576 | $config = CRM_Core_Config::singleton(); | |
9161952c | 577 | if (isset($params['isEmailPdf']) && $params['isEmailPdf'] == 1) { |
d141946b | 578 | $pdfHtml = CRM_Contribute_BAO_ContributionPage::addInvoicePdfToEmail($params['contributionId'], $params['contactId']); |
9161952c | 579 | if (empty($params['attachments'])) { |
be2fb01f | 580 | $params['attachments'] = []; |
9161952c | 581 | } |
14bf6806 | 582 | $params['attachments'][] = CRM_Utils_Mail::appendPDF('Invoice.pdf', $pdfHtml, $mailContent['format']); |
9161952c | 583 | } |
6a488035 TO |
584 | $pdf_filename = ''; |
585 | if ($config->doNotAttachPDFReceipt && | |
586 | $params['PDFFilename'] && | |
587 | $params['html'] | |
588 | ) { | |
6a488035 | 589 | if (empty($params['attachments'])) { |
be2fb01f | 590 | $params['attachments'] = []; |
6a488035 | 591 | } |
14bf6806 | 592 | $params['attachments'][] = CRM_Utils_Mail::appendPDF($params['PDFFilename'], $params['html'], $mailContent['format']); |
9849720e RK |
593 | if (isset($params['tplParams']['email_comment'])) { |
594 | $params['html'] = $params['tplParams']['email_comment']; | |
d9e4ebe7 | 595 | $params['text'] = strip_tags($params['tplParams']['email_comment']); |
9849720e | 596 | } |
6a488035 TO |
597 | } |
598 | ||
599 | $sent = CRM_Utils_Mail::send($params); | |
600 | ||
601 | if ($pdf_filename) { | |
602 | unlink($pdf_filename); | |
603 | } | |
604 | } | |
605 | ||
be2fb01f | 606 | return [$sent, $mailContent['subject'], $mailContent['text'], $mailContent['html']]; |
6a488035 | 607 | } |
96025800 | 608 | |
6a488035 | 609 | } |