3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
34 require_once 'Mail/mime.php';
37 * Class CRM_Core_BAO_MessageTemplate.
39 class CRM_Core_BAO_MessageTemplate
extends CRM_Core_DAO_MessageTemplate
{
42 * Fetch object based on array of properties.
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.
49 * @return CRM_Core_BAO_MessageTemplate
51 public static function retrieve(&$params, &$defaults) {
52 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
53 $messageTemplates->copyValues($params);
54 if ($messageTemplates->find(TRUE)) {
55 CRM_Core_DAO
::storeValues($messageTemplates, $defaults);
56 return $messageTemplates;
62 * Update the is_active flag in the db.
65 * Id of the database record.
66 * @param bool $is_active
67 * Value we want to set the is_active field.
70 * DAO object on success, NULL otherwise
72 public static function setIsActive($id, $is_active) {
73 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_MessageTemplate', $id, 'is_active', $is_active);
77 * Add the Message Templates.
79 * @param array $params
80 * Reference array contains the values submitted by the form.
85 public static function add(&$params) {
86 $hook = empty($params['id']) ?
'create' : 'edit';
87 CRM_Utils_Hook
::pre($hook, 'MessageTemplate', CRM_Utils_Array
::value('id', $params), $params);
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']);
94 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
95 $messageTemplates->copyValues($params);
96 $messageTemplates->save();
98 if (!empty($fileParams)) {
99 $params['file_id'] = $fileParams;
100 CRM_Core_BAO_File
::filePostProcess(
101 $params['file_id']['location'],
103 'civicrm_msg_template',
104 $messageTemplates->id
,
109 $params['file_id']['type']
113 CRM_Utils_Hook
::post($hook, 'MessageTemplate', $messageTemplates->id
, $messageTemplates);
114 return $messageTemplates;
118 * Delete the Message Templates.
120 * @param int $messageTemplatesID
122 public static function del($messageTemplatesID) {
123 // make sure messageTemplatesID is an integer
124 if (!CRM_Utils_Rule
::positiveInteger($messageTemplatesID)) {
125 CRM_Core_Error
::fatal(ts('Invalid Message template'));
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";
133 $params = array(1 => array($messageTemplatesID, 'Integer'));
134 CRM_Core_DAO
::executeQuery($query, $params);
136 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
137 $messageTemplates->id
= $messageTemplatesID;
138 $messageTemplates->delete();
139 CRM_Core_Session
::setStatus(ts('Selected message template has been deleted.'), ts('Deleted'), 'success');
143 * Get the Message Templates.
152 public static function getMessageTemplates($all = TRUE, $isSMS = FALSE) {
155 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
156 $messageTemplates->is_active
= 1;
157 $messageTemplates->is_sms
= $isSMS;
160 $messageTemplates->workflow_id
= 'NULL';
162 $messageTemplates->find();
163 while ($messageTemplates->fetch()) {
164 $msgTpls[$messageTemplates->id
] = $messageTemplates->msg_title
;
171 * @param int $contactId
173 * @param int $messageTemplateID
178 public static function sendReminder($contactId, $email, $messageTemplateID, $from) {
180 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
181 $messageTemplates->id
= $messageTemplateID;
183 $domain = CRM_Core_BAO_Domain
::getDomain();
185 $hookTokens = array();
187 if ($messageTemplates->find(TRUE)) {
188 $body_text = $messageTemplates->msg_text
;
189 $body_html = $messageTemplates->msg_html
;
190 $body_subject = $messageTemplates->msg_subject
;
192 $body_text = CRM_Utils_String
::htmlToText($body_html);
195 $params = array(array('contact_id', '=', $contactId, 0, 0));
196 list($contact, $_) = CRM_Contact_BAO_Query
::apiQuery($params);
199 $contact = reset($contact);
201 if (!$contact ||
is_a($contact, 'CRM_Core_Error')) {
207 // get tokens to be replaced
208 $tokens = array_merge(CRM_Utils_Token
::getTokens($body_text),
209 CRM_Utils_Token
::getTokens($body_html),
210 CRM_Utils_Token
::getTokens($body_subject));
212 // get replacement text for these tokens
213 $returnProperties = array("preferred_mail_format" => 1);
214 if (isset($tokens['contact'])) {
215 foreach ($tokens['contact'] as $key => $value) {
216 $returnProperties[$value] = 1;
219 list($details) = CRM_Utils_Token
::getTokenDetails(array($contactId),
223 'CRM_Core_BAO_MessageTemplate');
224 $contact = reset($details);
227 $hookTokens = array();
228 CRM_Utils_Hook
::tokens($hookTokens);
229 $categories = array_keys($hookTokens);
231 // do replacements in text and html body
232 $type = array('html', 'text');
233 foreach ($type as $key => $value) {
234 $bodyType = "body_{$value}";
236 CRM_Utils_Token
::replaceGreetingTokens($
$bodyType, NULL, $contact['contact_id']);
237 $
$bodyType = CRM_Utils_Token
::replaceDomainTokens($
$bodyType, $domain, TRUE, $tokens, TRUE);
238 $
$bodyType = CRM_Utils_Token
::replaceContactTokens($
$bodyType, $contact, FALSE, $tokens, FALSE, TRUE);
239 $
$bodyType = CRM_Utils_Token
::replaceComponentTokens($
$bodyType, $contact, $tokens, TRUE);
240 $
$bodyType = CRM_Utils_Token
::replaceHookTokens($
$bodyType, $contact, $categories, TRUE);
246 $smarty = CRM_Core_Smarty
::singleton();
251 $
$elem = $smarty->fetch("string:{$$elem}");
254 // do replacements in message subject
255 $messageSubject = CRM_Utils_Token
::replaceContactTokens($body_subject, $contact, FALSE, $tokens);
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);
260 $messageSubject = $smarty->fetch("string:{$messageSubject}");
262 // set up the parameters for CRM_Utils_Mail::send
264 'groupName' => 'Scheduled Reminder Sender',
266 'toName' => $contact['display_name'],
268 'subject' => $messageSubject,
270 if (!$html ||
$contact['preferred_mail_format'] == 'Text' ||
271 $contact['preferred_mail_format'] == 'Both'
273 // render the & entities in text mode, so that the links work
274 $mailParams['text'] = str_replace('&', '&', $text);
276 if ($html && ($contact['preferred_mail_format'] == 'HTML' ||
277 $contact['preferred_mail_format'] == 'Both'
280 $mailParams['html'] = $html;
283 $result = CRM_Utils_Mail
::send($mailParams);
286 $messageTemplates->free();
292 * Revert a message template to its default subject+text+HTML state.
294 * @param int $id id of the template
296 public static function revert($id) {
297 $diverted = new CRM_Core_BAO_MessageTemplate();
298 $diverted->id
= (int) $id;
301 if ($diverted->N
!= 1) {
302 CRM_Core_Error
::fatal(ts('Did not find a message template with id of %1.', array(1 => $id)));
305 $orig = new CRM_Core_BAO_MessageTemplate();
306 $orig->workflow_id
= $diverted->workflow_id
;
307 $orig->is_reserved
= 1;
311 CRM_Core_Error
::fatal(ts('Message template with id of %1 does not have a default to revert to.', array(1 => $id)));
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
;
322 * Send an email from the specified template based on an array of params.
324 * @param array $params
325 * A string-keyed array of function params, see function body for details.
328 * Array of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates
330 public static function sendTemplate($params) {
332 // option group name of the template
334 // option value name of the template
336 // ID of the template
337 'messageTemplateID' => NULL,
338 // contact id if contact tokens are to be replaced
340 // additional template params (other than the ones already set in the template singleton)
341 'tplParams' => array(),
344 // the recipient’s name
346 // the recipient’s email - mail is sent only if set
352 // the Reply-To: header
355 'attachments' => NULL,
356 // whether this is a test email (and hence should include the test banner)
358 // filename of optional PDF version to add as attachment (do not include path)
359 'PDFFilename' => NULL,
361 $params = array_merge($defaults, $params);
363 CRM_Utils_Hook
::alterMailParams($params, 'messageTemplate');
365 if ((!$params['groupName'] ||
366 !$params['valueName']
368 !$params['messageTemplateID']
370 CRM_Core_Error
::fatal(ts("Message template's option group and/or option value or ID missing."));
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'));
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'));
389 $dao = CRM_Core_DAO
::executeQuery($query, $sqlParams);
393 if ($params['messageTemplateID']) {
394 CRM_Core_Error
::fatal(ts('No such message template: id=%1.', array(1 => $params['messageTemplateID'])));
397 CRM_Core_Error
::fatal(ts('No such message template: option group %1, option value %2.', array(
398 1 => $params['groupName'],
399 2 => $params['valueName'],
404 $mailContent = array(
405 'subject' => $dao->subject
,
406 'text' => $dao->text
,
407 'html' => $dao->html
,
408 'format' => $dao->format
,
412 CRM_Utils_Hook
::alterMailContent($mailContent);
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);
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']);
430 // replace tokens in the three elements (in subject as if it was the text body)
431 $domain = CRM_Core_BAO_Domain
::getDomain();
432 $hookTokens = array();
433 $mailing = new CRM_Mailing_BAO_Mailing();
434 $mailing->subject
= $mailContent['subject'];
435 $mailing->body_text
= $mailContent['text'];
436 $mailing->body_html
= $mailContent['html'];
437 $tokens = $mailing->getTokens();
438 CRM_Utils_Hook
::tokens($hookTokens);
439 $categories = array_keys($hookTokens);
441 $contactID = CRM_Utils_Array
::value('contactId', $params);
444 $contactParams = array('contact_id' => $contactID);
445 $returnProperties = array();
447 if (isset($tokens['subject']['contact'])) {
448 foreach ($tokens['subject']['contact'] as $name) {
449 $returnProperties[$name] = 1;
453 if (isset($tokens['text']['contact'])) {
454 foreach ($tokens['text']['contact'] as $name) {
455 $returnProperties[$name] = 1;
459 if (isset($tokens['html']['contact'])) {
460 foreach ($tokens['html']['contact'] as $name) {
461 $returnProperties[$name] = 1;
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
469 list($contact) = CRM_Utils_Token
::getTokenDetails($contactParams,
472 CRM_Utils_Token
::flattenTokens($tokens),
473 // we should consider adding groupName and valueName here
474 'CRM_Core_BAO_MessageTemplate'
476 $contact = $contact[$contactID];
479 $mailContent['subject'] = CRM_Utils_Token
::replaceDomainTokens($mailContent['subject'], $domain, FALSE, $tokens['text'], TRUE);
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);
484 $mailContent['subject'] = CRM_Utils_Token
::replaceContactTokens($mailContent['subject'], $contact, FALSE, $tokens['text'], FALSE, TRUE);
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);
488 $contactArray = array($contactID => $contact);
489 CRM_Utils_Hook
::tokenValues($contactArray,
492 CRM_Utils_Token
::flattenTokens($tokens),
493 // we should consider adding groupName and valueName here
494 'CRM_Core_BAO_MessageTemplate'
496 $contact = $contactArray[$contactID];
498 $mailContent['subject'] = CRM_Utils_Token
::replaceHookTokens($mailContent['subject'], $contact, $categories, TRUE);
499 $mailContent['text'] = CRM_Utils_Token
::replaceHookTokens($mailContent['text'], $contact, $categories, TRUE);
500 $mailContent['html'] = CRM_Utils_Token
::replaceHookTokens($mailContent['html'], $contact, $categories, TRUE);
503 // strip whitespace from ends and turn into a single line
504 $mailContent['subject'] = "{strip}{$mailContent['subject']}{/strip}";
506 // parse the three elements with Smarty
507 $smarty = CRM_Core_Smarty
::singleton();
508 foreach ($params['tplParams'] as $name => $value) {
509 $smarty->assign($name, $value);
516 $mailContent[$elem] = $smarty->fetch("string:{$mailContent[$elem]}");
519 // send the template, honouring the target user’s preferences (if any)
522 // create the params array
523 $params['subject'] = $mailContent['subject'];
524 $params['text'] = $mailContent['text'];
525 $params['html'] = $mailContent['html'];
527 if ($params['toEmail']) {
528 $contactParams = array(array('email', 'LIKE', $params['toEmail'], 0, 1));
529 list($contact, $_) = CRM_Contact_BAO_Query
::apiQuery($contactParams);
531 $prefs = array_pop($contact);
533 if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'HTML') {
534 $params['text'] = NULL;
537 if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'Text') {
538 $params['html'] = NULL;
541 $config = CRM_Core_Config
::singleton();
542 if (isset($params['isEmailPdf']) && $params['isEmailPdf'] == 1) {
543 $pdfHtml = CRM_Contribute_BAO_ContributionPage
::addInvoicePdfToEmail($params['contributionId'], $params['contactId']);
544 if (empty($params['attachments'])) {
545 $params['attachments'] = array();
547 $params['attachments'][] = CRM_Utils_Mail
::appendPDF('Invoice.pdf', $pdfHtml, $mailContent['format']);
550 if ($config->doNotAttachPDFReceipt
&&
551 $params['PDFFilename'] &&
554 if (empty($params['attachments'])) {
555 $params['attachments'] = array();
557 $params['attachments'][] = CRM_Utils_Mail
::appendPDF($params['PDFFilename'], $params['html'], $mailContent['format']);
558 if (isset($params['tplParams']['email_comment'])) {
559 $params['html'] = $params['tplParams']['email_comment'];
560 $params['text'] = strip_tags($params['tplParams']['email_comment']);
564 $sent = CRM_Utils_Mail
::send($params);
567 unlink($pdf_filename);
571 return array($sent, $mailContent['subject'], $mailContent['text'], $mailContent['html']);