3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
90 $messageTemplates->copyValues($params);
91 $messageTemplates->save();
93 CRM_Utils_Hook
::post($hook, 'MessageTemplate', $messageTemplates->id
, $messageTemplates);
94 return $messageTemplates;
98 * Delete the Message Templates.
100 * @param int $messageTemplatesID
102 public static function del($messageTemplatesID) {
103 // make sure messageTemplatesID is an integer
104 if (!CRM_Utils_Rule
::positiveInteger($messageTemplatesID)) {
105 CRM_Core_Error
::fatal(ts('Invalid Message template'));
108 // Set mailing msg template col to NULL
109 $query = "UPDATE civicrm_mailing
110 SET msg_template_id = NULL
111 WHERE msg_template_id = %1";
113 $params = array(1 => array($messageTemplatesID, 'Integer'));
114 CRM_Core_DAO
::executeQuery($query, $params);
116 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
117 $messageTemplates->id
= $messageTemplatesID;
118 $messageTemplates->delete();
119 CRM_Core_Session
::setStatus(ts('Selected message template has been deleted.'), ts('Deleted'), 'success');
123 * Get the Message Templates.
132 public static function getMessageTemplates($all = TRUE, $isSMS = FALSE) {
135 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
136 $messageTemplates->is_active
= 1;
137 $messageTemplates->is_sms
= $isSMS;
140 $messageTemplates->workflow_id
= 'NULL';
142 $messageTemplates->find();
143 while ($messageTemplates->fetch()) {
144 $msgTpls[$messageTemplates->id
] = $messageTemplates->msg_title
;
151 * @param int $contactId
153 * @param int $messageTemplateID
158 public static function sendReminder($contactId, $email, $messageTemplateID, $from) {
160 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
161 $messageTemplates->id
= $messageTemplateID;
163 $domain = CRM_Core_BAO_Domain
::getDomain();
165 $hookTokens = array();
167 if ($messageTemplates->find(TRUE)) {
168 $body_text = $messageTemplates->msg_text
;
169 $body_html = $messageTemplates->msg_html
;
170 $body_subject = $messageTemplates->msg_subject
;
172 $body_text = CRM_Utils_String
::htmlToText($body_html);
175 $params = array(array('contact_id', '=', $contactId, 0, 0));
176 list($contact, $_) = CRM_Contact_BAO_Query
::apiQuery($params);
179 $contact = reset($contact);
181 if (!$contact ||
is_a($contact, 'CRM_Core_Error')) {
187 // get tokens to be replaced
188 $tokens = array_merge(CRM_Utils_Token
::getTokens($body_text),
189 CRM_Utils_Token
::getTokens($body_html),
190 CRM_Utils_Token
::getTokens($body_subject));
192 // get replacement text for these tokens
193 $returnProperties = array("preferred_mail_format" => 1);
194 if (isset($tokens['contact'])) {
195 foreach ($tokens['contact'] as $key => $value) {
196 $returnProperties[$value] = 1;
199 list($details) = CRM_Utils_Token
::getTokenDetails(array($contactId),
203 'CRM_Core_BAO_MessageTemplate');
204 $contact = reset($details);
207 $hookTokens = array();
208 CRM_Utils_Hook
::tokens($hookTokens);
209 $categories = array_keys($hookTokens);
211 // do replacements in text and html body
212 $type = array('html', 'text');
213 foreach ($type as $key => $value) {
214 $bodyType = "body_{$value}";
216 CRM_Utils_Token
::replaceGreetingTokens($
$bodyType, NULL, $contact['contact_id']);
217 $
$bodyType = CRM_Utils_Token
::replaceDomainTokens($
$bodyType, $domain, TRUE, $tokens, TRUE);
218 $
$bodyType = CRM_Utils_Token
::replaceContactTokens($
$bodyType, $contact, FALSE, $tokens, FALSE, TRUE);
219 $
$bodyType = CRM_Utils_Token
::replaceComponentTokens($
$bodyType, $contact, $tokens, TRUE);
220 $
$bodyType = CRM_Utils_Token
::replaceHookTokens($
$bodyType, $contact, $categories, TRUE);
226 $smarty = CRM_Core_Smarty
::singleton();
231 $
$elem = $smarty->fetch("string:{$$elem}");
234 // do replacements in message subject
235 $messageSubject = CRM_Utils_Token
::replaceContactTokens($body_subject, $contact, FALSE, $tokens);
236 $messageSubject = CRM_Utils_Token
::replaceDomainTokens($messageSubject, $domain, TRUE, $tokens);
237 $messageSubject = CRM_Utils_Token
::replaceComponentTokens($messageSubject, $contact, $tokens, TRUE);
238 $messageSubject = CRM_Utils_Token
::replaceHookTokens($messageSubject, $contact, $categories, TRUE);
240 $messageSubject = $smarty->fetch("string:{$messageSubject}");
242 // set up the parameters for CRM_Utils_Mail::send
244 'groupName' => 'Scheduled Reminder Sender',
246 'toName' => $contact['display_name'],
248 'subject' => $messageSubject,
250 if (!$html ||
$contact['preferred_mail_format'] == 'Text' ||
251 $contact['preferred_mail_format'] == 'Both'
253 // render the & entities in text mode, so that the links work
254 $mailParams['text'] = str_replace('&', '&', $text);
256 if ($html && ($contact['preferred_mail_format'] == 'HTML' ||
257 $contact['preferred_mail_format'] == 'Both'
260 $mailParams['html'] = $html;
263 $result = CRM_Utils_Mail
::send($mailParams);
266 $messageTemplates->free();
272 * Revert a message template to its default subject+text+HTML state.
274 * @param int $id id of the template
276 public static function revert($id) {
277 $diverted = new CRM_Core_BAO_MessageTemplate();
278 $diverted->id
= (int) $id;
281 if ($diverted->N
!= 1) {
282 CRM_Core_Error
::fatal(ts('Did not find a message template with id of %1.', array(1 => $id)));
285 $orig = new CRM_Core_BAO_MessageTemplate();
286 $orig->workflow_id
= $diverted->workflow_id
;
287 $orig->is_reserved
= 1;
291 CRM_Core_Error
::fatal(ts('Message template with id of %1 does not have a default to revert to.', array(1 => $id)));
294 $diverted->msg_subject
= $orig->msg_subject
;
295 $diverted->msg_text
= $orig->msg_text
;
296 $diverted->msg_html
= $orig->msg_html
;
297 $diverted->pdf_format_id
= is_null($orig->pdf_format_id
) ?
'null' : $orig->pdf_format_id
;
302 * Send an email from the specified template based on an array of params.
304 * @param array $params
305 * A string-keyed array of function params, see function body for details.
308 * Array of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates
310 public static function sendTemplate($params) {
312 // option group name of the template
314 // option value name of the template
316 // ID of the template
317 'messageTemplateID' => NULL,
318 // contact id if contact tokens are to be replaced
320 // additional template params (other than the ones already set in the template singleton)
321 'tplParams' => array(),
324 // the recipient’s name
326 // the recipient’s email - mail is sent only if set
332 // the Reply-To: header
335 'attachments' => NULL,
336 // whether this is a test email (and hence should include the test banner)
338 // filename of optional PDF version to add as attachment (do not include path)
339 'PDFFilename' => NULL,
341 $params = array_merge($defaults, $params);
343 if ((!$params['groupName'] ||
344 !$params['valueName']
346 !$params['messageTemplateID']
348 CRM_Core_Error
::fatal(ts("Message template's option group and/or option value or ID missing."));
351 if ($params['messageTemplateID']) {
352 // fetch the three elements from the db based on id
353 $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
354 FROM civicrm_msg_template mt
355 WHERE mt.id = %1 AND mt.is_default = 1';
356 $sqlParams = array(1 => array($params['messageTemplateID'], 'String'));
359 // fetch the three elements from the db based on option_group and option_value names
360 $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
361 FROM civicrm_msg_template mt
362 JOIN civicrm_option_value ov ON workflow_id = ov.id
363 JOIN civicrm_option_group og ON ov.option_group_id = og.id
364 WHERE og.name = %1 AND ov.name = %2 AND mt.is_default = 1';
365 $sqlParams = array(1 => array($params['groupName'], 'String'), 2 => array($params['valueName'], 'String'));
367 $dao = CRM_Core_DAO
::executeQuery($query, $sqlParams);
371 if ($params['messageTemplateID']) {
372 CRM_Core_Error
::fatal(ts('No such message template: id=%1.', array(1 => $params['messageTemplateID'])));
375 CRM_Core_Error
::fatal(ts('No such message template: option group %1, option value %2.', array(
376 1 => $params['groupName'],
377 2 => $params['valueName'],
382 $subject = $dao->subject
;
385 $format = $dao->format
;
388 // add the test banner (if requested)
389 if ($params['isTest']) {
390 $query = "SELECT msg_subject subject, msg_text text, msg_html html
391 FROM civicrm_msg_template mt
392 JOIN civicrm_option_value ov ON workflow_id = ov.id
393 JOIN civicrm_option_group og ON ov.option_group_id = og.id
394 WHERE og.name = 'msg_tpl_workflow_meta' AND ov.name = 'test_preview' AND mt.is_default = 1";
395 $testDao = CRM_Core_DAO
::executeQuery($query);
398 $subject = $testDao->subject
. $subject;
399 $text = $testDao->text
. $text;
400 $html = preg_replace('/<body(.*)$/im', "<body\\1\n{$testDao->html}", $html);
404 // replace tokens in the three elements (in subject as if it was the text body)
405 $domain = CRM_Core_BAO_Domain
::getDomain();
406 $hookTokens = array();
407 $mailing = new CRM_Mailing_BAO_Mailing();
408 $mailing->subject
= $subject;
409 $mailing->body_text
= $text;
410 $mailing->body_html
= $html;
411 $tokens = $mailing->getTokens();
412 CRM_Utils_Hook
::tokens($hookTokens);
413 $categories = array_keys($hookTokens);
415 $contactID = CRM_Utils_Array
::value('contactId', $params);
418 $contactParams = array('contact_id' => $contactID);
419 $returnProperties = array();
421 if (isset($tokens['subject']['contact'])) {
422 foreach ($tokens['subject']['contact'] as $name) {
423 $returnProperties[$name] = 1;
427 if (isset($tokens['text']['contact'])) {
428 foreach ($tokens['text']['contact'] as $name) {
429 $returnProperties[$name] = 1;
433 if (isset($tokens['html']['contact'])) {
434 foreach ($tokens['html']['contact'] as $name) {
435 $returnProperties[$name] = 1;
439 // @todo CRM-17253 don't resolve contact details if there are no tokens
440 // effectively comment out this next (performance-expensive) line
441 // but unfortunately testing is a bit think on the ground to that needs to
443 list($contact) = CRM_Utils_Token
::getTokenDetails($contactParams,
446 CRM_Utils_Token
::flattenTokens($tokens),
447 // we should consider adding groupName and valueName here
448 'CRM_Core_BAO_MessageTemplate'
450 $contact = $contact[$contactID];
453 $subject = CRM_Utils_Token
::replaceDomainTokens($subject, $domain, FALSE, $tokens['text'], TRUE);
454 $text = CRM_Utils_Token
::replaceDomainTokens($text, $domain, FALSE, $tokens['text'], TRUE);
455 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html'], TRUE);
458 $subject = CRM_Utils_Token
::replaceContactTokens($subject, $contact, FALSE, $tokens['text'], FALSE, TRUE);
459 $text = CRM_Utils_Token
::replaceContactTokens($text, $contact, FALSE, $tokens['text'], FALSE, TRUE);
460 $html = CRM_Utils_Token
::replaceContactTokens($html, $contact, FALSE, $tokens['html'], FALSE, TRUE);
462 $contactArray = array($contactID => $contact);
463 CRM_Utils_Hook
::tokenValues($contactArray,
466 CRM_Utils_Token
::flattenTokens($tokens),
467 // we should consider adding groupName and valueName here
468 'CRM_Core_BAO_MessageTemplate'
470 $contact = $contactArray[$contactID];
472 $subject = CRM_Utils_Token
::replaceHookTokens($subject, $contact, $categories, TRUE);
473 $text = CRM_Utils_Token
::replaceHookTokens($text, $contact, $categories, TRUE);
474 $html = CRM_Utils_Token
::replaceHookTokens($html, $contact, $categories, TRUE);
477 // strip whitespace from ends and turn into a single line
478 $subject = "{strip}$subject{/strip}";
480 // parse the three elements with Smarty
481 $smarty = CRM_Core_Smarty
::singleton();
482 foreach ($params['tplParams'] as $name => $value) {
483 $smarty->assign($name, $value);
490 $
$elem = $smarty->fetch("string:{$$elem}");
493 // send the template, honouring the target user’s preferences (if any)
496 // create the params array
497 $params['subject'] = $subject;
498 $params['text'] = $text;
499 $params['html'] = $html;
501 if ($params['toEmail']) {
502 $contactParams = array(array('email', 'LIKE', $params['toEmail'], 0, 1));
503 list($contact, $_) = CRM_Contact_BAO_Query
::apiQuery($contactParams);
505 $prefs = array_pop($contact);
507 if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'HTML') {
508 $params['text'] = NULL;
511 if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'Text') {
512 $params['html'] = NULL;
515 $config = CRM_Core_Config
::singleton();
516 if (isset($params['isEmailPdf']) && $params['isEmailPdf'] == 1) {
517 $pdfHtml = CRM_Contribute_BAO_ContributionPage
::addInvoicePdfToEmail($params['contributionId'], $params['contactId']);
518 if (empty($params['attachments'])) {
519 $params['attachments'] = array();
521 $params['attachments'][] = CRM_Utils_Mail
::appendPDF('Invoice.pdf', $pdfHtml, $format);
524 if ($config->doNotAttachPDFReceipt
&&
525 $params['PDFFilename'] &&
528 if (empty($params['attachments'])) {
529 $params['attachments'] = array();
531 $params['attachments'][] = CRM_Utils_Mail
::appendPDF($params['PDFFilename'], $params['html'], $format);
532 if (isset($params['tplParams']['email_comment'])) {
533 $params['html'] = $params['tplParams']['email_comment'];
534 $params['text'] = strip_tags($params['tplParams']['email_comment']);
538 $sent = CRM_Utils_Mail
::send($params);
541 unlink($pdf_filename);
545 return array($sent, $subject, $text, $html);