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